zoukankan      html  css  js  c++  java
  • 【CUDA开发】Thrust库

    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_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提供了一个简单的排序函数,该函数只需要提供向量开头和结尾的索引。它把任务分配到不同的线程块上并且执行任何规约和线程间的通信操作。下面举个排序函数的例子:
    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_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;
    }
  • 相关阅读:
    Linux 下杀毒可用工具 clamav
    Docker 添加环境系统文件配置
    Docker 空间大小设置
    Docker 扩容 容器空间大小
    bzoj 1088 DP
    bzoj 1096 斜率优化DP
    spoj p104 Matrix-Tree定理
    bzoj 1016 深搜
    WC后记
    bzoj 1301 后缀数组
  • 原文地址:https://www.cnblogs.com/huty/p/8517917.html
Copyright © 2011-2022 走看看