Logo AND Algorithmique Numérique Distribuée

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