Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid warnings about renamed options with --help-aliases.
[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;
13    int rank;
14    int success = 1;
15    int retval;
16    int sendcount = 1;            // one double to each process
17    int recvcount = 1;
18    int i;
19    double *sndbuf = NULL;
20    double rcvd;
21    int root = 0;                 // arbitrary choice
22
23    MPI_Init(&argc, &argv);
24    MPI_Comm_size(MPI_COMM_WORLD, &size);
25    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
26
27    // on root, initialize sendbuf
28    if (root == rank) {
29      sndbuf = malloc(size * sizeof(double));
30      for (i = 0; i < size; i++) {
31        sndbuf[i] = (double) i;
32      }
33    }
34
35    retval = MPI_Scatter(sndbuf, sendcount, MPI_DOUBLE, &rcvd, recvcount, MPI_DOUBLE, root, MPI_COMM_WORLD);
36    if (root == rank) {
37      free(sndbuf);
38    }
39    if (retval != MPI_SUCCESS) {
40      fprintf(stderr, "(%s:%d) MPI_Scatter() returned retval=%d\n", __FILE__, __LINE__, retval);
41      return 0;
42    }
43    // verification
44    if ((double) rank != rcvd) {
45      fprintf(stderr, "[%d] has %f instead of %d\n", rank, rcvd, rank);
46      success = 0;
47    }
48
49   /* test 1 */
50   if (0 == rank)
51     printf("** Small Test Result: ...\n");
52   if (!success)
53     printf("\t[%d] failed.\n", rank);
54   else
55     printf("\t[%d] ok.\n", rank);
56
57   MPI_Finalize();
58   return 0;
59 }