Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / src / smpi / colls / smpi_mpich_selector.cpp
index b5a0858..16f01f0 100644 (file)
@@ -1,12 +1,14 @@
 /* selector for collective algorithms based on mpich decision logic */
 
-/* Copyright (c) 2009-2010, 2013-2017. The SimGrid Team.
+/* Copyright (c) 2009-2022. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "colls_private.h"
+#include "colls_private.hpp"
+
+#include <memory>
 
 /* This is the default implementation of allreduce. The algorithm is:
 
@@ -58,7 +60,7 @@
 */
 namespace simgrid{
 namespace smpi{
-int Coll_allreduce_mpich::allreduce(void *sbuf, void *rbuf, int count,
+int allreduce__mpich(const void *sbuf, void *rbuf, int count,
                         MPI_Datatype dtype, MPI_Op op, MPI_Comm comm)
 {
     size_t dsize, block_dsize;
@@ -68,6 +70,14 @@ int Coll_allreduce_mpich::allreduce(void *sbuf, void *rbuf, int count,
     dsize = dtype->size();
     block_dsize = dsize * count;
 
+    /*MPICH uses SMP algorithms for all commutative ops now*/
+    if (not comm->is_smp_comm()) {
+      if(comm->get_leaders_comm()==MPI_COMM_NULL){
+        comm->init_smp();
+      }
+      if(op->is_commutative())
+        return allreduce__mvapich2_two_level(sbuf, rbuf,count, dtype, op, comm);
+    }
 
     /* find nearest power-of-two less than or equal to comm_size */
     int pof2 = 1;
@@ -76,14 +86,10 @@ int Coll_allreduce_mpich::allreduce(void *sbuf, void *rbuf, int count,
 
     if (block_dsize > large_message && count >= pof2 && (op==MPI_OP_NULL || op->is_commutative())) {
       //for long messages
-       return (Coll_allreduce_rab_rdb::allreduce (sbuf, rbuf,
-                                                                   count, dtype,
-                                                                   op, comm));
+       return allreduce__rab_rdb(sbuf, rbuf, count, dtype, op, comm);
     }else {
       //for short ones and count < pof2
-      return (Coll_allreduce_rdb::allreduce (sbuf, rbuf,
-                                                                   count, dtype,
-                                                                   op, comm));
+      return allreduce__rdb(sbuf, rbuf, count, dtype, op, comm);
     }
 }
 
@@ -134,11 +140,11 @@ int Coll_allreduce_mpich::allreduce(void *sbuf, void *rbuf, int count,
    End Algorithm: MPI_Alltoall
 */
 
-int Coll_alltoall_mpich::alltoall( void *sbuf, int scount,
-                                             MPI_Datatype sdtype,
-                                             void* rbuf, int rcount,
-                                             MPI_Datatype rdtype,
-                                             MPI_Comm comm)
+int alltoall__mpich(const void *sbuf, int scount,
+                    MPI_Datatype sdtype,
+                    void* rbuf, int rcount,
+                    MPI_Datatype rdtype,
+                    MPI_Comm comm)
 {
     int communicator_size;
     size_t dsize, block_dsize;
@@ -164,42 +170,42 @@ int Coll_alltoall_mpich::alltoall( void *sbuf, int scount,
     block_dsize = dsize * scount;
 
     if ((block_dsize < short_size) && (communicator_size >= 8)) {
-        return Coll_alltoall_bruck::alltoall(sbuf, scount, sdtype,
-                                                    rbuf, rcount, rdtype,
-                                                    comm);
+        return alltoall__bruck(sbuf, scount, sdtype,
+                               rbuf, rcount, rdtype,
+                               comm);
 
     } else if (block_dsize < medium_size) {
-        return Coll_alltoall_basic_linear::alltoall(sbuf, scount, sdtype,
-                                                           rbuf, rcount, rdtype,
-                                                           comm);
+        return alltoall__mvapich2_scatter_dest(sbuf, scount, sdtype,
+                                               rbuf, rcount, rdtype,
+                                               comm);
     }else if (communicator_size%2){
-        return Coll_alltoall_ring::alltoall(sbuf, scount, sdtype,
-                                                           rbuf, rcount, rdtype,
-                                                           comm);
+        return alltoall__pair(sbuf, scount, sdtype,
+                              rbuf, rcount, rdtype,
+                              comm);
     }
 
-    return Coll_alltoall_ring::alltoall (sbuf, scount, sdtype,
-                                                    rbuf, rcount, rdtype,
-                                                    comm);
+    return alltoall__ring(sbuf, scount, sdtype,
+                          rbuf, rcount, rdtype,
+                          comm);
 }
 
-int Coll_alltoallv_mpich::alltoallv(void *sbuf, int *scounts, int *sdisps,
-                                              MPI_Datatype sdtype,
-                                              void *rbuf, int *rcounts, int *rdisps,
-                                              MPI_Datatype rdtype,
-                                              MPI_Comm  comm
-                                              )
+int alltoallv__mpich(const void *sbuf, const int *scounts, const int *sdisps,
+                     MPI_Datatype sdtype,
+                     void *rbuf, const int *rcounts, const int *rdisps,
+                     MPI_Datatype rdtype,
+                     MPI_Comm  comm
+                     )
 {
     /* For starters, just keep the original algorithm. */
-    return Coll_alltoallv_bruck::alltoallv(sbuf, scounts, sdisps, sdtype,
-                                                        rbuf, rcounts, rdisps,rdtype,
-                                                        comm);
+    return alltoallv__bruck(sbuf, scounts, sdisps, sdtype,
+                            rbuf, rcounts, rdisps,rdtype,
+                            comm);
 }
 
 
-int Coll_barrier_mpich::barrier(MPI_Comm  comm)
+int barrier__mpich(MPI_Comm  comm)
 {
-    return Coll_barrier_ompi_bruck::barrier(comm);
+    return barrier__ompi_bruck(comm);
 }
 
 /* This is the default implementation of broadcast. The algorithm is:
@@ -245,9 +251,9 @@ int Coll_barrier_mpich::barrier(MPI_Comm  comm)
 */
 
 
-int Coll_bcast_mpich::bcast(void *buff, int count,
-                                          MPI_Datatype datatype, int root,
-                                          MPI_Comm  comm
+int bcast__mpich(void *buff, int count,
+                 MPI_Datatype datatype, int root,
+                 MPI_Comm  comm
                                           )
 {
     /* Decision function based on MX results for
@@ -259,6 +265,14 @@ int Coll_bcast_mpich::bcast(void *buff, int count,
     //int segsize = 0;
     size_t message_size, dsize;
 
+    if (not comm->is_smp_comm()) {
+      if(comm->get_leaders_comm()==MPI_COMM_NULL){
+        comm->init_smp();
+      }
+      if(comm->is_uniform())
+        return bcast__SMP_binomial(buff, count, datatype, root, comm);
+    }
+
     communicator_size = comm->size();
 
     /* else we need data size for decision function */
@@ -269,18 +283,15 @@ int Coll_bcast_mpich::bcast(void *buff, int count,
        single-element broadcasts */
     if ((message_size < small_message_size) || (communicator_size <= 8)) {
         /* Binomial without segmentation */
-        return  Coll_bcast_binomial_tree::bcast (buff, count, datatype,
-                                                      root, comm);
+        return  bcast__binomial_tree(buff, count, datatype, root, comm);
 
     } else if (message_size < intermediate_message_size && !(communicator_size%2)) {
         // SplittedBinary with 1KB segments
-        return Coll_bcast_scatter_rdb_allgather::bcast(buff, count, datatype,
-                                                         root, comm);
+        return bcast__scatter_rdb_allgather(buff, count, datatype, root, comm);
 
     }
      //Handle large message sizes
-     return Coll_bcast_scatter_LR_allgather::bcast (buff, count, datatype,
-                                                     root, comm);
+     return bcast__scatter_LR_allgather(buff, count, datatype, root, comm);
 
 }
 
@@ -341,15 +352,23 @@ int Coll_bcast_mpich::bcast(void *buff, int count,
 */
 
 
-int Coll_reduce_mpich::reduce( void *sendbuf, void *recvbuf,
+int reduce__mpich(const void *sendbuf, void *recvbuf,
                                             int count, MPI_Datatype  datatype,
                                             MPI_Op   op, int root,
                                             MPI_Comm   comm
                                             )
 {
     int communicator_size=0;
-    //int segsize = 0;
     size_t message_size, dsize;
+
+    if (not comm->is_smp_comm()) {
+      if(comm->get_leaders_comm()==MPI_COMM_NULL){
+        comm->init_smp();
+      }
+      if (op->is_commutative() == 1)
+        return reduce__mvapich2_two_level(sendbuf, recvbuf, count, datatype, op, root, comm);
+    }
+
     communicator_size = comm->size();
 
     /* need data size for decision function */
@@ -361,10 +380,9 @@ int Coll_reduce_mpich::reduce( void *sendbuf, void *recvbuf,
     pof2 >>= 1;
 
     if ((count < pof2) || (message_size < 2048) || (op != MPI_OP_NULL && not op->is_commutative())) {
-      return Coll_reduce_binomial::reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
+      return reduce__binomial(sendbuf, recvbuf, count, datatype, op, root, comm);
     }
-        return Coll_reduce_scatter_gather::reduce(sendbuf, recvbuf, count, datatype, op, root, comm/*, module,
-                                                     segsize, max_requests*/);
+    return reduce__scatter_gather(sendbuf, recvbuf, count, datatype, op, root, comm);
 }
 
 
@@ -417,12 +435,12 @@ int Coll_reduce_mpich::reduce( void *sendbuf, void *recvbuf,
 */
 
 
-int Coll_reduce_scatter_mpich::reduce_scatter( void *sbuf, void *rbuf,
-                                                    int *rcounts,
-                                                    MPI_Datatype dtype,
-                                                    MPI_Op  op,
-                                                    MPI_Comm  comm
-                                                    )
+int reduce_scatter__mpich(const void *sbuf, void *rbuf,
+                          const int *rcounts,
+                          MPI_Datatype dtype,
+                          MPI_Op  op,
+                          MPI_Comm  comm
+                          )
 {
     int comm_size, i;
     size_t total_message_size;
@@ -439,14 +457,12 @@ int Coll_reduce_scatter_mpich::reduce_scatter( void *sbuf, void *rbuf,
     }
 
     if( (op==MPI_OP_NULL || op->is_commutative()) &&  total_message_size > 524288) {
-        return Coll_reduce_scatter_mpich_pair::reduce_scatter (sbuf, rbuf, rcounts,
-                                                                    dtype, op,
-                                                                    comm);
+        return reduce_scatter__mpich_pair(sbuf, rbuf, rcounts, dtype, op, comm);
     } else if ((op != MPI_OP_NULL && not op->is_commutative())) {
-      int is_block_regular = 1;
+      bool is_block_regular = true;
       for (i = 0; i < (comm_size - 1); ++i) {
         if (rcounts[i] != rcounts[i + 1]) {
-          is_block_regular = 0;
+          is_block_regular = false;
           break;
         }
       }
@@ -458,12 +474,12 @@ int Coll_reduce_scatter_mpich::reduce_scatter( void *sbuf, void *rbuf,
 
       if (pof2 == comm_size && is_block_regular) {
         /* noncommutative, pof2 size, and block regular */
-        return Coll_reduce_scatter_mpich_noncomm::reduce_scatter(sbuf, rbuf, rcounts, dtype, op, comm);
+        return reduce_scatter__mpich_noncomm(sbuf, rbuf, rcounts, dtype, op, comm);
       }
 
-      return Coll_reduce_scatter_mpich_rdb::reduce_scatter(sbuf, rbuf, rcounts, dtype, op, comm);
+      return reduce_scatter__mpich_rdb(sbuf, rbuf, rcounts, dtype, op, comm);
     }else{
-       return Coll_reduce_scatter_mpich_rdb::reduce_scatter(sbuf, rbuf, rcounts, dtype, op, comm);
+       return reduce_scatter__mpich_rdb(sbuf, rbuf, rcounts, dtype, op, comm);
     }
 }
 
@@ -513,12 +529,12 @@ int Coll_reduce_scatter_mpich::reduce_scatter( void *sbuf, void *rbuf,
    End Algorithm: MPI_Allgather
 */
 
-int Coll_allgather_mpich::allgather(void *sbuf, int scount,
-                                              MPI_Datatype sdtype,
-                                              void* rbuf, int rcount,
-                                              MPI_Datatype rdtype,
-                                              MPI_Comm  comm
-                                              )
+int allgather__mpich(const void *sbuf, int scount,
+                     MPI_Datatype sdtype,
+                     void* rbuf, int rcount,
+                     MPI_Datatype rdtype,
+                     MPI_Comm  comm
+                     )
 {
     int communicator_size, pow2_size;
     size_t dsize, total_dsize;
@@ -541,17 +557,11 @@ int Coll_allgather_mpich::allgather(void *sbuf, int scount,
        - for everything else use ring.
     */
     if ((pow2_size == communicator_size) && (total_dsize < 524288)) {
-        return Coll_allgather_rdb::allgather(sbuf, scount, sdtype,
-                                                                 rbuf, rcount, rdtype,
-                                                                 comm);
+        return allgather__rdb(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
     } else if (total_dsize <= 81920) {
-        return Coll_allgather_bruck::allgather(sbuf, scount, sdtype,
-                                                     rbuf, rcount, rdtype,
-                                                     comm);
+        return allgather__bruck(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
     }
-    return Coll_allgather_ring::allgather(sbuf, scount, sdtype,
-                                                rbuf, rcount, rdtype,
-                                                comm);
+    return allgather__ring(sbuf, scount, sdtype, rbuf, rcount, rdtype, comm);
 }
 
 
@@ -591,13 +601,13 @@ int Coll_allgather_mpich::allgather(void *sbuf, int scount,
 
    End Algorithm: MPI_Allgatherv
 */
-int Coll_allgatherv_mpich::allgatherv(void *sbuf, int scount,
-                                               MPI_Datatype sdtype,
-                                               void* rbuf, int *rcounts,
-                                               int *rdispls,
-                                               MPI_Datatype rdtype,
-                                               MPI_Comm  comm
-                                               )
+int allgatherv__mpich(const void *sbuf, int scount,
+                      MPI_Datatype sdtype,
+                      void* rbuf, const int *rcounts,
+                      const int *rdispls,
+                      MPI_Datatype rdtype,
+                      MPI_Comm  comm
+                      )
 {
     int communicator_size, pow2_size,i;
     size_t total_dsize;
@@ -614,17 +624,11 @@ int Coll_allgatherv_mpich::allgatherv(void *sbuf, int scount,
     for (pow2_size  = 1; pow2_size < communicator_size; pow2_size <<=1);
 
     if ((pow2_size == communicator_size) && (total_dsize < 524288)) {
-        return Coll_allgatherv_mpich_rdb::allgatherv(sbuf, scount, sdtype,
-                                                                 rbuf, rcounts, rdispls, rdtype,
-                                                                 comm);
+        return allgatherv__mpich_rdb(sbuf, scount, sdtype, rbuf, rcounts, rdispls, rdtype, comm);
     } else if (total_dsize <= 81920) {
-        return Coll_allgatherv_ompi_bruck::allgatherv(sbuf, scount, sdtype,
-                                                     rbuf, rcounts, rdispls, rdtype,
-                                                     comm);
+        return allgatherv__ompi_bruck(sbuf, scount, sdtype, rbuf, rcounts, rdispls, rdtype, comm);
     }
-    return Coll_allgatherv_mpich_ring::allgatherv(sbuf, scount, sdtype,
-                                                rbuf, rcounts, rdispls, rdtype,
-                                                comm);
+    return allgatherv__mpich_ring(sbuf, scount, sdtype, rbuf, rcounts, rdispls, rdtype, comm);
 }
 
 /* This is the default implementation of gather. The algorithm is:
@@ -649,17 +653,17 @@ int Coll_allgatherv_mpich::allgatherv(void *sbuf, int scount,
    End Algorithm: MPI_Gather
 */
 
-int Coll_gather_mpich::gather(void *sbuf, int scount,
-                                           MPI_Datatype sdtype,
-                                           void* rbuf, int rcount,
-                                           MPI_Datatype rdtype,
-                                           int root,
-                                           MPI_Comm  comm
-                                           )
+int gather__mpich(const void *sbuf, int scount,
+                  MPI_Datatype sdtype,
+                  void* rbuf, int rcount,
+                  MPI_Datatype rdtype,
+                  int root,
+                  MPI_Comm  comm
+                  )
 {
-        return Coll_gather_ompi_binomial::gather (sbuf, scount, sdtype,
-                                                      rbuf, rcount, rdtype,
-                                                      root, comm);
+    return gather__ompi_binomial(sbuf, scount, sdtype,
+                                 rbuf, rcount, rdtype,
+                                 root, comm);
 }
 
 /* This is the default implementation of scatter. The algorithm is:
@@ -684,25 +688,21 @@ int Coll_gather_mpich::gather(void *sbuf, int scount,
 */
 
 
-int Coll_scatter_mpich::scatter(void *sbuf, int scount,
-                                            MPI_Datatype sdtype,
-                                            void* rbuf, int rcount,
-                                            MPI_Datatype rdtype,
-                                            int root, MPI_Comm  comm
-                                            )
+int scatter__mpich(const void *sbuf, int scount,
+                   MPI_Datatype sdtype,
+                   void* rbuf, int rcount,
+                   MPI_Datatype rdtype,
+                   int root, MPI_Comm  comm
+                   )
 {
+  std::unique_ptr<unsigned char[]> tmp_buf;
   if(comm->rank()!=root){
-      sbuf=xbt_malloc(rcount*rdtype->get_extent());
-      scount=rcount;
-      sdtype=rdtype;
-  }
-  int ret= Coll_scatter_ompi_binomial::scatter (sbuf, scount, sdtype,
-                                                       rbuf, rcount, rdtype,
-                                                       root, comm);
-  if(comm->rank()!=root){
-      xbt_free(sbuf);
+    tmp_buf = std::make_unique<unsigned char[]>(rcount * rdtype->get_extent());
+    sbuf   = tmp_buf.get();
+    scount = rcount;
+    sdtype = rdtype;
   }
-  return ret;
+  return scatter__ompi_binomial(sbuf, scount, sdtype, rbuf, rcount, rdtype, root, comm);
 }
 }
 }