I am writing a simple cuda program where I am creating a 2D array in the device and then I am doing very basic operation in the kernel function and after the operation I am copying it back to a 2D array of the host. I wrote this code after following several threads of stackoverlow and also cuda forum. I followed what was suggested but the output of the code I am getting is 0 whereas I am expecting an output of 10 for all the members of the array. I am posting my code below :
__global__ void test_kernel(int *dev_ptr[])
{
int tidx = threadIdx.x;
int tidy = threadIdx.y;
dev_ptr[tidx][tidy] = dev_ptr[tidx][tidy] +10;
}
int main(int argc,char *argv[])
{
int env_end =50;
int **h_ptr ;
int **d_ptr ;
int **env_t;
int i,k,j;
/************************************************************************/
/* cpu
/************************************************************************/
env_t =(int **) malloc(env_end * sizeof *env_t);
for(k=0;k<env_end;k++)
{env_t[k] = (int *)malloc(env_end* env_end* sizeof *env_t[0]);
}
for (k = 1; k < env_end; ++k)
env_t[k] = env_t[k - 1] + env_end;
memset(*env_t, 0, env_end * env_end* sizeof **env_t);
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[i][j]); }
if (j==env_end-1)
{printf("\n"); }
}
/************************************************************************/
/* gpu
/************************************************************************/
h_ptr = (int **)malloc(env_end*sizeof(int *));
for (i=0;i<env_end;i++)
{ cudaMalloc((void **)&h_ptr[i],env_end*sizeof(int));
cudaMemcpy(h_ptr[i],&env_t[i][0],env_end*sizeof(int),cudaMemcpyHostToDevice);
}
cudaMalloc((void ***)d_ptr,env_end*sizeof(int));
cudaMemcpy(d_ptr,h_ptr,env_end*sizeof(int),cudaMemcpyHostToDevice);
/************************************************************************/
/* kernel function and declaration
/************************************************************************/
dim3 blockDim(env_end,env_end,1);
test_kernel<<<1,blockDim>>>(d_ptr);
/************************************************************************/
/* Copying data back to host
************************************************************************/
for (i=0;i<env_end;i++)
{cudaMemcpy(env_t[i],h_ptr[i],env_end*sizeof(int),cudaMemcpyDeviceToHost);
}
for (i=0;i<env_end;i++)
{ for(j=0;j<env_end;j++)
{printf("%d\t",env_t[i][j]); }
if (j==env_end-1)
{printf("\n"); }
}
/************************************************************************/
/* Freeing the memory locations
/************************************************************************/
for (i=0;i<env_end;i++)
{cudaFree(h_ptr[i]);
}
cudaFree(d_ptr);
free(h_ptr);
for (i=0;i<env_end;i++)
{ free(env_t[i]);
}
free(env_t);
}
One more thing is that I am writing the code in MS visual studio 2010 and I am getting a debug assertion failed notification. I am not sure what I have done wrong and why this notification is coming. Thanks for all your help.