Logo AND Algorithmique Numérique Distribuée

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