Global Memory write per block ?


I have written a code in which all threads in a block update some shared variable and then I use __syncthreads() to make sure that all threads in the block are actually done updating the same shared memory variable.

Now, I want that one global memory variable should be updated with the resultant value of the shared variable. Can it be done ?

I ran the code in emuDebug mode and I get the error following error when I update the global memory:-
[font=“Courier”]Unhandled exception at 0x10012418 in Prog.exe: 0xC0000027: Unwind exception code[/font]


The sample code for the above case looks like:

__global__ void sum_kernel(float *d_array_x, float *d_array_y, float *d_array_z,  unsigned int num_elements, float *d_sum)


	int block_index = blockIdx.x;

	int thread_index = threadIdx.x;


	__shared__ float sh_sum;

	sh_sum = 0;


	sh_sum += d_array_x[block_index*blockDim.x + thread_index] + d_array_y[block_index*blockDim.x + thread_index] + d_array_z[block_index*blockDim.x + thread_index];



//Updating Global Memory per block

d_sum[block_index] = sh_sum/num_elements;


I get an error in the last line of the code, as “unwind exception.”

So I just want to know that am I updating the GMEM in right way? Any help would be appreciated. :thumbup:

All threads of one block update this position. You should wrap the last call into an if-statement and specify only one thread id e.g.:

If (threadIdx.x = 0) {



I assume you are doing the reduction correctly beforehand, I didn’t check!


Thanks Johannes…I will try that thread wrapping :))