CUDA allocating array of arrays

前端 未结 5 1087
Happy的楠姐
Happy的楠姐 2020-12-03 12:37

I have some trouble with allocate array of arrays in CUDA.

void ** data;
cudaMalloc(&data, sizeof(void**)*N); // allocates without problems
for(int i = 0         


        
5条回答
  •  孤城傲影
    2020-12-03 13:24

    You have to allocate the pointers to a host memory, then allocate device memory for each array and store it's pointer in the host memory. Then allocate the memory for storing the pointers into the device and then copy the host memory to the device memory. One example is worth 1000 words:

    __global__ void multi_array_kernel( int N, void** arrays ){
        // stuff
    }
    
    
    int main(){
    
        const int N_ARRAYS = 20;
        void *h_array = malloc(sizeof(void*) * N_ARRAYS);
        for(int i = 0; i < N_ARRAYS; i++){
            cudaMalloc(&h_array[i], i * sizeof(void*));
            //TODO: check error
        }
        void *d_array = cudaMalloc(sizeof(void*) * N_ARRAYS);
    
        // Copy to device Memory
        cudaMemcpy(d_array, h_array, sizeof(void*) * N_ARRAYS, cudaMemcpyHostToDevice);
    
        multi_array_kernel<1,1>(N_ARRAYS, d_array);
        cudaThreadSynchronize();
    
        for(int i = 0; i < N_ARRAYS; i++){
            cudaFree(h_array[i]); //host not device memory
            //TODO: check error
        }
        cudaFree(d_array);
        free(h_array);
    }
    

提交回复
热议问题