Logo AND Algorithmique Numérique Distribuée

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