安裝mpi4py
mpi4py是一個Python庫,它提供了與MPI(Message Passing Interface)兼容的接口,使得Python程序能夠利用MPI實現并行計算。mpi4py 的核心是基于MPI標準的C/C++實現,它能夠在高性能計算環境下進行高效的并行處理。mpi4py的主要特點:
- 兼容性強:mpi4py 兼容 MPI-1 和 MPI-2 標準,支持廣泛的 MPI 實現,包括 MPICH、Open MPI 等。
- 簡單易用:mpi4py 提供了直觀的 API,用戶可以通過簡單的函數調用實現復雜的并行計算。
- 高性能:由于 mpi4py 底層使用 C 語言實現,能夠充分利用 MPI 的性能優勢。
安裝mpi4py需要mpi支持,編譯安裝mpich或者openmpi以提供支持,個人更傾向于mpich。
apt-get install mpich
yum install mpich
源碼安裝mpich:https://fakerth.blog.csdn.net/article/details/135157375。如下所示,安裝mpi4py失敗的原因基本都是因為系統環境中沒有mpi。
gcc -pthread -B /root/archiconda3/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/root/archiconda3/include/python3.7m -c _configtest.c -o _configtest.o_configtest.c:2:10: fatal error: mpi.h: No such file or directory#include <mpi.h>^~~~~~~compilation terminated.failure.removing: _configtest.c _configtest.oerror: Cannot compile MPI programs. Check your configuration!!!
安裝mpich:
tar -zvxf mpich-4.1.2.tar.gz
cd mpich-4.1.2
./configure
make -j 10
sudo make install
安裝成功后直接安裝mpi4py即可。
如果沒有root權限:
tar -zvxf mpich-4.1.2.tar.gz
cd mpich-4.1.2
./configure --prefix=/home/mpich-install
make -j 10
sudo make install
安裝mpi4py時添加mpi的環境變量:
export PATH=/home/mpich-install/bin/:$PATH
export LD_LIBRARY_PATH=/home/mpich-install/lib/:$LD_LIBRARY_PATH
export CPATH=/home/mpich-install/include/:$CPATH
fakerth@fakerth-IdeaCentre-GeekPro-17IRB:~$ pip install mpi4py
Defaulting to user installation because normal site-packages is not writeable
Collecting mpi4pyDownloading mpi4py-3.1.6.tar.gz (2.4 MB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 1.7 MB/s eta 0:00:00Installing build dependencies ... doneGetting requirements to build wheel ... donePreparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mpi4pyBuilding wheel for mpi4py (pyproject.toml) ... doneCreated wheel for mpi4py: filename=mpi4py-3.1.6-cp310-cp310-linux_x86_64.whl size=2751721 sha256=4e979b26a6f6d72cf358fbb12a3b4c7baee2d33e1abcba7df12c2f04bcbf21f9Stored in directory: /home/fakerth/.cache/pip/wheels/4c/ca/89/8fc1fb1c620afca13bb41c630b1f948bbf446e0aaa4b762e10
Successfully built mpi4py
Installing collected packages: mpi4py
Successfully installed mpi4py-3.1.6
一個簡單的mpi程序測試:

from mpi4py import MPIcomm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()if rank == 0:data = {'key1': [1, 2, 3], 'key2': ('a', 'b', 'c')}comm.send(data, dest=1, tag=11)print(f"Process {rank} sent data: {data}")
elif rank == 1:data = comm.recv(source=0, tag=11)print(f"Process {rank} received data: {data}")
mpirun -n 2 python3 testmpi4py.py
安裝dlio_profiler_py
DLIO Profiler for Python(dlio_profiler_py)是一個用于分析深度學習 I/O 工作負載的 Python 包。當前的 I/O 分析器(例如 Darshan 和 Recorder)不允許執行應用程序級和 I/O 級分析,這使得分析 AI 和工作流等工作負載變得困難。它提供了分析諸如 TensorFlow 和 PyTorch 等深度學習框架的 I/O 模式的實用工具。該分析器有助于理解這些框架的 I/O 行為,這對于優化性能特別重要,特別是在大規模分布式訓練場景中。
pip install dlio_profiler_py
在ubuntu上是可以直接安裝成功的,在redhat上卻需要安裝hwloc。hwloc下載:https://github.com/open-mpi/hwloc
編譯安裝:
autoreconf
./configure --prefix=/home/hwloc-install
make && make install
添加環境變量:
export PATH=/home/hwloc-install/bin/:$PATH
export LD_LIBRARY_PATH=/home/hwloc-install/lib/:$LD_LIBRARY_PATH
fatal error: hwloc.h: No such file or directory
解決方法:
export CPATH=/home/hwloc-install/include/:$CPATH
note:/usr/bin/ld: cannot find -lhwloc
解決方法:
export LIBRARY_PATH=/home/hwloc-install/lib/:$LIBRARY_PATH
$LIBRARY_PATH 是一個環境變量,用于指定編譯器在鏈接階段查找庫文件(如 .so 或 .a 文件)的目錄路徑。設置這個環境變量可以告訴編譯器在哪里找到你需要鏈接的庫文件。