Created
July 9, 2021 04:59
-
-
Save nariaki3551/687db63a81060304db291d175f9d10f1 to your computer and use it in GitHub Desktop.
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> | |
#include <vector> | |
int main( int argc, char *argv[] ) | |
{ | |
int size, rank; | |
MPI_Init( &argc, &argv ); | |
MPI_Comm_size( MPI_COMM_WORLD, &size ); | |
MPI_Comm_rank( MPI_COMM_WORLD, &rank ); | |
std::vector<int> sbuf(2, 0); | |
sbuf[0] = 2*rank; | |
sbuf[1] = 2*rank + 1; | |
std::vector<int> rbuf; | |
if ( rank == 0 ) rbuf.resize(8); // メモリ確保は受信rankのみで良い | |
std::cout << "rank " << rank << " send " << sbuf[0] << " " << sbuf[1] << std::endl; | |
// Gather data to rank = 0 | |
MPI_Gather( | |
sbuf.data(), 2, MPI_INT, // 送信データ情報(アドレス始点, データ数, データ型) | |
rbuf.data(), 2, MPI_INT, // 受信データ情報(アドレス始点, データ数, データ型) | |
0, // 受信rank | |
MPI_COMM_WORLD // コミュニケータ | |
); | |
if ( rank == 0 ) | |
{ | |
std::cout << "rbuf "; | |
for ( int i = 0; i < 8; i++ ) | |
std::cout << " " << rbuf[i]; | |
std::cout << std::endl; | |
} | |
MPI_Finalize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment