On passing my pointer to point to the array, I am getting the foll error: argument of type "float " is incompatible with parameter of type "float ()[32768]
relevant Snippets of my code are:
#define N 32768
__global__ void op(float k_a[][N])
{
//some operation
}
float *ptr_a=(float*)malloc(N*N*sizeof(float));
float *d_ptr_a;cudaMalloc((void**)&d_ptr_a,N*N*sizeof(float));
cudaMemcpy(d_ptr_a,ptr_a,N*N*sizeof(float),cudaMemcpyHostToDevice);
op<<<nblocks,nthreadsperblock>>>(d_ptr_a)
Can some tell me whats going wrong? I am a beginner to CUDA.
What we have here is an incorrect use of pointers and arrays. According to the definition of the routine op
it requires a pointer to array of length N of type float
. On the other hand when it is called a pointer to type float
is provided as an argument, thus resulting in an error message of types not being compatible.
If op
routine is to tackle with a 2D array of float
values, it is better to have its argument be a pointer to float
. In such a case all of the M
"rows" of the original 2D array, accessed with an index y
, sit in the memory one after another, in a single line, like that:
[row0][row1][row2]...[rowM-1]
Every such row contains N
"columns", accessed by index x
. To get a value for some pair (x, y)
we have to produce a global index that will access the 1D array:
index = y * N + x;
Simple as that.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments