user1285419
user1285419

Reputation: 2225

how to apply atomicAdd to each of element of an array in CUDA?

I have a code from CUDA example to atomicAdd a single variable

__global__ void myadd(int *data)
{
  unsigned int x = blockIdx.x;
  unsigned int y = threadIdx.x;
  if ( (x%2==0) && (y%2==1) ) atomicAdd(data,1);
}

int main(void)
{
  int *Hdt;
  Hdt = (int*)malloc(sizeof(int));
  // ... CUDA initialization here
  myadd<<<20, 10>>>(Hdt);
  cudaFree(Hdt);
}

It works good for me. But I am expanding my code so I would like to pass an array instead of a number to the kernel

__global__ void myadd(int *data)
{
  unsigned int x = blockIdx.x;
  unsigned int y = threadIdx.x;
  unsigned int z = threadIdx.y;
  if ( (x%2==0) && (y%2==1) && (z>4) ) atomicAdd(data[z],1);
}

int main(void)
{
  int *Hdt;
  Hdt = (int*)malloc(sizeof(20*int));
  // ... CUDA initialization here
  myadd<<<20, dim3(10, 20)>>>(Hdt);
  cudaFree(Hdt);
}

But it doesn't compile, the error message is:

error: no instance of overloaded function "atomicAdd" matches the argument list argument types are: (int, int)

Upvotes: 2

Views: 5711

Answers (1)

Joky
Joky

Reputation: 1628

replace:

atomicAdd(data[z],1);

with

atomicAdd(&data[z],1);

If you look carefully, in the first case you were giving a pointer as first argument to atomicAdd().

Upvotes: 8

Related Questions