暂无图片
暂无图片
2
暂无图片
暂无图片
暂无图片

异构计算实战:PG-Strom编译与安装

原创 FairyFar 2025-02-27
188

本文是《异构计算实战》系列文章第5篇。

编译和安装PG-Strom

一、简介

PG-StromPostgreSQL数据库的扩展模块,专为大规模数据分析设计,通过GPU加速、NVMe SSD直连和Apache Arrow 列式存储技术,显著提升复杂查询性能。

核心功能与技术亮点

  1. GPU 并行计算加速
    • 将 CPU 与 GPU 混合并行,优化聚合、排序、JOIN 等复杂操作,适合 OLAP 场景。
    • 支持将高频访问数据预加载至 GPU 显存,减少数据传输开销。
  2. GPU-Direct I/O over NVMe SSD
    • 绕过传统内存路径,允许 GPU 直接读取NVMe SSD数据,提升吞吐量并降低延迟。
  3. 列式存储优化
    • 自动创建列存副本,加速分析型查询,减少 I/O 开销[。
  4. Apache Arrow 集成
    • 支持 Arrow 列式数据格式,实现与大数据生态工具无缝兼容。
  5. 资源智能分配
    • 动态分配 CPU 与 GPU 任务,平衡计算密集型与轻量级操作。

典型应用场景

  • 实时数据分析:结合流处理框架,加速实时聚合与复杂计算。
  • 机器学习推理:利用 GPU 并行性加速模型推理与特征工程。
  • 大规模 JOIN 操作:通过 GPU 并行处理超大规模表关联。

二、文档

官方文档很详细,这里不再复制粘贴。

PG-Strom Manual

三、编译安装

本地环境硬件太差,换个GPU云主机捣鼓更流畅。

关于云环境的准备过程详见本系列之:使用Kaggle云GPU及SSH登录

注:云主机的环境是Ubuntu。

3.1 编译安装PostgreSQL

安装依赖:

apt install libreadline-dev apt install flex apt install bison

下载REL_16_STABLE分支源码:

mkdir /opt/postgres mkdir /opt/postgres/pg_bin mkdir /opt/postgres/pg_db cd /opt/postgres git clone https://git.postgresql.org/git/postgresql.git cd postgresql git checkout -b REL_16_STABLE origin/REL_16_STABLE

编译Debug版本的PostgreSQL:

./configure --enable-cassert --enable-debug --without-icu --prefix=/opt/postgres/pg_bin/ CFLAGS="-ggdb -O0" make -j4 make install

安装PG:

# 准备一个普通用户,因为PG不能使用root用户启动服务。 adduser yz chown yz:yz -R /opt/postgres/pg_bin/ chown yz:yz -R /opt/postgres/pg_db/ # 切换到普通用户下操作 su - yz vim ~/pg_env.sh # pg_env.sh脚本文件内容如下: export PGHOME=/opt/postgres/pg_bin export PGDATA=/opt/postgres/pg_db PATH=$PGHOME/bin:$PATH:$HOME/bin PG_CONFIG=$PGHOME/bin/pg_config source ~/pg_env.sh initdb -D /opt/postgres/pg_db # 可以这样启动PG: pg_ctl -D /opt/postgres/pg_db -l logfile start

3.2 编译安装pg-strom

下载pg-strom源码:

su - yz mkdir pg-strom cd pg-strom git clone https://github.com/heterodb/pg-strom.git

编译:

source ~/pg_env.sh cd pg-strom/src make make install

配置pg-strom:

https://heterodb.github.io/pg-strom/install/

3.3 宕机问题修复

经测试pg-strom在多款GPU环境下宕机,PG-Strom GPU Service后端进程无法启动。

问题出在nvcc选项不合适,需要去掉--source-in-ptx选项。

需要修改一下代码:

diff --git a/src/Makefile.cuda b/src/Makefile.cuda index ab92a7a6..39056325 100644 --- a/src/Makefile.cuda +++ b/src/Makefile.cuda @@ -38,7 +38,7 @@ endif # flags to build GPU libraries __NVCC_CFLAGS += -I $(shell $(PG_CONFIG) --includedir-server) \ diff --git a/src/Makefile.cuda b/src/Makefile.cuda index ab92a7a6..39056325 100644 --- a/src/Makefile.cuda +++ b/src/Makefile.cuda @@ -38,7 +38,7 @@ endif # flags to build GPU libraries __NVCC_CFLAGS += -I $(shell $(PG_CONFIG) --includedir-server) \ --maxrregcount=$(MAXREGCOUNT) \ - --source-in-ptx -lineinfo \ + -lineinfo \ -DHAVE_FLOAT2 \ -DCUDA_MAXTHREADS_PER_BLOCK=$(MAXTHREADS_PER_BLOCK) \ $(__NVCC_TARGET) diff --git a/src/gpu_service.c b/src/gpu_service.c index 3d714568..f6a1a895 100644 --- a/src/gpu_service.c +++ b/src/gpu_service.c @@ -677,7 +677,7 @@ __rebuild_gpu_fatbin_file(const char *fatbin_dir, appendStringInfo(&cmd, " /bin/sh -x -c '%s/bin/nvcc" " --maxrregcount=%d" - " --source-in-ptx -lineinfo" + " -lineinfo" " -I. -I%s " " -DHAVE_FLOAT2 " " -DCUDA_MAXTHREADS_PER_BLOCK=%u "

问题修复后,PG进程如下:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND yz 10972 0.0 0.0 4248 3624 pts/1 S 11:43 0:00 -bash yz 13610 0.1 0.4 5958516 150996 ? Ss 12:04 0:00 postgres -D pg_db yz 13613 0.0 0.0 5958516 3396 ? Ss 12:04 0:00 postgres: checkpointer yz 13614 0.0 0.1 5958676 43340 ? Ss 12:04 0:00 postgres: background writer yz 13616 0.1 0.5 16557636 168532 ? Ssl 12:04 0:00 postgres: PG-Strom GPU Service yz 13629 0.0 0.0 5958516 20828 ? Ss 12:04 0:00 postgres: walwriter yz 13631 0.0 0.0 5960140 5760 ? Ss 12:04 0:00 postgres: autovacuum launcher yz 13633 0.0 0.0 5960144 6156 ? Ss 12:04 0:00 postgres: logical replication launcher

执行SQL示例:

postgres=# CREATE EXTENSION pg_strom ; CREATE EXTENSION postgres=# explain select t1.a from t1 join t2 on t1.a=t2.a; QUERY PLAN ----------------------------------------------------------------------- Custom Scan (GpuJoin) on t1 (cost=167.38..503.29 rows=32512 width=4) GPU Projection: t1.a GPU Join Quals [1]: (t1.a = t2.a) ... [nrows: 2550 -> 32512] GPU Outer Hash [1]: t1.a GPU Inner Hash [1]: t2.a -> Seq Scan on t2 (cost=0.00..35.50 rows=2550 width=4) (6 rows)
最后修改时间:2025-02-27 09:53:07
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论