LoginSignup
6
3

More than 1 year has passed since last update.

MS-MPIを使ったRust並列処理

Posted at

はじめに

Rustで並列処理を行ってみます。並列処理としてMicrosoft MPI(MS-MPI)を利用します。RustでMS-MPIが実行できるコードを作成するため、mpiクレートを利用します。

環境

  • Windows 10 home
  • Rust(1.53.0)
  • Visual Studio Code(1.62.2)
  • MS-MPI(10.1.2)

準備

MS-MPIのインストール方法は、以前の記事を参考ください。Rustもインストールしておきます。

MS-MPIのインストールが終われば、以下の図のようにMS-MPIの環境変数を設定します。環境変数を設定しないと、mpiクレートにあるmpi-sysのビルド時にエラーが発生しました。
mpi_envorinment_variable_trim.png

mpiクレート

RustでMPIを使うため、mpiクレートを利用します。crates.ioのmpiクレートだと、2021/11/13時点で、MS-MPIに対応していないため、githubにあるmpiクレート使います。cargo new で作成したフォルダのCargo.tomlに、以下のようにgithubのURLを指定します。

command
cargo new mpi_qiita
Cargo.toml
[package]
name = "mpi_qiita"
version = "0.1.0"
edition = "2018"

[dependencies]
mpi = {git="https://github.com/rsmpi/rsmpi", branch="master"}

Rust並列処理コード

以前のc++で作成したコードと同じ動きをするRustコードを作成します。

main.rs
use mpi::traits::*;
fn main() {
    let universe = mpi::initialize().unwrap();
    let world = universe.world();
    let size = world.size();
    let rank = world.rank();
    print!("Hello world I am {} of {}", rank, size);
}

ビルドします。

command
cargo build

mpi_qiitaというパッケージ名にしたので、mpi_qiita\target\debug\mpi_qiita.exeという実行ファイルが出来上がります。

MS-MPIでの実行

MS-MPIで実行ファイルを動かします。

command
mpiexec -n 7 mpi_qiita.exe

rust_mpi_exec.png

参考

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