MPI

環境

  • Scientifix Linux 7
  • マシンは、適当なもの2台以上用意
  • Intel fortran
  • mpich
  • PHITS(をMPIで走らせたい)

mpichのインストール

IntelFortranなどの他のコンパイラを使用する場合は、tar玉からオプション指定でインストールすると良い。
gfortran(標準gcc)の場合は、apt-getとかの方が簡単
以下でインストールディレクトリを指定。
指定しなくても大丈夫だけど、どこにインストールされるかよく分からないので、今回は指定している。

gfortranの場合は、
./configure --prefix=/usr/local/mpich

インテルfortranの場合は、以下。
※インテルの場合は各マシンにもインテルfortranをインストールする必要がある。実行時にインテルfortranのライブラリが必要。ライブラリだけインストールできればOKだが、やり方が分からないので、本体ごとインストールする。
./configure --prefix=/usr/local/mpich FC=ifort F90=ifort F77=ifort

後は、root権限で
make
make install

何か失敗してアンインストールしたい場合は、
make uninstall

指定するとパスが通らないので、/bash_profileに以下の記述を行う。
gfortranの場合は、intelと入ってる行は書かなくてOK
export PATH=/usr/local/mpich/bin:$PATH
export PATH=/opt/intel/bin/:$PATH
export LD_LIBRARY_PATH=/usr/local/openmpi/lib/:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/opt/intel/lib/intel64:$LD_LIBRARY_PATH

分散並列処理MPIのHelloWorld

  • mpihello.f90
program hello_parallel

  ! Include the MPI library definitons:
  include 'mpif.h'

  integer numtasks, rank, ierr, rc, len, i
  character*(MPI_MAX_PROCESSOR_NAME) name

  ! Initialize the MPI library:
  call MPI_INIT(ierr)
  if (ierr .ne. MPI_SUCCESS) then
     print *,'Error starting MPI program. Terminating.'
     call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
  end if

  ! Get the number of processors this job is using:
  call MPI_COMM_SIZE(MPI_COMM_WORLD, numtasks, ierr)

  ! Get the rank of the processor this thread is running on.  (Each
  ! processor has a unique rank.)
  call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) 

  ! Get the name of this processor (usually the hostname)
  call MPI_GET_PROCESSOR_NAME(name, len, ierr)
  if (ierr .ne. MPI_SUCCESS) then
     print *,'Error getting processor name. Terminating.'
     call MPI_ABORT(MPI_COMM_WORLD, rc, ierr)
  end if

  print "('hello_parallel.f: Number of tasks=',I3,' My rank=',I3,' My name=',A,'')",&
       numtasks, rank, trim(name)

  ! Tell the MPI library to release all resources it is using:
  call MPI_FINALIZE(ierr) 

end program hello_parallel

  • mpihello.c
#include <stdio.h>
#include "mpi.h"

int main(int argc,char** argv)
{
  int count,myid;
  int name_len;
  char pro_name[MPI_MAX_PROCESSOR_NAME]; 

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&count);
  MPI_Comm_rank(MPI_COMM_WORLD,&myid);
  MPI_Get_processor_name(pro_name,&name_len);

  printf("Hello world from %i of %i by %s\n",myid,count,pro_name);
  MPI_Finalize(); 

  return 0;
}


コンパイル

  • fortran
mpif90 –o mpihello mpihello.f
  • C
mpicc mpihello.c -o mpihello

分散処理PCの定義(例)

  • machine.hosts
192.168.0.1 slots=2
192.168.0.2 slots=2

分散処理の実行

複数PCでの実行が上手くいかない。SSHログインはできるのだが、MPIを実行するとTCPエラーなんちゃらが出てしまう。1台のPCなら問題なく走るのに。
上手く行かないのは、SL7(Scientific Linux7)に入っているFireWallが原因だった。
セキュリティ上、問題なければ、このFireWallを停止すれば、問題なく走るようになる。
停止させるには、rootで
systemctl stop firewalld.service
systemctl disable firewalld.service

停止を確認するには、
firewall-cmd --state

あとは、色々なサイトで紹介されている通り、実行すればOK
nの後の数字は、使用するコア数
mpiexec -hosts 192.168.0.1,192.168.0.2 -n 4 ./a.out

実行時のよくあるエラーメッセージ

  • exeがなんとかと言われる
クライアント側に実行ファイルが無いのが原因。ホスト側でコンパイルした実行ファイルをコピーする。
  • firewallがなんとかと言われる
上述しているfirewallが有効なのが問題。セキュリティ上の問題がなければ?無効にすればOK。

正常にHelloworldが動くと

>mpiexec -hosts 192.168.0.1,192.168.0.2,192.168.0.3 -n 35 ./a.out
hello_parallel.f: Number of tasks= 35 My rank=  0 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank=  3 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank=  6 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank=  9 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 21 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 24 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 27 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 30 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 33 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 12 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 15 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank= 18 My name=pc1
hello_parallel.f: Number of tasks= 35 My rank=  4 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank=  2 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 13 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 11 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 22 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 17 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 10 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank=  8 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 16 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 14 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 19 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 20 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 25 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 26 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 31 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 29 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 34 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank=  5 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank=  1 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 23 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank=  7 My name=pc2
hello_parallel.f: Number of tasks= 35 My rank= 32 My name=pc3
hello_parallel.f: Number of tasks= 35 My rank= 28 My name=pc2

分散処理参考サイト

最終更新:2015年04月10日 17:59