Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a few potential memory leaks in SMPI colls
[simgrid.git] / src / smpi / colls / reduce_scatter / reduce_scatter-mpich.cpp
index 3679390..46f59c6 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2019. The SimGrid Team.
+/* Copyright (c) 2013-2021. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
@@ -24,24 +24,21 @@ static inline int MPIU_Mirror_permutation(unsigned int x, int bits)
 namespace simgrid{
 namespace smpi{
 
-int Coll_reduce_scatter_mpich_pair::reduce_scatter(const void *sendbuf, void *recvbuf, const int recvcounts[],
-                              MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
+int reduce_scatter__mpich_pair(const void *sendbuf, void *recvbuf, const int recvcounts[],
+                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
 {
     int   rank, comm_size, i;
     MPI_Aint extent, true_extent, true_lb;
     unsigned char* tmp_recvbuf;
     int mpi_errno = MPI_SUCCESS;
     int total_count, dst, src;
-    int is_commutative;
     comm_size = comm->size();
     rank = comm->rank();
 
     extent =datatype->get_extent();
     datatype->extent(&true_lb, &true_extent);
 
-    if (op->is_commutative()) {
-        is_commutative = 1;
-    }
+    bool is_commutative = (op == MPI_OP_NULL || op->is_commutative());
 
     int* disps = new int[comm_size];
 
@@ -111,8 +108,11 @@ int Coll_reduce_scatter_mpich_pair::reduce_scatter(const void *sendbuf, void *re
                   /* copy result back into recvbuf */
                   mpi_errno =
                       Datatype::copy(tmp_recvbuf, recvcounts[rank], datatype, recvbuf, recvcounts[rank], datatype);
-                  if (mpi_errno)
+                  if (mpi_errno) {
+                   delete[] disps;
+                    smpi_free_tmp_buffer(tmp_recvbuf);
                     return (mpi_errno);
+                 }
                 }
                 else {
                   if (op != MPI_OP_NULL)
@@ -120,8 +120,11 @@ int Coll_reduce_scatter_mpich_pair::reduce_scatter(const void *sendbuf, void *re
                   /* copy result back into recvbuf */
                   mpi_errno = Datatype::copy(tmp_recvbuf, recvcounts[rank], datatype,
                                              ((char*)recvbuf + disps[rank] * extent), recvcounts[rank], datatype);
-                  if (mpi_errno)
+                  if (mpi_errno) {
+                   delete[] disps;
+                    smpi_free_tmp_buffer(tmp_recvbuf);
                     return (mpi_errno);
+                 }
                 }
             }
         }
@@ -134,7 +137,11 @@ int Coll_reduce_scatter_mpich_pair::reduce_scatter(const void *sendbuf, void *re
                                        recvcounts[rank], datatype,
                                        recvbuf,
                                        recvcounts[rank], datatype );
-            if (mpi_errno) return(mpi_errno);
+            if (mpi_errno) {
+             delete[] disps;
+              smpi_free_tmp_buffer(tmp_recvbuf);
+             return (mpi_errno);
+           }
         }
 
         delete[] disps;
@@ -144,8 +151,8 @@ int Coll_reduce_scatter_mpich_pair::reduce_scatter(const void *sendbuf, void *re
 }
 
 
-int Coll_reduce_scatter_mpich_noncomm::reduce_scatter(const void *sendbuf, void *recvbuf, const int recvcounts[],
-                              MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
+int reduce_scatter__mpich_noncomm(const void *sendbuf, void *recvbuf, const int recvcounts[],
+                                  MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
 {
     int mpi_errno = MPI_SUCCESS;
     int comm_size = comm->size() ;
@@ -264,7 +271,7 @@ int Coll_reduce_scatter_mpich_noncomm::reduce_scatter(const void *sendbuf, void
 
 
 
-int Coll_reduce_scatter_mpich_rdb::reduce_scatter(const void *sendbuf, void *recvbuf, const int recvcounts[],
+int reduce_scatter__mpich_rdb(const void *sendbuf, void *recvbuf, const int recvcounts[],
                               MPI_Datatype datatype, MPI_Op op, MPI_Comm comm)
 {
     int   rank, comm_size, i;
@@ -274,16 +281,14 @@ int Coll_reduce_scatter_mpich_rdb::reduce_scatter(const void *sendbuf, void *rec
     int mask, dst_tree_root, my_tree_root, j, k;
     int received;
     MPI_Datatype sendtype, recvtype;
-    int nprocs_completed, tmp_mask, tree_root, is_commutative=0;
+    int nprocs_completed, tmp_mask, tree_root;
     comm_size = comm->size();
     rank = comm->rank();
 
     extent =datatype->get_extent();
     datatype->extent(&true_lb, &true_extent);
 
-    if ((op==MPI_OP_NULL) || op->is_commutative()) {
-        is_commutative = 1;
-    }
+    bool is_commutative = (op == MPI_OP_NULL || op->is_commutative());
 
     int* disps = new int[comm_size];
 
@@ -312,8 +317,10 @@ int Coll_reduce_scatter_mpich_rdb::reduce_scatter(const void *sendbuf, void *rec
     else
       mpi_errno = Datatype::copy(recvbuf, total_count, datatype, tmp_results, total_count, datatype);
 
-    if (mpi_errno)
+    if (mpi_errno) {
+      delete[] disps;
       return (mpi_errno);
+    }
 
     mask = 0x1;
     i    = 0;