Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
play with mpich3 cmake files
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / get-elements-pairtype.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 "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10
11 static int verbose = 0;
12
13 /* tests */
14 int double_int_test(void);
15
16 /* helper functions */
17 int parse_args(int argc, char **argv);
18
19 int main(int argc, char **argv)
20 {
21     int err, errs = 0;
22
23     MPI_Init(&argc, &argv); /* MPI-1.2 doesn't allow for MPI_Init(0,0) */
24     parse_args(argc, argv);
25
26     /* To improve reporting of problems about operations, we
27        change the error handler to errors return */
28     MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
29
30     /* perform some tests */
31     err = double_int_test();
32     if (err && verbose) fprintf(stderr, "%d errors in double_int test.\n",
33                                 err);
34     errs += err;
35
36     /* print message and exit */
37     if (errs) {
38         fprintf(stderr, "Found %d errors\n", errs);
39     }
40     else {
41         printf(" No Errors\n");
42     }
43     MPI_Finalize();
44     return 0;
45 }
46
47 /* send a { double, int, double} tuple and receive as a pair of
48  * MPI_DOUBLE_INTs. this should (a) be valid, and (b) result in an
49  * element count of 3.
50  */
51 int double_int_test(void)
52 {
53     int err, errs = 0, count;
54
55     struct { double a; int b; double c; } foo;
56     struct { double a; int b; double c; int d; } bar;
57
58     int blks[3] = { 1, 1, 1 };
59     MPI_Aint disps[3] = { 0, 0, 0 };
60     MPI_Datatype types[3] = { MPI_DOUBLE, MPI_INT, MPI_DOUBLE };
61     MPI_Datatype stype;
62
63     MPI_Status recvstatus;
64
65     /* fill in disps[1..2] with appropriate offset */
66     disps[1] = (MPI_Aint) ((char *) &foo.b - (char *) &foo.a);
67     disps[2] = (MPI_Aint) ((char *) &foo.c - (char *) &foo.a);
68    
69     MPI_Type_create_struct(3, blks, disps, types, &stype);
70     MPI_Type_commit(&stype);
71
72     err = MPI_Sendrecv((const void *) &foo, 1, stype, 0, 0,
73                        (void *) &bar, 2, MPI_DOUBLE_INT, 0, 0,
74                        MPI_COMM_SELF, &recvstatus);
75     if (err != MPI_SUCCESS) {
76         errs++;
77         if (verbose) fprintf(stderr, "MPI_Sendrecv returned error (%d)\n",
78                              err);
79         return errs;
80     }
81
82     err = MPI_Get_elements(&recvstatus, MPI_DOUBLE_INT, &count);
83     if (err != MPI_SUCCESS) {
84         errs++;
85         if (verbose) fprintf(stderr, "MPI_Get_elements returned error (%d)\n",
86                              err);
87     }
88
89     if (count != 3) {
90         errs++;
91         if (verbose) fprintf(stderr,
92                              "MPI_Get_elements returned count of %d, should be 3\n",
93                              count);
94     }
95
96     MPI_Type_free( &stype );
97
98     return errs;
99 }
100
101 int parse_args(int argc, char **argv)
102 {
103     if (argc > 1 && strcmp(argv[1], "-v") == 0)
104         verbose = 1;
105     return 0;
106 }