建立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相關的代碼與其他代碼檔案上分離。