0
votes

I have the code, which looks like:

...
const N=10000; 
std::array<std::pair <int,int>,N> nnt; 
bool compar(std::pair<int,int> i, std::pair <int,int> j) {return (int) 
(i.second) > (int)(j.second);}
...
int main(int argc, char **argv)
{
  #pragma acc data create(...,nnt)
    {
       #pragma acc parallel loop
         {...}
         //the nnt array is filled here
         //here i need to sort nnt allocated on gpu, using the 
         //comparator compar()
    }
}

So i need to sort an array of pairs, alocated on the GPU by the means of CUDA of OpenAcc. As far as i understood, it is unlikely that i will be able to sort std::array of std::pair's on GPU.

Actually, i need to sort one array, allocated on the gpu, by another one alocated on the gpu, i. e. if there are

int a[N];
int b[N];

which are allocated or copied to the GPU by the means of CUDA or OpenAcc, i need to sort the array a by the values of the array b, and i need this sort to be done on GPU. May be, there are some CUDA functions that will help or the CUDA Thrust sort functions could be used (like thrust::stable_sort), i don't know. Is there a way to do it?

1

1 Answers

2
votes

Is there a way to do it?

yes, one possible method would be to use thrust::sort_by_key, which allows you to sort device data using a device pointer.

This blog explains the method to interface between thrust and OpenACC. Including the passage of a deviceptr between routines.

This example code may be of interest. Specifically, the hash example gives a fully-worked example of calling thrust::sort_by_key from OpenACC.