zoukankan      html  css  js  c++  java
  • cuda中利用Thrust库做排序

    Thrust是cuda自带的c++库,cuda安装好之后,这个库也默认安装好了。

    这个库基本是采用类似STL的接口方式,因此对于开发者非常友好,开发者不再需要关注内存与显存相关的问题了。

    下面是一个简单的排序代码 main.cu:

    #include <thrust/host_vector.h>
    #include <thrust/device_vector.h>
    #include <thrust/generate.h>
    #include <thrust/sort.h>
    #include <thrust/copy.h>
    #include <algorithm>
    #include <vector>
    #include <time.h>
    
    int main(void)
    {
        thrust::host_vector<int> h_vec(1024*1024);
        std::generate(h_vec.begin(), h_vec.end(), rand);
    
        std::vector<int> vec(h_vec.size());
        thrust::copy(h_vec.begin(), h_vec.end(), vec.begin());
    
        thrust::device_vector<int> d_vec = h_vec;
    
        clock_t time1,time2;
    
        time1 = clock();
        thrust::sort(d_vec.begin(), d_vec.end());
        time2 = clock();
        std::cout<<(double)(time2-time1)/CLOCKS_PER_SEC<<std::endl;
    
        time1 = clock();
        std::sort(vec.begin(),vec.end());
        time2 = clock();
        std::cout<<(double)(time2-time1)/CLOCKS_PER_SEC<<std::endl;
    
        time1 = clock();
        thrust::sort(h_vec.begin(), h_vec.end());
        time2 = clock();
        std::cout<<(double)(time2-time1)/CLOCKS_PER_SEC<<std::endl;
    
        return 0;
    }

    结果如下:

    可以看出加速性能还是很好的。

    参考:https://docs.nvidia.com/cuda/thrust/

  • 相关阅读:
    Openrasp源码分析
    feifeicms后台任意文件读取
    python之迭代器和生成器
    java之导入excel
    jquery单击事件的写法
    java之高效操作文件
    多条件搜索优化sql
    java之代码复用
    java之接口文档规范
    eclipse之常用快捷键
  • 原文地址:https://www.cnblogs.com/tiandsp/p/12405713.html
Copyright © 2011-2022 走看看