Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix build and dist, add missing folder
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / triangular-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 <math.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include "mpi.h"
11 #include "mpitest.h"
12
13 int main(int argc, char *argv[]);
14
15 /* helper functions */
16 int parse_args(int argc, char **argv);
17
18 static int verbose = 0;
19
20 int main(int argc, char *argv[])
21 {
22     /* Variable declarations */
23     int a[100][100], b[100][100];
24     int disp[100], block[100];
25     MPI_Datatype ltype;
26         
27     int bufsize, position = 0;
28     void *buffer;
29         
30     int i, j, errs = 0;
31         
32     /* Initialize a to some known values and zero out b. */
33     for(i = 0; i < 100; i++) {
34         for(j = 0; j < 100; j++) {
35             a[i][j] = 1000*i + j;
36             b[i][j] = 0;
37         }
38     }
39         
40     /* Initialize MPI */
41     MTest_Init( &argc, &argv );
42   
43     parse_args(argc, argv);
44
45     for(i = 0; i < 100; i++) {
46         /* Fortran version has disp(i) = 100*(i-1) + i and block(i) = 100-i. */
47         /* This code here is wrong. It compacts everything together,
48          * which isn't what we want.
49          * What we want is to put the lower triangular values into b and leave
50          * the rest of it unchanged, right?
51          */
52         block[i] = i+1;
53         disp[i] = 100*i;
54     }
55         
56     /* Create datatype for lower triangular part. */
57     MPI_Type_indexed(100, block, disp, MPI_INT, &ltype);
58     MPI_Type_commit(&ltype);
59         
60     /* Pack it. */
61     MPI_Pack_size(1, ltype, MPI_COMM_WORLD, &bufsize);
62     buffer = (void *) malloc((unsigned) bufsize);
63     MPI_Pack( a, 1, ltype, buffer, bufsize, &position, MPI_COMM_WORLD );
64         
65     /* Unpack the buffer into b. */
66     position = 0;
67     MPI_Unpack(buffer, bufsize, &position, b, 1, ltype, MPI_COMM_WORLD);
68         
69     for(i = 0; i < 100; i++) {
70         for(j = 0; j < 100; j++) {
71             if (j > i && b[i][j] != 0) {
72                 errs++;
73                 if (verbose) fprintf(stderr, "b[%d][%d] = %d; should be %d\n",
74                                      i, j, b[i][j], 0);
75             }
76             else if (j <= i && b[i][j] != 1000*i + j) {
77                 errs++;
78                 if (verbose) fprintf(stderr, "b[%d][%d] = %d; should be %d\n",
79                                      i, j, b[i][j], 1000*i + j);
80             }
81         }
82     }
83
84     MTest_Finalize( errs );
85     MPI_Finalize();
86     return 0;
87 }
88
89 int parse_args(int argc, char **argv)
90 {
91     int ret;
92
93     while ((ret = getopt(argc, argv, "v")) >= 0)
94     {
95         switch (ret) {
96             case 'v':
97                 verbose = 1;
98                 break;
99         }
100     }
101     return 0;
102 }