Copy data 3D kernel

Hi everybody,

I cannot understand why this code is unpredictable. I am trying to copy data from a smaller 3D array “y” (N1,N2,N3) to a larger 3D arary “x” (n1,n2,n3):

static global void copy_charge3D_kernel(float x, float y, int n1, int n2, int N1, int N2, int N3)
{
const int ig = blockIdx.x
blockDim.x+threadIdx.x;
const int jg = blockIdx.y
blockDim.y+threadIdx.y;
int k;

if (ig < N1 && jg < N2)
{
  for (k = 0; k < N3; k++)
  {
	x[k*n1*n2+jg*n1+ig] = y[k*N1*N2+jg*N1+ig];
  }
}
__syncthreads();

}

Does anyone see any problems with this kernel? Any help is appreciated.

Thank you in advance!
Alex.

I’m not sure what data layout you’re dealing with but this seems wrong if you’re using absolute positioning in a 2D row-major matrix:

const int ig = blockIdx.x*blockDim.x+threadIdx.x;
const int jg = blockIdx.y*blockDim.y+threadIdx.y;

it should rather be:

const int ig = blockIdx.x*blockDim.x+threadIdx.x;
const int jg = (blockIdx.y*blockDim.y + threadIdx.y)*gridDim.x;

if you’re not, sorry for misunderstanding your question.
Also: this seems more suited to CUDA Programming forum

Thanks a lot for a suggestion. I will try CUDA Programming forum.

Alex.