Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / hindexed-zeros.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
12 static int verbose = 0;
13
14 int main(int argc, char *argv[]);
15 int parse_args(int argc, char **argv);
16 int hindexed_zerotype_test(void);
17 int hindexed_sparsetype_test(void);
18
19 struct test_struct_1 {
20     int a, b, c, d;
21 };
22
23 int main(int argc, char *argv[])
24 {
25     int err, errs = 0;
26
27     /* Initialize MPI */
28     MPI_Init(&argc, &argv);
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     err = hindexed_zerotype_test();
36     if (verbose && err)
37         fprintf(stderr, "error in hindexed_zerotype_test\n");
38     errs += err;
39
40     err = hindexed_sparsetype_test();
41     if (verbose && err)
42         fprintf(stderr, "error in hindexed_sparsetype_test\n");
43     errs += err;
44
45     /* print message and exit */
46     if (errs) {
47         fprintf(stderr, "Found %d errors\n", errs);
48     }
49     else {
50         printf(" No Errors\n");
51     }
52     MPI_Finalize();
53     return 0;
54 }
55
56 /* tests with an hindexed type with all zero length blocks */
57 int hindexed_zerotype_test(void)
58 {
59     int err, errs = 0;
60     int count, elements;
61     MPI_Datatype mytype;
62     MPI_Request request;
63     MPI_Status status;
64
65     int blks[] = { 0, 0, 0 };
66     MPI_Aint disps[] = { 0, 4, 16 };
67
68     err = MPI_Type_hindexed(3, blks, disps, MPI_INT, &mytype);
69     if (err != MPI_SUCCESS) {
70         errs++;
71         if (verbose) {
72             fprintf(stderr, "MPI_Type_hindexed returned error\n");
73         }
74     }
75
76     MPI_Type_commit(&mytype);
77
78     err = MPI_Irecv(NULL, 2, mytype, 0, 0, MPI_COMM_SELF, &request);
79     if (err != MPI_SUCCESS) {
80         errs++;
81         if (verbose) {
82             fprintf(stderr, "MPI_Irecv returned error\n");
83         }
84     }
85
86     err = MPI_Send(NULL, 1, mytype, 0, 0, MPI_COMM_SELF);
87     if (err != MPI_SUCCESS) {
88         errs++;
89         if (verbose) {
90             fprintf(stderr, "MPI_Send returned error\n");
91         }
92     }
93
94     err = MPI_Wait(&request, &status);
95     if (err != MPI_SUCCESS) {
96         errs++;
97         if (verbose) {
98             fprintf(stderr, "MPI_Wait returned error\n");
99         }
100     }
101
102     /* verify count and elements */
103     err = MPI_Get_count(&status, mytype, &count);
104     if (err != MPI_SUCCESS) {
105         errs++;
106         if (verbose) {
107             fprintf(stderr, "MPI_Get_count returned error\n");
108         }
109     }
110     if (count != 0) {
111         errs++;
112         if (verbose) {
113             fprintf(stderr, "count = %d; should be 0\n", count);
114         }
115     }
116
117     err = MPI_Get_elements(&status, mytype, &elements);
118     if (err != MPI_SUCCESS) {
119         errs++;
120         if (verbose) {
121             fprintf(stderr, "MPI_Get_elements returned error\n");
122         }
123     }
124     if (elements != 0) {
125         errs++;
126         if (verbose) {
127             fprintf(stderr, "elements = %d; should be 0\n", elements);
128         }
129     }
130
131     MPI_Type_free(&mytype);
132
133     return errs;
134 }
135
136 /* tests a short receive into a sparse hindexed type with a zero
137  * length block in it.  sort of eccentric, but we've got the basic
138  * stuff covered with other tests.
139  */
140 int hindexed_sparsetype_test(void)
141 {
142     int err, errs = 0;
143     int i, count, elements;
144     MPI_Datatype mytype;
145     MPI_Request request;
146     MPI_Status status;
147
148     int sendbuf[6] = { 1, 2, 3, 4, 5, 6 };
149     int recvbuf[16];
150     int correct[16] = { 1, -2, 4, -4, 2, 3, 5, -8, -9, -10, 6,
151         -12, -13, -14, -15, -16
152     };
153
154     int blks[] = { 1, 0, 2, 1 };
155     MPI_Aint disps[] = { 0, 1 * sizeof(int), 4 * sizeof(int), 2 * sizeof(int) };
156
157     err = MPI_Type_hindexed(4, blks, disps, MPI_INT, &mytype);
158     if (err != MPI_SUCCESS) {
159         errs++;
160         if (verbose) {
161             fprintf(stderr, "MPI_Type_hindexed returned error\n");
162         }
163     }
164
165     MPI_Type_commit(&mytype);
166
167     for (i = 0; i < 16; i++)
168         recvbuf[i] = -(i + 1);
169
170     err = MPI_Irecv(recvbuf, 2, mytype, 0, 0, MPI_COMM_SELF, &request);
171     if (err != MPI_SUCCESS) {
172         errs++;
173         if (verbose) {
174             fprintf(stderr, "MPI_Irecv returned error\n");
175         }
176     }
177
178     err = MPI_Send(sendbuf, 6, MPI_INT, 0, 0, MPI_COMM_SELF);
179     if (err != MPI_SUCCESS) {
180         errs++;
181         if (verbose) {
182             fprintf(stderr, "MPI_Send returned error\n");
183         }
184     }
185
186     err = MPI_Wait(&request, &status);
187     if (err != MPI_SUCCESS) {
188         errs++;
189         if (verbose) {
190             fprintf(stderr, "MPI_Wait returned error\n");
191         }
192     }
193
194     /* verify data */
195     for (i = 0; i < 16; i++) {
196         if (recvbuf[i] != correct[i]) {
197             errs++;
198             if (verbose) {
199                 fprintf(stderr, "recvbuf[%d] = %d; should be %d\n", i, recvbuf[i], correct[i]);
200             }
201         }
202     }
203
204     /* verify count and elements */
205     err = MPI_Get_count(&status, mytype, &count);
206     if (err != MPI_SUCCESS) {
207         errs++;
208         if (verbose) {
209             fprintf(stderr, "MPI_Get_count returned error\n");
210         }
211     }
212     if (count != MPI_UNDEFINED) {
213         errs++;
214         if (verbose) {
215             fprintf(stderr, "count = %d; should be MPI_UNDEFINED (%d)\n", count, MPI_UNDEFINED);
216         }
217     }
218
219     err = MPI_Get_elements(&status, mytype, &elements);
220     if (err != MPI_SUCCESS) {
221         errs++;
222         if (verbose) {
223             fprintf(stderr, "MPI_Get_elements returned error\n");
224         }
225     }
226     if (elements != 6) {
227         errs++;
228         if (verbose) {
229             fprintf(stderr, "elements = %d; should be 6\n", elements);
230         }
231     }
232
233     MPI_Type_free(&mytype);
234
235     return errs;
236 }
237
238
239 int parse_args(int argc, char **argv)
240 {
241     /*
242      * int ret;
243      *
244      * while ((ret = getopt(argc, argv, "v")) >= 0)
245      * {
246      * switch (ret) {
247      * case 'v':
248      * verbose = 1;
249      * break;
250      * }
251      * }
252      */
253     if (argc > 1 && strcmp(argv[1], "-v") == 0)
254         verbose = 1;
255     return 0;
256 }