cudaMemset() - does it set bytes or integers?
Asked Answered
R

1

16

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?

Ruff answered 14/11, 2012 at 20:52 Comment(0)
U
25

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.

Unaware answered 14/11, 2012 at 21:43 Comment(3)
Yes, I was looking at my results incorrectly. Try changing cudaMemset(dJunk, 0x12, 32); to cudaMemset(dJunk, 0x1234, 32); It appears cudaError_t cudaMemset (void * devPtr, int value, size_t count ) should be: cudaError_t cudaMemset (void * devPtr, char value, size_t count ) i.e. VALUE is of size byte. It is a bit misleading to use size int. (Just pointing that out.)Ruff
@Doug: I am still not sure what your point is. The very documentation you quote in your question clearly says that value is treated as a byte value. The function behaves identically to the standard C memset, the only difference is that the byte value is passed in the LSB of a 32 bit word. Incidentally, there is a true 32 bit memset function in the driver API if that is what you are really looking for.Unaware
This approach does not work if the data type is float, not int. In such a case, Junk is always a zero array. How to make it work for float values?Snakebite

© 2022 - 2024 — McMap. All rights reserved.