10
13

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 5 years have passed since last update.

【第一回】Raspberry piで並列処理を行う

Posted at

目的

Raspberry piを使用して並列計算を行う
MPIについての学習兼並列処理についても学習

用意するもの

Raspberry Pi * 2台以上

以下
pi-cluster01 : master
pi-cluster02 : node
pi-cluster03 : node

と、して、同一ネットワーク上かつssh通信が出来ること。

MPIとは

MPI(Message Passing Interface)
1つの目的を複数のコンピュータで分散・並列処理する際に、この実行基盤の1つが MPI (Message Passing Interface)と呼ばれる仕様・規格。
MPICH, Open MPI など複数の実装系が存在しています。

有名所でいうと「32 ノード構成・ド派手な The RPiCluster」
動画もあります。

上記の実行には「python-mpi4py」を使用しています。

お試しで1ノードで並列Hello World

まずは1ノードで環境を作成していきます。
必要なパッケージを各ノードへインストールします

# apt-get install python-mpi4py

次に並列処理に対応したプログラムを作成します。

hello_world.py
# !/usr/bin/env python
"""
Parallel Hello World
"""
 
from mpi4py import MPI
import sys
 
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
 
print "Hello, World! I am process %d of %d on %s." % (rank, size, name)

上記の実行結果はこちら

# mpirun -np 4 hello.py 
Hello, World! I am process 0 of 4 on rasko01.
Hello, World! I am process 1 of 4 on rasko01.
Hello, World! I am process 2 of 4 on rasko01.
Hello, World! I am process 3 of 4 on rasko01.
簡単にプログラムの解説
  • MPI_COMM_WORLDは、コミュニケータとよばれる概念を保存する変数
  • コミュニケータは、操作を行う対象のプロセッサ群を定める
  • 初期状態では、0番~numprocs –1番までのプロセッサが、1つのコミュニケータに割り当てられる
    この名前が、“MPI_COMM_WORLD”
  • RANKとは各「MPIプロセス」の「識別番号」のこと。
補足

ちょっとMPIを勉強してる人なら疑問に思うと思いますが、
MPI_Init()と、MPI_Finalize()がありません。
これらの関数はmpi4pyモジュールの初回import時と、プロセス終了時に、それぞれ自動的に呼ばれているとのことです。


今回はここまで
第二回からは実際に複数ノードを使用してプログラムを実行します。

10
13
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
10
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?