#include #include #include #include // for sleep #include #include using namespace std; #include // ============================================================== // version C++ // ============================================================== int main(int argc, char ** argv) { // maximum number of CPUs int max_cpus; // cpu identifier (called rank) int cpu_rank; // C-string to store name of the host char cpu_name[MPI::MAX_PROCESSOR_NAME]; // length of the C-string int length; // initialization also using command line parameters MPI::Init(argc, argv); // get number of programs running max_cpus = MPI::COMM_WORLD.Get_size(); // get program identifier cpu_rank = MPI::COMM_WORLD.Get_rank(); // get cpu name memset(cpu_name, 0, MPI::MAX_PROCESSOR_NAME); MPI::Get_processor_name(cpu_name, length); cerr << cpu_rank << "/" << max_cpus << " on machine " << cpu_name << endl; // data to exchange int send_data; int recv_data; // identifier (rank) of remove processor (master or slave) int remote_cpu, source_cpu; // Status for communication MPI::Status status; int TAG_0 = 0; if (cpu_rank == 0) { remote_cpu = 1; send_data = 11111; } else if (cpu_rank == 1) { remote_cpu = 0; send_data = 22222; } MPI::COMM_WORLD.Sendrecv( &send_data, // Send buffer 1, // Number of elements to send MPI::INT, // Datatype of send elements remote_cpu, // Destination rank 0, // Send tag &recv_data, // Receive buffer 1, // Number of elements to receive MPI::INT, // Datatype of receive elements remote_cpu, // Source rank 0, // Receive tag status // Status object ); if (cpu_rank == 0) { cerr << "Master: recv_data=" << recv_data << endl; } else if (cpu_rank == 1) { cerr << "Slave: recv_data=" << recv_data << endl; } MPI::Finalize(); exit(EXIT_SUCCESS); }