因为有些矩阵运算常见的pycuda无法满足,需要用到vector,vector在cuda里面就是thrust。
下载thrust官方example。
git clone https://github.com/thrust/thrust.git
——————————————————————————————————————————
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/sequence.h>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/replace.h>
#include <thrust/functional.h>
#include <iostream>
int main(void)
{
thrust::device_vector<int> X(9);
thrust::device_vector<int> Y(9);
thrust::device_vector<int> Z(9);
thrust::sequence(X.begin(), X.end());//赋值给x,是一个排列。
thrust::copy(X.begin(), X.end(), std::ostream_iterator<int>(std::cout, "\n")); //将x的值打印出来
thrust::transform(X.begin(), X.end(), Y.begin(), thrust::negate<int>());//将x的值取负赋值给y
thrust::fill(Z.begin(), Z.end(), 3);//z的值全部都是3
std::cout<<std::endl;
thrust::transform(X.begin(), X.end(), Z.begin(), Y.begin(), thrust::multiplies<int>());//这里面的最后一个参数是运算,可以在头文件里面找。它的意思是把x里面的每个元素乘以z里面的每个元素,最终生成y里的每个元素。相当于矩阵里面的a=np.array([0,1,2,3,4,5,6,7,8]),b=3,c=a*b...
thrust::copy(Y.begin(), Y.end(), std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
我是因为在一个视频流矩阵里面,要对每一个坐标进行矩阵乘法运算,搞了半天,还差一点点。