Dealing with 3d pointer array in CUDA

Hi,

I have a 3d pointer data(int ***source) on which i want to perform the operation in device.
One way is to flatten this data by copying the 3d to 1d pointer and then use cudaMemcpy to copy the data from host to device.

But is there any way we can directly 3d pointer data to device memory. I tried in below way but its not working

int kernel(){

int N = 10, M = 10, W = 10;
    int*** a; // host array
	a = new int** [N];
    for (int i = 0; i < N; i++) {
        a[i] = new int* [M];
        for (int j = 0; j < M; j++) {
            a[i][j] = new int[W];
            for (int w = 0; w < W; w++)
            {
                a[i][j][w] = w + (j * W) + (i * M * W);
            }
        }
    }
    int* b; //device pointer array
    cudaError_t cudaStatus = cudaMalloc((void**)&b, (N * M * W * sizeof(int)));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        return 1;
    }

    cudaStatus = cudaMemcpy(b, a, (N * M * W * sizeof(int)), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        return 1;
    }
}

Is there any proper way to do this.

Regards,
Bhargav

Thank you @Robert_Crovella for the response. I could see that you had mentioned we can not use cudaMemcpy2D/3D for the data created with new. My data is created dynamically with new, so i can not use cudaMallocPitch /cudaMemcpy3d.
So i think the best and less complex way would be to flatten the array. Please confirm if my understanding is correct.

I think flattening is a good choice. If your array widths are known at compile time, it’s possible to get an equally-efficient realization using array types. The width “known at compile-time” topic is mentioned in the linked article.

No, width is dynamic in my case not known at compile time. I will go with flattening. I was looking for correct approach for some time, thanks a lot for the confirmation.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.