MPIのことはじめ

では、実際に並列処理を実現していきましょう。
まずは、逐次(一つのノードで動くプログラム)を作成します。
こんな感じ

include<stdio.h>
#define N 10000000

int main(int argc,char *argv[])
{
        printf("hello world\n");
        return 0;
}

では、コンパイルして実行してみましょう。

$ gcc 001-1.c -o 001-1
$ ./001-1
hello world

では、これにMPIの関数を埋め込んで並列処理を実現しましょう。
やり方はとっても簡単。

#include<stdio.h>
#include"mpi.h" ★★★
#define N 10000000

int main(int argc,char *argv[])
{
        MPI_Init(&argc,&argv); ★★★
        printf("hello world\n");
        MPI_Finalize();★★★
        return 0;
}

★★★の部分が今回加えたところです。これだけで作成完了です。ということで実際に動かしてみましょう。
そのためにはコンパイルと実行の際にMPIのプログラムを使います。
こんな様に…、

$ mpicc -o 001-2 ./001-2.c 
$ mpirun --machinefile machinefile ./001-2
hello world
hello world
hello world

今回はここまで。

戻る