在CUDA中使用thrust :: sort时的分段错误 [英] Segmentation error when using thrust::sort in CUDA

查看:414
本文介绍了在CUDA中使用thrust :: sort时的分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将比较函数作为参数传递给推力排序,根据类型对象的数组进行排序。

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.

定义::

The class defination:

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);
  }
};

运行时,我得到Segmentation错误,但是我可以在另一个内核中迭代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.

虽然你没有显示一个完整的代码,基于上面的语句,你做的事情可能不工作,我会期望seg故障

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函数的参数。像STL一样,Thrust允许这种用法,它将调度算法的主机路径。如果有问题的指针实际上是一个指向设备内存的指针,那么在调用函数之前你需要使用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.

如果由 cudaMalloc 创建的 cutInfoptr 是一个原始指针恰好是一个设备指针)。当你把它传递给推力,推力看到它是一个原始指针,并分派主机路径。当你传递的(device)指针在主机路径的主机代码中被解引用时,你会得到一个seg错误。

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.

解决方案是将它包装在一个thrust :: device_ptr指针,摘录此处的快速入门指南示例:

The 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);

这篇关于在CUDA中使用thrust :: sort时的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆