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 36 37 38 39 40 41 42 43 44 45 46 47
|
#include <iostream>
#include "mpi.h"
using namespace std;
struct Node
{
int i;
double x;
char c;
};
void makeNodetype( MPI_Datatype &Nodetype )
{
Node n;
MPI_Datatype types [3] = { MPI_INT, MPI_DOUBLE, MPI_CHAR };
int blocks [3] = { 1, 1, 1 };
MPI_Aint displacements[3] = { (MPI_Aint)&n.i, (MPI_Aint)&n.x, (MPI_Aint)&n.c };
for ( int i = 1; i < 3; i++ ) displacements[i] -= displacements[0];
displacements[0] = 0;
MPI_Type_create_struct( 3, blocks, displacements, types, &Nodetype );
MPI_Type_commit( &Nodetype );
}
int main( int argc, char* argv[] )
{
int rank;
Node node;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
// Set up datatype
MPI_Datatype Nodetype;
makeNodetype( Nodetype );
if ( rank == 0 ) node = { 42, 3.14159, 'C' }; // Root sets content of node ...
MPI_Bcast( &node, 1, Nodetype, 0, MPI_COMM_WORLD ); // ... and broadcasts to all
cout << "Processor " << rank << ": " << node.i << " " << node.x << " " << node.c << endl;
MPI_Type_free( &Nodetype );
MPI_Finalize();
}
|