0

我开始使用推力。我只是写了一个简单的函数来填充向量,这里我有

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。

顺便说一句,如果我将函数作为模板传递,如何确定函数中的向量类型?

ps 关于thrust::system::detail::generic::uninitialized_fill_n 的错误太多

4

1 回答 1

2

reserve对向量大小没有影响。因此,您的代码没有做任何有用的事情,因为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
$
于 2013-08-22T18:54:34.137 回答