Global Variable

Dear All;

I am trying to define a global variable for example z
such that
A[z++] = p[i];

where A

  • and p
  • are arrays
    and i is a thread ranges from 0 to 3
    so I have four threads

    the problem, always array A takes the value of last thread
    meaning that z always equal last thread

    I need to have A[0] equal P[i=first arrival thread]
    and then A[1]= P[i=second arrival thread]
    and so on…
    but some how z variable is always assign for the last thread
    Would please some one help me on assigning the z variable?

    Thanks all,

  • at global scope:

    __device__ int z = 0;

    in thread code:

    int my_z = atomicAdd(&z, 1);
    A[my_z] = p[i];