栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

CUDA中使用thrust进行排序和注意事项

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

CUDA中使用thrust进行排序和注意事项

在CUDA中进行排序时,如果用自定义cuda十分耗时,我们选择cuda自定义函数进行测试,选择thrust::sort函数,新建文件,命名为sort.cu,代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(void)
{
    thrust::host_vector h_vec(10000*20);
    std::generate(h_vec.begin(), h_vec.end(), rand);

    std::vector vec(h_vec.size());
    thrust::copy(h_vec.begin(), h_vec.end(), vec.begin());

    thrust::device_vector 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< 

新建CMakeLists.txt,添加内容如下:

CMAKE_MINIMUM_REQUIRED(VERSION 3.5)
PROJECT(thrust_examples)
set(CMAKE_BUILD_TYPE Release)
find_package(CUDA)
include_directories(${CUDA_INCLUDE_DIRS})
message(STATUS "${CUDA_INCLUDE_DIRS}")
message(STATUS "${CUDA_LIBRARIES}")
cuda_add_executable(thrust_examples sort.cu)

 注意事项:

1、引用thrust时,一定要写在.cu文件中,并且编译成cuda,详细见:

c++ - Thrust Static Assertion when using in cpp files - Stack Overflow

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690292.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号