在CUDA中使用thrust

在CUDA中使用thrust

本文介绍了在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:我参考了

推荐答案

虽然你没有显示一个完整的代码,基于上面的语句,你做的事情可能不工作,我会期望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.

请注意:

如果由 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时的分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:06