Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update datatype mpich tests
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / lots-of-types.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 <math.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "mpi.h"
11 #include "mpitest.h"
12
13 /*
14    The default behavior of the test routines should be to briefly indicate
15    the cause of any errors - in this test, that means that verbose needs
16    to be set. Verbose should turn on output that is independent of error
17    levels.
18 */
19 static int verbose = 1;
20
21 int main(int argc, char *argv[]);
22 int parse_args(int argc, char **argv);
23 int lots_of_types_test(void);
24
25 struct test_struct_1 {
26     int a, b, c, d;
27 };
28
29 int main(int argc, char *argv[])
30 {
31     int err, errs = 0;
32
33     /* Initialize MPI */
34     MTest_Init(&argc, &argv);
35     parse_args(argc, argv);
36
37     /* To improve reporting of problems about operations, we
38      * change the error handler to errors return */
39     MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
40
41     err = lots_of_types_test();
42     if (verbose && err)
43         fprintf(stderr, "error in lots_of_types_test\n");
44     errs += err;
45
46     /* print message and exit */
47     MTest_Finalize(errs);
48
49     MPI_Finalize();
50     return 0;
51 }
52
53 /* this test allocates 1024 indexed datatypes with 1024 distinct blocks
54  * each.  it's possible that a low memory machine will run out of memory
55  * running this test; it appears to take ~25MB of memory at this time.
56  * -- Rob Ross, 11/2/2005
57  */
58 #define NUM_DTYPES 1024
59 #define NUM_BLOCKS 1024
60 int lots_of_types_test(void)
61 {
62     int err, errs = 0;
63     int i;
64     MPI_Datatype mytypes[NUM_DTYPES];
65
66     int sendbuf[4] = { 1, 2, 3, 4 };
67
68     int count, elements;
69     MPI_Request request;
70     MPI_Status status;
71
72     /* note: first element of struct has zero blklen and should be dropped */
73     int disps[NUM_BLOCKS];
74     int blks[NUM_BLOCKS];
75
76     for (i = 0; i < NUM_DTYPES; i++)
77         mytypes[i] = MPI_DATATYPE_NULL;
78
79     for (i = 0; i < NUM_DTYPES; i++) {
80         int j;
81
82         disps[0] = 0;
83         blks[0] = 4;
84
85         for (j = 1; j < NUM_BLOCKS; j++) {
86             disps[j] = 4 * j;
87             blks[j] = (j % 3) + 1;
88         }
89
90         err = MPI_Type_indexed(NUM_BLOCKS, blks, disps, MPI_INT, &mytypes[i]);
91         if (err != MPI_SUCCESS) {
92             errs++;
93             if (verbose) {
94                 fprintf(stderr, "MPI_Type_indexed returned error on type %d\n", i);
95             }
96             mytypes[i] = MPI_DATATYPE_NULL;
97             goto fn_exit;
98         }
99
100         MPI_Type_commit(&mytypes[i]);
101     }
102
103     for (i = 0; i < NUM_DTYPES; i++) {
104         int j;
105         int recvbuf[4] = { -1, -1, -1, -1 };
106
107         /* we will only receive 4 ints, so short buffer is ok */
108         err = MPI_Irecv(recvbuf, 1, mytypes[i], 0, 0, MPI_COMM_SELF, &request);
109         if (err != MPI_SUCCESS) {
110             errs++;
111             if (verbose) {
112                 fprintf(stderr, "MPI_Irecv returned error\n");
113             }
114         }
115
116         err = MPI_Send(sendbuf, 4, MPI_INT, 0, 0, MPI_COMM_SELF);
117         if (err != MPI_SUCCESS) {
118             errs++;
119             if (verbose) {
120                 fprintf(stderr, "MPI_Send returned error\n");
121             }
122         }
123
124         err = MPI_Wait(&request, &status);
125         if (err != MPI_SUCCESS) {
126             errs++;
127             if (verbose) {
128                 fprintf(stderr, "MPI_Wait returned error\n");
129             }
130         }
131
132         /* verify data */
133         for (j = 0; j < 4; j++) {
134             if (recvbuf[j] != sendbuf[j]) {
135                 errs++;
136                 if (verbose) {
137                     fprintf(stderr, "recvbuf[%d] = %d; should be %d\n", j, recvbuf[j], sendbuf[j]);
138                 }
139             }
140         }
141
142         /* verify count and elements */
143         err = MPI_Get_count(&status, mytypes[i], &count);
144         if (err != MPI_SUCCESS) {
145             errs++;
146             if (verbose) {
147                 fprintf(stderr, "MPI_Get_count returned error\n");
148             }
149         }
150         if (count != MPI_UNDEFINED) {
151             errs++;
152             if (verbose) {
153                 fprintf(stderr, "count = %d; should be MPI_UNDEFINED (%d)\n", count, MPI_UNDEFINED);
154             }
155         }
156
157         err = MPI_Get_elements(&status, mytypes[i], &elements);
158         if (err != MPI_SUCCESS) {
159             errs++;
160             if (verbose) {
161                 fprintf(stderr, "MPI_Get_elements returned error\n");
162             }
163         }
164         if (elements != 4) {
165             errs++;
166             if (verbose) {
167                 fprintf(stderr, "elements = %d; should be 4\n", elements);
168             }
169         }
170     }
171
172   fn_exit:
173     for (i = 0; i < NUM_DTYPES; i++) {
174         if (mytypes[i] != MPI_DATATYPE_NULL)
175             MPI_Type_free(&mytypes[i]);
176     }
177
178     return errs;
179 }
180
181
182 int parse_args(int argc, char **argv)
183 {
184     /*
185      * int ret;
186      *
187      * while ((ret = getopt(argc, argv, "v")) >= 0)
188      * {
189      * switch (ret) {
190      * case 'v':
191      * verbose = 1;
192      * break;
193      * }
194      * }
195      */
196     if (argc > 1 && strcmp(argv[1], "-v") == 0)
197         verbose = 1;
198     return 0;
199 }