Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / scatter2.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2003 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7
8 #include "mpi.h"
9 #include "mpitest.h"
10 #include <stdlib.h>
11 #include <stdio.h>
12
13 /* This example sends a vector and receives individual elements, but the
14    root process does not receive any data */
15
16 int main(int argc, char **argv)
17 {
18     MPI_Datatype vec;
19     double *vecin, *vecout, ivalue;
20     int root, i, n, stride, err = 0;
21     int rank, size;
22     MPI_Aint vextent;
23
24     MTest_Init(&argc, &argv);
25
26     MPI_Comm_size(MPI_COMM_WORLD, &size);
27     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
28
29     n = 12;
30     stride = 10;
31     vecin = (double *) malloc(n * stride * size * sizeof(double));
32     vecout = (double *) malloc(n * sizeof(double));
33
34     MPI_Type_vector(n, 1, stride, MPI_DOUBLE, &vec);
35     MPI_Type_commit(&vec);
36     MPI_Type_extent(vec, &vextent);
37     if (vextent != ((n - 1) * (MPI_Aint) stride + 1) * sizeof(double)) {
38         err++;
39         printf("Vector extent is %ld, should be %ld\n",
40                (long) vextent, (long) (((n - 1) * stride + 1) * sizeof(double)));
41     }
42     /* Note that the exted of type vector is from the first to the
43      * last element, not n*stride.
44      * E.g., with n=1, the extent is a single double */
45
46     for (i = 0; i < n * stride * size; i++)
47         vecin[i] = (double) i;
48     for (root = 0; root < size; root++) {
49         for (i = 0; i < n; i++)
50             vecout[i] = -1.0;
51         if (rank == root) {
52             MPI_Scatter(vecin, 1, vec, MPI_IN_PLACE, -1, MPI_DATATYPE_NULL, root, MPI_COMM_WORLD);
53         }
54         else {
55             MPI_Scatter(NULL, -1, MPI_DATATYPE_NULL, vecout, n, MPI_DOUBLE, root, MPI_COMM_WORLD);
56             ivalue = rank * ((n - 1) * stride + 1);
57             for (i = 0; i < n; i++) {
58                 if (vecout[i] != ivalue) {
59                     printf("[%d] Expected %f but found %f for vecout[%d]\n",
60                            rank, ivalue, vecout[i], i);
61                     err++;
62                 }
63                 ivalue += stride;
64             }
65         }
66     }
67
68     free(vecin);
69     free(vecout);
70     MTest_Finalize(err);
71     MPI_Type_free(&vec);
72     MPI_Finalize();
73     return 0;
74 }