// ================================================================== // Author: Jean-Michel Richer // Email: jean-michel.richer@univ-angers.fr // Date: Aug 2020 // Last modified: September 2026 // Purpose: Demonstrate basic send functionality, send array of float // from master (rank=0) to slave (rank=1) // ================================================================== #include #include // for sleep #include #include #include #include using namespace std; #include "ezmpi.hpp" using namespace ez::mpi; /** * run master and slaves * - the master: */ void run(int argc, char* argv[]) { // data to send vector vec; int vec_size; // create process here so that it will be destroyed // at the end of the procedure Process process(argc, argv); if (process.is_master()) { // ---------------------------------------------------------- // Code of the master // ---------------------------------------------------------- // set remote cpu identifier that will communicate with master process.remote(1); // processor of id 0 (master) fills and sends the array vec_size = 10; vec.resize(vec_size); iota(vec.begin(), vec.end(), 1); // 1- send size of array to remote process.send(vec_size); // 2- send data of array to remote and receive result float result = 0; process.sendrecv(vec.data(), vec_size, result); process << "- result is " << result << " for length=" << vec_size; process << ", expected=" << (vec_size * (vec_size + 1)) / 2 << endl; } else if (process.id() == ez::mpi::SLAVE_1) { // ---------------------------------------------------------- // Code for the first slave // ---------------------------------------------------------- // tells to Process to send data to master processor process.remote(0); // process of id 1 will receive the array and compute the sum // 1- we receive the array length and allocate space process.recv(vec_size); vec.resize(vec_size); // 2- we receive the data process.recv(vec.data(), vec_size); // 3- compute sum float sum = accumulate(vec.begin(), vec.end(), 0); // 4- send back result process.send(sum); } else { // ---------------------------------------------------------- // other processes (if any): don't do anything // ---------------------------------------------------------- process << "- is idle" << endl; } process.logs(cout); } // ============================================================== // version C++ // ============================================================== 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); }