使用example即Robert Crovella建议我制作了以下解决方案。再次感谢Rob。
#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;
}
您可以使用像[this]这样的方法(http://stackoverflow.com/questions/29597224/sorting-packed-vertices-with-thrust)。你需要'sort_by_key'吗?从这个问题来看,我不清楚你是做什么的。你只需要构建一个推力函子来建立你想要的排序规则。 –
你的权利。我不需要钥匙,我只是认为这将是避免提取单个元素的有用工具。您指出的解决方案看起来非常有用,尽管我对函子不太熟悉。你有很好的参考/例子吗?特别是解释函子论证的东西? – inJeans
函数是一个C++函数对象。如果你谷歌,你会发现许多参考指南,如[这一个](http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html)。这不是一个推动特定的概念。此外,推力[快速入门指南](https://github.com/thrust/thrust/wiki/Quick-Start-Guide)很有用,但它并不专门针对函子。如果你想进行推力编程,函子是相当重要的工具。但除此之外,先前链接的例子的一个简单的修改可以用来对推力中的“float3”数组进行排序。 –