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();
};
Sort:
thrust::sort(cutInfoptr,cutInfoptr+n,cmp());
cutInfoptr is a pointer of type TetraCutInfo having the address of the device memory allocated using cudaMalloc.
Comparison function
struct cmp
{
__host__ __device__
bool operator()(const TetraCutInfo x, TetraCutInfo y)
{
return (x.type_cut < y.type_cut);
}
};
On running this I am getting Segmentation fault, however I am able to iterate through cutInfoptr in another kernel.
PS: I referred to the example in the link https://code.google.com/p/thrust/source/browse/examples/sort.cu
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.
Note the information given in the thrust quick start guide:
The
cutInfoptr
you referenced, if being created bycudaMalloc
, 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.The solution is to wrap it in a thrust::device_ptr pointer, excerpting the quick start guide example here:
Segfaults are far more likely to be caused by the host code, and I would suggest checking the CPU codepath first.