Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still not our
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / structpack2.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 "mpitest.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10 /* The next is for isprint */
11 #include <ctype.h>
12
13 int main( int argc, char *argv[])
14 {
15         struct a {      int     i;
16                         char    c;
17                 } s[10], s1[10];
18         int j;
19         int errs = 0, toterrs;
20         int rank, size, tsize;
21         MPI_Aint text;
22         int blens[2];
23         MPI_Aint disps[2];
24         MPI_Datatype bases[2];
25         MPI_Datatype str, con;
26         char *buffer;
27         int   bufsize, position, insize;
28
29         MTest_Init( &argc, &argv );
30
31         MPI_Comm_rank( MPI_COMM_WORLD, &rank );
32         MPI_Comm_size( MPI_COMM_WORLD, &size );
33
34         for( j = 0; j < 10; j ++ ) {
35                 s[j].i = j + rank;
36                 s[j].c = j + rank + 'a';
37         }
38
39         blens[0] = blens[1] = 1;
40         disps[0] = 0; disps[1] = sizeof(int);
41         bases[0] = MPI_INT; bases[1] = MPI_CHAR;
42         MPI_Type_struct( 2, blens, disps, bases, &str );
43         MPI_Type_commit( &str );
44         MPI_Type_contiguous( 10, str, &con );
45         MPI_Type_commit( &con );
46         MPI_Type_size( con, &tsize );
47         MPI_Type_extent( con, &text );
48
49 #ifdef DEBUG
50         printf("Size of MPI array is %d, extent is %d\n", tsize, text );
51 #endif
52
53 #ifdef DEBUG
54         {
55         void * p1, *p2;
56         p1 = s;
57         p2 = &(s[10].i);  /* This statement may fail on some systems */
58         printf("C array starts at %p and ends at %p for a length of %d\n",
59                 s, &(s[9].c), (char *)p2-(char *)p1 );
60         }
61 #endif
62         MPI_Type_extent( str, &text );
63 #ifdef DEBUG
64         MPI_Type_size( str, &tsize );
65         printf("Size of MPI struct is %d, extent is %d\n", tsize, (int)text );
66         printf("Size of C struct is %d\n", sizeof(struct a) );
67 #endif
68         if (text != sizeof(struct a)) {
69             printf( "Extent of struct a (%d) does not match sizeof (%d)\n",
70                     (int)text, (int)sizeof(struct a) );
71             errs++;
72         }
73
74         MPI_Pack_size(1, con, MPI_COMM_WORLD, &bufsize);
75         buffer = (char *) malloc(bufsize);
76
77         position = 0;
78         MPI_Pack(s,1,con,buffer,bufsize,&position,MPI_COMM_WORLD);
79         insize   = position;
80         position = 0;
81         MPI_Unpack(buffer,insize,&position,s1,1,con,MPI_COMM_WORLD );
82
83         for( j = 0; j < 10; j++ ) {
84 #ifdef DEBUG
85                 printf("%d Sent: %d %c, Got: %d %c\n", rank,
86                         s[j].i, s[j].c, s1[j].i, s1[j].c );
87 #endif
88                 if ( s1[j].i != j + rank ) {
89                     errs++;
90                     printf( "Got s[%d].i = %d (%x); expected %d\n", j, s1[j].i,
91                             s1[j].i, j + rank );
92                 }
93                 if ( s1[j].c != 'a' + j + rank ) {
94                     errs++;
95                     /* If the character is not a printing character,
96                        this can generate an file that diff, for example,
97                        believes is a binary file */
98                     if (isprint( (int)(s1[j].c) )) {
99                         printf( "Got s[%d].c = %c; expected %c\n", j, s1[j].c,
100                                 j + rank + 'a');
101                     }
102                     else {
103                         printf( "Got s[%d].c = %x; expected %c\n", j, (int)s1[j].c,
104                                 j + rank + 'a');
105                     }
106                 }
107         }
108
109         MPI_Type_free( &str );
110         MPI_Type_free( &con );
111         MTest_Finalize( errs );
112         MPI_Finalize();
113         return 0;
114 }