使用Robert Crovella建议的示例,我制定了以下解决方案。再次感谢罗布。
#include <thrust/sort.h>
#include <thrust/device_ptr.h>
struct sort_float3 {
__host__ __device__
bool operator()(const float3 &a, const float3 &b) const {
if (a.x <= b.x && a.y <= b.y && a.z < b.z) return true;
else if (a.x <= b.x && a.y < b.y) return true;
else if (a.x < b.x) return true;
else return false;
}
};
int main(void)
{
float3 *h_array;
// Define your host array
float3 *d_array;
cudaMallocHost( (void**)&d_array,
number_of_elements * sizeof(float3) );
cudaMemcpy( d_array,
h_array,
number_of_elements * sizeof(float3),
cudaMemcpyHostToDevice );
thrust::device_ptr<float3> th_array( d_array );
thrust::sort( th_array,
th_array+number_of_elements ,
sort_float3() );
return 0;
}