// ================================================================== // Author: Jean-Michel Richer // Date: 20 Aug 2016 // Purpose: Use of MPI::Scatter // The master process creates its own local data and they are send // to other process (of rank not equal to 0) // ================================================================== #include #include // for sleep #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); // local vector int local_vec_size = 10; vector local_vec(local_vec_size, 0); // big vector int big_vec_size = process.nbr_processes() * local_vec_size; vector big_vec(big_vec_size); if (process.is_master()) { // master process will send data to others iota(big_vec.begin(), big_vec.end(), 1); process << "- big vector = " << big_vec << endl; } // Master sends data to all others process.scatter(big_vec, big_vec_size, local_vec); // Each process reports the data it has received process << "- local vector = " << local_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); }