--- title: mpi4py安裝使用說明 | zh tags: Guide, TWNIA3, TW GA: --- <p style="text-align:right;">更新日期:Feb. 29, 2024</p> # mpi4py安裝使用 :::warning :warning: 執行環境須與編譯安裝的環境設定一致,勿混用!!! * 使用conda安裝者,請以conda環境執行。 * 使用pip方式安裝者,請以安裝時的環境設定執行。 ::: [TOC] ## conda 安裝方式與執行 透過conda安裝是最簡單快速,缺點是mpi4py、MPI library版本受限於conda版本,無法隨意選擇版本組合。 1. 登入系統載入conda模組,並建立欲執行的環境 :::spoiler 點此查看範例,以下同。 ``` [demo_user@lgn301 ~]$ ml pkg/Anaconda3 # 載入conda模組 (base) [demo_user@lgn301 ~]$ conda create -n demo # 建立conda demo環境 Collecting package metadata (current_repodata.json): done Solving environment: done ...(略)... ## Package Plan ## environment location: /home/demo_user/.conda/envs/demo Proceed ([y]/n)? y Preparing transaction: done Verifying transaction: done Executing transaction: done # # To activate this environment, use # # $ conda activate demo # # To deactivate an active environment, use # # $ conda deactivate ``` ::: 2. 安裝MPI library 可省略此一步驟,直接安裝mpi4py,若之前未安裝過MPI library,預設是安裝mpich。 :::spoiler 範例 ``` [demo_user@lgn301 ~]$ conda activate demo #啟用 demo 的conda環境設定 (demo) [demo_user@lgn301 ~]$ conda install openmpi # 安裝OpenMPI Collecting package metadata (current_repodata.json): done Solving environment: done ...(略)... ## Package Plan ## environment location: /home/demo_user/.conda/envs/demo added / updated specs: - openmpi The following packages will be downloaded: package | build ---------------------------|----------------- _openmp_mutex-5.1 | 1_gnu 21 KB libgcc-ng-11.2.0 | h1234567_1 5.3 MB libgfortran-ng-7.5.0 | ha8ba4b0_17 22 KB libgfortran4-7.5.0 | ha8ba4b0_17 995 KB libgomp-11.2.0 | h1234567_1 474 KB libstdcxx-ng-11.2.0 | h1234567_1 4.7 MB mpi-1.0 | openmpi 7 KB openmpi-4.0.2 | hb1b8bf9_1 3.5 MB zlib-1.2.13 | h5eee18b_0 103 KB ------------------------------------------------------------ Total: 15.1 MB The following NEW packages will be INSTALLED: _libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main _openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1 libgfortran-ng pkgs/main/linux-64::libgfortran-ng-7.5.0-ha8ba4b0_17 libgfortran4 pkgs/main/linux-64::libgfortran4-7.5.0-ha8ba4b0_17 libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1 libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1 mpi pkgs/main/linux-64::mpi-1.0-openmpi openmpi pkgs/main/linux-64::openmpi-4.0.2-hb1b8bf9_1 zlib pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0 Proceed ([y]/n)? y Downloading and Extracting Packages _openmp_mutex-5.1 | 21 KB | ############################### | 100% mpi-1.0 | 7 KB | ############################### | 100% libgcc-ng-11.2.0 | 5.3 MB | ############################### | 100% zlib-1.2.13 | 103 KB | ############################### | 100% libstdcxx-ng-11.2.0 | 4.7 MB | ############################### | 100% openmpi-4.0.2 | 3.5 MB | ############################### | 100% libgomp-11.2.0 | 474 KB | ############################### | 100% libgfortran-ng-7.5.0 | 22 KB | ############################### | 100% libgfortran4-7.5.0 | 995 KB | ############################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done ``` ::: 3. 安裝mpi4py :::spoiler 範例 ``` (demo) [demo_user@lgn301 ~]$ conda install mpi4py # 安裝mpi4py Collecting package metadata (current_repodata.json): done Solving environment: done ...(略))... ## Package Plan ## environment location: /home/demo_user/.conda/envs/demo added / updated specs: - mpi4py The following packages will be downloaded: package | build ---------------------------|----------------- bzip2-1.0.8 | h5eee18b_5 262 KB ca-certificates-2023.12.12 | h06a4308_0 126 KB ld_impl_linux-64-2.38 | h1181459_1 654 KB libffi-3.4.4 | h6a678d5_0 142 KB libuuid-1.41.5 | h5eee18b_0 27 KB mpi4py-3.1.4 | py311h3e5f7c9_0 541 KB ncurses-6.4 | h6a678d5_0 914 KB openssl-3.0.13 | h7f8727e_0 5.2 MB pip-23.3.1 | py311h06a4308_0 3.3 MB python-3.11.8 | h955ad1f_0 32.9 MB readline-8.2 | h5eee18b_0 357 KB setuptools-68.2.2 | py311h06a4308_0 1.2 MB sqlite-3.41.2 | h5eee18b_0 1.2 MB tk-8.6.12 | h1ccaba5_0 3.0 MB tzdata-2024a | h04d1e81_0 116 KB wheel-0.41.2 | py311h06a4308_0 145 KB xz-5.4.6 | h5eee18b_0 651 KB ------------------------------------------------------------ Total: 50.7 MB The following NEW packages will be INSTALLED: bzip2 pkgs/main/linux-64::bzip2-1.0.8-h5eee18b_5 ca-certificates pkgs/main/linux-64::ca-certificates-2023.12.12-h06a4308_0 ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1 libffi pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0 libuuid pkgs/main/linux-64::libuuid-1.41.5-h5eee18b_0 mpi4py pkgs/main/linux-64::mpi4py-3.1.4-py311h3e5f7c9_0 ncurses pkgs/main/linux-64::ncurses-6.4-h6a678d5_0 openssl pkgs/main/linux-64::openssl-3.0.13-h7f8727e_0 pip pkgs/main/linux-64::pip-23.3.1-py311h06a4308_0 python pkgs/main/linux-64::python-3.11.8-h955ad1f_0 readline pkgs/main/linux-64::readline-8.2-h5eee18b_0 setuptools pkgs/main/linux-64::setuptools-68.2.2-py311h06a4308_0 sqlite pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0 tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0 tzdata pkgs/main/noarch::tzdata-2024a-h04d1e81_0 wheel pkgs/main/linux-64::wheel-0.41.2-py311h06a4308_0 xz pkgs/main/linux-64::xz-5.4.6-h5eee18b_0 Proceed ([y]/n)? y Downloading and Extracting Packages bzip2-1.0.8 | 262 KB | ############################### | 100% python-3.11.8 | 32.9 MB | ############################### | 100% openssl-3.0.13 | 5.2 MB | ############################### | 100% sqlite-3.41.2 | 1.2 MB | ############################### | 100% pip-23.3.1 | 3.3 MB | ############################### | 100% wheel-0.41.2 | 145 KB | ############################### | 100% ncurses-6.4 | 914 KB | ############################### | 100% readline-8.2 | 357 KB | ############################### | 100% ld_impl_linux-64-2.3 | 654 KB | ############################### | 100% libuuid-1.41.5 | 27 KB | ############################### | 100% xz-5.4.6 | 651 KB | ############################### | 100% tzdata-2024a | 116 KB | ############################### | 100% setuptools-68.2.2 | 1.2 MB | ############################### | 100% mpi4py-3.1.4 | 541 KB | ############################### | 100% tk-8.6.12 | 3.0 MB | ############################### | 100% ca-certificates-2023 | 126 KB | ############################### | 100% libffi-3.4.4 | 142 KB | ############################### | 100% Preparing transaction: done Verifying transaction: done Executing transaction: done ``` ::: 4. 測試 測試程式參考自[Github](https://github.com/jbornschein/mpi4py-examples) :::spoiler 範例 ``` (demo) [demo_user@lgn301 ~]$ cd test (demo) [demo_user@lgn301 test]$ cat hello.py #!/usr/bin/env python from __future__ import print_function from mpi4py import MPI comm = MPI.COMM_WORLD print("Hello! I'm rank %d from %d running in total..." % (comm.rank, comm.size)) comm.Barrier() # wait for everybody to synchronize _here_ (demo) [demo_user@lgn301 test]$ which mpirun ~/.conda/envs/demo/bin/mpirun (demo) [demo_user@lgn301 test]$ which python3 ~/.conda/envs/demo/bin/python3 (demo) [demo_user@lgn301 test]$ mpirun -n 4 python3 hello.py Hello! I'm rank 0 from 4 running in total... Hello! I'm rank 1 from 4 running in total... Hello! I'm rank 2 from 4 running in total... Hello! I'm rank 3 from 4 running in total... ``` ::: 5. 撰寫job script :::spoiler 範例 ``` (demo) [demo_user@lgn301 test]$ cat mpi4py.sh #!/bin/bash #SBATCH -A ABC777777 # Account name/project number #SBATCH -J mpi4py_test # Job name #SBATCH -p ctest #SBATCH -n 4 # Number of MPI tasks (i.e. processes) #SBATCH -c 1 # Number of cores per MPI task #SBATCH -o %j.out # Path to the standard output and error files relative to the working directory #SBATCH -e %j.err # Path to the standard output and error files relative to the working directory ml purge ml pkg/Anaconda3 conda activate demo mpirun -n 4 python hello.py ``` :::info :bulb:若是安裝mpich 請以mpiexec執行 ::: 6. 提交job script (demo) [demo_user@lgn301 test]$ sbatch mpi4py.sh Submitted batch job 9004003 7. 查看結果 :::spoiler 點此查看 ``` [demo_user@lgn301 test]$ cat 9004003.out Hello! I'm rank 1 from 4 running in total... Hello! I'm rank 2 from 4 running in total... Hello! I'm rank 3 from 4 running in total... Hello! I'm rank 0 from 4 running in total... ``` ::: ## pip 安裝方式與執行 透過pip3安裝可自由選擇編譯器、MPI library、Python版本組合,尤其是節點間若有安裝高速網路介面者,可藉由支援的特殊協定達成快速交換資料訊息。 1. 登入系統,載入欲使用的編譯器、MPI library與Python 本例係以Intel oneAPI compiler搭配 Intel oneAPI MPI為示範,亦可使用gcc、clang...等編譯器搭配其他MPI,如OpenMPI、MPICH、MVAPICH2...等 ``` [demo_user@lgn302 ~]$ ml intel intelmpi Python [demo_user@lgn302 ~]$ ml Currently Loaded Modules: 1) intel/2024 2) intelmpi/2021.11 3) Python/3.12.2 ``` 2. 安裝mpi4py至個人目錄下 ``` [demo_user@lgn302 ~]$ pip3 list Package Version ------- ------- pip 24.0 [demo_user@lgn302 ~]$ pip3 install mpi4py --user ## 安裝mpi4py至個人目錄 Collecting mpi4py Using cached mpi4py-3.1.5-cp312-cp312-linux_x86_64.whl Installing collected packages: mpi4py Successfully installed mpi4py-3.1.5 [demo_user@lgn302 ~]$ pip3 list Package Version ------- ------- mpi4py 3.1.5 pip 24.0 ``` 3. 撰寫job script 測試程式參考自[Github](https://github.com/jbornschein/mpi4py-examples) :::spoiler 點此查看範例 ``` [demo_user@lgn302 ~]$ cd test [demo_user@lgn302 test]$ cat hello.py #!/usr/bin/env python from __future__ import print_function from mpi4py import MPI comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() node_name = MPI.Get_processor_name() # get the name of the node print("Hello world! I'm rank %d from %d running in total at %s." % (comm.rank, comm.size, node_name)) #print("Hello! I'm rank %d from %d running in total..." % (comm.rank, comm.size)) comm.Barrier() # wait for everybody to synchronize _here_ ``` ``` [demo_user@lgn302 test]$ cat impi4py.sh #!/bin/bash #SBATCH -A ABC123456 # Account name/project number #SBATCH -J mpi4py_test # Job name #SBATCH -p hetero #SBATCH -n 8 # Number of MPI tasks (i.e. processes) #SBATCH -c 1 # Number of cores per MPI task #SBATCH -N 2 # Maximum number of nodes to be allocated #SBATCH -o %j.out # Path to the standard output and error files relative to the working directory #SBATCH -e %j.err # Path to the standard output and error files relative to the working directory ml purge ml intel/2024 intelmpi/2021.11 Python/3.12.2 mpirun python3 hello.py ``` ::: 4. 提交計算工作 [demo_user@lgn302 test]$ sbatch impi4py.sh Submitted batch job 9006770 5. 查看結果 :::spoiler 點此查看結果 ``` [demo_user@lgn302 test]$ cat 9006770.out MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found MPI startup(): Warning: I_MPI_PMI_LIBRARY will be ignored since the hydra process manager was found Hello world! I'm rank 0 from 8 running in total at cpn3001.cluster. Hello world! I'm rank 1 from 8 running in total at cpn3001.cluster. Hello world! I'm rank 4 from 8 running in total at cpn3002.cluster. Hello world! I'm rank 2 from 8 running in total at cpn3001.cluster. Hello world! I'm rank 5 from 8 running in total at cpn3002.cluster. Hello world! I'm rank 3 from 8 running in total at cpn3001.cluster. Hello world! I'm rank 6 from 8 running in total at cpn3002.cluster. Hello world! I'm rank 7 from 8 running in total at cpn3002.cluster. ``` ::: :::warning :warning: 執行環境須與編譯安裝的環境設定一致,勿混用!!! * 使用conda安裝者,請以cond環境執行。 * 使用pip方式安裝者,請以安裝時的環境設定執行。 * 須跨節點執行者,建議以pip3方式安裝。 :::