Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update datatype mpich tests
[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 {
15         int i;
16         char c;
17     } s[10], s1[10];
18     int j;
19     int errs = 0;
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     MPI_Status status;
27
28     MTest_Init(&argc, &argv);
29
30     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31     MPI_Comm_size(MPI_COMM_WORLD, &size);
32
33     for (j = 0; j < 10; j++) {
34         s[j].i = j + rank;
35         s[j].c = j + rank + 'a';
36     }
37
38     blens[0] = blens[1] = 1;
39     disps[0] = 0;
40     disps[1] = sizeof(int);
41     bases[0] = MPI_INT;
42     bases[1] = MPI_CHAR;
43     MPI_Type_struct(2, blens, disps, bases, &str);
44     MPI_Type_commit(&str);
45     MPI_Type_contiguous(10, str, &con);
46     MPI_Type_commit(&con);
47     MPI_Type_size(con, &tsize);
48     MPI_Type_extent(con, &text);
49
50     MTestPrintfMsg(0, "Size of MPI array is %d, extent is %d\n", tsize, text);
51
52     /* The following block of code is only for verbose-level output */
53     {
54         void *p1, *p2;
55         p1 = s;
56         p2 = &(s[10].i);        /* This statement may fail on some systems */
57         MTestPrintfMsg(0,
58                        "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
62     MPI_Type_extent(str, &text);
63     MPI_Type_size(str, &tsize);
64     MTestPrintfMsg(0, "Size of MPI struct is %d, extent is %d\n", tsize, (int) text);
65     MTestPrintfMsg(0, "Size of C struct is %d\n", sizeof(struct a));
66     if (text != sizeof(struct a)) {
67         fprintf(stderr,
68                 "Extent of struct a (%d) does not match sizeof (%d)\n",
69                 (int) text, (int) sizeof(struct a));
70         errs++;
71     }
72
73     MPI_Send(s, 1, con, rank ^ 1, 0, MPI_COMM_WORLD);
74     MPI_Recv(s1, 1, con, rank ^ 1, 0, MPI_COMM_WORLD, &status);
75
76     for (j = 0; j < 10; j++) {
77         MTestPrintfMsg(0, "%d Sent: %d %c, Got: %d %c\n", rank, 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, j + status.MPI_SOURCE);
81         }
82         if (s1[j].c != 'a' + j + status.MPI_SOURCE) {
83             errs++;
84             /* If the character is not a printing character,
85              * this can generate a file that diff, for example,
86              * believes is a binary file */
87             if (isprint((int) (s1[j].c))) {
88                 fprintf(stderr, "Got s[%d].c = %c; expected %c\n",
89                         j, s1[j].c, j + status.MPI_SOURCE + 'a');
90             }
91             else {
92                 fprintf(stderr, "Got s[%d].c = %x; expected %c\n",
93                         j, (int) s1[j].c, j + status.MPI_SOURCE + 'a');
94             }
95         }
96     }
97
98     MPI_Type_free(&str);
99     MPI_Type_free(&con);
100
101     MTest_Finalize(errs);
102     MPI_Finalize();
103     return 0;
104 }