新建CUDA工程,新建thrustTest.cpp,键入如下代码:
#include <thrust/device_ptr.h>
#include <thrust/device_vector.h>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/functional.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
void thrustTest()
{
{
// allocate three device_vectors with 10 elements
thrust::device_vector<int> X(10);
thrust::device_vector<int> Y(10);
thrust::device_vector<int> Z(10);
// initialize X to 0,1,2,3, ....
thrust::sequence(X.begin(), X.end());
// compute Y = -X
thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>());
// fill Z with twos
thrust::fill(Z.begin(), Z.end(), 2);
// compute Y = X mod 2
// thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::modulus<int>());
}
}
编译会出现如下错误: ../for_each.inl(96) : error C2027: use of undefined type 'thrust::detail::STATIC_ASSERTION_FAILURE<x>'
解决办法:
右键thrustTest.cpp,选择属性,在“常规”->“项类型”里选择 CUDA C/C++,确定,再重新编译即可。
需要注意的是,使用CUDA C/C++来编译的话,有些代码可能会编译不通过,例如我在thrustTest.cpp里添加Eigen库的调用,就编译不通过,解决办法是将cuda相关的代码与其他代码文件上分离。