给出一种将CUDA Thrust迁移至rocThrust的宏兼容层

解读

在国内游戏、工业仿真、数字孪生项目里,“一套代码同时跑 NVIDIA 与 AMD 显卡” 是硬需求。CUDA Thrust 与 rocThrust 的 API 表面一致,但头文件、命名空间、设备后端、编译链完全不同。面试官真正想考察的是:

  1. 能否在零运行时开销的前提下,用宏把两套 API 黏合成“单源”;
  2. 能否处理编译单元隔离(CUDA 用 .cu,HIP 用 .cpp);
  3. 能否让 CMake/编译脚本无感切换;
  4. 能否把 Thrust 的定制 allocator、stream、execution policy 一并转过去。

一句话:不是“翻译函数”,而是“让 10 万行老代码不用改调用点”。

知识点

  • CUDACC / HIPCC / HIP_PLATFORM_AMD 三大宏的检测顺序与优先级
  • THRUST_NS / ROC_THRUST_NS 命名空间别名技巧
  • thrust::device / thrust::hip::par 执行策略在 CUDA12 与 ROCm5.6 下的差异
  • host device 与 host device hip 的注解兼容
  • CMake OBJECT_LIBRARY 把 .cu/.cpp 编译成同一接口库
  • #pragma once vs #ifndef 在跨编译器时的坑
  • thrust::counting_iterator<T> 的模板参数在 CUDA 允许 int64_t,rocThrust 必须显式加 THRUST_ROC_64BIT 宏
  • 自定义宏的“二次展开”陷阱:#define THRUST_FN(x) thrust::x 在遇到 thrust::transform_if 时会把 :: 解析错误

答案

给出可直接落地的“单头文件”宏兼容层 thrust_adapt.hpp,并配套 CMake 片段。

// thrust_adapt.hpp
#pragma once

// 1. 检测后端
#if defined(__CUDACC__)
    #define THRUST_BACKEND_CUDA 1
    #include <thrust/version.h>
#elif defined(__HIPCC__) && defined(__HIP_PLATFORM_AMD__)
    #define THRUST_BACKEND_ROC 1
    // 强制 rocThrust 暴露 CUDA 风格 API
    #define THRUST_HIP_BACKEND 1
    #include <rocthrust/version.h>
#else
    #error "Only CUDA or ROCm/HIP compiler supported"
#endif

// 2. 统一命名空间
#ifdef THRUST_BACKEND_CUDA
    #define THRUST_NS thrust
#elif defined(THRUST_BACKEND_ROC)
    #define THRUST_NS thrust::hip
#endif

// 3. 统一执行策略
#ifdef THRUST_BACKEND_CUDA
    #define THRUST_DEVICE   thrust::device
    #define THRUST_HOST     thrust::host
#elif defined(THRUST_BACKEND_ROC)
    #define THRUST_DEVICE   thrust::hip::par
    #define THRUST_HOST     thrust::host
#endif

// 4. 统一算法宏(二次展开技巧)
#define THRUST_WRAP2(func)  THRUST_NS::func
#define THRUST_WRAP(func)   THRUST_WRAP2(func)

// 5. 常用算法快捷宏,保持调用点零改动
#define thrust_for_each     THRUST_WRAP(for_each)
#define thrust_transform    THRUST_WRAP(transform)
#define thrust_reduce       THRUST_WRAP(reduce)
#define thrust_sort         THRUST_WRAP(sort)

// 6. 迭代器 & 内存
#define thrust_device_vector    THRUST_NS::device_vector
#define thrust_host_vector      THRUST_NS::host_vector
#define thrust_counting_iterator THRUST_NS::counting_iterator

// 7. 自定义 allocator 透传
#ifdef THRUST_BACKEND_CUDA
    template<typename T>
    using thrust_device_allocator = thrust::cuda::experimental::pinned_allocator<T>;
#elif defined(THRUST_BACKEND_ROC)
    template<typename T>
    using thrust_device_allocator = thrust::hip::allocator<T>;
#endif

CMake 片段(国内常用 Ninja + Clang/hipcc 双链):

if(USE_CUDA)
    enable_language(CUDA)
    add_library(my_thrust_impl OBJECT src/thrust_algo.cu)
    target_compile_definitions(my_thrust_impl PRIVATE THRUST_BACKEND_CUDA)
elseif(USE_ROCM)
    find_package(hip REQUIRED)
    set_source_files_properties(src/thrust_algo.cpp PROPERTIES LANGUAGE HIP)
    add_library(my_thrust_impl OBJECT src/thrust_algo.cpp)
    target_compile_definitions(my_thrust_impl PRIVATE THRUST_BACKEND_ROC)
    target_link_libraries(my_thrust_impl hip::device)
endif()

调用侧永远只写:

#include "thrust_adapt.hpp"
thrust_device_vector<float> d_vec(1024);
thrust_transform(d_vec.begin(), d_vec.end(), d_vec.begin(), []__host__ __device__(float x){ return x*2; });

零调用点改动,切换编译链即可在 A100 与 MI250 上跑出相同结果。

拓展思考

  1. 性能陷阱:rocThrust 的 thrust::hip::par 默认不开启 async,需要手动加 thrust::hip::par.on(stream),而 CUDA 12 的 thrust::device 已经隐式异步。兼容层可再封一层 THRUST_ASYNC(policy, stream) 宏,把 stream 参数透传下去。
  2. C++17 以上:用 inline constexpr 变量模板代替宏,能保留 IDE 跳转与重载决议,但国内很多项目仍卡 C++14,宏方案最稳。
  3. 编译吞吐:AMD 机器上若用 hipcc 直接编译 .cpp,会触发 clang 前端两次(host+device),可把 thrust 算法文件拆成 .hip 后缀并加 -x hip 显式指定,减少 30% 编译时间。
  4. CI 双跑:在 GitLab 国内自建 Runner 里,用 docker 镜像 nvidia/cuda:12.2-devel-ubuntu22.04 与 rocm/dev-ubuntu-22.04:5.7 做双阶段构建,保证每次 MR 都同时过 cuThrust 与 rocThrust,防止“宏兼容层”被偷偷击穿。