2015-10-08 34 views
0

具体如何排序float3的数组?因此,.x组件是主要的分类标准,.y组件是次要分类标准,而.z组件是第三级分类标准。如何对CUDA矢量类型的数组进行排序

有没有一个简单的解决方案,可以打一个电话cub:: DeviceRadixSortthrust::sort_by_key

目前我想也许我可以创建一个uint32键阵列,其中每个元素的前三分之一的数字取自输入数组的第一个三分之一的组成部分,第一个三分之一的数字取自第一个数字输入阵列的第三个组件为.y,最后三分之一的数字取自输入阵列的第一个三分之一组件.z。还是有更好的解决方案?

+1

您可以使用像[this]这样的方法(http://stackoverflow.com/questions/29597224/sorting-packed-vertices-with-thrust)。你需要'sort_by_key'吗?从这个问题来看,我不清楚你是做什么的。你只需要构建一个推力函子来建立你想要的排序规则。 –

+0

你的权利。我不需要钥匙,我只是认为这将是避免提取单个元素的有用工具。您指出的解决方案看起来非常有用,尽管我对函子不太熟悉。你有很好的参考/例子吗?特别是解释函子论证的东西? – inJeans

+1

函数是一个C++函数对象。如果你谷歌,你会发现许多参考指南,如[这一个](http://www.cprogramming.com/tutorial/functors-function-objects-in-c++.html)。这不是一个推动特定的概念。此外,推力[快速入门指南](https://github.com/thrust/thrust/wiki/Quick-Start-Guide)很有用,但它并不专门针对函子。如果你想进行推力编程,函子是相当重要的工具。但除此之外,先前链接的例子的一个简单的修改可以用来对推力中的“float3”数组进行排序。 –

回答

2

使用exampleRobert 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; 
} 
相关问题