1

I have a problem related to CUDA. I create a array which is 2D with :

char **WordMatrix = new char*[N]; // M and N set by the user
for(int i = 0; i < N; ++i) 
{
    WordMatrix[i] = new char[M];
}

I fill the array with a loop. After that where my problem is lying : I can't managed to allocate the memory in device and copy from host to device.

I saw other questions related to this but I did not understand the logic. Like in this topic : https://devtalk.nvidia.com/default/topic/410182/double-pointer-allocation/ or in here.

I want to understand how to do it with an code example which uses cudaMemcpy and cudaMalloc and 2D Array also explanation of why we need supporting(if you use the approach in the link) pointer?

Oguzhan D.
  • 169
  • 1
  • 2
  • 15

2 Answers2

4

My understanding is CUDA accepts only linearized 2D arrays, so really a 1D array.

int  *my_array = new int[height*width];
for (int h = 0; h < height; h++){
    for (int w = 0; w < width; w++)
        my_array[width * h + w] = value;
}

You can then copy that to device memory as in the other answer.

Also, this question has more info: Allocate 2D Array on Device Memory in CUDA.

Community
  • 1
  • 1
Gswffye
  • 180
  • 3
  • 13
  • 1
    "CUDA accepts only linearized 2D arrays" This is not correct. CUDA can work with 2D arrays. The marked duplicate question demonstrates this. However it's usually not recommended. – Robert Crovella Jul 14 '14 at 02:37
2
  1. Create your 2D array in one piece new char[N*M]

  2. Allocate the same amout of memory on GPU cudaMalloc(... sizeof(char)*N*M)

  3. Copy your CPU memory (1.) to GPU (2.) with cudaMemcpy(... hostToDevice)

dari
  • 2,255
  • 14
  • 21