2016-03-07 96 views
0

我已經用remove_if編寫了程序。它使用由cudaMalloc分配的數組,並由程序的前一部分填充(在設備中)。刪除陣列後將被下一部分使用(在設備中;無推力)。我想避免任何複製設備主機,主機設備。我用這個例子: https://github.com/thrust/thrust/blob/master/examples/cuda/wrap_pointer.cu沒有重載函數「thrust :: remove_if」的實例匹配參數列表

NVCC寫道: **「remove_if.cu(19):錯誤:沒有重載函數實例 「推力::的remove_if」 相匹配的參數列表 參數類型有:(推力: :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); 
} 

回答

2

雖然不是很明顯,爲什麼從錯誤,問題在於謂詞函子的你試圖範圍使用。由於您已將範函聲明爲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); 
} 

使仿函數的定義是在全球範圍內,我想你會發現代碼編譯正確。

相關問題