Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0097c351d8160f5d5f53c7b3c137fc559ff1e8d3
[simgrid.git] / teshsuite / smpi / coll-gather / coll-gather.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 status;
18
19   int root = 0;
20
21   MPI_Init(&argc, &argv);
22   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
23   MPI_Comm_size(MPI_COMM_WORLD, &size);
24
25   int count = 2;
26   int* sb = (int *) xbt_malloc(count * sizeof(int));
27   int* rb = (int *) xbt_malloc(count * size * sizeof(int));
28
29   for (int i = 0; i < count; ++i)
30     sb[i] = rank * count + i;
31   for (int i = 0; i < count * size; ++i)
32     rb[i] = 0;
33
34   printf("[%d] sndbuf=[", rank);
35   for (int i = 0; i < count; i++)
36     printf("%d ", sb[i]);
37   printf("]\n");
38
39   status = MPI_Gather(sb, count, MPI_INT, rb, count, MPI_INT, root, MPI_COMM_WORLD);
40
41   if (rank == root) {
42     printf("[%d] rcvbuf=[", rank);
43     for (int i = 0; i < count * size; i++)
44       printf("%d ", rb[i]);
45     printf("]\n");
46
47     if (status != MPI_SUCCESS) {
48       printf("allgather returned %d\n", status);
49       fflush(stdout);
50     }
51   }
52   xbt_free(sb);
53   xbt_free(rb);
54   MPI_Barrier(MPI_COMM_WORLD);
55   MPI_Finalize();
56   return (EXIT_SUCCESS);
57 }