在 CUDA 中使用推力::排序时出现分段错误 [英] Segmentation error when using thrust::sort in CUDA
问题描述
我试图通过将比较函数作为参数传递给推力排序,根据其类型对类对象数组进行排序.
I am trying to sort an array of class objects based on its type by passing a comparison function as the parameter to the thrust sort.
类定义:
class TetraCutInfo
{
public:
int tetraid;
unsigned int ncutEdges;
unsigned int ncutNodes;
unsigned int type_cut;
__host__ __device__ TetraCutInfo();
};
排序:
thrust::sort(cutInfoptr,cutInfoptr+n,cmp());
cutInfoptr 是 TetraCutInfo 类型的指针,具有使用 cudaMalloc 分配的设备内存地址.
cutInfoptr is a pointer of type TetraCutInfo having the address of the device memory allocated using cudaMalloc.
比较功能
struct cmp
{
__host__ __device__
bool operator()(const TetraCutInfo x, TetraCutInfo y)
{
return (x.type_cut < y.type_cut);
}
};
在运行它时,我遇到了分段错误,但是我能够在另一个内核中迭代 cutInfoptr.
On running this I am getting Segmentation fault, however I am able to iterate through cutInfoptr in another kernel.
PS: 我参考了链接 https://code.google.com/p/thrust/source/browse/examples/sort.cu
推荐答案
cutInfoptr 是 TetraCutInfo 类型的指针,具有使用 cudaMalloc 分配的设备内存地址.
cutInfoptr is a pointer of type TetraCutInfo having the address of the device memory allocated using cudaMalloc.
虽然您没有展示完整的代码,但根据您所做的上述声明,事情可能无法正常工作,并且我预计会出现段错误,因为该指针被取消引用.
Although you haven't shown a complete code, based on the above statement you made, things probably won't work, and I would expect a seg fault as that pointer gets dereferenced.
请注意 thrust 快速入门指南中提供的信息:
Note the information given in the thrust quick start guide:
您可能想知道当原始"指针用作推力函数的参数.与 STL 一样,Thrust 允许这种用法,它将分发算法的主机路径.如果有问题的指针实际上是指向设备内存的指针,那么您需要在调用该函数之前用推力::device_ptr 包装它.
You may wonder what happens when a "raw" pointer is used as an argument to a Thrust function. Like the STL, Thrust permits this usage and it will dispatch the host path of the algorithm. If the pointer in question is in fact a pointer to device memory then you'll need to wrap it with thrust::device_ptr before calling the function.
您引用的 cutInfoptr
,如果由 cudaMalloc
创建,则是原始指针";(这也恰好是一个设备指针).当您将它传递给推力时,推力会看到它是一个原始指针,并分派主机路径".当您传递的(设备)指针在主机路径中的主机代码中被取消引用时,您会遇到段错误.
The cutInfoptr
you referenced, if being created by cudaMalloc
, is a "raw pointer" (which also happens to be a device pointer). When you pass it to thrust, thrust sees that it is a raw pointer, and dispatches the "host path". When the (device) pointer you pass is dereferenced in host code in the host path, you get a seg fault.
一种解决方案是将其包裹在一个推力::device_ptr指针中,这里摘录快速入门指南示例:
One solution is to wrap it in a thrust::device_ptr pointer, excerpting the quick start guide example here:
size_t N = 10;
// raw pointer to device memory
int * raw_ptr;
cudaMalloc((void **) &raw_ptr, N * sizeof(int));
// wrap raw pointer with a device_ptr
thrust::device_ptr<int> dev_ptr(raw_ptr);
// use device_ptr in thrust algorithms
thrust::fill(dev_ptr, dev_ptr + N, (int) 0);
另一种可能的解决方案是使用适当的执行策略进行分派,例如<代码>推力::设备代码>.
Another possible solution is to dispatch with an appropriate execution policy, such as thrust::device
.
这篇关于在 CUDA 中使用推力::排序时出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!