First Chance Exception Error

I got a first chance exception error in Visual Studio when i run this code:

[codebox]#include <stdio.h>

#include <stdlib.h>

#include “cutil_inline.h”

global void initial(float* d,float* x,int widthx,int lengthy,int heightz)

{

unsigned int xcoord=threadIdx.x+blockIdx.x*blockDim.x;

unsigned int ycoord=threadIdx.y*blockIdx.y*blockDim.y;

unsigned int zcoord=threadIdx.z*blockIdx.z*blockDim.z;

unsigned int increy=blockDim.y*gridDim.y;

unsigned int increz=increy*blockDim.z*gridDim.z;

unsigned int thid=xcoord+(ycoord)*increy+(zcoord)*increz;

if(xcoord<widthx&&ycoord<lengthy&&zcoord<heightz)

{

	if(xcoord==0||xcoord==widthx)

	x[thid]=0.1;

	else

		if(ycoord==0||ycoord==lengthy)

			x[thid]=0.2;

		else

			if(zcoord==0||zcoord==heightz)

				x[thid]=0.3;

			else

				x[thid]=1;

}  

}

int main(int argc, char* argv)

{

float *d,*x;

float*ans;

int widthx=100,lengthy=100,heightz=100;

int size=(widthx+2)(lengthy+2)(heightz+2);

int threadx=16,thready=16,threadz=2;

unsigned int timer;

dim3 dimBlock(threadx,thready,threadz);

dim3 dimGrid((widthx+2+threadx-1)/threadx,(lengthy+2+thready-1)/thready,(heightz+2+threadz-1)/threadz);

ans=(float*)malloc(size*sizeof(float));

cudaThreadSynchronize();

cutCreateTimer(&timer);

cutStartTimer(timer);

cudaMalloc((void**)&d,size*sizeof(float));

cudaMalloc((void**)&x,size*sizeof(float));

initial<<<dimGrid,dimBlock>>>(d,x,widthx,lengthy,heightz);

cudaThreadSynchronize();

cudaMemcpy(ans,x,size*sizeof(float),cudaMemcpyDeviceToHost);

cudaThreadSynchronize();

cutStopTimer(timer);

FILE *fileid=fopen(“output.txt”,“w”);

for(int i=0;i<size;i++)

{

fprintf(fileid,"%f\n",ans[i]);

}

FILE *fileid2=fopen(“time.txt”,“w”);

fprintf(fileid2,"%f ms ", cutGetTimerValue(timer));

cutDeleteTimer(timer);

return 0;

}[/codebox]

I have tried different block size, but to no avail. What could be the problem here?

Well, I dont think I found your err but maybe some mistakes:

  1. you wrote xcoord=threadIdx.x+blockIdx.x*blockDim.x;

    and ycoord=threadIdx.yblockIdx.yblockDim.y; (same for z)

    Maybe you meant ycoord=threadIdx.y+blockIdx.y*blockDim.y; (same for z)?

  2. you dont seem to have a h2d memcpy before calling your kernel but maybe you just didnt copy&paste it or this rly for measuring time only

  3. I noticed you wrote x[thid]=0.1; so you r storing doubles in float arrays… could be evil (out-of-bounds) or just an implicit type conversion - Im not quite sure here

I didnt check the values of your thread/block id depending indices btw maybe your gremlin is hiding there :)