Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8900313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:04:34+00:00 2026-06-15T01:04:34+00:00

I am having troubles understanding a bug that I have in a simple Cuda

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:04:35+00:00Added an answer on June 15, 2026 at 1:04 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some troubles understanding and implementing the MVC pattern. I have a
I'm having troubles on understanding well how to use manifests. That's my problem: I've
I'm having troubles understanding the MATCH in MySql. I have a table with Full
I'm having troubles understanding pointer arithmetic or how memory is assigned. In the code
I am having troubles with understanding how to load data with Core Data. This
I am having some troubles understanding the object relationships in Django. I wrote this:
I am having a bit of trouble understanding this bug. Here is the code:
I'm having some troubles understanding how Guice's singleton instantiations works. I've read the available
I'm having some troubles understanding MPL placeholders. Could someone please explain me why this
I'm having troubles understanding how realloc works. If I malloc'ed a buffer and copied

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.