Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1cd22028d5a99b533ae1904221872bf0fad6c5fa
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / transpose-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 <math.h>
8 #include <stdio.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
18 int main(int argc, char *argv[]);
19 int parse_args(int argc, char **argv);
20
21 int main(int argc, char *argv[])
22 {
23     /* Variable declarations */
24     int a[100][100], b[100][100];
25     MPI_Datatype row, xpose;
26     MPI_Aint sizeofint;
27         
28     int /* err, */ errs = 0;
29     int bufsize, position = 0;
30     void *buffer;
31   
32     int i, j;
33   
34     /* Initialize a to some known values. */
35     for(i = 0; i < 100; i++) {
36         for(j = 0; j < 100; j++) {
37             a[i][j] = i*1000+j;
38             b[i][j] = -1;
39         }
40     }
41   
42     /* Initialize MPI */
43     MPI_Init(&argc, &argv);
44     parse_args(argc, argv);
45
46     MPI_Type_extent(MPI_INT, &sizeofint);
47         
48     /* Create datatypes. */
49     MPI_Type_vector(100, 1, 100, MPI_INT, &row);
50     MPI_Type_hvector(100, 1, sizeofint, row, &xpose);
51     MPI_Type_commit(&xpose);
52         
53     /* Pack it. */
54     MPI_Pack_size(1, xpose, MPI_COMM_WORLD, &bufsize);
55     buffer = (char *) malloc((unsigned) bufsize);
56
57     /* To improve reporting of problems about operations, we
58        change the error handler to errors return */
59     MPI_Comm_set_errhandler( MPI_COMM_WORLD, MPI_ERRORS_RETURN );
60
61     /* err = */ MPI_Pack(a,
62                    1,
63                    xpose,
64                    buffer,
65                    bufsize,
66                    &position,
67                    MPI_COMM_WORLD);
68         
69     /* Unpack the buffer into b. */
70     position = 0;
71     /* err = */ MPI_Unpack(buffer,
72                      bufsize,
73                      &position,
74                      b,
75                      100*100,
76                      MPI_INT,
77                      MPI_COMM_WORLD);
78
79     for (i = 0; i < 100; i++) {
80         for (j = 0; j < 100; j++) {
81             if(b[i][j] != a[j][i]) {
82                 errs++;
83                 if (verbose) fprintf(stderr, "b[%d][%d] = %d, should be %d\n",
84                                      i, j, b[i][j], a[j][i]);
85             }
86         }
87     }
88
89     MPI_Type_free(&xpose);
90     MPI_Type_free(&row);
91     
92     /* print message and exit */
93     if (errs) {
94         fprintf(stderr, "Found %d errors\n", errs);
95     }
96     else {
97         printf(" No Errors\n");
98     }
99     MPI_Finalize();
100     return 0;
101 }
102
103
104 int parse_args(int argc, char **argv)
105 {
106     /*
107     int ret;
108
109     while ((ret = getopt(argc, argv, "v")) >= 0)
110     {
111         switch (ret) {
112             case 'v':
113                 verbose = 1;
114                 break;
115         }
116     }
117     */
118     if (argc > 1 && strcmp(argv[1], "-v") == 0)
119         verbose = 1;
120     return 0;
121 }