Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / teshsuite / smpi / coll-reduce / coll-reduce.c
1 /* Copyright (c) 2009-2019. 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
20   MPI_Init(&argc, &argv);
21   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
22   MPI_Comm_size(MPI_COMM_WORLD, &size);
23
24   unsigned long long* sb = (unsigned long long *) xbt_malloc(size * sizeof(unsigned long long));
25   unsigned long long* rb = (unsigned long long *) xbt_malloc(size * sizeof(unsigned long long));
26
27   for (i = 0; i < size; ++i) {
28     sb[i] = rank*size + i;
29     rb[i] = 0;
30   }
31   printf("[%d] sndbuf=[", rank);
32   for (i = 0; i < size; i++)
33     printf("%llu ", sb[i]);
34   printf("]\n");
35
36   int root=0;
37   status = MPI_Reduce(sb, rb, size, MPI_UNSIGNED_LONG_LONG, MPI_SUM, root, MPI_COMM_WORLD);
38   MPI_Barrier(MPI_COMM_WORLD);
39
40   if (rank == root) {
41     printf("[%d] rcvbuf=[", rank);
42     for (i = 0; i < size; i++)
43       printf("%llu ", rb[i]);
44     printf("]\n");
45     if (status != MPI_SUCCESS) {
46       printf("all_to_all returned %d\n", status);
47       fflush(stdout);
48     }
49   }
50
51   printf("[%d] second sndbuf=[", rank);
52   for (i = 0; i < 1; i++)
53     printf("%llu ", sb[i]);
54   printf("]\n");
55
56   root=size-1;
57   status = MPI_Reduce(sb, rb, 1, MPI_UNSIGNED_LONG_LONG, MPI_PROD, root, MPI_COMM_WORLD);
58   MPI_Barrier(MPI_COMM_WORLD);
59
60   if (rank == root) {
61     printf("[%d] rcvbuf=[", rank);
62     for (i = 0; i < 1; i++)
63       printf("%llu ", rb[i]);
64     printf("]\n");
65     if (status != MPI_SUCCESS) {
66       printf("all_to_all returned %d\n", status);
67       fflush(stdout);
68     }
69   }
70   free(sb);
71   free(rb);
72   MPI_Finalize();
73   return (EXIT_SUCCESS);
74 }