CentOS9上にOpenMPI(Python: mpi4py) + OpenMP(C言語: gcc)を動かしていきます。MPIでプロセスを生成して、その各プロセスでMP(C言語)を呼び出し、その中でスレッドを動かします。
1. 必要なモジュールのインストール(root権限で実施)
CentOS9にはPythonはインストール済み -> Python 3.9.19
(1) pipをインストール
$ sudo yum install python3-pip
(2) Python開発モジュールをインストール(これがないとmpi4pyがうまくインストールできなかった)
$ sudo yum install python3-devel
(3) gccをインストール
(4)OpenMPIをインストール+パスの追加$ sudo yum install gcc
$ sudo yum install openmpi
$ echo 'export PATH=/usr/lib64/openmpi/bin:$PATH' >> ~/.bashrc
$ echo 'export LD_LIBRARY_PATH=/usr/lib64/openmpi/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
$ source ~/.bashrc
#include <stdio.h> | |
#include <omp.h> | |
void openmp_function() { | |
#pragma omp parallel | |
{ | |
int thread_id = omp_get_thread_num(); | |
int num_threads = omp_get_num_threads(); | |
printf("Hello from thread %d out of %d threads\n", thread_id, num_threads); | |
} | |
} | |
int main() { | |
openmp_function(); | |
return 0; | |
} |
$ gcc -fopenmp -o omp_program omp_program.c
3. mpi4pyをインストール(ユーザ権限で実施)$ ./omp_program
$ pip3 install mpi4py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() print(f"Hello from rank {rank}")
さくらサーバの場合警告が出たので、以下を設定してから実行export PSM3_ALLOW_LOC_ALIASES=1export PSM3_ENABLE_SYMMAP=0export PSM3_NIC=ens3export OMPI_MCA_mtl_ofi_provider_exclude="psm3"
4. MPをmpi4pyから呼び出して、プロセス2、スレッド4で動かすmpirun -np 4 python3 run_mpi.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#include <stdio.h> #include <omp.h> #include <stdlib.h> void openmp_function(int rank) { #pragma omp parallel { int thread_id = omp_get_thread_num(); int num_threads = omp_get_num_threads(); printf("Rank %d, Hello from thread %d out of %d threads\n", rank, thread_id, num_threads); } } int main(int argc, char** argv) { if (argc != 2) { fprintf(stderr, "Usage: %s <rank>\n", argv[0]); return 1; } int rank = atoi(argv[1]); openmp_function(rank); return 0; } MPの動作確認gcc -fopenmp -o omp_program_with_rank omp_program_with_rank.c
./omp_program_with_rank 0
Rank 0, Hello from thread 0 out of 6 threads
Rank 0, Hello from thread 3 out of 6 threads
Rank 0, Hello from thread 1 out of 6 threads
Rank 0, Hello from thread 4 out of 6 threads
Rank 0, Hello from thread 2 out of 6 threads
Rank 0, Hello from thread 5 out of 6 threads
(2) [run_mpi_with_rank.py]の作成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
from mpi4py import MPI import subprocess def run_c_program(rank): # 環境変数を設定してスレッド数を指定 env = {"OMP_NUM_THREADS": "4"} command = ["./omp_program_with_rank", str(rank)] result = subprocess.run(command, env=env) return result.returncode if __name__ == "__main__": comm = MPI.COMM_WORLD rank = comm.Get_rank() size = comm.Get_size() if rank < 2: # 2プロセスのみ実行 print(f"Running on rank {rank} out of {size} processors") run_c_program(rank) 実行mpirun -np 2 python3 run_mpi_with_rank.pyRunning on rank 0 out of 2 processorsRunning on rank 1 out of 2 processorsRank 0, Hello from thread 0 out of 4 threadsRank 0, Hello from thread 3 out of 4 threadsRank 0, Hello from thread 2 out of 4 threadsRank 0, Hello from thread 1 out of 4 threadsRank 1, Hello from thread 0 out of 4 threadsRank 1, Hello from thread 3 out of 4 threadsRank 1, Hello from thread 2 out of 4 threadsRank 1, Hello from thread 1 out of 4 threads
#!/bin/bash | |
#------- qsub option ----------- | |
#PBS -q SQUID | |
#PBS --group=GXXXXX | |
#PBS -m eab | |
#PBS -M mizuno@mail.address.jp | |
#PBS -l cpunum_job=76 | |
#PBS -l elapstim_req=00:01:00 | |
#PBS -l memsz_job=248GB | |
#PBS -b 1 | |
#------- Program execution ----------- | |
module load BasePy/2021 | |
module load BaseCPU | |
source /sqfs/work/GXXXXX/vXXXXXX/test-env/bin/activate | |
cd $PBS_O_WORKDIR | |
mpiexec -np 8 python3 run_mpi_with_rank.py > run_mpi_with_rank.txt |
0 件のコメント:
コメントを投稿