|
thrust简单示例:版本、主机与设备向量的定义、初始化、fill、copy
thrust入门指南(并行算法库,类似C++的STL)
Thrust: Code at the speed of light 推力:代码光速运行
https://github.com/NVIDIA/thrust
设置VScode支持.cu文件语法高亮及跳转的方法: 搜索插件cudacpp
问题:*.cu在VScode不能像*.cc或*.cpp一样在c++及c++ intelligence插件有关键字的高亮以及go to definition等的操作
解决方案:添加*.cu与*.cpp文件的关联
在搜索框中输入 “files.associations”,得到查找结果 添加字典 好像不行!
可以通过nvcc --version在命令行上运行来确认已安装CUDA
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Sun_Jul_28_19:07:16_PDT_2019
Cuda compilation tools, release 10.1, V10.1.243
如果您使用的是CUDA 4.0或更高版本,那么您的系统上已经安装了Thrust
Thrust提供两个矢量容器,host_vector和device_vector
- #include <thrust/version.h>
- #include <thrust/host_vector.h>
- #include <thrust/device_vector.h>
- #include <thrust/copy.h>
- #include <thrust/fill.h>
- #include <thrust/sequence.h>
- #include <iostream>
- int main(void)
- {
- int major = THRUST_MAJOR_VERSION;
- int minor = THRUST_MINOR_VERSION;
- int submimor = THRUST_SUBMINOR_VERSION;
- int version = THRUST_VERSION;
- //Thrust v1.9.6
- //1.9.6 CUDA Toolkit 10.1 Update 2
- std::cout << "Thrust v" << major << "." << minor<< "." << submimor << std::endl;
- thrust::device_vector<int> D(10,1); // 初始化长度为10的gpu设备向量D 值均是1
- thrust::fill(D.begin(),D.begin()+7,9); // 填充前7位的值为9
- thrust::host_vector<int> H(D.begin(),D.begin()+5); // 用D的前5个值初始化 主机向量H
- thrust::sequence(H.begin(),H.end()); // 将主机向量H设置一个序列
- thrust::copy(H.begin(),H.end(),D.begin()); // 向量之间的拷贝 H主机向量 到 D设备向量的开头
- //该copy函数可用于将一系列主机或设备元素复制到另一个主机或设备向量
- for(int i=0;i<D.size();i++){
- //printf("值:%d,",int(D[i]));
- std::cout << "d=" << D[i] << std::endl;
- }
- for(int i=0;i<H.size();i++){
- std::cout << "h=" << H[i] << std::endl;
- }
- return 0;
- }
复制代码
|
|