Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / scatter3.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 contiguous data and receives a vector on some nodes
14    and contiguous data on others.  There is some evidence that some
15    MPI implementations do not check recvcount on the root process; this
16    test checks for that case
17 */
18
19 int main(int argc, char **argv)
20 {
21     MPI_Datatype vec;
22     double *vecin, *vecout, ivalue;
23     int root, i, n, stride, errs = 0;
24     int rank, size;
25     MPI_Aint vextent;
26
27     MTest_Init(&argc, &argv);
28
29     MPI_Comm_size(MPI_COMM_WORLD, &size);
30     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
31
32     n = 12;
33     stride = 10;
34     /* Note that vecout really needs to be only (n-1)*stride+1 doubles, but
35      * this is easier and allows a little extra room if there is a bug */
36     vecout = (double *) malloc(n * stride * sizeof(double));
37     vecin = (double *) malloc(n * size * sizeof(double));
38
39     MPI_Type_vector(n, 1, stride, MPI_DOUBLE, &vec);
40     MPI_Type_commit(&vec);
41     MPI_Type_extent(vec, &vextent);
42     if (vextent != ((n - 1) * (MPI_Aint) stride + 1) * sizeof(double)) {
43         errs++;
44         printf("Vector extent is %ld, should be %ld\n",
45                (long) vextent, (long) (((n - 1) * stride + 1) * sizeof(double)));
46     }
47     /* Note that the exted of type vector is from the first to the
48      * last element, not n*stride.
49      * E.g., with n=1, the extent is a single double */
50
51     for (i = 0; i < n * size; i++)
52         vecin[i] = (double) i;
53     for (root = 0; root < size; root++) {
54         for (i = 0; i < n * stride; i++)
55             vecout[i] = -1.0;
56         if (rank == root) {
57             /* Receive into a vector */
58             MPI_Scatter(vecin, n, MPI_DOUBLE, vecout, 1, vec, root, MPI_COMM_WORLD);
59             for (i = 0; i < n; i++) {
60                 ivalue = n * root + i;
61                 if (vecout[i * stride] != ivalue) {
62                     errs++;
63                     printf("[%d] Expected %f but found %f for vecout[%d] on root\n",
64                            rank, ivalue, vecout[i * stride], i * stride);
65                 }
66             }
67         }
68         else {
69             /* Receive into contiguous data */
70             MPI_Scatter(NULL, -1, MPI_DATATYPE_NULL, vecout, n, MPI_DOUBLE, root, MPI_COMM_WORLD);
71             for (i = 0; i < n; i++) {
72                 ivalue = rank * n + i;
73                 if (vecout[i] != ivalue) {
74                     printf("[%d] Expected %f but found %f for vecout[%d]\n",
75                            rank, ivalue, vecout[i], i);
76                     errs++;
77                 }
78             }
79         }
80     }
81
82     free(vecin);
83     free(vecout);
84     MTest_Finalize(errs);
85     MPI_Type_free(&vec);
86     MPI_Finalize();
87     return 0;
88 }