ソース 5.1.1 1対1ブロッキング通信
C |
---|
プログラム |
#include <stdio.h> #include <mpi.h> #define NUMOF(array) (sizeof (array) / sizeof *(array)) #define STRLEN(literal) (NUMOF(literal) - 1) enum TAG_KIND // 通信タグ { TAG_HELLO, // Hello 文字列通信タグ }; enum RANK_KIND // ランク { RANK_SENDER, // 送信側のランク RANK_RECVER, // 受信側のランク }; // 送信 void Send() { char hello[] = "Hello, host1!"; MPI_Send(hello, STRLEN(hello), MPI_CHAR, RANK_RECVER, TAG_HELLO, MPI_COMM_WORLD); } // 受信 void Recv() { char hello[64]; int recvCount; MPI_Status status; MPI_Recv(hello, STRLEN(hello), MPI_CHAR, RANK_SENDER, TAG_HELLO, MPI_COMM_WORLD, &status); MPI_Get_count(&status, MPI_CHAR, &recvCount); hello[recvCount] = '\0'; printf("Received bytes : %d\n", recvCount); printf("Received text : %s\n", hello); } int main(int argc, char** argv) { int rank; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); switch(rank) { case RANK_SENDER: Send(); break; case RANK_RECVER: Recv(); break; } MPI_Finalize(); printf("Exit : %d\n", rank); } |
実行結果例 |
Exit : 0 Received bytes : 13 Received text : Hello, host1! Exit : 1 |
FORTRAN77 |
プログラム1 params.h |
Include 'mpif.h' C Hello 文字列通信タグ Integer TAG_HELLO Parameter (TAG_HELLO = 0) C 受信バッファサイズ Integer BUF_SIZE Parameter (BUF_SIZE = 64) C 送信側のランク Integer RANK_SENDER Parameter (RANK_SENDER = 0) C 受信側のランク Integer RANK_RECVER Parameter (RANK_RECVER = 1) |
プログラム2 |
C 送信 Subroutine Send Implicit None Include 'params.h' Integer err Character hello*13/'Hello, host1!'/ Call MPI_Send(hello, Len(hello), MPI_CHARACTER, * RANK_RECVER, TAG_HELLO, MPI_COMM_WORLD, err) Return End C 受信 Subroutine Recv Implicit None Include 'params.h' Integer err Character hello(BUF_SIZE) Integer status(MPI_STATUS_SIZE) Integer recvCount Integer i Call MPI_Recv(hello, BUF_SIZE, MPI_CHARACTER, RANK_SENDER, * TAG_HELLO, MPI_COMM_WORLD, status, err) Call MPI_Get_count(status, MPI_CHARACTER, recvCount, err) Write(*, *) 'Received bytes : ', recvCount Write(*, *) 'Received text : ', (hello(i), i = 1, recvCount) Return End Program TestLAM Implicit None Include 'params.h' Integer err Integer rank Call MPI_Init(err) Call MPI_Comm_rank(MPI_COMM_WORLD, rank, err) If(rank .eq. RANK_SENDER) Then Call Send Else If(rank .eq. RANK_RECVER) Then Call Recv EndIf Call MPI_Finalize(err) Write(*, *) 'Exit : ', rank Stop End |
実行結果例 |
Received bytes : 13 Received text : Hello, host1! Exit : 0 Exit : 1 |
C++ |
プログラム |
#include <iostream> #include <mpi++.h> #define NUMOF(array) (sizeof (array) / sizeof *(array)) #define STRLEN(literal) (NUMOF(literal) - 1) enum TAG_KIND // 通信タグ { TAG_HELLO, // Hello 文字列通信タグ }; enum RANK_KIND // ランク { RANK_SENDER, // 送信側のランク RANK_RECVER, // 受信側のランク }; using namespace std; // 送信 void Send() { const char hello[] = "Hello, host1!"; MPI::COMM_WORLD.Send(hello, STRLEN(hello), MPI::CHAR, RANK_RECVER, TAG_HELLO); } // 受信 void Recv() { char hello[64]; int recvCount; MPI::Status status; MPI::COMM_WORLD.Recv(hello, STRLEN(hello), MPI::CHAR, RANK_SENDER, TAG_HELLO, status); recvCount = status.Get_count(MPI::CHAR); hello[recvCount] = '\0'; cout << "Received count : " << recvCount << endl; cout << "Received text : " << hello << endl; } int main(int argc, char** argv) { MPI::Init(argc, argv); int rank = MPI::COMM_WORLD.Get_rank(); switch(rank) { case RANK_SENDER: Send(); break; case RANK_RECVER: Recv(); break; } MPI::Finalize(); cout << "Exit : " << rank << endl; } |
実行結果例 |
Exit : 0 Received count : 13 Received text : Hello, host1! Exit : 1 |
Last update was done on 2002.11.14