Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Please scan-build.
[simgrid.git] / teshsuite / smpi / coll-allreduce / coll-allreduce.c
1 /* Copyright (c) 2009-2010, 2013-2018. 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 <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include "mpi.h"
12
13 int main(int argc, char *argv[])
14 {
15   int rank;
16   int size;
17   int i;
18   int status;
19   int mult=1;
20
21   MPI_Init(&argc, &argv);
22   int maxlen = argc >= 2 ? atoi(argv[1]) : 1;
23
24   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25   MPI_Comm_size(MPI_COMM_WORLD, &size);
26   if (maxlen > 1)
27     mult = maxlen > size ? size : maxlen;
28   int* sb = xbt_new0(int, size * maxlen);
29   int* rb = xbt_new0(int, size * maxlen);
30
31   for (i = 0; i < size *maxlen; ++i) {
32     sb[i] = rank*size + i;
33     rb[i] = 0;
34   }
35
36   printf("[%d] sndbuf=[", rank);
37   for (i = 0; i < size *mult; i++)
38     printf("%d ", sb[i]);
39   printf("]\n");
40   status = MPI_Allreduce(sb, rb, size *maxlen, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
41
42   printf("[%d] rcvbuf=[", rank);
43   for (i =  0; i < size *mult; i++)//do not print everything
44     printf("%d ", rb[i]);
45   printf("]\n");
46
47   if (rank == 0 && status != MPI_SUCCESS) {
48     printf("all_to_all returned %d\n", status);
49     fflush(stdout);
50   }
51   xbt_free(sb);
52   xbt_free(rb);
53   MPI_Finalize();
54   return (EXIT_SUCCESS);
55 }