Logo AND Algorithmique Numérique Distribuée

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