I am trying to declare and initialize an array of arrays in CUDA. I am using the following piece of code:
int** h_array = (int**)malloc(num_of_arrays * sizeof(int*));
int** d_array;
cudaMallocHost((void**)&d_array, num_of_arrays * sizeof(int*));
for(size_t i = 0 ; i < num_of_arrays ; i++){
cudaMallocHost(&h_array[i], array_size * sizeof(int));
}
for(size_t i = 0 ; i < num_of_arrays ; i++){
cudaMemcpy(d_array[i], h_array[i], array_size * sizeof(int), cudaMemcpyHostToDevice);
}
int** h_array2 = (int**)malloc(num_of_arrays * sizeof(int*));
Please note that h_array2 is initialized correctly(it is an array of arrays, for which each array is initialized correctly). Then I try to do the following:
for(size_t i = 0 ; i < num_of_arrays ; i++){
cudaMemcpy(d_array[i], h_arra2[i], array_size * sizeof(int), cudaMemcpyHostToDevice);
}
To sum up, I try to declare and initialize an array of arrays in the device memory. I know that I can not access device memory from host memory.
The code above seems not to work.
Could you please tell me what is wrong and help me? Thank you in advance.