Logo AND Algorithmique Numérique Distribuée

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