Logo AND Algorithmique Numérique Distribuée

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