So, now my question is, is there any way to make thrust::sort() function work with cufftComplex when the data already copied to the device?
Yes.
Thrust provides versions of the sort
operation that accept user defined comparison operators, to order the data any way you wish.
An example usage with a custom functor is given in the thrust sorting example (using the evens_before_odds
functor).
So you'll need a comparison functor. I don't know how you sort complex values, but as an example if you wanted to sort them based on the magnitude you could do:
#include <cuComplex.h>
#include <cufft.h>
struct my_complex_sort
{
__host__ __device__
bool operator()(cufftComplex x, cufftComplex y)
{
return (cuCabs(x) > cuCabs(y));
}
};
If you already have cufftComplex
data on the device, you could create a thrust pointer for it. Let's suppose the pointer to your device data is d_data
and the data size is N
:
thrust::device_ptr<cufftComplex> dp_data = thrust::device_pointer_cast(d_data);
Then you could sort the data with:
thrust::sort(dp_data, dp_data+N, my_complex_sort());
(disclaimer: coded in browser, not tested)