1

CUDAで提供されているThrustライブラリをいじっています。ユーザー定義の構造体のデバイス ベクトルに対して包括的スキャンと排他的スキャンを実行しようとしました。これがコードです。

#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".

私は何をすべきか?参考までに、排他的スキャンの結果は

0 0
2 2
4 4
6 6
8 8
4

1 に答える 1

1

この行を変更する

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

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

それ以外の場合は、整数値を受け入れるコンストラクターを構造体に配置して、スキャン操作のデフォルト値が構造体に暗黙的にキャストされるようにします。

  struct mystruct {
     mystruct(int a) : first(a), second(a) {}
     ...
  };
于 2013-06-04T13:17:34.560 に答える