// ================================================================== // Author: Jean-Michel Richer // Date: Aug 2020 // Last modified: November 2020 // Purpose: Use of MPI::BCast // The master process creates its own local data and they are send // to other processes (that have a rank not equal to the master) // ================================================================== #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); // declare a vector of vec_size integers int vec_size = 10; vector vec(vec_size, 0); if (process.is_master()) { // master process will: // 1- fille the vector // 2- send the vector to other processes (see broadcast below) iota(vec.begin(), vec.end(), 1); process << "- create global array =" << vec << endl; } // the master process sends the vector to all other processes // the instruction must be executed by all other processes process.broadcast(vec); // Each process reports the data it has received if (process.id() != MASTER) { process << "- local copy array=" << 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); }