Logo AND Algorithmique Numérique Distribuée

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