Structures revisited


Up: Datatypes and Heterogenity Next: Interleaving data Previous: Vectors revisited

When sending an array of a structure, it is important to ensure that MPI and the C compiler have the same value for the size of each structure. The most portable way to do this is to add an MPI_UB to the structure definition for the end of the structure. In the previous example, this is

/* initialize types and displs with addresses of items */ 
MPI_Address( &cmdline.display, &displs[0] ); 
MPI_Address( &cmdline.maxiter, &displs[1] ); 
MPI_Address( &cmdline.xmin,    &displs[2] ); 
MPI_Address( &cmdline.width,   &displs[3] ); 
MPI_Address( &cmdline+1,       &displs[4] ); 
types[0] = MPI_CHAR; 
types[1] = MPI_INT; 
types[2] = MPI_DOUBLE; 
types[3] = MPI_INT; 
types[4] = MPI_UB; 
for (i = 4; i >= 0; i--) 
    displs[i] -= displs[0]; 
MPI_Type_struct( 5, blockcounts, displs, types, &cmdtype ); 
MPI_Type_commit( &cmdtype ); 



Up: Datatypes and Heterogenity Next: Interleaving data Previous: Vectors revisited