Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9722167b7ed199432d0b3506aed5c58b0801b795
[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) fprintf(stderr, "error in lots_of_types_test\n");
43     errs += err;
44
45     /* print message and exit */
46     MTest_Finalize( errs );
47
48     MPI_Finalize();
49     return 0;
50 }
51
52 /* this test allocates 1024 indexed datatypes with 1024 distinct blocks
53  * each.  it's possible that a low memory machine will run out of memory
54  * running this test; it appears to take ~25MB of memory at this time.
55  * -- Rob Ross, 11/2/2005
56  */
57 #define NUM_DTYPES 1024
58 #define NUM_BLOCKS 1024
59 int lots_of_types_test(void)
60 {
61     int err, errs = 0;
62     int i;
63     MPI_Datatype mytypes[NUM_DTYPES];
64
65     int sendbuf[4] = { 1, 2, 3, 4 };
66
67     int count, elements;
68     MPI_Request request;
69     MPI_Status status;
70
71     /* note: first element of struct has zero blklen and should be dropped */
72     int disps[NUM_BLOCKS];
73     int blks[NUM_BLOCKS];
74
75     for (i=0; i < NUM_DTYPES; i++)
76         mytypes[i] = MPI_DATATYPE_NULL;
77
78     for (i=0; i < NUM_DTYPES; i++) {
79         int j;
80
81         disps[0] = 0;
82         blks[0]  = 4;
83         
84         for (j=1; j < NUM_BLOCKS; j++) {
85             disps[j] = 4 * j;
86             blks[j]  = (j % 3) + 1;
87         }
88
89         err = MPI_Type_indexed(NUM_BLOCKS, blks, disps, MPI_INT, &mytypes[i]);
90         if (err != MPI_SUCCESS) {
91             errs++;
92             if (verbose) {
93                 fprintf(stderr, "MPI_Type_indexed returned error on type %d\n",
94                         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",
138                             j, recvbuf[j], sendbuf[j]);
139                 }
140             }
141         }
142
143         /* verify count and elements */
144         err = MPI_Get_count(&status, mytypes[i], &count);
145         if (err != MPI_SUCCESS) {
146             errs++;
147             if (verbose) {
148                 fprintf(stderr, "MPI_Get_count returned error\n");
149             }
150         }
151         if (count != MPI_UNDEFINED) {
152             errs++;
153             if (verbose) {
154                 fprintf(stderr, "count = %d; should be MPI_UNDEFINED (%d)\n",
155                         count, MPI_UNDEFINED);
156             }
157         }
158         
159         err = MPI_Get_elements(&status, mytypes[i], &elements);
160         if (err != MPI_SUCCESS) {
161             errs++;
162             if (verbose) {
163                 fprintf(stderr, "MPI_Get_elements returned error\n");
164             }
165         }
166         if (elements != 4) {
167             errs++;
168             if (verbose) {
169                 fprintf(stderr, "elements = %d; should be 4\n", elements);
170             }
171         }
172     }
173
174  fn_exit:
175     for (i=0; i < NUM_DTYPES; i++) {
176         if (mytypes[i] != MPI_DATATYPE_NULL)
177             MPI_Type_free(&mytypes[i]);
178     }
179
180     return errs;
181 }
182
183
184 int parse_args(int argc, char **argv)
185 {
186     /*
187     int ret;
188
189     while ((ret = getopt(argc, argv, "v")) >= 0)
190     {
191         switch (ret) {
192             case 'v':
193                 verbose = 1;
194                 break;
195         }
196     }
197     */
198     if (argc > 1 && strcmp(argv[1], "-v") == 0)
199         verbose = 1;
200     return 0;
201 }