Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9b63466624d6f024b0339a85212b1189e9af68cf
[simgrid.git] / teshsuite / smpi / coll-reduce / coll-reduce.c
1 /* Copyright (c) 2009-2010, 2013-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 <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <errno.h>
11 #include "mpi.h"
12
13 #ifndef EXIT_SUCCESS
14 #define EXIT_SUCCESS 0
15 #define EXIT_FAILURE 1
16 #endif
17
18 int main(int argc, char *argv[])
19 {
20   int rank, size;
21   int i;
22   int *sb;
23   int *rb;
24   int status;
25
26   MPI_Init(&argc, &argv);
27   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
28   MPI_Comm_size(MPI_COMM_WORLD, &size);
29
30   sb = (int *) xbt_malloc(size * sizeof(int));
31   rb = (int *) xbt_malloc(size * sizeof(int));
32
33   for (i = 0; i < size; ++i) {
34     sb[i] = rank*size + i;
35     rb[i] = 0;
36   }
37   printf("[%d] sndbuf=[", rank);
38   for (i = 0; i < size; i++)
39     printf("%d ", sb[i]);
40   printf("]\n");
41
42   int root=0;
43   status = MPI_Reduce(sb, rb, size, MPI_INT, MPI_SUM, root, MPI_COMM_WORLD);
44   MPI_Barrier(MPI_COMM_WORLD);
45
46   if (rank == root) {
47     printf("[%d] rcvbuf=[", rank);
48     for (i = 0; i < size; i++)
49       printf("%d ", rb[i]);
50     printf("]\n");
51     if (status != MPI_SUCCESS) {
52       printf("all_to_all returned %d\n", status);
53       fflush(stdout);
54     }
55   }
56
57   printf("[%d] second sndbuf=[", rank);
58   for (i = 0; i < 1; i++)
59     printf("%d ", sb[i]);
60   printf("]\n");
61
62   root=size-1;
63   status = MPI_Reduce(sb, rb, 1, MPI_INT, MPI_PROD, root, MPI_COMM_WORLD);
64   MPI_Barrier(MPI_COMM_WORLD);
65
66   if (rank == root) {
67     printf("[%d] rcvbuf=[", rank);
68     for (i = 0; i < 1; i++)
69       printf("%d ", rb[i]);
70     printf("]\n");
71     if (status != MPI_SUCCESS) {
72       printf("all_to_all returned %d\n", status);
73       fflush(stdout);
74     }
75   }
76   free(sb);
77   free(rb);
78   MPI_Finalize();
79   return (EXIT_SUCCESS);
80 }