1

I need to create an array which takes two arguments: array and its size.

I've got a function like this:

__global__ void reverseArray(int *data, int size){

    int tid = blockIdx.x// Total blocks

}

How can I reverse array with this function?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
asdasd
  • 55
  • 1
  • 1
  • 7

1 Answers1

1

It depends on your launch parameters, but you can try doing

__global__ void reverseArray(int *data,int count){
    const int tid = threadIdx.x + blockIdx.x*blockDim.x;
    if(tid < count/2)
    {
        const int new_tid = count - tid - 1;
        int prev_valA = data[tid];
        int prev_valB = data[new_tid];

        data[new_tid] = prev_valA;
        data[tid] = prev_valB;
    }
}

I'm assuming this is a continuation of your previous question?

Also, note that this assumes you're only using the x-dimension for your kernel launch parameters

Community
  • 1
  • 1
alrikai
  • 4,123
  • 3
  • 24
  • 23
  • Since this code as posted updates `data[new_tid]` without any consideration as to whether that element has been picked up by the thread that is using it to update the corresponding value, it will be broken. You can fix this pretty much by having each thread update *both* values in the `data[]` array that are being swapped with each other (and launching approximately half as many threads). – Robert Crovella May 16 '13 at 19:22
  • @RobertCrovella good point, I'll update my answer – alrikai May 16 '13 at 19:27