nbonneel
nbonneel

Reputation: 3326

Cuda strange bug

I am having troubles understanding a bug that I have in a simple Cuda kernel. I shrinked down my kernel to the minimum that still shows the error.

I have a "Polygon" class that just stores a number of points. I have a function that "adds a point" (just increments the counter), and I add 4 points to all polygons in my array of polygons. Finally, I call a function that updates the number of points using a loop. If, in this loop, I call new_nbpts++ once, I obtain the expected answer : all polygons have 4 points. If in the same loop I call new_nbpts++ a second time, then my polygons have a garbage number of points (4194304 points) which is not correct (I should get 8).

I expect there is something I misunderstood though.

Complete kernel:

#include <stdio.h>
#include <cuda.h>


class Polygon {
public:
  __device__ Polygon():nbpts(0){};
  __device__ void addPt() {
    nbpts++;
  }; 
  __device__ void update() {
    int new_nbpts = 0;
    for (int i=0; i<nbpts; i++) {
        new_nbpts++;
        new_nbpts++;  // calling that a second time screws up my result
    }
    nbpts = new_nbpts;
  }

 int nbpts;
};


__global__ void cut_poly(Polygon* polygons, int N)
{
  int idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (idx>=N) return;

  Polygon pol;
  pol.addPt();
  pol.addPt();
  pol.addPt();
  pol.addPt();

  for (int i=0; i<N; i++) {
    pol.update();
  }

  polygons[idx] = pol;
}



int main(int argc, unsigned char* argv[])
{
  const int N = 20; 
  Polygon p_h[N], *p_d;

  cudaError_t err = cudaMalloc((void **) &p_d, N * sizeof(Polygon));   

  int block_size = 4;
  int n_blocks = N/block_size + (N%block_size == 0 ? 0:1);
  cut_poly <<< n_blocks, block_size >>> (p_d, N);

  cudaMemcpy(p_h, p_d, sizeof(Polygon)*N, cudaMemcpyDeviceToHost);

  for (int i=0; i<N; i++)
   printf("%d\n", p_h[i].nbpts);

  cudaFree(p_d);

  return 0;
}

Upvotes: 0

Views: 200

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 152173

Why are you doing this at the end of your kernel:

  for (int i=0; i<N; i++) {
    pol.update();
  }

?

Remember each thread has it's own instance of:

Polygon pol;

If you want to update each thread's instance of pol at the end of the kernel, you only need to do:

pol.update();

Now, what happens in your case?

Suppose your update() code only has one:

new_nbpts++; 

in it.

Your for loop of 0 to N-1 calling pol.update() will, on each iteration:

  1. set new_nbpts to zero
  2. increment new_nbpts a total of nbpts times.
  3. replace the value of nbpts with new_nbpts

Hopefully you can see this has the effect of leaving nbpts unchanged. Even after N iterations of the for loop that is calling pol.update(), the value of nbpts is unchanged.

Now what happens if I have:

new_nbpts++;
new_nbpts++;

in my update() method? Then on each call of pol.update(), I will:

  1. set new_nbpts to zero
  2. increase new_nbpts by two a total of nbpts times
  3. replace the value of nbpts with new nbpts

Hopefully you can see this has the effect of doubling nbpts on each call of pol.update()

Now, since you are calling pol.update() N times in each thread, you are doubling the starting value of nbpts N times, i.e. nbpts *2^N. Since nbpts starts out (in this case) as 4, we have 4*2^20=4194304

I'm not really sure what you're after with all this, but my guess is you were running that for loop at the end of the kernel thinking you were going to update all the different instances of Polygon pol that way. But that's not how to do it, and all you need is a single

pol.update();

at the end of the kernel, if that was your intention.

Upvotes: 2

Related Questions