Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / coll-gather / coll-gather.c
1 /* Copyright (c) 2009-2023. 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   MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
25
26   int count = 2;
27   int* sb = (int *) xbt_malloc(count * sizeof(int));
28   int* rb = (int *) xbt_malloc(count * size * sizeof(int));
29
30   for (int i = 0; i < count; ++i)
31     sb[i] = rank * count + i;
32   for (int i = 0; i < count * size; ++i)
33     rb[i] = 0;
34
35   printf("[%d] sndbuf=[", rank);
36   for (int i = 0; i < count; i++)
37     printf("%d ", sb[i]);
38   printf("]\n");
39
40   status = MPI_Gather(NULL, count, MPI_INT, rb, count, MPI_INT, root, MPI_COMM_WORLD);
41   if(status!=MPI_ERR_BUFFER)
42     printf("MPI_Gather did not return MPI_ERR_BUFFER for empty sendbuf\n");
43   status = MPI_Gather(sb, -1, MPI_INT, rb, count, MPI_INT, root, MPI_COMM_WORLD);
44   if(status!=MPI_ERR_COUNT)
45     printf("MPI_Gather did not return MPI_ERR_COUNT for -1 sendcount\n");
46   status = MPI_Gather(sb, count, MPI_DATATYPE_NULL, rb, count, MPI_INT, root, MPI_COMM_WORLD);
47   if(status!=MPI_ERR_TYPE)
48     printf("MPI_Gather did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL sendtype\n");
49   status = MPI_Gather(sb, count, MPI_INT, rb, count, MPI_INT, -1, MPI_COMM_WORLD);
50   if(status!=MPI_ERR_ROOT)
51     printf("MPI_Gather did not return MPI_ERR_ROOT for root -1\n");
52   status = MPI_Gather(sb, count, MPI_INT, rb, count, MPI_INT, size+1, MPI_COMM_WORLD);
53   if(status!=MPI_ERR_ROOT)
54     printf("MPI_Gather did not return MPI_ERR_ROOT for root > size\n");
55   status = MPI_Gather(sb, count, MPI_INT, rb, count, MPI_INT, root, MPI_COMM_NULL);
56   if(status!=MPI_ERR_COMM)
57     printf("MPI_Gather did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
58
59   status = MPI_Gather(sb, count, MPI_INT, rb, count, MPI_INT, root, MPI_COMM_WORLD);
60
61   if (rank == root) {
62     printf("[%d] rcvbuf=[", rank);
63     for (int i = 0; i < count * size; i++)
64       printf("%d ", rb[i]);
65     printf("]\n");
66
67     if (status != MPI_SUCCESS) {
68       printf("gather returned %d\n", status);
69       fflush(stdout);
70     }
71   }
72   xbt_free(sb);
73   xbt_free(rb);
74   MPI_Barrier(MPI_COMM_WORLD);
75   MPI_Finalize();
76   return EXIT_SUCCESS;
77 }