Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8f4c004f939dc4252f20c442c8498c67143459fd
[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     {
61         fprintf(stderr," Error on pack size! Got %d; expecting %d\n", (int) bufsize, 2916);
62     }
63     buffer = (void *) malloc((unsigned) bufsize);
64
65     /* -1 to indices on sheet to compensate for Fortran --> C */
66     MPI_Pack_external((char*)"external32",
67                       &(a[0][2][1]),
68                       1, threeslice,
69                       buffer,
70                       bufsize,
71                       &position);
72
73     /* Unpack the buffer into e. */
74     position = 0;
75     MPI_Unpack_external((char*)"external32",
76                         buffer,
77                         bufsize,
78                         &position,
79                         e, 9*9*9,
80                         MPI_INT);
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     int ret;
121
122     while ((ret = getopt(argc, argv, "v")) >= 0)
123     {
124         switch (ret) {
125             case 'v':
126                 verbose = 1;
127                 break;
128         }
129     }
130     return 0;
131 }