没有重载函数的实例 "thrust::remove_if" 与参数列表匹配

No instance of overloaded function "thrust::remove_if" matches the argument list

我用remove_if写过程序。它使用由 cudaMalloc 分配并由程序的前一部分(在设备中)填充的数组。删除阵列后将由下一部分使用(在设备中;无推力)。我想避免任何复制设备主机、主机设备。我用这个例子: https://github.com/thrust/thrust/blob/master/examples/cuda/wrap_pointer.cu

Nvcc 写道: ** "remove_if.cu(19): error: no instance of overloaded function "thrust::remove_if" 匹配参数列表 参数类型为:(thrust::device_ptr、thrust::device_ptr、is_zero)”。**

我写了一个简单的程序示例,但有同样的错误:

#include <stdio.h>
#include "book.h"
#include <thrust/remove.h>
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
int main(void)
{
  int *dev_lsubpix;
  struct is_zero
  {
    __host__ __device__
    bool operator()(int x)
    {
      return (x<1);
    }
  };
HANDLE_ERROR( cudaMalloc((void**)&dev_lsubpix, 10*sizeof(int)));
thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(dev_lsubpix);
  int new_length=     thrust::remove_if(dev_ptr, dev_ptr+10, is_zero())-dev_ptr;
  cudaFree(dev_lsubpix);
}

虽然错误的原因不是很明显,但问题在于您尝试使用的谓词函子的范围。因为您已经在 main 范围内声明了函子,所以它不是 main 之外的有效类型,并且编译器不知道如何处理匿名类型。

如果您像这样重构代码:

#include <stdio.h>
#include <thrust/remove.h>
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
struct is_zero
{
    __host__ __device__
        bool operator()(int x)
        {
            return (x<1);
        }
};

int main(void)
{
    int *dev_lsubpix;
    cudaMalloc((void**)&dev_lsubpix, 10*sizeof(int));
    thrust::device_ptr<int> dev_ptr = thrust::device_pointer_cast(dev_lsubpix);
    int new_length = thrust::remove_if(dev_ptr, dev_ptr+10, is_zero())-dev_ptr;
    cudaFree(dev_lsubpix);
}

这样仿函数定义在全局范围内,我想你会发现代码编译正确。