【发布时间】:2014-02-17 00:58:24
【问题描述】:
我是 CUDA 开发的新手,我正在尝试使用推力库的排序方法对结构数组进行排序。我的结构是这样的:
#define N 307200
struct distanceVector {
Point3D a, b;
float distance;
};
我想按“距离”对数组进行排序,但是,排序函数需要两个随机访问迭代器,而且由于我没有使用向量,所以我没有任何向量。我试过做这样的事情:
bool distance_sort(distanceVector A, distanceVector B){
return (A.distance > B.distance);
}
distanceVector * MyStructArray;
cudaMalloc((void**)&MyStructArray, sizeof(distanceVector) * N);
//LAUNCH KERNEL WHICH FILLS MYSTRUCTARRAY AND THEN...
thrust::sort(MyStructArray, MyStructArray + N, distance_sort);
...我在 [thrust's guide][1] 中看到了一个示例:
#include <thrust/sort.h>
#include <thrust/functional.h>
...
const int N = 6;
int A[N] = {1, 4, 2, 8, 5, 7};
thrust::stable_sort(A, A + N, thrust::greater<int>());
// A is now {8, 7, 5, 4, 2, 1}
虽然它可以编译,但在执行期间我收到“访问冲突读取位置 0x405e041c”。错误。 调试应用程序时,在 insert_sort.h 文件中的此部分停止:
for(RandomAccessIterator i = first + 1; i != last; ++i)
{
value_type tmp = *i;
if (wrapped_comp(tmp, *first)).......
有没有办法在不使用推力向量的情况下解决这个问题?
【问题讨论】:
-
展示你如何定义
N和MyStructArray。 -
我添加了您建议的更改
标签: c++ arrays sorting cuda thrust