How to reverse an array in multiple-blocks with ju

2019-08-09 09:50发布

问题:

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?

回答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



标签: cuda reverse