-2

I allocated the memory for an array of unsigned char using cudaMalloc and the initialized using cudaMemset.

unsigned char *device_channel_data;
cudaMalloc( device_channel_data, sizeof(unsigned char) * image_size);
cudaMemset( *device_channel_data, 0, sizeof(unsigned char) * image_size);

After that I'm checking whether it is really set to 0 by copying the data back to host. I'm printing some of the elements to check the data but the values printed are random.

unsigned char *host_channel_channel;
cudaMemcpy(host_channel_channel, device_channel_data, sizeof(unsigned char) * image_size, cudaMemcpyDeviceToHost);

    for(int i = 0; i < 10; i ++)
    {
        std::cout<< (int)host_channel_channel[i] << std::endl;
    }

I want to initialize the device_channel data to 0. My knowledge with pointers and CUDA programming is very limited. I'm just starting with CUDA Programming. Thanks in advance for the help.

Abhishek
  • 23
  • 4
  • 3
    `cudaMalloc((void**)&device_channel_data, sizeof(unsigned char) * image_size); cudaMemset(device_channel_data, 0, sizeof(unsigned char) * image_size);` – njuffa Oct 07 '19 at 02:12

1 Answers1

2

The answer of your question is in the documentation of CUDA: https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__MEMORY.html

For error handling (Use of cudamalloc(). Why the double pointer?) the CUDA-API need a pointer to pointer as an input parameter for allocating the memory, while for memset a simple pointer is needed. Change your code to:

unsigned char *device_channel_data;
cudaMalloc( (void**)&device_channel_data, sizeof(unsigned char) * image_size);
cudaMemset( device_channel_data, 0, sizeof(unsigned char) * image_size);

and everything should work fine.