// ================================================================== // Author: Jean-Michel Richer // Date: Aug 2020 // Last modified: November 2020 // Purpose: Use of MPI::Gather // Each process creates its own local vector of data and they are all // sent to the master process (of rank 0) // ================================================================== #include #include // for sleep #include #include #include #include #include using namespace std; #include "ezmpi.hpp" using namespace ez::mpi; /** * run master and slaves */ void run(int argc, char* argv[]) { Process process(argc, argv); // -------------------------------------------------------------- // declaration of data // -------------------------------------------------------------- // this is for the master vector big_vec; // this is for the master and the slaves int local_vec_size = 10; vector local_vec(local_vec_size); // depending on the process fill the local vector with numbers iota(local_vec.begin(), local_vec.end(), process.id() * 10 + 1); process << "- local vector=" << local_vec << endl; if (process.is_master()) { // master process will gather data from others // so define the size of the vector that will // receive other data from the slaves and // fill it with 0's big_vec.resize(process.nbr_processes() * local_vec_size, 0); } // gather data from all processes to master // note: this must be executed by all processes process.gather(local_vec.data(), local_vec_size, big_vec.data()); // finally print the content of the big vector on the master if (process.is_master()) { process << "- big vector = " << big_vec << endl; } process.logs(cout); } // ================================================================== // C++ version // ================================================================== int main(int argc, char** argv) { // call a function that contains the code for the master and // the slaves in order to avoid problems of initialization and // finalization of processes run(argc, argv); exit(EXIT_SUCCESS); }