Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix dist
[simgrid.git] / teshsuite / smpi / mpich3-test / coll / scattern.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 <stdlib.h>
8 #include <stdio.h>
9
10 /* This example sends a vector and receives individual elements */
11
12 int main(int argc, char **argv)
13 {
14     MPI_Datatype vec;
15     double *vecin, *vecout, ivalue;
16     int root, i, n, stride, err = 0;
17     int rank, size;
18
19     MPI_Init(&argc, &argv);
20
21     MPI_Comm_size(MPI_COMM_WORLD, &size);
22     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23
24     n = 12;
25     stride = 10;
26     vecin = (double *) malloc(n * stride * size * sizeof(double));
27     vecout = (double *) malloc(n * sizeof(double));
28
29     MPI_Type_vector(n, 1, stride, MPI_DOUBLE, &vec);
30     MPI_Type_commit(&vec);
31
32     for (i = 0; i < n * stride * size; i++)
33         vecin[i] = (double) i;
34     for (root = 0; root < size; root++) {
35         for (i = 0; i < n; i++)
36             vecout[i] = -1.0;
37         MPI_Scatter(vecin, 1, vec, vecout, n, MPI_DOUBLE, root, MPI_COMM_WORLD);
38         ivalue = rank * ((n - 1) * stride + 1);
39         for (i = 0; i < n; i++) {
40             if (vecout[i] != ivalue) {
41                 printf("Expected %f but found %f\n", ivalue, vecout[i]);
42                 err++;
43             }
44             ivalue += stride;
45         }
46     }
47     i = err;
48     MPI_Allreduce(&i, &err, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
49     if (rank == 0) {
50         if (err > 0)
51             printf("Found %d errors!\n", err);
52         else
53             printf(" No Errors\n");
54     }
55     free(vecin);
56     free(vecout);
57     MPI_Type_free(&vec);
58     MPI_Finalize();
59     return 0;
60
61 }