Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix HAVE_FOOBAR flags handling
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / dataalign.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2014 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 /* The next is for isprint */
9 #include <ctype.h>
10 #include "mpitest.h"
11
12 int main( int argc, char *argv[])
13 {
14         struct a {      int     i;
15                         char    c;
16                 } s[10], s1[10];
17         int j;
18         int errs = 0;
19         int rank, size, tsize;
20         MPI_Aint text;
21         int blens[2];
22         MPI_Aint disps[2];
23         MPI_Datatype bases[2];
24         MPI_Datatype str, con;
25         MPI_Status status;
26
27         MTest_Init( &argc, &argv );
28
29         MPI_Comm_rank( MPI_COMM_WORLD, &rank );
30         MPI_Comm_size( MPI_COMM_WORLD, &size );
31
32         for( j = 0; j < 10; j ++ ) {
33                 s[j].i = j + rank;
34                 s[j].c = j + rank + 'a';
35         }
36
37         blens[0] = blens[1] = 1;
38         disps[0] = 0; disps[1] = sizeof(int);
39         bases[0] = MPI_INT; bases[1] = MPI_CHAR;
40         MPI_Type_struct( 2, blens, disps, bases, &str );
41         MPI_Type_commit( &str );
42         MPI_Type_contiguous( 10, str, &con );
43         MPI_Type_commit( &con );
44         MPI_Type_size( con, &tsize );
45         MPI_Type_extent( con, &text );
46
47         MTestPrintfMsg( 0, "Size of MPI array is %d, extent is %d\n",
48                         tsize, text );
49
50         /* The following block of code is only for verbose-level output */
51         {
52         void * p1, *p2;
53         p1 = s;
54         p2 = &(s[10].i);  /* This statement may fail on some systems */
55         MTestPrintfMsg( 0,
56                 "C array starts at %p and ends at %p for a length of %d\n",
57                 s, &(s[9].c), (char *)p2-(char *)p1 );
58         }
59
60         MPI_Type_extent( str, &text );
61         MPI_Type_size( str, &tsize );
62         MTestPrintfMsg( 0, "Size of MPI struct is %d, extent is %d\n",
63                         tsize, (int)text );
64         MTestPrintfMsg( 0, "Size of C struct is %d\n", sizeof(struct a) );
65         if (text != sizeof(struct a)) {
66             fprintf( stderr,
67                      "Extent of struct a (%d) does not match sizeof (%d)\n",
68                      (int)text, (int)sizeof(struct a) );
69             errs++;
70         }
71
72         MPI_Send( s, 1, con, rank ^ 1, 0, MPI_COMM_WORLD );
73         MPI_Recv( s1, 1, con, rank ^ 1, 0, MPI_COMM_WORLD, &status );
74
75         for( j = 0; j < 10; j++ ) {
76             MTestPrintfMsg( 0, "%d Sent: %d %c, Got: %d %c\n", rank,
77                             s[j].i, s[j].c, s1[j].i, s1[j].c );
78             if ( s1[j].i != j + status.MPI_SOURCE ) {
79                 errs++;
80                 fprintf( stderr, "Got s[%d].i = %d; expected %d\n", j, s1[j].i,
81                         j + status.MPI_SOURCE );
82             }
83             if ( s1[j].c != 'a' + j + status.MPI_SOURCE ) {
84                 errs++;
85                 /* If the character is not a printing character,
86                    this can generate a file that diff, for example,
87                    believes is a binary file */
88                 if (isprint( (int)(s1[j].c) )) {
89                     fprintf( stderr, "Got s[%d].c = %c; expected %c\n",
90                              j, s1[j].c, j + status.MPI_SOURCE + 'a');
91                 }
92                 else {
93                     fprintf( stderr, "Got s[%d].c = %x; expected %c\n",
94                              j, (int)s1[j].c, j + status.MPI_SOURCE + 'a');
95                 }
96             }
97         }
98
99         MPI_Type_free( &str );
100         MPI_Type_free( &con );
101
102         MTest_Finalize( errs );
103         MPI_Finalize();
104         return 0;
105 }