Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
functioning MPI_Comm_get_name, MPI_Comm_set_name
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / slice-pack.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 <math.h>
9 #include <stdlib.h>
10 #include "mpitest.h"
11 #include "mpitestconf.h"
12 #ifdef HAVE_STRING_H
13 #include <string.h>
14 #endif
15
16 static int verbose = 0;
17 int a[100][100][100], e[9][9][9];
18
19 int main(int argc, char *argv[]);
20
21 /* helper functions */
22 static int parse_args(int argc, char **argv);
23
24 int main(int argc, char *argv[])
25 {
26     /* Variable declarations */
27     MPI_Datatype oneslice, twoslice, threeslice;
28     int errs = 0;
29     MPI_Aint sizeofint;
30
31     int bufsize, position;
32     void *buffer;
33
34     int i, j, k;
35
36     /* Initialize a to some known values. */
37     for (i = 0; i < 100; i++) {
38         for (j = 0; j < 100; j++) {
39             for (k = 0; k < 100; k++) {
40                 a[i][j][k] = i * 1000000 + j * 1000 + k;
41             }
42         }
43     }
44
45     /* Initialize MPI */
46     MPI_Init(&argc, &argv);
47     MPI_Type_extent(MPI_INT, &sizeofint);
48
49     parse_args(argc, argv);
50
51     /* Create data types. */
52     /* NOTE: This differs from the way that it's done on the sheet. */
53     /* On the sheet, the slice is a[0, 2, 4, ..., 16][2-10][1-9]. */
54     /* Below, the slice is a[0-8][2-10][1, 3, 5, ..., 17]. */
55     MPI_Type_vector(9, 1, 2, MPI_INT, &oneslice);
56     MPI_Type_hvector(9, 1, 100 * sizeofint, oneslice, &twoslice);
57     MPI_Type_hvector(9, 1, 100 * 100 * sizeofint, twoslice, &threeslice);
58
59     MPI_Type_commit(&threeslice);
60
61     /* Pack it into a buffer. */
62     position = 0;
63     MPI_Pack_size(1, threeslice, MPI_COMM_WORLD, &bufsize);
64     buffer = (void *) malloc((unsigned) bufsize);
65
66     /* -1 to indices on sheet to compensate for Fortran --> C */
67     MPI_Pack(&(a[0][2][1]), 1, threeslice, buffer, bufsize, &position, MPI_COMM_WORLD);
68
69     /* Unpack the buffer into e. */
70     position = 0;
71     MPI_Unpack(buffer, bufsize, &position, e, 9 * 9 * 9, MPI_INT, MPI_COMM_WORLD);
72
73     /* Display errors, if any. */
74     for (i = 0; i < 9; i++) {
75         for (j = 0; j < 9; j++) {
76             for (k = 0; k < 9; k++) {
77                 /* The truncation in integer division makes this safe. */
78                 if (e[i][j][k] != a[i][j + 2][k * 2 + 1]) {
79                     errs++;
80                     if (verbose) {
81                         printf("Error in location %d x %d x %d: %d, should be %d.\n",
82                                i, j, k, e[i][j][k], a[i][j + 2][k * 2 + 1]);
83                     }
84                 }
85             }
86         }
87     }
88
89     /* Release memory. */
90     free(buffer);
91
92     if (errs) {
93         fprintf(stderr, "Found %d errors\n", errs);
94     }
95     else {
96         printf(" No Errors\n");
97     }
98
99     MPI_Type_free(&oneslice);
100     MPI_Type_free(&twoslice);
101     MPI_Type_free(&threeslice);
102
103     MPI_Finalize();
104     return 0;
105 }
106
107 /* parse_args()
108  */
109 static int parse_args(int argc, char **argv)
110 {
111     /*
112      * int ret;
113      *
114      * while ((ret = getopt(argc, argv, "v")) >= 0)
115      * {
116      * switch (ret) {
117      * case 'v':
118      * verbose = 1;
119      * break;
120      * }
121      * }
122      */
123     if (argc > 1 && strcmp(argv[1], "-v") == 0)
124         verbose = 1;
125     return 0;
126 }