Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
missed these spots while renaming an option
[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, int count, MPI_Datatype datatype, int typebufsz);
21
22 int main(int argc, char *argv[])
23 {
24     int err, errs = 0;
25
26     MPI_Init(&argc, &argv);     /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
27     parse_args(argc, argv);
28
29     /* To improve reporting of problems about operations, we
30      * change the error handler to errors return */
31     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
32
33     err = short_int_pack_test();
34     errs += err;
35
36     /* print message and exit */
37     if (errs) {
38         fprintf(stderr, "Found %d errors\n", errs);
39     }
40     else {
41         printf(" No Errors\n");
42     }
43     MPI_Finalize();
44     return 0;
45 }
46
47 int short_int_pack_test(void)
48 {
49     int i, err, errs = 0;
50
51     struct shortint {
52         short a;
53         int b;
54     } 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, "error packing/unpacking in short_int_pack_test()\n");
65         }
66         errs += err;
67     }
68
69     for (i = 0; i < 16; i++) {
70         if (sibuf[i].a != (short) (i * 2)) {
71             err++;
72             if (verbose) {
73                 fprintf(stderr,
74                         "buf[%d] has invalid short (%d); should be %d\n",
75                         i, (int) sibuf[i].a, i * 2);
76             }
77         }
78         if (sibuf[i].b != i * 2 + 1) {
79             err++;
80             if (verbose) {
81                 fprintf(stderr,
82                         "buf[%d] has invalid int (%d); should be %d\n",
83                         i, (int) sibuf[i].b, i * 2 + 1);
84             }
85         }
86     }
87
88     return errs;
89 }
90
91 /* pack_and_unpack()
92  *
93  * Perform packing and unpacking of a buffer for the purposes of checking
94  * to see if we are processing a type correctly.  Zeros the buffer between
95  * these two operations, so the data described by the type should be in
96  * place upon return but all other regions of the buffer should be zero.
97  *
98  * Parameters:
99  * typebuf - pointer to buffer described by datatype and count that
100  *           will be packed and then unpacked into
101  * count, datatype - description of typebuf
102  * typebufsz - size of typebuf; used specifically to zero the buffer
103  *             between the pack and unpack steps
104  *
105  */
106 static int pack_and_unpack(char *typebuf, int count, MPI_Datatype datatype, int typebufsz)
107 {
108     char *packbuf;
109     int err, errs = 0, pack_size, type_size, position;
110
111     err = MPI_Type_size(datatype, &type_size);
112     if (err != MPI_SUCCESS) {
113         errs++;
114         if (verbose) {
115             fprintf(stderr, "error in MPI_Type_size call; aborting after %d errors\n", errs);
116         }
117         return errs;
118     }
119
120     type_size *= count;
121
122     err = MPI_Pack_size(count, datatype, MPI_COMM_SELF, &pack_size);
123     if (err != MPI_SUCCESS) {
124         errs++;
125         if (verbose) {
126             fprintf(stderr, "error in MPI_Pack_size call; aborting after %d errors\n", errs);
127         }
128         return errs;
129     }
130     packbuf = (char *) malloc(pack_size);
131     if (packbuf == NULL) {
132         errs++;
133         if (verbose) {
134             fprintf(stderr, "error in malloc call; aborting after %d errors\n", errs);
135         }
136         return errs;
137     }
138
139     position = 0;
140     err = MPI_Pack(typebuf, count, datatype, packbuf, type_size, &position, MPI_COMM_SELF);
141
142     if (position != type_size) {
143         errs++;
144         if (verbose)
145             fprintf(stderr, "position = %d; should be %d (pack)\n", position, type_size);
146     }
147
148     memset(typebuf, 0, typebufsz);
149     position = 0;
150     err = MPI_Unpack(packbuf, type_size, &position, typebuf, count, datatype, MPI_COMM_SELF);
151     if (err != MPI_SUCCESS) {
152         errs++;
153         if (verbose) {
154             fprintf(stderr, "error in MPI_Unpack call; aborting after %d errors\n", errs);
155         }
156         return errs;
157     }
158     free(packbuf);
159
160     if (position != type_size) {
161         errs++;
162         if (verbose)
163             fprintf(stderr, "position = %d; should be %d (unpack)\n", position, type_size);
164     }
165
166     return errs;
167 }
168
169 int parse_args(int argc, char **argv)
170 {
171     /*
172      * int ret;
173      *
174      * while ((ret = getopt(argc, argv, "v")) >= 0)
175      * {
176      * switch (ret) {
177      * case 'v':
178      * verbose = 1;
179      * break;
180      * }
181      * }
182      */
183     if (argc > 1 && strcmp(argv[1], "-v") == 0)
184         verbose = 1;
185     return 0;
186 }