#include #include "mpi.h" /* Exercise 6.9.3, see Chap 6, p. 108 in PPMPI. by John Weathewax */ main( int argc, char* argv[] ){ int my_rank; int p; int i,j; float value; MPI_Datatype sparse_m_elt_t; /* declarations: */ void Build_sparse_matrix_entry(int* i_ptr, int* j_ptr, float* value_ptr, MPI_Datatype* sparse_m_elt_t_ptr); MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&p); MPI_Comm_rank(MPI_COMM_WORLD,&my_rank); if( my_rank==1 ){ /* create a sparse_matrix_entry and send to process 0 */ i=2; j=3; value=3.14159; } /* Create a sparse matrix type... */ Build_sparse_matrix_entry(&i,&j,&value,&sparse_m_elt_t); MPI_Bcast(&i,1,sparse_m_elt_t,1,MPI_COMM_WORLD); if( my_rank==0 ){ /* print the sparse element type */ printf("Process 0: Now i=%d\n",i); printf("Process 0: Now j=%d\n",j); printf("Process 0: Now value=%f\n",value); } MPI_Finalize(); } /*------------------------------------------------------------------------ * Create a sparse MPI type... */ void Build_sparse_matrix_entry( int* i_ptr, /* in */ int* j_ptr, /* in */ float* value_ptr, /* in */ MPI_Datatype* sparse_m_elt_t_ptr /* out */){ int block_lengths[3]; MPI_Aint displacements[3]; MPI_Datatype typelist[3]; MPI_Aint start_address, address; block_lengths[0] = block_lengths[1] = block_lengths[2] = 1; /* Build a derived type consisting of two ints and a float */ typelist[0] = MPI_INT; typelist[1] = MPI_INT; typelist[2] = MPI_FLOAT; /* First element value "i" is at displacement 0 */ displacements[0] = 0; /* Calculate the other displacements relative to "i" */ MPI_Address(i_ptr,&start_address); /* Calculate the displacement of "j" relative to "i" */ MPI_Address(j_ptr,&address); displacements[1] = address-start_address; /* Calculate the displacement of "value" relative to "i" */ MPI_Address(value_ptr,&address); displacements[2] = address-start_address; /* Build the derived type */ MPI_Type_struct(3,block_lengths,displacements,typelist, sparse_m_elt_t_ptr); /* Commit this type */ MPI_Type_commit(sparse_m_elt_t_ptr); }