#include "mpi.h" #include "chrono" #include "boost/mpi/environment.hpp" #include "boost/mpi/communicator.hpp" #include "boost/serialization/vector.hpp" #include "iostream" #include "vector" #include "thread" using namespace std; using namespace std::chrono; int main(int argc, char* argv[]) { boost::mpi::environment env(argc, argv); boost::mpi::communicator world; double resize = 0, send = 0, recv = 0; vector data, empty; uint64_t size = 0; if (world.rank() == 0) { cout << "mpi" << endl; size = 1000000; auto temp = high_resolution_clock::now(); MPI_Send(&size, 1, MPI_UINT64_T, 1, 0, MPI_COMM_WORLD); send += duration_cast>(high_resolution_clock::now() - temp).count(); temp = high_resolution_clock::now(); data.resize(size); resize += duration_cast>(high_resolution_clock::now() - temp).count(); data[size - 1] = 123; temp = high_resolution_clock::now(); MPI_Send(data.data(), int(data.size()), MPI_DOUBLE, 1, 0, MPI_COMM_WORLD); send += duration_cast>(high_resolution_clock::now() - temp).count(); } else { auto temp = high_resolution_clock::now(); MPI_Recv(&size, 1, MPI_UINT64_T, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); recv += duration_cast>(high_resolution_clock::now() - temp).count(); temp = high_resolution_clock::now(); data.resize(size); resize += duration_cast>(high_resolution_clock::now() - temp).count(); temp = high_resolution_clock::now(); MPI_Recv(data.data(), int(data.size()), MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE); recv += duration_cast>(high_resolution_clock::now() - temp).count(); if (data[size - 1] != 123) abort(); } cout << world.rank() << " resize: " << resize << ", send: " << send << ", recv: " << recv << std::endl; resize = send = recv = 0; data = empty; this_thread::sleep_for(seconds(1)); world.barrier(); this_thread::sleep_for(seconds(1)); if (world.rank() == 0) { cout << "boost::mpi" << endl; size = 1000000; auto temp = high_resolution_clock::now(); data.resize(size); resize += duration_cast>(high_resolution_clock::now() - temp).count(); data[size - 1] = 123; temp = high_resolution_clock::now(); world.send(1, 0, data); send += duration_cast>(high_resolution_clock::now() - temp).count(); } else { auto temp = high_resolution_clock::now(); world.recv(0, 0, data); recv += duration_cast>(high_resolution_clock::now() - temp).count(); if (data[size - 1] != 123) abort(); } cout << world.rank() << " resize: " << resize << ", send: " << send << ", recv: " << recv << std::endl; }