Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / simple-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 /* tests */
17 int builtin_float_test(void);
18 int vector_of_vectors_test(void);
19 int optimizable_vector_of_basics_test(void);
20
21 /* helper functions */
22 int parse_args(int argc, char **argv);
23
24 int main(int argc, char **argv)
25 {
26     int err, errs = 0;
27
28     MPI_Init(&argc, &argv);     /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
29     parse_args(argc, argv);
30
31     /* To improve reporting of problems about operations, we
32      * change the error handler to errors return */
33     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
34
35     /* perform some tests */
36     err = builtin_float_test();
37     if (err && verbose)
38         fprintf(stderr, "%d errors in builtin float test.\n", err);
39     errs += err;
40
41     err = vector_of_vectors_test();
42     if (err && verbose)
43         fprintf(stderr, "%d errors in vector of vectors test.\n", err);
44     errs += err;
45
46     err = optimizable_vector_of_basics_test();
47     if (err && verbose)
48         fprintf(stderr, "%d errors in vector of basics test.\n", err);
49     errs += err;
50
51     /* print message and exit */
52     if (errs) {
53         fprintf(stderr, "Found %d errors\n", errs);
54     }
55     else {
56         printf(" No Errors\n");
57     }
58     MPI_Finalize();
59     return 0;
60 }
61
62 /* builtin_float_test()
63  *
64  * Tests functionality of get_envelope() and get_contents() on a MPI_FLOAT.
65  *
66  * Returns the number of errors encountered.
67  */
68 int builtin_float_test(void)
69 {
70     int nints, nadds, ntypes, combiner;
71
72     int errs = 0;
73
74     MPI_Type_get_envelope(MPI_FLOAT, &nints, &nadds, &ntypes, &combiner);
75
76     if (combiner != MPI_COMBINER_NAMED)
77         errs++;
78
79     /* Note: it is erroneous to call MPI_Type_get_contents() on a basic. */
80     return errs;
81 }
82
83 /* vector_of_vectors_test()
84  *
85  * Builds a vector of a vector of ints.  Assuming an int array of size 9
86  * integers, and treating the array as a 3x3 2D array, this will grab the
87  * corners.
88  *
89  * Returns the number of errors encountered.
90  */
91 int vector_of_vectors_test(void)
92 {
93     MPI_Datatype inner_vector;
94     MPI_Datatype outer_vector;
95     int array[9] = { 1, -1, 2,
96         -2, -3, -4,
97         3, -5, 4
98     };
99
100     char *buf;
101     int i, err, errs = 0, sizeoftype, position;
102
103     /* set up type */
104     err = MPI_Type_vector(2, 1, 2, MPI_INT, &inner_vector);
105     if (err != MPI_SUCCESS) {
106         errs++;
107         if (verbose)
108             fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs + 1);
109         return errs;
110     }
111
112     err = MPI_Type_vector(2, 1, 2, inner_vector, &outer_vector);
113     if (err != MPI_SUCCESS) {
114         errs++;
115         if (verbose)
116             fprintf(stderr, "error in MPI call; aborting after %d errors\n", errs + 1);
117         return errs;
118     }
119
120     MPI_Type_commit(&outer_vector);
121     MPI_Type_size(outer_vector, &sizeoftype);
122     if (sizeoftype != 4 * sizeof(int)) {
123         errs++;
124         if (verbose)
125             fprintf(stderr, "size of type = %d; should be %d\n",
126                     (int) sizeoftype, (int) (4 * sizeof(int)));
127         return errs;
128     }
129
130     buf = (char *) malloc(sizeoftype);
131
132     position = 0;
133     err = MPI_Pack(array, 1, outer_vector, buf, sizeoftype, &position, MPI_COMM_WORLD);
134
135     if (position != sizeoftype) {
136         errs++;
137         if (verbose)
138             fprintf(stderr, "position = %d; should be %d (pack)\n", position, sizeoftype);
139     }
140
141     memset(array, 0, 9 * sizeof(int));
142     position = 0;
143     err = MPI_Unpack(buf, sizeoftype, &position, array, 1, outer_vector, MPI_COMM_WORLD);
144
145     if (position != sizeoftype) {
146         errs++;
147         if (verbose)
148             fprintf(stderr, "position = %d; should be %d (unpack)\n", position, sizeoftype);
149     }
150
151     for (i = 0; i < 9; i++) {
152         int goodval;
153         switch (i) {
154         case 0:
155             goodval = 1;
156             break;
157         case 2:
158             goodval = 2;
159             break;
160         case 6:
161             goodval = 3;
162             break;
163         case 8:
164             goodval = 4;
165             break;
166         default:
167             goodval = 0;
168             break;
169         }
170         if (array[i] != goodval) {
171             errs++;
172             if (verbose)
173                 fprintf(stderr, "array[%d] = %d; should be %d\n", i, array[i], goodval);
174         }
175     }
176
177     free(buf);
178     MPI_Type_free(&inner_vector);
179     MPI_Type_free(&outer_vector);
180     return errs;
181 }
182
183 /* optimizable_vector_of_basics_test()
184  *
185  * Builds a vector of ints.  Count is 10, blocksize is 2, stride is 2, so this
186  * is equivalent to a contig of 20.
187  *
188  * Returns the number of errors encountered.
189  */
190 int optimizable_vector_of_basics_test(void)
191 {
192     MPI_Datatype parent_type;
193     int array[20] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
194         16, 17, 18, 19
195     };
196     char *buf;
197     int i, sizeofint, sizeoftype, position;
198
199     int errs = 0;
200
201     MPI_Type_size(MPI_INT, &sizeofint);
202
203     if (sizeofint != sizeof(int)) {
204         errs++;
205         if (verbose)
206             fprintf(stderr, "size of MPI_Int = %d; should be %d\n", sizeofint, (int) sizeof(int));
207     }
208
209     /* set up type */
210     MPI_Type_vector(10, 2, 2, MPI_INT, &parent_type);
211
212     MPI_Type_commit(&parent_type);
213
214     MPI_Type_size(parent_type, &sizeoftype);
215
216     if (sizeoftype != 20 * sizeof(int)) {
217         errs++;
218         if (verbose)
219             fprintf(stderr, "size of vector = %d; should be %d\n",
220                     (int) sizeoftype, (int) (20 * sizeof(int)));
221     }
222
223     buf = (char *) malloc(sizeoftype);
224
225     position = 0;
226     MPI_Pack(array, 1, parent_type, buf, sizeoftype, &position, MPI_COMM_WORLD);
227
228     if (position != sizeoftype) {
229         errs++;
230         if (verbose)
231             fprintf(stderr, "position = %d; should be %d (pack)\n", position, sizeoftype);
232     }
233
234     memset(array, 0, 20 * sizeof(int));
235     position = 0;
236     MPI_Unpack(buf, sizeoftype, &position, array, 1, parent_type, MPI_COMM_WORLD);
237
238     if (position != sizeoftype) {
239         errs++;
240         if (verbose)
241             fprintf(stderr, "position = %d; should be %d (unpack)\n", position, sizeoftype);
242     }
243
244     for (i = 0; i < 20; i++) {
245         if (array[i] != i) {
246             errs++;
247             if (verbose)
248                 fprintf(stderr, "array[%d] = %d; should be %d\n", i, array[i], i);
249         }
250     }
251
252     free(buf);
253     MPI_Type_free(&parent_type);
254     return errs;
255 }
256
257
258 int parse_args(int argc, char **argv)
259 {
260     /*
261      * int ret;
262      *
263      * while ((ret = getopt(argc, argv, "v")) >= 0)
264      * {
265      * switch (ret) {
266      * case 'v':
267      * verbose = 1;
268      * break;
269      * }
270      * }
271      */
272     if (argc > 1 && strcmp(argv[1], "-v") == 0)
273         verbose = 1;
274     return 0;
275 }