我开始使用推力。我只写了一个简单的函数来填充向量,这里我有
template <class T> void fillzeros(T &v, int size)
{
v.reserve(size);
thrust::fill(v.begin(), v.end(), 0);
}
void main(void)
{
thrust::device_vector<float> V;
thrust::host_vector<float> W;
fillzeros(V, 100); // works
fillzeros(W, 100); // it doesn't compile, lots of error comes out
// if I want to crease W, I have to do
W = V; // so I must generate device vector in advance?
// I also try the following example shown in the thrust website, still don't compile
thrust::device_vector<int> vv(4);
thrust::fill(thrust::device, vv.begin(), vv.end(), 137);
}
似乎我无法直接创建和分配device_vector。我必须先创建host_vector并将其分配给device_vector。
顺便说一句,如果我把它作为模板传递,我如何确定函数中的矢量类型?
P.S。关于thrust :: system :: detail :: generic :: unintialized_fill_n
的错误太多了答案 0 :(得分:2)
reserve
在矢量大小上有no effect。因此,您的代码没有任何用处,因为W
和V
的大小为零,最终为大小零。这段代码对我很好:
$ cat t224.cu
#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust/fill.h>
#include <thrust/copy.h>
#include <iostream>
template <class T> void fillzeros(T &v, int size)
{
v.resize(size);
thrust::fill(v.begin(), v.end(), 0);
}
int main(void)
{
thrust::device_vector<float> V;
thrust::host_vector<float> W;
fillzeros(V, 100);
fillzeros(W, 100);
std::cout<< "V:" << std::endl;
thrust::copy(V.begin(), V.end(), std::ostream_iterator<float>( std::cout, " "));
std::cout<< std::endl << "W:" << std::endl;
thrust::copy(W.begin(), W.end(), std::ostream_iterator<float>( std::cout, " "));
std::cout<< std::endl;
return 0;
}
$ nvcc -arch=sm_20 -o t224 t224.cu
$ ./t224
V:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
W:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$