NCCL环境搭建
更新时间:2025-08-20
概览
NCCL(NVIDIA Collective multi-GPU Communication Library)是一个Nvidia专门为多GPU之间提供集合通讯的通讯库,在高性能计算(HPC)和分布式AI训练场景中,NCCL 的性能直接影响多GPU或多节点的通信效率。本文提供一种开箱即用的NCCL性能极简验证方法,通过预置环境与自动化脚本,跳过繁琐流程,直接聚焦高性能计算GPU实例的NCCL性能。
需求场景
- 大规模人工智能训练
- 高性能计算HPC
配置步骤
环境准备
- 已安装 GPU 驱动和 CUDA 工具包,可通过nvidia-smi和nvcc -V检查版本
- 已安装 nvidia_peermem.ko 模块,若未安装请执行 modprode nvidia_peermem 进行安装
- 所有待测试的节点下载并运行集成NCCL环境的容器 此实例将主机的/nfs路径挂载到容器/nfs路径,可按需更改
Plain Text
1#!/bin/bash
2
3wget http://mirrors.baidubce.com/nvidia-binary-driver/scripts/gpu_docker.sh -O gpu_docker.sh && bash gpu_docker.sh
4
5IB_DEVICES=$(find /dev/infiniband/* -maxdepth 1 -not -type d | xargs -I{} echo '--device {}:{}')
6GDRDRV_DEVICES=$(find /dev/gdrdrv -maxdepth 1 -not -type d | xargs -I{} echo '--device {}:{}')
7
8DOCKER_NAME=aixl-test
9IMAGE=registry.baidubce.com/ai-native-dev/aixl:latest
10
11mkdir -p /nfs
12
13docker run ${IB_DEVICES} ${GDRDRV_DEVICES} -v /etc/topo:/etc/topo -v /nfs:/nfs --name $DOCKER_NAME -d --rm -it --gpus=all --cap-add SYS_NICE --cap-add IPC_LOCK --network=host --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 --ulimit nofile=65535 $IMAGE bash
NCCL测试
1、登陆任意一台需要测试的实例,跳转到/nfs路径,创建hostfile文件并记录所有待测试实例的IP地址
Plain Text
1cd /nfs
2echo -e "主机1 IP\n主机2 IP" > /nfs/hostfile #主机 IP替换为实际VPC IP地址
2、创建NCCL执行脚本
Plain Text
1cat > /nfs/nccl_all_reduce.sh << EOF
2NP=\$(( \$(cat /nfs/hostfile | wc -l) * 8 ))
3mpirun --hostfile /nfs/hostfile --allow-run-as-root -np "\$NP" --bind-to numa --map-by ppr:8:node -x NCCL_DEBUG_SUBSYS=init,net,graph,env,tuning -x NCCL_DEBUG=INFO -x NCCL_DEBUG_FILE=/var/log/nccl_log.%h.%p -x NCCL_SOCKET_IFNAME=eth0 -x NCCL_IB_HCA==mlx5_1,mlx5_2,mlx5_3,mlx5_4,mlx5_5,mlx5_6,mlx5_7,mlx5_8 -x NCCL_IB_GID_INDEX=3 -x NCCL_IB_QPS_PER_CONNECTION=8 -x NCCL_IB_TIMEOUT=23 -x NCCL_IB_RETRY_CNT=7 -x UCX_TLS=tcp -x NCCL_IB_TC=162 -x UCX_NET_DEVICES=eth0 -x NCCL_SET_THREAD_NAME=1 -x NCCL_TOPO_DUMP_FILE=/var/log/nccl_topo.xml -x NCCL_IB_ADAPTIVE_ROUTING=1 all_reduce_perf_mpi -b 1G -e 8G -f2 -g1 -t1 -c0 -w 20 -n20
4EOF
3、登陆容器内,并执行命令
Plain Text
1docker exec -it aixl-test bash
2bash /nfs/nccl_all_reduce.sh
得到回显结果,示例如下
Plain Text
1# out-of-place in-place
2# size count type redop time algbw busbw error time algbw busbw error
3# (B) (elements) (us) (GB/s) (GB/s) (us) (GB/s) (GB/s)
4 1024 256 float sum 44.40 0.02 0.04 N/A 43.90 0.02 0.04 N/A
5 2048 512 float sum 45.19 0.05 0.08 N/A 45.04 0.05 0.09 N/A
6 4096 1024 float sum 44.93 0.09 0.17 N/A 45.27 0.09 0.17 N/A
7 8192 2048 float sum 47.20 0.17 0.33 N/A 46.98 0.17 0.33 N/A
8 16384 4096 float sum 65.53 0.25 0.47 N/A 54.88 0.30 0.56 N/A
9 32768 8192 float sum 364.6 0.09 0.17 N/A 117.5 0.28 0.52 N/A
10 65536 16384 float sum 146.0 0.45 0.84 N/A 165.2 0.40 0.74 N/A
11 131072 32768 float sum 155.2 0.84 1.58 N/A 183.7 0.71 1.34 N/A
12 262144 65536 float sum 150.3 1.74 3.27 N/A 148.7 1.76 3.31 N/A
13 524288 131072 float sum 172.0 3.05 5.72 N/A 183.8 2.85 5.35 N/A
14 1048576 262144 float sum 204.6 5.13 9.61 N/A 203.5 5.15 9.66 N/A
15 2097152 524288 float sum 271.8 7.72 14.47 N/A 272.0 7.71 14.46 N/A
16 4194304 1048576 float sum 283.8 14.78 27.72 N/A 284.2 14.76 27.67 N/A
17 8388608 2097152 float sum 317.0 26.47 49.62 N/A 306.3 27.39 51.35 N/A
18 16777216 4194304 float sum 456.7 36.73 68.87 N/A 598.6 28.03 52.55 N/A
19 33554432 8388608 float sum 788.1 42.58 79.84 N/A 798.7 42.01 78.77 N/A
20 67108864 16777216 float sum 1232.4 54.45 102.10 N/A 1228.3 54.64 102.44 N/A
21 134217728 33554432 float sum 2270.0 59.13 110.86 N/A 2547.1 52.69 98.80 N/A
22 268435456 67108864 float sum 4609.7 58.23 109.19 N/A 4645.1 57.79 108.36 N/A
23 536870912 134217728 float sum 8093.6 66.33 124.37 N/A 7971.2 67.35 126.28 N/A
24 1073741824 268435456 float sum 15227 70.51 132.21 N/A 14959 71.78 134.59 N/A
25# Out of bounds values : 0 OK
26# Avg bus bandwidth : 39.4978