4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

M1 Mac(Monterey12.3.1)でOpenMPI.jlを動かす

Last updated at Posted at 2022-05-29

M1 MacでOPENMPI.jlを実行する

M1 Macで、juliaの並列計算が行えるかどうかテストを行いました。

ここでは、並列計算を実行するための環境設定とサンプルコードが動くかどうかを確かめてみます。
サンプルコードは、こちらを参照
https://docs.juliahub.com/MPI/nO0XF/0.15.1/examples/01-hello/

パッケージのインストール

open-mpiのインストールをする

brew install --build-from-source openmpi

次にMPI.jlのインストールをする。パッケージモードで以下を実行する。

add MPI

これで準備が整ったかと思い、以下のドキュメントを参照して、サンプルコードを回してみると
https://docs.juliahub.com/MPI/nO0XF/0.15.1/examples/01-hello/

実行すると、以下のエラーが出てしまった。

┌ Warning:     You appear to have run julia under a different `mpiexec` than the one used by MPI.jl.
│     See the documentation for details.
└ @ MPI ~/.julia/packages/MPI/08SPr/src/environment.jl:38

openmpiとMPI.jlをインストールしたのに、それがjulia側から見えておらず、並列計算がうまく実行できていないようです。

そこで、 juliaを開いて、環境変数を以下のように設定するとうまくいきました。
open-mpiのバージョンは各自のものを設定します。
こちらの記事を参考にしました。

ENV["JULIA_MPI_BINARY"] = "system"
ENV["JULIA_MPI_PATH"] = "/opt/homebrew/Cellar/open-mpi/4.1.2"
ENV["JULIA_MPI_LIBRARY"] = "/opt/homebrew/lib/libmpi.dylib"
ENV["JULIA_MPI_ABI"] = "OpenMPI"
ENV["JULIA_MPIEXEC"] = "/opt/homebrew/bin/mpirun"
using Pkg
Pkg.build("MPI"; verbose=true)

https://docs.juliahub.com/MPI/nO0XF/0.15.1/examples/01-hello/
の実行結果は、

$ mpirun -np 4 julia helloworld.jl 
Hello world, I am rank 0 of 4
Hello world, I am rank 1 of 4
Hello world, I am rank 2 of 4
Hello world, I am rank 3 of 4

きちんと動いた。
また、ブロードキャストのコードの方は

$ mpirun -np 4 julia b.jl         
 Running on 4 processes
rank = 1, A = ComplexF64[1.0 + 2.0im, 2.0 + 4.0im, 3.0 + 6.0im, 4.0 + 8.0im, 5.0 + 10.0im]
rank = 3, A = ComplexF64[1.0 + 2.0im, 2.0 + 4.0im, 3.0 + 6.0im, 4.0 + 8.0im, 5.0 + 10.0im]
rank = 2, A = ComplexF64[1.0 + 2.0im, 2.0 + 4.0im, 3.0 + 6.0im, 4.0 + 8.0im, 5.0 + 10.0im]
rank = 0, A = ComplexF64[1.0 + 2.0im, 2.0 + 4.0im, 3.0 + 6.0im, 4.0 + 8.0im, 5.0 + 10.0im]
rank = 0, B = Dict("foo" => "bar")
rank = 2, B = Dict("foo" => "bar")
rank = 1, B = Dict("foo" => "bar")
rank = 3, B = Dict("foo" => "bar")
rank = 0, f(3) = 14
rank = 1, f(3) = 14
rank = 2, f(3) = 14
rank = 3, f(3) = 14

reduceのコードは、以下のようにM1マックに対応していない機能があるため動かなかった。

ERROR: ERROR: ERROR: ERROR: LoadError: LoadError: LoadError: LoadError: User-defined reduction operators are currently not supported on non-Intel architectures.

send/receiveのコードは

$ mpirun -np 4 julia send.jl  
0: Sending   0 -> 1 = [0.0, 0.0, 0.0, 0.0]
1: Sending   1 -> 2 = [1.0, 1.0, 1.0, 1.0]
2: Sending   2 -> 3 = [2.0, 2.0, 2.0, 2.0]
3: Sending   3 -> 0 = [3.0, 3.0, 3.0, 3.0]
1: Received 0 -> 1 = [0.0, 0.0, 0.0, 0.0]
0: Received 3 -> 0 = [3.0, 3.0, 3.0, 3.0]
3: Received 2 -> 3 = [2.0, 2.0, 2.0, 2.0]
2: Received 1 -> 2 = [1.0, 1.0, 1.0, 1.0]

サンプルコードは動くことを確かめた。

リビルド

自作パッケージの環境の中で、juliaのバージョンをアップデートした場合、MPIもリビルドしないといけないようです。

例えば、QCMaterial.jlという自作パッケージが存在したとして、そのディレクトリに移動して、
julia --project=@. ~/QCMaterial.jl を実行します。
そのあと以下のような環境変数を設定して、MPIをリビルドしたらうまく計算が回りました。

ENV["JULIA_MPIEXEC"] ="/opt/openmpi/3.1.5/gcc-9.3.0/bin/mpirun"
ENV["JULIA_MPI_BINARY"]="system"
Pkg.build("MPI", verbose=true)

まとめ

M1 MacでOPENMPI.jlの動作を確かめた。無事に動くことを確認しました。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?