2011-12-26 4 views
1

私はCUDAに付属しているThrustライブラリを試しています。私はユーザー定義の構造体のデバイスベクトルに包括的で排他的なスキャンを実行しようとしていました。ここにコードがあります。ユーザー定義の構造体のthrust :: device_vectorで排他スキャンを実行します。コンパイラエラー

#include <iostream> 
#include <thrust/copy.h> 
#include <thrust/count.h> 
#include <thrust/device_vector.h> 
#include <thrust/fill.h> 
#include <thrust/functional.h> 
#include <thrust/host_vector.h> 
#include <thrust/replace.h> 
#include <thrust/scan.h> 
#include <thrust/sequence.h> 
#include <thrust/transform.h> 
#include <thrust/version.h> 
#include <vector> 

struct mystruct 
{ 
    int first; 
    int second; 

}; 


//Overload the + operator for the used defined struct 
    __host__ __device__ 
    mystruct operator + (mystruct a, mystruct b) 

    { 
    mystruct c; 
    c.first =a.first +b.first; 
    c.second=a.second+b.second; 
    return c; 
    } 



int main(void) 
{ 

    thrust::host_vector<mystruct> host_vec(5); 
    thrust::device_vector<mystruct> dev_vec(5); 

    host_vec[0].first=2 ; host_vec[0].second=2 ; 
    host_vec[1].first=2 ; host_vec[1].second=2 ; 
    host_vec[2].first=2 ; host_vec[2].second=2 ; 
    host_vec[3].first=2 ; host_vec[3].second=2 ; 
    host_vec[4].first=2 ; host_vec[4].second=2 ; 

    thrust::copy(host_vec.begin(), host_vec.end(), dev_vec.begin());//copy to device 
    thrust::inclusive_scan(dev_vec.begin(), dev_vec.end(), dev_vec.begin()); //In-place inclusive scan 
    //thrust::exclusive_scan(dev_vec.begin(), dev_vec.end(), dev_vec.begin()); //In-place exclusive scan 

    std::cout<<"The inclusive scanned mystruct vector is "<<std::endl;//Print the scan 
    thrust::copy(dev_vec.begin(),dev_vec.end(),host_vec.begin());//copy back to host 
    for (int i = 0; i < host_vec.size(); ++i)//print the scan 
    { 
    std::cout<< host_vec[i].first<<" "<< host_vec[i].second << std::endl; 
    } 



return 0; 
} 

上記のコードは完全に実行され、私に望ましい結果が得られます。

私は上記のコードで排他スキャンをコメントアウトしました。 これを実行しようとすると、の包括スキャンの場所にがあると、次のコンパイラエラーが発生します。

Desktop: nvcc temp.cu 
/usr/local/cuda/bin/../include/thrust/detail/scan.inl(68): error: no suitable constructor exists to convert from "int" to "mystruct" 
      detected during instantiation of "OutputIterator thrust::exclusive_scan(InputIterator, InputIterator, OutputIterator) [with InputIterator=thrust::detail::normal_iterator<thrust::device_ptr<mystruct>>, OutputIterator=thrust::detail::normal_iterator<thrust::device_ptr<mystruct>>]" 
temp.cu(54): here 

1 error detected in the compilation of "/tmp/tmpxft_00003330_00000000-4_temp.cpp1.ii". 

どうすればよいですか?参考のために排他的なスキャンの結果は、そうでない場合

thrust::inclusive_scan(dev_vec.begin(), dev_vec.end(), 
         dev_vec.begin(), mystruct(), thrust::plus<mystruct>()); 

で、このライン

thrust::inclusive_scan(dev_vec.begin(), dev_vec.end(), dev_vec.begin()); 

を変更

0 0 
2 2 
4 4 
6 6 
8 8 
+3

エラーメッセージはかなり明白です。クラスにはコンストラクタがありません。このコンストラクタもデバイスとホスト関数である必要があります。 – talonmies

+3

あなたが使用している '' 'exclusive_scan''のバージョンは、暗黙的に、合計の初期値が' '' 0'''であると仮定しています。だから、それをそういうものにしたいのであれば、 '' 'mystruct'''に、' '' first''と '' second''の両方を初期化する '' int''を使うコンストラクタを与える必要があります。 ''。 –

答えて

1

でなければならない整数値を受け入れ、あなたの構造体にコンストラクタを置くので、スキャン操作のデフォルト値は、構造体に暗黙的にキャストされます。

struct mystruct { 
    mystruct(int a) : first(a), second(a) {} 
    ... 
    }; 
関連する問題