// ================================================================== // 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 // integers from master (rank=0) to slave (rank=1) // ================================================================== // Don't forget to launch with at least two processes: // // mpirun -n 2 bin/ezmpi_send_and_recv.exe // or // mpirun -n 4 bin/ezmpi_send_and_recv.exe // // ================================================================== #include #include #include #include using namespace std; #include "ezmpi.hpp" using namespace ez::mpi; /** * @brief return a string representation of an array */ string to_string(int* array, int size) { ostringstream oss; oss << '['; if (size > 0) { oss << array[0]; for (int i = 1; i < size; ++i) { oss << ',' << array[i]; } } oss << ']'; return oss.str(); } /** * run master and slaves */ void run(int argc, char* argv[]) { // data to send int* array_data = nullptr; int array_size; // create process here so that it will be destroyed // at the end of the procedure Process process(argc, argv); process.log(true); if (process.is_master()) { // ---------------------------------------------------------- // Code for the master // ---------------------------------------------------------- // set remote cpu identifier that will communicate with master process.remote(1); array_size = 10; array_data = new int[array_size]; iota(&array_data[0], &array_data[array_size], 1); process << to_string(array_data, array_size) << endl; // send size of the array to slave1 process.send(array_size); // send data of array to slave1 process.send(array_data, array_size); // wait for slave1 to compute and send back sum float result = 0; process.recv(result); process << "- result is " << result << " for length=" << array_size; process << ", expected=" << (array_size * (array_size + 1)) / 2 << endl; } else if (process.is_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 // we receive the array length and allocate space process.recv(array_size); array_data = new int[array_size]; // we receive the data process.recv(array_data, array_size); process << "- receive array " << to_string(array_data, array_size) << endl; // 3- compute sum float sum = std::accumulate(&array_data[0], &array_data[array_size], 0); // 4- send back result process.send(sum); } else { // ---------------------------------------------------------- // Code for the other processes: don't do anything // ---------------------------------------------------------- process << "- is idle" << endl; } process.logs(cout); } /** * main function * */ int main(int argc, char** argv) { srand(time(nullptr)); // 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); }