Logo AND Algorithmique Numérique Distribuée

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