成都山猫vi设计公司,苏州关键词优化企业,兰州建网站,区块链网站建设方案Thrust库从C的STL中得到灵感#xff0c;将最简单的类似于STL的结构放在Thrust库中#xff0c;比如STL中的vector。此外#xff0c;Thrust库还包含STL中的算法和迭代器。Thrust函数库提供了两个向量容器#xff0c;分别为主机和设备提供了向量类并且分别驻留在主机和设备的全… 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向量对象 C/C code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include thrust/host_vector.h #include thrust/device_vector.h #include iostream int main(void) { // H has storage for 4 integers thrust::host_vectorint 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_vectorint 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提供了一个简单的排序函数该函数只需要提供向量开头和结尾的索引。它把任务分配到不同的线程块上并且执行任何规约和线程间的通信操作。下面举个排序函数的例子 C/C code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #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_vectorint host_array(NUM_ELEM); thrust::generate(host_array.begin(), host_array.end(), rand); thrust::device_vectorint device_array host_array; thrust::sort(device_array.begin(), device_array.end()); thrust::sort(host_array.begin(), host_array.end()); thrust::host_vectorint host_array_sorted device_array; return 0; } 转载于:https://www.cnblogs.com/huty/p/8517917.html