Logo AND Algorithmique Numérique Distribuée

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