1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <vector>
#include "mpi.h"
using namespace std;
int main( int argc, char* argv[] )
{
int rank, nproc;
MPI_Status stat;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &nproc );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
int tag = 1;
if ( rank == 0 ) // Root processor (0)
{
vector<double> A = { 10, 20, 30, 40, 50 }; // Some data
int n = A.size();
MPI_Send( &n, 1, MPI_INT, 1, tag, MPI_COMM_WORLD ); // Send size
MPI_Send( A.data(), n, MPI_DOUBLE, 1, tag, MPI_COMM_WORLD ); // Send data
}
else if ( rank == 1 ) // Next processor (1)
{
int n;
MPI_Recv( &n, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &stat ); // Receive size
vector<double> B(n); // Allocate buffer
MPI_Recv( B.data(), n, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, &stat ); // Receive data
cout << "Processor " << rank << " received " << n << " pieces of data: ";
for ( double x : B ) cout << x << " ";
cout << '\n';
}
MPI_Finalize();
}
| |