7 int main( int argc, char *argv[])
13 int errs = 0, toterrs;
14 int rank, size, tsize;
18 MPI_Datatype bases[2];
19 MPI_Datatype str, con;
22 MPI_Init( &argc, &argv );
24 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
25 MPI_Comm_size( MPI_COMM_WORLD, &size );
27 for( j = 0; j < 10; j ++ ) {
29 s[j].c = j + rank + 'a';
32 blens[0] = blens[1] = 1;
33 disps[0] = 0; disps[1] = sizeof(int);
34 bases[0] = MPI_INT; bases[1] = MPI_CHAR;
35 MPI_Type_struct( 2, blens, disps, bases, &str );
36 MPI_Type_commit( &str );
37 MPI_Type_contiguous( 10, str, &con );
38 MPI_Type_commit( &con );
39 MPI_Type_size( con, &tsize );
40 MPI_Type_extent( con, &text );
43 printf("Size of MPI array is %d, extent is %d\n", tsize, text );
50 p2 = &(s[10].i); /* This statement may fail on some systems */
51 printf("C array starts at %p and ends at %p for a length of %d\n",
52 s, &(s[9].c), (char *)p2-(char *)p1 );
55 MPI_Type_extent( str, &text );
57 MPI_Type_size( str, &tsize );
58 printf("Size of MPI struct is %d, extent is %d\n", tsize, (int)text );
59 printf("Size of C struct is %d\n", sizeof(struct a) );
61 if (text != sizeof(struct a)) {
62 printf( "Extent of struct a (%d) does not match sizeof (%d)\n",
63 (int)text, (int)sizeof(struct a) );
67 MPI_Send( s, 1, con, rank ^ 1, 0, MPI_COMM_WORLD );
68 MPI_Recv( s1, 1, con, rank ^ 1, 0, MPI_COMM_WORLD, &status );
70 for( j = 0; j < 10; j++ ) {
72 printf("%d Sent: %d %c, Got: %d %c\n", rank,
73 s[j].i, s[j].c, s1[j].i, s1[j].c );
75 if ( s1[j].i != j + status.MPI_SOURCE ) {
77 printf( "Got s[%d].i = %d; expected %d\n", j, s1[j].i,
78 j + status.MPI_SOURCE );
80 if ( s1[j].c != 'a' + j + status.MPI_SOURCE ) {
82 /* If the character is not a printing character,
83 this can generate an file that diff, for example,
84 believes is a binary file */
85 if (isprint( (int)(s1[j].c) )) {
86 printf( "Got s[%d].c = %c; expected %c\n", j, s1[j].c,
87 j + status.MPI_SOURCE + 'a');
90 printf( "Got s[%d].c = %x; expected %c\n", j, (int)s1[j].c,
91 j + status.MPI_SOURCE + 'a');
96 MPI_Allreduce( &errs, &toterrs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD );
98 if (toterrs > 0) printf( "Found %d errors\n", toterrs );
99 else printf( " No Errors\n" );
101 MPI_Type_free( &str );
102 MPI_Type_free( &con );