0

具体来说,我如何对数组进行排序float3?这样.x组件是主要排序标准,.y组件是次要排序标准,.z组件是三级排序标准。

是否有一个简单的解决方案可以对cub:: DeviceRadixSortor进行一次调用thrust::sort_by_key

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

4

1 回答 1

3

使用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;
}
于 2015-10-09T00:05:17.797 回答