Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Include directory is in source_dir, not in binary_dir.
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / struct-empty-el.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 single_struct_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 = single_struct_test();
35     if (verbose && err) fprintf(stderr, "error in single_struct_test\n");
36     errs += err;
37
38     /* print message and exit */
39     if (errs) {
40         fprintf(stderr, "Found %d errors\n", errs);
41     }
42     else {
43         printf(" No Errors\n");
44     }
45     MPI_Finalize();
46     return 0;
47 }
48
49 int single_struct_test(void)
50 {
51     int err, errs = 0;
52     int count, elements;
53     int sendbuf[6] = { 1, 2, 3, 4, 5, 6 };
54     struct test_struct_1 ts1[2];
55     MPI_Datatype mystruct;
56     MPI_Request request;
57     MPI_Status status;
58
59     /* note: first element of struct has zero blklen and should be dropped */
60     MPI_Aint disps[3]     = { 2*sizeof(float), 0,       2*sizeof(int) };
61     int blks[3]           = { 0,               1,       2 };
62     MPI_Datatype types[3] = { MPI_FLOAT,       MPI_INT, MPI_INT };
63
64     ts1[0].a = -1;
65     ts1[0].b = -1;
66     ts1[0].c = -1;
67     ts1[0].d = -1;
68
69     ts1[1].a = -1;
70     ts1[1].b = -1;
71     ts1[1].c = -1;
72     ts1[1].d = -1;
73
74     err = MPI_Type_struct(3, blks, disps, types, &mystruct);
75     if (err != MPI_SUCCESS) {
76         errs++;
77         if (verbose) {
78             fprintf(stderr, "MPI_Type_struct returned error\n");
79         }
80     }
81
82     MPI_Type_commit(&mystruct);
83
84     err = MPI_Irecv(ts1, 2, mystruct, 0, 0, MPI_COMM_SELF, &request);
85     if (err != MPI_SUCCESS) {
86         errs++;
87         if (verbose) {
88             fprintf(stderr, "MPI_Irecv returned error\n");
89         }
90     }
91
92     err = MPI_Send(sendbuf, 6, MPI_INT, 0, 0, MPI_COMM_SELF);
93     if (err != MPI_SUCCESS) {
94         errs++;
95         if (verbose) {
96             fprintf(stderr, "MPI_Send returned error\n");
97         }
98     }
99
100     err = MPI_Wait(&request, &status);
101     if (err != MPI_SUCCESS) {
102         errs++;
103         if (verbose) {
104             fprintf(stderr, "MPI_Wait returned error\n");
105         }
106     }
107
108     /* verify data */
109     if (ts1[0].a != 1) {
110         errs++;
111         if (verbose) {
112             fprintf(stderr, "ts1[0].a = %d; should be %d\n", ts1[0].a, 1);
113         }
114     }
115     if (ts1[0].b != -1) {
116         errs++;
117         if (verbose) {
118             fprintf(stderr, "ts1[0].b = %d; should be %d\n", ts1[0].b, -1);
119         }
120     }
121     if (ts1[0].c != 2) {
122         errs++;
123         if (verbose) {
124             fprintf(stderr, "ts1[0].c = %d; should be %d\n", ts1[0].c, 2);
125         }
126     }
127     if (ts1[0].d != 3) {
128         errs++;
129         if (verbose) {
130             fprintf(stderr, "ts1[0].d = %d; should be %d\n", ts1[0].d, 3);
131         }
132     }
133     if (ts1[1].a != 4) {
134         errs++;
135         if (verbose) {
136             fprintf(stderr, "ts1[1].a = %d; should be %d\n", ts1[1].a, 4);
137         }
138     }
139     if (ts1[1].b != -1) {
140         errs++;
141         if (verbose) {
142             fprintf(stderr, "ts1[1].b = %d; should be %d\n", ts1[1].b, -1);
143         }
144     }
145     if (ts1[1].c != 5) {
146         errs++;
147         if (verbose) {
148             fprintf(stderr, "ts1[1].c = %d; should be %d\n", ts1[1].c, 5);
149         }
150     }
151     if (ts1[1].d != 6) {
152         errs++;
153         if (verbose) {
154             fprintf(stderr, "ts1[1].d = %d; should be %d\n", ts1[1].d, 6);
155         }
156     }
157
158     /* verify count and elements */
159     err = MPI_Get_count(&status, mystruct, &count);
160     if (err != MPI_SUCCESS) {
161         errs++;
162         if (verbose) {
163             fprintf(stderr, "MPI_Get_count returned error\n");
164         }
165     }
166     if (count != 2) {
167         errs++;
168         if (verbose) {
169             fprintf(stderr, "count = %d; should be 2\n", count);
170         }
171     }
172
173     err = MPI_Get_elements(&status, mystruct, &elements);
174     if (err != MPI_SUCCESS) {
175         errs++;
176         if (verbose) {
177             fprintf(stderr, "MPI_Get_elements returned error\n");
178         }
179     }
180     if (elements != 6) {
181         errs++;
182         if (verbose) {
183             fprintf(stderr, "elements = %d; should be 6\n", elements);
184         }
185     }
186
187     MPI_Type_free(&mystruct);
188
189     return errs;
190 }
191
192
193 int parse_args(int argc, char **argv)
194 {
195     /*
196     int ret;
197
198     while ((ret = getopt(argc, argv, "v")) >= 0)
199     {
200         switch (ret) {
201             case 'v':
202                 verbose = 1;
203                 break;
204         }
205     }
206     */
207     if (argc > 1 && strcmp(argv[1], "-v") == 0)
208         verbose = 1;
209     return 0;
210 }