Logo AND Algorithmique Numérique Distribuée

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