Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still not our
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / pairtype-pack.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "mpitestconf.h"
10 #ifdef HAVE_STRING_H
11 #include <string.h>
12 #endif
13
14 static int verbose = 0;
15
16 int short_int_pack_test(void);
17
18 /* helper functions */
19 int parse_args(int argc, char **argv);
20 static int pack_and_unpack(char *typebuf,
21                            int count,
22                            MPI_Datatype datatype,
23                            int typebufsz);
24
25 int main(int argc, char *argv[])
26 {
27     int err, errs = 0;
28
29     MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
30     parse_args(argc, argv);
31
32     /* To improve reporting of problems about operations, we
33        change the error handler to errors return */
34     MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
35
36     err = short_int_pack_test();
37     errs += err;
38
39     /* print message and exit */
40     if (errs) {
41         fprintf(stderr, "Found %d errors\n", errs);
42     }
43     else {
44         printf(" No Errors\n");
45     }
46     MPI_Finalize();
47     return 0;
48 }
49
50 int short_int_pack_test(void)
51 {
52     int i, err, errs = 0;
53
54     struct shortint { short a; int b; } sibuf[16];
55
56     for (i=0; i < 16; i++) {
57         sibuf[i].a = (short) (i * 2);
58         sibuf[i].b = i * 2 + 1;
59     }
60
61     err = pack_and_unpack((char *) sibuf, 16, MPI_SHORT_INT, sizeof(sibuf));
62     if (err != 0) {
63         if (verbose) {
64             fprintf(stderr,
65                     "error packing/unpacking in short_int_pack_test()\n");
66         }
67         errs += err;
68     }
69
70     for (i=0; i < 16; i++) {
71         if (sibuf[i].a != (short) (i * 2)) {
72             err++;
73             if (verbose) {
74                 fprintf(stderr,
75                         "buf[%d] has invalid short (%d); should be %d\n",
76                         i, (int) sibuf[i].a, i * 2);
77             }
78         }
79         if (sibuf[i].b != i * 2 + 1) {
80             err++;
81             if (verbose) {
82                 fprintf(stderr,
83                         "buf[%d] has invalid int (%d); should be %d\n",
84                         i, (int) sibuf[i].b, i * 2 + 1);
85             }
86         }
87     }
88
89     return errs;
90 }
91
92 /* pack_and_unpack()
93  *
94  * Perform packing and unpacking of a buffer for the purposes of checking
95  * to see if we are processing a type correctly.  Zeros the buffer between
96  * these two operations, so the data described by the type should be in
97  * place upon return but all other regions of the buffer should be zero.
98  *
99  * Parameters:
100  * typebuf - pointer to buffer described by datatype and count that
101  *           will be packed and then unpacked into
102  * count, datatype - description of typebuf
103  * typebufsz - size of typebuf; used specifically to zero the buffer
104  *             between the pack and unpack steps
105  *
106  */
107 static int pack_and_unpack(char *typebuf,
108                            int count,
109                            MPI_Datatype datatype,
110                            int typebufsz)
111 {
112     char *packbuf;
113     int err, errs = 0, pack_size, type_size, position;
114
115     err = MPI_Type_size(datatype, &type_size);
116     if (err != MPI_SUCCESS) {
117         errs++;
118         if (verbose) {
119             fprintf(stderr,
120                     "error in MPI_Type_size call; aborting after %d errors\n",
121                     errs);
122         }
123         return errs;
124     }
125
126     type_size *= count;
127
128     err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
129     if (err != MPI_SUCCESS) {
130         errs++;
131         if (verbose) {
132             fprintf(stderr,
133                     "error in MPI_Pack_size call; aborting after %d errors\n",
134                     errs);
135         }
136         return errs;
137     }
138     packbuf = (char *) malloc(pack_size);
139     if (packbuf == NULL) {
140         errs++;
141         if (verbose) {
142             fprintf(stderr,
143                     "error in malloc call; aborting after %d errors\n",
144                     errs);
145         }
146         return errs;
147     }
148
149     position = 0;
150     err = MPI_Pack(typebuf,
151                    count,
152                    datatype,
153                    packbuf,
154                    type_size,
155                    &position,
156                    MPI_COMM_SELF);
157
158     if (position != type_size) {
159         errs++;
160         if (verbose) fprintf(stderr, "position = %d; should be %d (pack)\n",
161                              position, type_size);
162     }
163
164     memset(typebuf, 0, typebufsz);
165     position = 0;
166     err = MPI_Unpack(packbuf,
167                      type_size,
168                      &position,
169                      typebuf,
170                      count,
171                      datatype,
172                      MPI_COMM_SELF);
173     if (err != MPI_SUCCESS) {
174         errs++;
175         if (verbose) {
176             fprintf(stderr,
177                     "error in MPI_Unpack call; aborting after %d errors\n",
178                     errs);
179         }
180         return errs;
181     }
182     free(packbuf);
183
184     if (position != type_size) {
185         errs++;
186         if (verbose) fprintf(stderr, "position = %d; should be %d (unpack)\n",
187                              position, type_size);
188     }
189
190     return errs;
191 }
192
193 int parse_args(int argc, char **argv)
194 {
195     /*
196     int ret;
197
198     while ((ret = getopt(argc, argv, "v")) >= 0)
199     {
200         switch (ret) {
201             case 'v':
202                 verbose = 1;
203                 break;
204         }
205     }
206     */
207     if (argc > 1 && strcmp(argv[1], "-v") == 0)
208         verbose = 1;
209     return 0;
210 }