Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[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 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     MPI_Pack(a, 1, xpose, buffer, bufsize, &position, MPI_COMM_WORLD);
62
63     /* Unpack the buffer into b. */
64     position = 0;
65     MPI_Unpack(buffer, bufsize, &position, b, 100 * 100, MPI_INT, MPI_COMM_WORLD);
66
67     for (i = 0; i < 100; i++) {
68         for (j = 0; j < 100; j++) {
69             if (b[i][j] != a[j][i]) {
70                 errs++;
71                 if (verbose)
72                     fprintf(stderr, "b[%d][%d] = %d, should be %d\n", i, j, b[i][j], a[j][i]);
73             }
74         }
75     }
76
77     MPI_Type_free(&xpose);
78     MPI_Type_free(&row);
79
80     /* print message and exit */
81     if (errs) {
82         fprintf(stderr, "Found %d errors\n", errs);
83     }
84     else {
85         printf(" No Errors\n");
86     }
87     free(buffer);
88     MPI_Finalize();
89     return 0;
90 }
91
92
93 int parse_args(int argc, char **argv)
94 {
95     /*
96      * int ret;
97      *
98      * while ((ret = getopt(argc, argv, "v")) >= 0)
99      * {
100      * switch (ret) {
101      * case 'v':
102      * verbose = 1;
103      * break;
104      * }
105      * }
106      */
107     if (argc > 1 && strcmp(argv[1], "-v") == 0)
108         verbose = 1;
109     return 0;
110 }