c++ - Atomic Add on Cuda not working.. -


my problem find out number of integer points in n dimensional sphere using cuda. dont understand wrong below code giving 0 output time. cuda compute capability 2.0 , tool kit version 3.10. help.

__global__ void count_in(int pow_rad, int ndim,int *digit,int w,unsigned int *count,double radius) {   long int i,j; int rem,idx,sq,num; int iy=blockdim.y * blockidx.y + threadidx.y; int ix=blockdim.x * blockidx.x + threadidx.x; int width=griddim.x*blockdim.x; int h=2*w+1; i=iy*width+ix; if(i>pow_rad) return;      sq=0;     idx=0;     num=i;     for(j=0;j<ndim;j++)         {digit[j]=0;}     while(num!=0)     {         rem=num%w;         num/=w;         digit[idx]=rem;         idx++;     }     for(j=0;j<ndim;j++)         {sq+=(digit[j]-h)*(digit[j]-h);}     if(sq<(radius*radius))         atomicinc(count,(unsigned int)1);     __syncthreads(); }  int main(int argc, char* argv[])  { const long ntrials = 5; int i; (int n = 0; n < ntrials; ++n) {     int *digit;     unsigned int *count;     std::cout<<n<<std::endl;     int pow_rad;     unsigned int num;     // select radius , number of dimensions @ random     const double r = drand48() * (rmax - rmin) + rmin;     const int   nd = lrand48() % (maxdim - 1) + 1;     cudamalloc((void**) &digit,sizeof(int)*nd);     cudamalloc((void**) &count,sizeof(unsigned int));     cudamemset(count,0,sizeof(unsigned int));     int h=(int)floor(r);     int w=2*h+1;     std::cout << "###"<< r <<" "<< nd<< std::endl;     for(i=1;i<=nd;i++)         pow_rad*=w;     int width=(int)sqrt(pow_rad);     // call function     dim3 dimblock(32,32);     dim3 dimgrid((width/32)+1,(width/32)+1);  count_in<<<dimgrid,dimblock>>>(pow_rad, nd,digit,w,count,r);     cudamemcpy(&num,count,sizeof(unsigned int),cudamemcpydevicetohost); std::cout << "-->"<<num << std::endl; } } 

i didn't @ of code, lines

    atomicinc(count,(unsigned int)1); 

seems show common misunderstanding of atomicinc function. second argument not amount increment, modulus; when global variable reaches amount, resets zero. value specified, each time statement executes variable count reset 0.

if change atomicinc atomicadd, or if change modulus large enough never reached, should work better.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -