Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix 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-2021. 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 bcast__default(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm)
15 {
16   return bcast__binomial_tree(buf, count, datatype, root, comm);
17 }
18
19 int barrier__default(MPI_Comm comm)
20 {
21   return barrier__ompi_basic_linear(comm);
22 }
23
24
25 int gather__default(const 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, 0);
30   return Request::wait(&request, MPI_STATUS_IGNORE);
31 }
32
33 int reduce_scatter__default(const void *sendbuf, void *recvbuf, const 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 = new int[size];
42   int regular=1;
43   for (int i = 0; i < size; i++) {
44     if(recvcounts[i]!=recvcounts[0]){
45       regular=0;
46       continue;
47     }
48     displs[i] = count;
49     count += recvcounts[i];
50   }
51   if(not regular){
52     delete[] displs;
53     return reduce_scatter__ompi(sendbuf, recvbuf, recvcounts, datatype, op, comm);
54   }
55
56   unsigned char* tmpbuf = smpi_get_tmp_sendbuffer(count * datatype->get_extent());
57
58   int ret = reduce__default(sendbuf, tmpbuf, count, datatype, op, 0, comm);
59   if(ret==MPI_SUCCESS)
60     ret = colls::scatterv(tmpbuf, recvcounts, displs, datatype, recvbuf, recvcounts[rank], datatype, 0, comm);
61   delete[] displs;
62   smpi_free_tmp_buffer(tmpbuf);
63   return ret;
64 }
65
66
67 int allgather__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
68                        void *recvbuf,int recvcount, MPI_Datatype recvtype, MPI_Comm comm)
69 {
70   MPI_Request request;
71   colls::iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, &request);
72   return Request::wait(&request, MPI_STATUS_IGNORE);
73 }
74
75 int allgatherv__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf,
76                         const int *recvcounts, const int *displs, MPI_Datatype recvtype, MPI_Comm comm)
77 {
78   MPI_Request request;
79   colls::iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcounts, displs, recvtype, comm, &request, 0);
80   MPI_Request* requests = request->get_nbc_requests();
81   int count = request->get_nbc_requests_size();
82   Request::waitall(count, requests, MPI_STATUS_IGNORE);
83   for (int other = 0; other < count; other++) {
84     Request::unref(&requests[other]);
85   }
86   delete[] requests;
87   Request::unref(&request);
88   return MPI_SUCCESS;
89 }
90
91 int scatter__default(const void *sendbuf, int sendcount, MPI_Datatype sendtype,
92                      void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)
93 {
94   MPI_Request request;
95   colls::iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, &request, 0);
96   return Request::wait(&request, MPI_STATUS_IGNORE);
97 }
98
99 int reduce__default(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root,
100                     MPI_Comm comm)
101 {
102   //non commutative case, use a working algo from openmpi
103   if (op != MPI_OP_NULL && (datatype->flags() & DT_FLAG_DERIVED || not op->is_commutative())) {
104     return reduce__ompi_basic_linear(sendbuf, recvbuf, count, datatype, op, root, comm);
105   }
106   MPI_Request request;
107   colls::ireduce(sendbuf, recvbuf, count, datatype, op, root, comm, &request, 0);
108   return Request::wait(&request, MPI_STATUS_IGNORE);
109 }
110
111 int allreduce__default(const void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
112 {
113   //FIXME: have mpi_ireduce and iallreduce handle derived datatypes correctly
114   if(datatype->flags() & DT_FLAG_DERIVED)
115     return allreduce__ompi(sendbuf, recvbuf, count, datatype, op, comm);
116   int ret;
117   ret = reduce__default(sendbuf, recvbuf, count, datatype, op, 0, comm);
118   if(ret==MPI_SUCCESS)
119     ret = bcast__default(recvbuf, count, datatype, 0, comm);
120   return ret;
121 }
122
123 int alltoall__default(const void *sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount, MPI_Datatype rdtype, MPI_Comm comm)
124 {
125   return alltoall__ompi(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
126 }
127
128 int alltoallv__default(const void *sendbuf, const int *sendcounts, const int *senddisps, MPI_Datatype sendtype,
129                        void *recvbuf, const int *recvcounts, const int *recvdisps, MPI_Datatype recvtype, MPI_Comm comm)
130 {
131   MPI_Request request;
132   colls::ialltoallv(sendbuf, sendcounts, senddisps, sendtype, recvbuf, recvcounts, recvdisps, recvtype, comm, &request,
133                     0);
134   return Request::wait(&request, MPI_STATUS_IGNORE);
135 }
136
137 }
138 }
139