LoginSignup
4
6

JuliaでMPI並列計算のちょっとしたコツ

Posted at

Juliaで並列計算をする際にハマりがちな件を適宜追加しようかと思います。MPI.jlを使用します。

バージョン

  • Julia 1.9

MPI.jlのインストール

すでにMPIがシステムに入っている計算機(スパコンなども含む)の場合、こちらに従ってインストールすれば良いのですが、簡単な方法について書いておきます。

まず、REPLで]キーを押してパッケージモードにして、

add MPIPreferences

としてMPIPreferencesをインストールします。次に、REPLで

using MPIPreferences
MPIPreferences.use_system_binary()

とします。これでエラーが出なければ、システムに入っているMPIを自動的に検出して設定してくれます。
対応しているMPIは

Open MPI
MPICH (v3.1 or later)
Intel MPI
Microsoft MPI
IBM Spectrum MPI
MVAPICH
Cray MPICH
Fujitsu MPI
HPE MPT/HMPT

です(2023年8月3日確認)。

この後

add MPI

をすればMPI.jlが使えます。

構造体の転送

構造体の転送はシンプルに行う事ができます。しかしわかりにくいかもしれませんので、サンプルコードを書いておきます。

using MPI

mutable struct MyStruct{T}
    x::T
    y::T
    data::Matrix{Float64}
    n::Int64
end

function main()
    MPI.Init()
    comm = MPI.COMM_WORLD
    nprocs = MPI.Comm_size(comm)
    myrank = MPI.Comm_rank(comm)

    mystruct = MyStruct(myrank,myrank*2,zeros(Float64,4,4),7)

    if myrank == 0
        mystruct.x = 100
        mystruct.y = 100
        mystruct.data = rand(4,4)	
    end


    mystruct = MPI.gather(mystruct,comm,root=0)
    
    MPI.Barrier(comm)

    for i=0:nprocs-1
        if myrank == i
	        println(i)
            println(mystruct)
        end
        MPI.Barrier(comm)

    end

    mystruct = MPI.bcast(mystruct,comm)
    for i=0:nprocs-1
        if myrank == i
            println(i)
            println(mystruct)
        end
        MPI.Barrier(comm)
    end

    MPI.Barrier(comm)
end
main()

これは、gatherとbcastのテストをしています。

4
6
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
6