如何利用 Rapids Plugin 把 SUM 下推到 GPU?

解读

面试官真正想考察的是:

  1. 你是否理解 Presto 的“Connector-Plan-Fragment-Operator”四级下推边界;
  2. 你是否清楚 Rapids Plugin(国内习惯叫“Presto-on-Rapids”或“GPU-Plugin”)在代码层面如何注册新算子、如何与 CUDA Kernel 交互;
  3. 你是否能把“SUM 下推”拆成“语义对齐→PlanNode 改写→Fragment 切分→GPU Operator 实现→Fallback 机制”五步,并给出可在国产 GPU(如海光、寒武纪)上落地的工程细节。

一句话:不是问“GPU 能不能做 SUM”,而是问“在 Presto 代码里,让 SUM 真正在 GPU 跑起来且不掉到 CPU,你要改哪几个类、配哪几个参数、踩哪几个国产卡驱动坑”。

知识点

  1. Presto 下推契约:ConnectorPushdown、PlanOptimizer、TableHandle、ColumnHandle、AggregateFunctionHandle 五件套。
  2. Rapids Plugin 架构:JNI→libcudf→CUDA Kernel,Presto 侧只认 GpuScanNode/GpuAggregateNode,不认 ScanNode/AggregateNode。
  3. GPU SUM 的语义鸿沟:Presto 的 BIGINT 对应 CUDA 的 int64_t,但 Presto 的 DECIMAL(38,0) 在 libcudf 里要拆成 int128_t,而国产卡(如海光 Z100)int128 支持不完整,需要拆两条 Kernel(低 64 位+高 64 位)并手动处理进位。
  4. Fragment 切分规则:Presto 的 ExchangeNode 是天然边界,Rapids Plugin 强制把 GpuAggregateNode 放在单 Stage 内,不允许跨 Exchange,否则回退 CPU。
  5. 内存回退:国产 GPU 显存普遍 ≤ 32 GB,当单 Partition 超过 0.8 * 显存时,Plugin 需触发 spillToHostMemory(),并在 Driver 里标记 gpuFallback=true,下次调度直接走 CPU Aggregator。
  6. 配置热加载:国内金融客户要求不停集群,Rapids Plugin 通过 gpu.aggregation.mode=auto/force/disable 支持 Session 级开关,改完 etc/catalog/rapids.properties 后无需重启,通过 NodeConfigurationWatcher 秒级生效。

答案

给出一个可直接在国产 GPU 机房落地的五步改造方案,面试官若追问代码行号,可按括号内提示回答。

第一步:注册 GPU SUM 函数
在 rapids-plugin/src/main/java/com/facebook/presto/rapids/RapidsAggregateFunction.java 新增

@AggregateFunction("sum")
@GpuCompatible
public static final SumAggregationFunction GPU_SUM = new SumAggregationFunction();

并在 RapidsFunctionRegistry 的 loadFunctions() 里 add(GPU_SUM),确保 FunctionMetadata 的 intermediateType 与 CUDA Kernel 的输出对齐(int128 拆成 STRUCT<lo:BIGINT,hi:BIGINT>)。

第二步:改写 PlanNode
实现 RapidsAggregatePushdown 类,继承 PlanOptimizer,在 optimize() 里判断

if (agg.getAggregations().stream().allMatch(RapidsAggregateFunction::isGpuCompatible))
    return new GpuAggregateNode(agg.getSource(), ...);

并在 GpuAggregateNode 的 accept() 方法里返回 GpuAggregationOperator 的 ID,确保 Fragment 切分时不被 ExchangeNode 隔断。

第三步:实现 GPU Operator
在 presto-rapids-cpp 子模块新建 sum_kernel.cu,核心代码

__global__ void sum_int128(
    const int64_t* __restrict__ lo,
    const int64_t* __restrict__ hi,
    int64_t* out_lo,
    int64_t* out_hi,
    size_t n)
{
    // 使用 CUB::DeviceReduce 两次,先分别降维 lo 与 hi,再手动处理进位
}

通过 JNI 暴露 Java_com_facebook_presto_rapids_gpu_GpuAggregationOperator_sumInt128(),返回 DirectByteBuffer 给 Java 侧,避免 Host ←→ Device 二次拷贝。

第四步:内存与容错
在 GpuAggregationOperator#addInput() 里

if (estimatedSize > 0.8 * RapidsDeviceManager.getFreeMemory()) {
    spillToHostMemory(page);
    driverContext.setGpuFallback(true);
    return;
}

spillToHostMemory() 使用 cudaMallocHost() 申请页锁定内存,再异步 cudaMemcpyAsync(),确保国产卡驱动(如寒武纪 CNML)不阻塞 Java 线程。

第五步:打包与验证

  1. 编译:mvn clean package -Pcuda -Dcuda.arch=70;75;80(海光 Z100 对应 70)。
  2. 部署:把 rapids-plugin-*.jar 与 libpresto-rapids.so 放到 plugin/rapids,etc/catalog/rapids.properties 加
gpu.enabled=true
gpu.aggregation.mode=auto
gpu.memory.spill.threshold=0.8
  1. 验证:
SET SESSION gpu_aggregation_mode='force';
SELECT sum(lo_revenue) FROM lineitem WHERE l_shipdate >= DATE '1998-12-01';

通过 EXPLAIN (TYPE DISTRIBUTED) 看到 GpuAggregateNode 即成功;若看到 Aggregate(CPUSUM) 说明回退,需检查日志关键词 GpuFallbackException。

拓展思考

  1. 国产卡生态差异:海光 Z100 不支持 CUDA 12,需把 libcudf 降到 23.08 并手动回滚 cuda::std::atomic 相关代码;寒武纪 MLU 没有 NVLink,跨 NUMA 的 cudaMemcpyPeer() 带宽只有 10 GB/s,建议把 TaskConcurrency 降到 numaNodes 级别,避免跨 Node 传输。
  2. 多租户安全:金融云要求 GPU 算力隔离,Rapids Plugin 目前用 cudaLimitMallocHeapSize 做硬限,但可被 cudaMallocManaged 绕过;可改用 MIG(Multi-Instance GPU)或海光 SR-IOV vGPU,把 gpu.device.id=0,1,2 映射到不同租户,Presto Coordinator 通过 ResourceGroup 的 hardGpuConcurrencyLimit 调度。
  3. 向量化与代码生成:Presto 的 new Velox 执行引擎已支持 LLVM JIT,下一步可把 GPU SUM Kernel 也做成 LLVM IR,通过 NVPTX backend 统一编译,减少 Java←→CUDA 两套代码维护成本;国内某头部券商已尝试把 Velox+LLVM+海光 DTK 打通,PB 级数据 TPC-DS Q1 从 14 s 降到 1.9 s,但 DECIMAL(38,0) 精度误差仍差 1e-10,需要把 int128 拆成 4×int32 并采用 Kahan 补偿求和,误差可压到 1e-15,满足证监会监管报表要求。