浅谈CUDA库——Thrust

_梦魇花葬 2014-06-12 05:00:51
加精
Thrust库从C++的STL中得到灵感,将最简单的类似于STL的结构放在Thrust库中,比如STL中的vector。此外,Thrust库还包含STL中的算法和迭代器。
Thrust函数库提供了两个向量容器,分别为主机和设备提供了向量类并且分别驻留在主机和设备的全局内存中。向量可以使用数组下标进行读取或者修改。然而,如果向量在设备上,那么对于每个这样的访问,Thrust通过PCI-E总线在后台执行单独的传输,因此,将这样一个结构放在循环里不是一个好的主意。
Thrust提供了大量的函数类型集合,包括:转换(transformation),规约(reduction),前缀求和(prefix sum),再排序(reordering),排序(sorting)。Thrust并不是传统意义上的函数库,因为它的所有内容都在所包含的头文件中。因此,要避免包含所有的文件。只要包含需要的头文件就行了。
通过如下代码,我们可以创建对应的host_vector和device_vector向量对象:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <iostream>

int main(void) {
// H has storage for 4 integers
thrust::host_vector<int> H(4);
// initialize individual elements
H[0] = 14;
H[1] = 20;
H[2] = 38;
H[3] = 46;
// H.size() returns the size of vector H
std::cout << "H has size " << H.size() << std::endl;
// print contents of H
for(int i = 0; i < H.size(); i++)
std::cout << "H[" << i << "] = " << H[i] << std::endl;

// resize H
H.resize(2);
std::cout << "H now has size " << H.size() << std::endl;

// Copy host_vector H to device_vector D
thrust::device_vector<int> D = H;
// elements of D can be modified
D[0] = 99;
D[1] = 88;
// print contents of D
for(int i = 0; i < D.size(); i++)
std::cout << "D[" << i << "] = " << D[i] << std::endl;
// H and D are automatically deleted when the function returns
return 0;
}

从代码中可以看出,声明一个host_vector和device_vector是很容易的,只要添上对应的头文件,并加上命名空间就可以了。Thrust的vector同C++ STL标准库中vector类似,可以动态改变大小。其它的一些操作可以参看官方文档。

一旦数据在Thrust设备向量或主机向量容器中,我们就可以使用大量Thrust提供的标准函数。比如,Thrust提供了一个简单的排序函数,该函数只需要提供向量开头和结尾的索引。它把任务分配到不同的线程块上并且执行任何规约和线程间的通信操作。下面举个排序函数的例子:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <cstdlib>

#define NUM_ELEM (1024 * 1024)

int main(void)
{
thrust::host_vector<int> host_array(NUM_ELEM);

thrust::generate(host_array.begin(), host_array.end(), rand);
thrust::device_vector<int> device_array = host_array;

thrust::sort(device_array.begin(), device_array.end());
thrust::sort(host_array.begin(), host_array.end());

thrust::host_vector<int> host_array_sorted = device_array;
return 0;
}
...全文
8554 184 打赏 收藏 转发到动态 举报
写回复
用AI写文章
184 条回复
切换为时间正序
请发表友善的回复…
发表回复
WarningCool 2016-11-23
  • 打赏
  • 举报
回复
用向量存储数据和数组存储数据效率上差别有多大?性能如何优化?
Infinite__z 2016-04-25
  • 打赏
  • 举报
回复
您好,我想问一下,cuda中可以这样定义vector<struct> v吗,能否实现自定义的vector
lumanman_ 2015-12-08
  • 打赏
  • 举报
回复
博主,你好,在cuda函数中用到了thrust的流压缩函数remove_if,那么在Linux下编译时,在命令行需要加什么呢?因为我同样的程序,在Windows下没错,在Linux下却报错:no instance of overloaded function "thrust::remove_if""matches the argument list.所以我想在Linux下编译时,是不是需要在命令行加什么?
ryantao2010 2014-07-06
  • 打赏
  • 举报
回复 1
LZ我可以问你个问题吗 我不是很理解既然thrust::device_vector不能作为变量传给__device__的函数,那么device_vector又有什么意义?不是跟host_vector一样只能作为CPU这边的变量吗 我现在想在kernel里用vector,但是CUDA不支持,我以为thrust可以解决这个问题,谁知道device_vector也不能为kernel所用。或许可以?但我不知道该如何操作
tianyuan08_impcas 2014-06-25
  • 打赏
  • 举报
回复
THRUST是很方便,不过,有个致命问题。。。
lxbwelove 2014-06-23
  • 打赏
  • 举报
回复
很好啊,非常好啊
baidu_16779277 2014-06-23
  • 打赏
  • 举报
回复
henhao
qq_16779125 2014-06-23
  • 打赏
  • 举报
回复
引用 49 楼 deideichan 的回复:
大神哇~受教了~~
值得一看的东西啊
qq_16779125 2014-06-23
  • 打赏
  • 举报
回复
好东西啊,值得分享!!!
qq_16794673 2014-06-23
  • 打赏
  • 举报
回复
不错不错~!!
baidu_16794513 2014-06-23
  • 打赏
  • 举报
回复
学习了!!!!
qq_16793821 2014-06-23
  • 打赏
  • 举报
回复
学习一下~ ~~~~
zhengzi008 2014-06-23
  • 打赏
  • 举报
回复
不错,学习一下
tatsuya218 2014-06-22
  • 打赏
  • 举报
回复
不错的介绍 让我们深入了解了,学习了知识
qq_16764253 2014-06-22
  • 打赏
  • 举报
回复
谢谢了,很有用
qq_16764143 2014-06-22
  • 打赏
  • 举报
回复
除部分工程规划处vhg 2014-06-12 17:00:51
qq_16763891 2014-06-22
  • 打赏
  • 举报
回复
很厉害,不错
alex_yang_2008 2014-06-22
  • 打赏
  • 举报
回复
东西不错。谢谢分享
edijayson 2014-06-21
  • 打赏
  • 举报
回复
不错不错,学习了
lcc42 2014-06-21
  • 打赏
  • 举报
回复
第一次学习“cuda”
加载更多回复(164)

579

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧