Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
simplify scatter and remove blanks
[simgrid.git] / teshsuite / smpi / coll-scatter / coll-scatter.c
1 /* Copyright (c) 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <mpi.h>
9
10 int main(int argc, char **argv)
11 {
12    int size, rank;
13    int success = 1;
14    int retval;
15    int sendcount = 1;            // one double to each process
16    int recvcount = 1;
17    int i;
18    double *sndbuf = NULL;
19    double rcvd;
20    int root = 0;                 // arbitrary choice
21
22    MPI_Init(&argc, &argv);
23    MPI_Comm_size(MPI_COMM_WORLD, &size);
24    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25
26    // on root, initialize sendbuf
27    if (root == rank) {
28      sndbuf = malloc(size * sizeof(double));
29      for (i = 0; i < size; i++) {
30        sndbuf[i] = (double) i;
31      }
32    }
33
34    retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, root, MPI_COMM_WORLD);
35    if (root == rank) {
36      free(sndbuf);
37    }
38    if (retval != MPI_SUCCESS) {
39      fprintf(stderr, "(%s:%d) MPI_Scatter() returned retval=%d\n", __FILE__, __LINE__, retval);
40      return 0;
41    }
42    // verification
43    if ((double) rank != rcvd) {
44      fprintf(stderr, "[%d] has %f instead of %d\n", rank, rcvd, rank);
45      success = 0;
46    }
47
48   /* test 1 */
49   if (0 == rank)
50     printf("** Small Test Result: ...\n");
51   if (!success)
52     printf("\t[%d] failed.\n", rank);
53   else
54     printf("\t[%d] ok.\n", rank);
55
56   MPI_Finalize();
57   return 0;
58 }