Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixing introduction.doc links
[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]),
68              1, threeslice,
69              buffer,
70              bufsize,
71              &position,
72              MPI_COMM_WORLD);
73
74     /* Unpack the buffer into e. */
75     position = 0;
76     MPI_Unpack(buffer,
77                bufsize,
78                &position,
79                e, 9*9*9,
80                MPI_INT,
81                MPI_COMM_WORLD);
82         
83     /* Display errors, if any. */
84     for (i = 0; i < 9; i++) {
85         for (j = 0; j < 9; j++) {
86             for (k = 0; k < 9; k++) {
87                /* The truncation in integer division makes this safe. */
88                 if (e[i][j][k] != a[i][j+2][k*2+1]) {
89                     errs++;
90                     if (verbose) {
91                         printf("Error in location %d x %d x %d: %d, should be %d.\n",
92                                i, j, k, e[i][j][k], a[i][j+2][k*2+1]);
93                     }
94                 }
95             }
96         }
97     } 
98   
99     /* Release memory. */
100     free(buffer);
101
102     if (errs) {
103         fprintf(stderr, "Found %d errors\n", errs);
104     }
105     else {
106         printf(" No Errors\n");
107     }
108
109     MPI_Type_free(&oneslice);
110     MPI_Type_free(&twoslice);
111     MPI_Type_free(&threeslice);
112
113     MPI_Finalize();
114     return 0;
115 }
116
117 /* parse_args()
118  */
119 static int parse_args(int argc, char **argv)
120 {
121     /*
122     int ret;
123
124     while ((ret = getopt(argc, argv, "v")) >= 0)
125     {
126         switch (ret) {
127             case 'v':
128                 verbose = 1;
129                 break;
130         }
131     }
132     */
133     if (argc > 1 && strcmp(argv[1], "-v") == 0)
134         verbose = 1;
135     return 0;
136 }