/* ----------------------------------------------------------------------- * Code: to.complete.c * Lab: MPI Derived Datatypes * This program sends contiguous data of mixed type (a C struct) * from one process to another. You have to initialize the values * for the parameters to the MPI_Type_struct call, and properly * insert them. * * Usage: to.complete * Runs on two nodes. * Author: Mike Hammill Last revised: 2/9/96 MWH * ------------------------------------------------------------------------ */ #include "mpi.h" #define PACKET_BLOCKS 2 /* number of elements in struct */ #define FMAX 14 /* length of array of forces */ #define PACKETTAG 99 /* MPI tag for packet to be sent and received */ #define DESTRANK 1 /* rank of destination process */ #define SOURCERANK 0 /* rank of source process */ #define MYMASS 150.0 /* arbitrary constant */ main( argc, argv ) int argc ; char **argv ; { /* -------------------------------------------------------------- */ /* You fill in the blocklengths */ /* -------------------------------------------------------------- */ int array_of_blocklengths[PACKET_BLOCKS] = {, }, /* for MPI struct */ i, /* loop index */ myrank ; /* rank in communicator of process */ MPI_Aint extent, /* extent of double */ array_of_displacements[PACKET_BLOCKS] ; /* for MPI struct */ MPI_Datatype /* -------------------------------------------------------------- */ /* You fill in the array_of_types */ /* -------------------------------------------------------------- */ array_of_types[PACKET_BLOCKS] = {, }, /* for MPI struct */ packettype ; MPI_Status status ; /* status of received message */ /* We want to send packet, a contiguous C struct with mixed types. */ /* Declare packet prior to constructing a new derived datatype. */ struct{ double mass ; /* an object's mass */ int force[FMAX] ; /* array of forces */ } packet ; /* ------------------------------------------------------------------------ */ /* Initialize packet to zero. */ packet.mass = 0.0 ; for (i=0; i < FMAX; i++) packet.force[i] = 0.0 ; /* Setup the MPI environment. */ MPI_Init( &argc, &argv ) ; MPI_Comm_rank( MPI_COMM_WORLD, &myrank ) ; printf ( " Task %d initialized\n", myrank ) ; /* Note that all the following variables are constants */ /* and depend only on the format of the C struct. */ /* -------------------------------------------------------------- */ /* You fill in the values for displacements */ /* -------------------------------------------------------------- */ array_of_displacements[0] = ; array_of_displacements[1] = ; /* Use these variables to create a new derived datatype and commit. */ /* -------------------------------------------------------------- */ /* You fill in arguments to the MPI_Type_struct call. */ /* -------------------------------------------------------------- */ MPI_Type_struct (, , , , ) ; MPI_Type_commit (&packettype) ; /* ------------------------------------------------------------------------ */ if ( myrank == SOURCERANK ) { /* Give packet values so we can verify they get sent. */ packet.mass = MYMASS ; for (i=0; i < FMAX; i++) packet.force[i] = (i+1) * 100 ; /* Send packettype with count=1. */ MPI_Send( &packet, 1, packettype, DESTRANK, PACKETTAG, MPI_COMM_WORLD ) ; } /* ------------------------------------------------------------------------ */ else if ( myrank == DESTRANK ) { /* Task with DESTRANK will receive packettype with count=1 */ /* Print packet before the receive. */ printf ("Value of packet before receive:\n") ; printf ("Mass = %.3f \n", packet.mass) ; printf ("Array of forces = " ) ; for ( i=0; i < FMAX; i++ ) printf ("%d ", packet.force[i] ) ; printf ( "\n" ) ; MPI_Recv( &packet, 1, packettype, SOURCERANK, PACKETTAG, MPI_COMM_WORLD, status ) ; /* Verify that structure got passed. */ /* Print packet before the receive. */ printf ("\nValue of packet after receive:\n") ; printf ("Mass = %.3f \n", packet.mass) ; printf ("Array of forces = " ) ; for ( i=0; i < FMAX; i++ ) printf ("%d ", packet.force[i] ) ; printf ( "\n" ) ; } /* ------------------------------------------------------------------------ */ MPI_Finalize() ; }