compute the average of four neighbors within a Matrix

Hi everybody,

I basically copied the code from Programming Guide because what i need to do is simple. I have an input matrix with random value and i will compute the average of every point’s four neighbors. So for the points in the borders and corners, i ignore them.

I copied the Matrix multiplication code from Programming guide, but i can’t specify the border points using “blockIdx” and “threadIdx” struct. I don’t know why because in my understanding that makes perfect sense. Please look into the if statement where i specify the points excluding borders and corners.

If you wann run and test it, i attached the .cu file so you may test it.

//this part determines the block and grid size. A is the input Matrix and d_A is the input matrix on device memory

	dim3 dimBlock(BLOCK_SIZE, BLOCK_SIZE);

	dim3 dimGrid ((A.width+dimBlock.x-1)/dimBlock.x, (A.height+dimBlock.y-1)/dimBlock.y);


	kernel<<<dimGrid, dimBlock>>>(d_A, width, height);

// data is a one dimensional array which stores the data.

__global__ static void kernel(float *data, int *w,int *h)



	int height = *h; int width = *w;

	float value = 0; float weight = 0.1; int iterator=1;

	int row = blockIdx.y*blockDim.y+ threadIdx.y;

	int col = blockIdx.x*blockDim.x+ threadIdx.x;

		//this part i wanna get the points which have four neighbors instead of three or two. That means i need to exclude the border points.

		//but it never behaves like it is supposed. I basically copied the code from programming guide.

		if( row !=0&& row != height-1 && col!= 0&& col !=width -1 ){










standard_iteration.txt (6.3 KB)