cudaMemset() - 它设置字节还是整数? [英] cudaMemset() - does it set bytes or integers?

查看:1526
本文介绍了cudaMemset() - 它设置字节还是整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在线文档:

  cudaError_t cudaMemset(void * devPtr,int value,size_t count)

使用常量字节值填充devPtr指向的内存区域的第一个计数字节。



参数:
devPtr - 指向设备内存的指针
value - 为指定内存的每个字节设置的值
count - 字节设置



此说明看起来不正确:

  int * dJunk; 
cudaMalloc((void **)& dJunk,32 *(sizeof(int));
cudaMemset(dJunk,0x12,32);
pre>

将所有32个整数设置为0x12,而不是0x12121212(Int vs Byte)



关于设置字节,计数和值以字节为单位描述,注意计数类型为 size_t ,值为int类型。设置字节大小为int值。



cudaMemset()在程序指南中没有提到
我必须假设我看到的行为是正确的,文档是不好的。



是否有更好的文档源?(在哪里?)

是否支持其他类型,即 float * dJunk; 工作?其他?

解决方案

文档是正确的,你对 cudaMemset 为 0x12 ,而不是设置字节值。所有32个整数到 0x12 ,viz:

  #include< cstdio> ; 

int main(void)
{
const int n = 32;
const size_t sz = size_t(n)* sizeof(int);
int * dJunk;
cudaMalloc((void **)& dJunk,sz);
cudaMemset(dJunk,0,sz);
cudaMemset(dJunk,0x12,32);

int * Junk = new int [n];

cudaMemcpy(Junk,dJunk,sz,cudaMemcpyDeviceToHost);

for(int i = 0; i fprintf(stdout,%d%x \\\
,i,Junk [i]);
}

cudaDeviceReset();
return 0;
}

产生

  $ nvcc memset.cu 
$ ./a.out

0 12121212
1 12121212
2 12121212
3 12121212
4 12121212
5 12121212
6 12121212
7 12121212
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 0
31 0

ie 。所有128个字节设置为0,然后前32个字节设置为 0x12 。正如文档中所述。


From online documentation:

cudaError_t cudaMemset (void * devPtr, int value, size_t count )

Fills the first count bytes of the memory area pointed to by devPtr with the constant byte value value.

Parameters: devPtr - Pointer to device memory value - Value to set for each byte of specified memory count - Size in bytes to set

This description doesn't appear to be correct as:

int *dJunk;
cudaMalloc((void**)&dJunk, 32*(sizeof(int));
cudaMemset(dJunk, 0x12, 32);

will set all 32 integers to 0x12, not 0x12121212. (Int vs Byte)

The description talks about setting bytes. Count and Value are described in terms of bytes. Notice count is of type size_t, and value is of type int. i.e. Set a byte-size to an int-value.

cudaMemset() is not mentioned in the prog guide. I have to assume the behavior I am seeing is correct, and the documentation is bad.

Is there a better documentation source out there? (Where?)
Are other types supported? i.e. Would float *dJunk; work? Others?

解决方案

The documentation is correct, and your interpretation of what cudaMemset does is wrong. The function really does set byte values. Your example sets the first 32 bytes to 0x12, not all 32 integers to 0x12, viz:

#include <cstdio>

int main(void)
{
    const int n = 32;
    const size_t sz = size_t(n) * sizeof(int);
    int *dJunk;
    cudaMalloc((void**)&dJunk, sz);
    cudaMemset(dJunk, 0, sz);
    cudaMemset(dJunk, 0x12, 32);

    int *Junk = new int[n];

    cudaMemcpy(Junk, dJunk, sz, cudaMemcpyDeviceToHost);

    for(int i=0; i<n; i++) {
        fprintf(stdout, "%d %x\n", i, Junk[i]);
    }

    cudaDeviceReset();
    return 0;
}

produces

$ nvcc memset.cu 
$ ./a.out 

0 12121212
1 12121212
2 12121212
3 12121212
4 12121212
5 12121212
6 12121212
7 12121212
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 0
21 0
22 0
23 0
24 0
25 0
26 0
27 0
28 0
29 0
30 0
31 0

ie. all 128 bytes set to 0, then first 32 bytes set to 0x12. Exactly as described by the documentation.

这篇关于cudaMemset() - 它设置字节还是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆