/* ----------------------------------------------------------------------- * Code: choose.c * Lab: MPI Derived Datatypes * This program sends one single column of a two-dimensional matrix * from one processor to another. As currently written, it does this * one column element at a time. Your task is to modify the program so it * sends the entire column at once using a derived datatype. For more * information on this problem, please read the exercise instructions * that lead you to this code. * * Usage: choose * Runs on two nodes. * Author: Mike Hammill Last revised: 2/9/96 MWH * ------------------------------------------------------------------------ */ #include "mpi.h" #define IMAX 15 /* number of rows in the matrix */ #define JMAX 20 /* number of columns in the matrix */ #define COL 5 /* column to send; note: 1 <= COL <= JMAX */ #define COLTAG 10 /* MPI tag for send and receive */ #define DESTRANK 1 /* rank of destination process */ #define SOURCERANK 0 /* rank of source process */ #define SPECIAL -1 /* arbitrary value loaded into the column */ /* to send; used for verification */ main( argc, argv ) int argc ; char **argv ; { int i, j, /* index variables */ myrank; /* rank in communicator of process */ MPI_Status status ; /* status of received message */ MPI_Datatype coltype ; /* derived datatype for you to define */ double matrix [IMAX][JMAX] ; /* 2D matrix containing column to send */ /* Fill matrix with zeros; we do this for later verification of data sent. */ for ( i=0; i < IMAX; i++ ) for ( j=0; j < JMAX; j++ ) matrix[i][j] = 0 ; /* Set up MPI environment */ MPI_Init( &argc, &argv ) ; MPI_Comm_rank( MPI_COMM_WORLD, &myrank ) ; printf ( "Task %d initialized\n", myrank ) ; /* ------------------------------------------------------- */ if ( myrank == SOURCERANK ) { /* In Task with rank of SOURCERANK. */ /* Fill the column to send, COL, with SPECIAL value. */ for ( i=0; i < IMAX; i++ ) matrix[i][COL] = SPECIAL ; /* Send the column, one element at a time. See if */ /* you can improve this situation by using a derived */ /* datatype. */ for ( i=0; i < IMAX; i++) MPI_Send( &matrix[i][COL], 1, MPI_DOUBLE, DESTRANK, COLTAG, MPI_COMM_WORLD ) ; } /* ------------------------------------------------------- */ else if ( myrank == DESTRANK ) { /* In Task with rank of DESTRANK. */ /* Print out matrix before the receive. */ printf ( "Matrix before receive:\n" ) ; for ( i=0; i < IMAX; i++ ) { for ( j=0; j < JMAX; j++ ) printf ("%.1f ", matrix[i][j]) ; printf ( "\n" ) ; } /* Receive the column one element at a time. */ /* Try to make this a receive of coltype with count=1. */ for ( i=0; i < IMAX; i++) MPI_Recv( &matrix[i][COL], 1, MPI_DOUBLE, SOURCERANK, COLTAG, MPI_COMM_WORLD, &status ) ; /* Verify the results; print out matrix after receive. */ printf ( "\nMatrix after receive:\n" ) ; for ( i=0; i < IMAX; i++ ) { for ( j=0; j < JMAX; j++ ) printf ("%.1f ", matrix[i][j]) ; printf ( "\n" ) ; } /* ------------------------------------------------------- */ } MPI_Finalize() ; }