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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

"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.
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)

1 Comment

Isn't this kind of creating 1D array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.