对用户定义结构的 Thrust::device_vector 执行独占扫描。编译错误
我正在修改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将此行更改
为
否则,将一个构造函数放置到接受整数值的结构中,因此扫描操作中的默认值可能会隐式转换为您的结构。
Changing this line
with
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.