Logo AND Algorithmique Numérique Distribuée

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