Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7134a58247d590c9407fa0821ffa2d1e5675e7c7
[simgrid.git] / teshsuite / smpi / coll-allgather / coll-allgather.c
1 /* Copyright (c) 2009-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <errno.h>
10 #include "mpi.h"
11 #include <time.h>
12 #include "simgrid/actor.h"
13
14 int main(int argc, char *argv[])
15 {
16   int rank;
17   int size;
18   int status;
19
20   int randomTime = sg_actor_self_get_pid() % 5;
21   sleep(randomTime);
22
23   MPI_Init(&argc, &argv);
24   MPI_Comm_rank(MPI_COMM_WORLD, &rank);
25   MPI_Comm_size(MPI_COMM_WORLD, &size);
26   MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
27
28   int count = 2;
29   int* sb = (int *) xbt_malloc(count * sizeof(int));
30   int* rb = (int *) xbt_malloc(count * size * sizeof(int));
31
32   for (int i = 0; i < count; ++i)
33     sb[i] = rank * count + i;
34   for (int i = 0; i < count * size; ++i)
35     rb[i] = 0;
36
37   status = MPI_Allgather(NULL, count, MPI_INT, rb, count, MPI_INT, MPI_COMM_WORLD);
38   if(status!=MPI_ERR_BUFFER)
39     printf("MPI_Allgather did not return MPI_ERR_BUFFER for empty sendbuf\n");
40   status = MPI_Allgather(sb, -1, MPI_INT, rb, count, MPI_INT, MPI_COMM_WORLD);
41   if(status!=MPI_ERR_COUNT)
42     printf("MPI_Allgather did not return MPI_ERR_COUNT for -1 sendcount\n");
43   status = MPI_Allgather(sb, count, MPI_DATATYPE_NULL, rb, count, MPI_INT, MPI_COMM_WORLD);
44   if(status!=MPI_ERR_TYPE)
45     printf("MPI_Allgather did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL sendtype\n");
46   status = MPI_Allgather(sb, count, MPI_INT, NULL, count, MPI_INT, MPI_COMM_WORLD);
47   if(status!=MPI_ERR_BUFFER)
48     printf("MPI_Allgather did not return MPI_ERR_BUFFER for empty recvbuf\n");
49   status = MPI_Allgather(sb, count, MPI_INT, rb, -1, MPI_INT, MPI_COMM_WORLD);
50   if(status!=MPI_ERR_COUNT)
51     printf("MPI_Allgather did not return MPI_ERR_COUNT for -1 recvcount\n");
52   status = MPI_Allgather(sb, count, MPI_INT, rb, count, MPI_DATATYPE_NULL, MPI_COMM_WORLD);
53   if(status!=MPI_ERR_TYPE)
54     printf("MPI_Allgather did not return MPI_ERR_TYPE for MPI_DATATYPE_NULL recvtype\n");
55   status = MPI_Allgather(sb, count, MPI_INT, rb, count, MPI_INT, MPI_COMM_NULL);
56   if(status!=MPI_ERR_COMM)
57     printf("MPI_Allgather did not return MPI_ERR_COMM for MPI_COMM_NULL comm\n");
58
59   printf("[%d] sndbuf=[", rank);
60   for (int i = 0; i < count; i++)
61     printf("%d ", sb[i]);
62   printf("]\n");
63
64   status = MPI_Allgather(sb, count, MPI_INT, rb, count, MPI_INT, MPI_COMM_WORLD);
65
66   printf("[%d] rcvbuf=[", rank);
67   for (int i = 0; i < count * size; i++)
68     printf("%d ", rb[i]);
69   printf("]\n");
70
71   if (rank == 0 && status != MPI_SUCCESS) {
72     printf("allgather returned %d\n", status);
73     fflush(stdout);
74   }
75   xbt_free(sb);
76   xbt_free(rb);
77   MPI_Finalize();
78   return EXIT_SUCCESS;
79 }