Logo AND Algorithmique Numérique Distribuée

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