在用户定义的struct的thrust :: device_vector上执行独占扫描。编译错误

时间:2011-12-26 23:13:18

标签: cuda thrust

我正在修补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

1 个答案:

答案 0 :(得分: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中,因此scan操作中的默认值可能会隐式地转换为您的struct。

  struct mystruct {
     mystruct(int a) : first(a), second(a) {}
     ...
  };