Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
avoid potential leak
[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   return Colls::finish_nbc_request(request);
31 }
32
33 int Coll_reduce_scatter_default::reduce_scatter(void *sendbuf, void *recvbuf, int *recvcounts, MPI_Datatype datatype, MPI_Op op,
34                              MPI_Comm comm)
35 {
36   int rank = comm->rank();
37
38   /* arbitrarily choose root as rank 0 */
39   int size = comm->size();
40   int count = 0;
41   int *displs = xbt_new(int, size);
42   for (int i = 0; i < size; i++) {
43     displs[i] = count;
44     count += recvcounts[i];
45   }
46   void *tmpbuf = static_cast<void*>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
47
48   int ret = Coll_reduce_default::reduce(sendbuf, tmpbuf, count, datatype, op, 0, comm);
49   if(ret==MPI_SUCCESS)
50     ret = Colls::scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
51   xbt_free(displs);
52   smpi_free_tmp_buffer(tmpbuf);
53   return ret;
54 }
55
56
57 int Coll_allgather_default::allgather(void *sendbuf, int sendcount, MPI_Datatype sendtype,
58                         void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
59 {
60   MPI_Request request;
61   Colls::iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, &request);
62   return Colls::finish_nbc_request(request);
63 }
64
65 int Coll_allgatherv_default::allgatherv(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
66                          int *recvcounts, int *displs, MPI_Datatype recvtype, MPI_Comm comm)
67 {
68   MPI_Request request;
69   Colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, &request);
70   MPI_Request* requests = request->get_nbc_requests();
71   int count = request->get_nbc_requests_size();
72   Request::waitall(count, requests, MPI_STATUS_IGNORE);
73   for (int other = 0; other < count; other++) {
74     Request::unref(&requests[other]);
75   }
76   delete[] requests;
77   Request::unref(&request);
78   return MPI_SUCCESS;
79 }
80
81 int Coll_scatter_default::scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype,
82                       void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
83 {
84   MPI_Request request;
85   Colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request);
86   return Colls::finish_nbc_request(request);
87 }
88
89 int Coll_reduce_default::reduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
90                      MPI_Comm comm)
91 {
92   const int system_tag = COLL_TAG_REDUCE;
93   MPI_Aint lb = 0;
94   MPI_Aint dataext = 0;
95
96   char* sendtmpbuf = static_cast<char *>(sendbuf);
97
98   int rank = comm->rank();
99   int size = comm->size();
100   if (size <= 0)
101     return MPI_ERR_COMM;
102   //non commutative case, use a working algo from openmpi
103   if (op != MPI_OP_NULL && not op->is_commutative()) {
104     return Coll_reduce_ompi_basic_linear::reduce(sendtmpbuf, recvbuf, count, datatype, op, root, comm);
105   }
106
107   if( sendbuf == MPI_IN_PLACE ) {
108     sendtmpbuf = static_cast<char *>(smpi_get_tmp_sendbuffer(count*datatype->get_extent()));
109     Datatype::copy(recvbuf, count, datatype,sendtmpbuf, count, datatype);
110   }
111
112   if(rank != root) {
113     // Send buffer to root
114     Request::send(sendtmpbuf, count, datatype, root, system_tag, comm);
115   } else {
116     datatype->extent(&lb, &dataext);
117     // Local copy from root
118     if (sendtmpbuf != nullptr && recvbuf != nullptr)
119       Datatype::copy(sendtmpbuf, count, datatype, recvbuf, count, datatype);
120     // Receive buffers from senders
121     MPI_Request *requests = xbt_new(MPI_Request, size - 1);
122     void **tmpbufs = xbt_new(void *, size - 1);
123     int index = 0;
124     for (int src = 0; src < size; src++) {
125       if (src != root) {
126         tmpbufs[index] = smpi_get_tmp_sendbuffer(count * dataext);
127         requests[index] =
128           Request::irecv_init(tmpbufs[index], count, datatype, src, system_tag, comm);
129         index++;
130       }
131     }
132     // Wait for completion of irecv's.
133     Request::startall(size - 1, requests);
134     for (int src = 0; src < size - 1; src++) {
135       index = Request::waitany(size - 1, requests, MPI_STATUS_IGNORE);
136       XBT_DEBUG("finished waiting any request with index %d", index);
137       if(index == MPI_UNDEFINED) {
138         break;
139       }else{
140         Request::unref(&requests[index]);
141       }
142       if(op) /* op can be MPI_OP_NULL that does nothing */
143         if(op!=MPI_OP_NULL) op->apply( tmpbufs[index], recvbuf, &count, datatype);
144     }
145       for(index = 0; index < size - 1; index++) {
146         smpi_free_tmp_buffer(tmpbufs[index]);
147       }
148     xbt_free(tmpbufs);
149     xbt_free(requests);
150
151   }
152   if( sendbuf == MPI_IN_PLACE ) {
153     smpi_free_tmp_buffer(sendtmpbuf);
154   }
155   return MPI_SUCCESS;
156 }
157
158 int Coll_allreduce_default::allreduce(void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
159 {
160   int ret;
161   ret = Coll_reduce_default::reduce(sendbuf, recvbuf, count, datatype, op, 0, comm);
162   if(ret==MPI_SUCCESS)
163     ret = Coll_bcast_default::bcast(recvbuf, count, datatype, 0, comm);
164   return ret;
165 }
166
167 int Coll_alltoall_default::alltoall( void *sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
168 {
169   return Coll_alltoall_ompi::alltoall(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
170 }
171
172 int Coll_alltoallv_default::alltoallv(void *sendbuf, int *sendcounts, int *senddisps, MPI_Datatype sendtype,
173                               void *recvbuf, int *recvcounts, int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
174 {
175   MPI_Request request;
176   Colls::ialltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm, &request);
177   return Colls::finish_nbc_request(request);
178 }
179
180 }
181 }
182