Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://framagit.org/simgrid/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-2019. 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{
12 namespace smpi{
13
14 int Coll_bcast_default::bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
15 {
16   return Coll_bcast_binomial_tree::bcast(buf, count, datatype, root, comm);
17 }
18
19 int Coll_barrier_default::barrier(MPI_Comm comm)
20 {
21   return Coll_barrier_ompi_basic_linear::barrier(comm);
22 }
23
24
25 int Coll_gather_default::gather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
26                      void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
27 {
28   MPI_Request request;
29   Colls::igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request);
30   MPI_Request* requests = request->get_nbc_requests();
31   int count = request->get_nbc_requests_size();
32   Request::waitall(count, requests, MPI_STATUS_IGNORE);
33   for (int i = 0; i < count; i++) {
34     if(requests[i]!=MPI_REQUEST_NULL)
35       Request::unref(&requests[i]);
36   }
37   delete[] requests;
38   Request::unref(&request);
39   return MPI_SUCCESS;
40 }
41
42 int Coll_reduce_scatter_default::reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
43                              MPI_Comm comm)
44 {
45   int rank = comm->rank();
46
47   /* arbitrarily choose root as rank 0 */
48   int size = comm->size();
49   int count = 0;
50   int *displs = xbt_new(int, size);
51   for (int i = 0; i < size; i++) {
52     displs[i] = count;
53     count += recvcounts[i];
54   }
55   void *tmpbuf = static_cast<void*>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
56
57   int ret = Coll_reduce_default::reduce(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   xbt_free(displs);
61   smpi_free_tmp_buffer(tmpbuf);
62   return ret;
63 }
64
65
66 int Coll_allgather_default::allgather(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   MPI_Request* requests = request->get_nbc_requests();
72   int count = request->get_nbc_requests_size();
73   Request::waitall(count, requests, MPI_STATUS_IGNORE);
74   for (int other = 0; other < count; other++) {
75     Request::unref(&requests[other]);
76   }
77   delete[] requests;
78   Request::unref(&request);
79   return MPI_SUCCESS;
80 }
81
82 int Coll_allgatherv_default::allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
83                          int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
84 {
85   MPI_Request request;
86   Colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, &request);
87   MPI_Request* requests = request->get_nbc_requests();
88   int count = request->get_nbc_requests_size();
89   Request::waitall(count, requests, MPI_STATUS_IGNORE);
90   for (int other = 0; other < count; other++) {
91     Request::unref(&requests[other]);
92   }
93   delete[] requests;
94   Request::unref(&request);
95   return MPI_SUCCESS;
96 }
97
98 int Coll_scatter_default::scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
99                       void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
100 {
101   MPI_Request request;
102   Colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request);
103   MPI_Request* requests = request->get_nbc_requests();
104   int count = request->get_nbc_requests_size();
105   Request::waitall(count, requests, MPI_STATUS_IGNORE);
106   for (int dst = 0; dst < count; dst++) {
107     if(requests[dst]!=MPI_REQUEST_NULL)
108       Request::unref(&requests[dst]);
109   }
110   delete[] requests;
111   Request::unref(&request);
112   return MPI_SUCCESS;
113 }
114
115
116
117 int Coll_reduce_default::reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
118                      MPI_Comm comm)
119 {
120   const int system_tag = COLL_TAG_REDUCE;
121   MPI_Aint lb = 0;
122   MPI_Aint dataext = 0;
123
124   char* sendtmpbuf = static_cast<char *>(sendbuf);
125
126   int rank = comm->rank();
127   int size = comm->size();
128   if (size <= 0)
129     return MPI_ERR_COMM;
130   //non commutative case, use a working algo from openmpi
131   if (op != MPI_OP_NULL && not op->is_commutative()) {
132     return Coll_reduce_ompi_basic_linear::reduce(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
133   }
134
135   if( sendbuf == MPI_IN_PLACE ) {
136     sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
137     Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
138   }
139
140   if(rank != root) {
141     // Send buffer to root
142     Request::send(sendtmpbuf, count, datatype, root, system_tag, comm);
143   } else {
144     datatype->extent(&lb, &dataext);
145     // Local copy from root
146     if (sendtmpbuf != nullptr && recvbuf != nullptr)
147       Datatype::copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
148     // Receive buffers from senders
149     MPI_Request *requests = xbt_new(MPI_Request, size - 1);
150     void **tmpbufs = xbt_new(void *, size - 1);
151     int index = 0;
152     for (int src = 0; src < size; src++) {
153       if (src != root) {
154         tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
155         requests[index] =
156           Request::irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
157         index++;
158       }
159     }
160     // Wait for completion of irecv's.
161     Request::startall(size - 1, requests);
162     for (int src = 0; src < size - 1; src++) {
163       index = Request::waitany(size - 1, requests, MPI_STATUS_IGNORE);
164       XBT_DEBUG("finished waiting any request with index %d", index);
165       if(index == MPI_UNDEFINED) {
166         break;
167       }else{
168         Request::unref(&requests[index]);
169       }
170       if(op) /* op can be MPI_OP_NULL that does nothing */
171         if(op!=MPI_OP_NULL) op->apply( tmpbufs[index], recvbuf, &count, datatype);
172     }
173       for(index = 0; index < size - 1; index++) {
174         smpi_free_tmp_buffer(tmpbufs[index]);
175       }
176     xbt_free(tmpbufs);
177     xbt_free(requests);
178
179   }
180   if( sendbuf == MPI_IN_PLACE ) {
181     smpi_free_tmp_buffer(sendtmpbuf);
182   }
183   return MPI_SUCCESS;
184 }
185
186 int Coll_allreduce_default::allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
187 {
188   int ret;
189   ret = Coll_reduce_default::reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
190   if(ret==MPI_SUCCESS)
191     ret = Coll_bcast_default::bcast(recvbuf, count, datatype, 0, comm);
192   return ret;
193 }
194
195 int Coll_alltoall_default::alltoall( void *sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
196 {
197   return Coll_alltoall_ompi::alltoall(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
198 }
199
200
201
202 int Coll_alltoallv_default::alltoallv(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype sendtype,
203                               void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
204 {
205   MPI_Request request;
206   int err = Colls::ialltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm, &request);
207   MPI_Request* requests = request->get_nbc_requests();
208   int count = request->get_nbc_requests_size();
209   XBT_DEBUG("<%d> wait for %d requests", comm->rank(), count);
210   Request::waitall(count, requests, MPI_STATUS_IGNORE);
211   for (int i = 0; i < count; i++) {
212     if(requests[i]!=MPI_REQUEST_NULL)
213       Request::unref(&requests[i]);
214   }
215   delete[] requests;
216   Request::unref(&request);
217   return err;
218 }
219
220 }
221 }
222