Logo AND Algorithmique Numérique Distribuée

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