Created
November 25, 2013 03:47
-
-
Save aokomoriuta/7636025 to your computer and use it in GitHub Desktop.
Windows(Visual Studio 2012)上でboost::MPIで並列計算できるようになるまで ref: http://qiita.com/aokomoriuta/items/cecaf3d5a3258606323b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define MSMPI_NO_DEPRECATE_20 | |
#include <iostream> | |
#include <boost/mpi.hpp> | |
int main() | |
{ | |
// MPI環境(MPI_InitとFinalizeをやってくれる) | |
boost::mpi::environment env(true); | |
// MPI_COMM_WORLDで通信するよう | |
boost::mpi::communicator world; | |
// 交換するデータ | |
std::string msg = "Hi"; | |
// 偶数番プロセスから | |
if((world.rank()%2) == 0) | |
{ | |
// 奇数番プロセスへ"Hello"を送信 | |
world.send(world.rank()+1, 0, std::string("Hello")); | |
} | |
// 奇数番プロセスでは | |
else | |
{ | |
// 偶数番プロセスから送られてきた文字列を受信 | |
world.recv(world.rank()-1, 0, msg); | |
} | |
// メッセージを表示 | |
std::cout << msg << " @ " << world.rank() << std::endl; | |
// 終了 | |
if(world.rank() == 0 ) | |
{ | |
system("pause"); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <mpi.h> | |
int main(int argc, char* argv[]) | |
{ | |
// 初期化 | |
MPI_Init(&argc, &argv); | |
// rank取得 | |
int rank; | |
MPI_Comm_rank(MPI_COMM_WORLD, &rank); | |
// rank0のみ | |
if (rank == 0) | |
{ | |
// OKで一時停止 | |
std::cout << "Rank 0 OK!" << std::endl; | |
std::system("pause"); | |
} | |
// それ以外は | |
else | |
{ | |
// Ready | |
std::cout << "Rank " << rank << " Ready!" << std::endl; | |
} | |
// 終了 | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment