Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
A few spelling mistakes and many replacements: [Ss]imgrid -> SimGrid.
[simgrid.git] / src / smpi / colls / smpi_default_selector.cpp
1 /* selector with default/naive SimGrid algorithms. These should not be trusted for performance evaluations */
2
3 /* Copyright (c) 2009-2023. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "colls_private.hpp"
9 #include "src/smpi/include/smpi_actor.hpp"
10
11 namespace simgrid::smpi {
12
13 int bcast__default(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
14 {
15   return bcast__binomial_tree(buf, count, datatype, root, comm);
16 }
17
18 int barrier__default(MPI_Comm comm)
19 {
20   return barrier__ompi_basic_linear(comm);
21 }
22
23
24 int gather__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
25                      void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
26 {
27   MPI_Request request;
28   colls::igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request, 0);
29   return Request::wait(&request, MPI_STATUS_IGNORE);
30 }
31
32 int reduce_scatter__default(const void *sendbuf, void *recvbuf, const int *recvcounts, MPI_Datatype datatype, MPI_Op op,
33                             MPI_Comm comm)
34 {
35   int rank = comm->rank();
36
37   /* arbitrarily choose root as rank 0 */
38   int size = comm->size();
39   int count = 0;
40   int* displs = new int[size];
41   int regular=1;
42   for (int i = 0; i < size; i++) {
43     if(recvcounts[i]!=recvcounts[0]){
44       regular=0;
45       break;
46     }
47     displs[i] = count;
48     count += recvcounts[i];
49   }
50   if(not regular){
51     delete[] displs;
52     return reduce_scatter__mpich(sendbuf, recvbuf, recvcounts, datatype, op, comm);
53   }
54
55   unsigned char* tmpbuf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
56
57   int ret = reduce__default(sendbuf, tmpbuf, count, datatype, op, 0, comm);
58   if(ret==MPI_SUCCESS)
59     ret = colls::scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
60   delete[] displs;
61   smpi_free_tmp_buffer(tmpbuf);
62   return ret;
63 }
64
65
66 int allgather__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
67                        void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
68 {
69   MPI_Request request;
70   colls::iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, &request);
71   return Request::wait(&request, MPI_STATUS_IGNORE);
72 }
73
74 int allgatherv__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
75                         const int *recvcounts, const int *displs, MPI_Datatype recvtype, MPI_Comm comm)
76 {
77   MPI_Request request;
78   colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, &request, 0);
79   auto requests = request->get_nbc_requests();
80   Request::waitall(requests.size(), requests.data(), MPI_STATUS_IGNORE);
81   for(auto& req: requests)
82     Request::unref(&req);
83   Request::unref(&request);
84   return MPI_SUCCESS;
85 }
86
87 int scatter__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
88                      void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
89 {
90   MPI_Request request;
91   colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request, 0);
92   return Request::wait(&request, MPI_STATUS_IGNORE);
93 }
94
95 int reduce__default(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
96                     MPI_Comm comm)
97 {
98   //non commutative case, use a working algo from openmpi
99   if (op != MPI_OP_NULL && (datatype->flags() & DT_FLAG_DERIVED || not op->is_commutative())) {
100     return reduce__ompi_basic_linear(sendbuf, recvbuf, count, datatype, op, root, comm);
101   }
102   MPI_Request request;
103   colls::ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, &request, 0);
104   return Request::wait(&request, MPI_STATUS_IGNORE);
105 }
106
107 int allreduce__default(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
108 {
109   //FIXME: have mpi_ireduce and iallreduce handle derived datatypes correctly
110   if(datatype->flags() & DT_FLAG_DERIVED)
111     return allreduce__ompi(sendbuf, recvbuf, count, datatype, op, comm);
112   int ret;
113   ret = reduce__default(sendbuf, recvbuf, count, datatype, op, 0, comm);
114   if(ret==MPI_SUCCESS)
115     ret = bcast__default(recvbuf, count, datatype, 0, comm);
116   return ret;
117 }
118
119 int alltoall__default(const void *sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
120 {
121   return alltoall__ompi(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
122 }
123
124 int alltoallv__default(const void *sendbuf, const int *sendcounts, const int *senddisps, MPI_Datatype sendtype,
125                        void *recvbuf, const int *recvcounts, const int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
126 {
127   MPI_Request request;
128   colls::ialltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm, &request,
129                     0);
130   return Request::wait(&request, MPI_STATUS_IGNORE);
131 }
132
133 } // namespace simgrid::smpi