[CUDA] 使用thrust::sort()函数排序

一、简介

本文介绍了如何使用thrust::sort()函数对device_vector<int>容器,或者int*指针表示的内存(显存)处的数据进行排序。

二、示例代码

1. 排序 device_vector<int>中的数据

main.cu文件内容:

cpp 复制代码
#include <iostream>
#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;
int main()
{

    int min = 0, max = 100;
    random_device seed;                           // 硬件生成随机数种子
    ranlux48 engine(seed());                      // 利用种子生成随机数引擎
    uniform_int_distribution<> distrib(min, max); // 设置随机数范围,并为均匀分布

    int n = 10;
    thrust::host_vector<int> data_host(n);
    thrust::device_vector<int> data_device(n);

    /* 在host_vector上生成随机数 */
    for (int i = 0; i < n; i++)
    {
        data_host[i] = distrib(engine);
    }
    std::cout << "Before sorting:\n";
    for (int i = 0; i < n; i++)
    {
        std::cout << data_host[i] << ",";
    }
    std::cout << "\n";

    /* 在device端进行排序 */

    data_device = data_host;                              // 将host端的数据拷贝到device端
    thrust::sort(data_device.begin(), data_device.end()); // 进行排序
    data_host = data_device;                              // 将排序完成后的device端的数据拷贝回host端

    std::cout << "After sorting:\n";
    for (int i = 0; i < n; i++)
    {
        std::cout << data_host[i] << ",";
    }
    std::cout << "\n";

    return 0;
}

CMakeLists.txt文件内容:

cpp 复制代码
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
project(HelloWolrd LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# CUDA设置
find_package(CUDA REQUIRED)
# 指定生成目标
cuda_add_executable(HelloWorld main.cu)

程序输出结果:

cpp 复制代码
Before sorting:
85,73,8,0,77,68,93,31,85,76,
After sorting:
0,8,31,68,73,76,77,85,85,93,

2. 排序 int*指针中的数据

main.cu文件内容:

cpp 复制代码
#include <iostream>
#include <cuda_runtime.h>
#include <thrust/device_vector.h>
#include <thrust/device_ptr.h>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <algorithm>
#include <random>
#include <chrono>

using namespace std;
int main()
{

    int min = 0, max = 100;
    random_device seed;                           // 硬件生成随机数种子
    ranlux48 engine(seed());                      // 利用种子生成随机数引擎
    uniform_int_distribution<> distrib(min, max); // 设置随机数范围,并为均匀分布

    int n = 10;
    int *data_host = new int[n];
    /* 生成随机数 */
    for (int i = 0; i < n; i++)
    {
        data_host[i] = distrib(engine);
    }
    std::cout << "Befor sorting:\n";
     for (int i = 0; i < n; i++)
    {
        std::cout << data_host[i] << ",";
    }
    std::cout << "\n";    

    /*数据拷贝到device端*/
    int *data_device = nullptr;
    cudaMalloc((void **)&data_device, n * sizeof(int));
    cudaMemcpy(data_device, data_host, n * sizeof(int), cudaMemcpyHostToDevice);

    /*使用thrust::sort()排序*/
    // thrust::sort()只支持迭代器,因此需要将int*指针转为device_ptr<int>容器类型
    thrust::device_ptr<int> t_ptr(data_device);
    thrust::sort(t_ptr, t_ptr + n);

    /*数据拷贝回host端*/
    cudaMemcpy(data_host, data_device, n * sizeof(int), cudaMemcpyDeviceToHost);
    
    std::cout << "After sorting:\n";
     for (int i = 0; i < n; i++)
    {
        std::cout << data_host[i] << ",";
    }
    std::cout << "\n";
    /*释放内存*/
    delete[] data_host;
    cudaFree(data_device);

    return 0;
}

CMakeLists.txt文件内容同上。

程序输出结果:

cpp 复制代码
Befor sorting:
5,82,71,3,90,69,8,0,23,81,
After sorting:
0,3,5,8,23,69,71,81,82,90,

三、自定义排序

更多自定义排序请参考thrust官方代码仓库:NVIDIA/thrust

相关推荐
爱吃涮毛肚的肥肥(暂时吃不了版)40 分钟前
仿腾讯会议——音频服务器部分
c++·qt·面试·职场和发展·音视频·腾讯会议
DpHard1 小时前
自用Vscode 配置c++ debug环境
c++·ide·vscode
hy____1232 小时前
C++多态的详细讲解
开发语言·c++
superior tigre2 小时前
C++学习:六个月从基础到就业——C++20:范围(Ranges)进阶
c++·学习·c++20
小葡萄20252 小时前
黑马程序员C++2024版笔记 第0章 C++入门
开发语言·c++·笔记
不二一3 小时前
linux c++头文件生成源文件 使用python脚本 配置vim快捷键
linux·c++·python
Lazy Dave3 小时前
C++开源库argh使用教程
c++·开源·argh
奇树谦4 小时前
VTK|显示三维图像的二维切片
c++·vtk·qt5·三维显示
老歌老听老掉牙4 小时前
Open CASCADE学习|几何体切片处理:OpenMP与OSD_Parallel并行方案深度解析
c++·学习·open cascade·切片处理
superior tigre5 小时前
C++学习:六个月从基础到就业——C++20:范围(Ranges)基础
c++·学习·c++20