对用户定义结构的 Thrust::device_vector 执行独占扫描。编译错误

发布于 2024-12-23 02:45:47 字数 2606 浏览 1 评论 0原文

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

I am tinkering with the Thrust library provided with CUDA. I was trying to perform inclusive and exclusive scans on a device vector of a user defined struct. Here is the code.

#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;
}

The above code which does an inclusive runs perfectly, giving me the desired result.

Now In the above code I have commented out the exclusive scan.
If I try to run this in place of inclusive scan then I get the following compiler error.

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

What should I do? For reference the result for exclusive scan must be

0 0
2 2
4 4
6 6
8 8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-12-30 02:45:47

将此行更改

  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) {}
     ...
  };

Changing this line

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

with

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

Otherwise, place a constructor to your struct which accepts an integer value, so the default value in the scan operation may be cast to your struct implicitly.

  struct mystruct {
     mystruct(int a) : first(a), second(a) {}
     ...
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文