very weird shared memory value cuda 2.3

Who can tell me why the values of weights are 0. after executing the kernel?

Their values are correct, i.e., 6, in emulation mode. But once the code runs on my GeForce 9800 GT, it produces all 0. And if I uncomment the last line and comment its previous line, then the results are also correct.

Why??? What happened to the shared memory?

#define SIZE (4)

HelloCUDA<<<1, SIZE, sizeof(float) * SIZE>>>(device_sum, device_weights);

__global__ static void HelloCUDA(float *d_sum, float *weights)


	extern __shared__ float sharedMem[];

	float *w = sharedMem;

	float *sum = (float *)&w;

	w[threadIdx.x] = threadIdx.x;


	if (threadIdx.x == 0) {

		*sum = 0.;

		for (int i = 0; i < blockDim.x; i++) {

			*sum += w[i];


		*d_sum = *sum;




	weights[threadIdx.x] = *sum;

		//weights[threadIdx.x] = *d_sum;


I found a bug, the size I assigned to the shared memory is wrong.

I could not find how to delete a post… sorry for the garbage…