Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix mismatched alloc/free.
[simgrid.git] / src / smpi / colls / scatter / scatter-ompi.cpp
index 04bf1ae..c4c41b0 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2013-2014. The SimGrid Team.
+/* Copyright (c) 2013-2019. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * Additional copyrights may follow
  */
 
+#include "../coll_tuned_topo.hpp"
+#include "../colls_private.hpp"
 
-#include "../colls_private.h"
-#include "../coll_tuned_topo.h"
+namespace simgrid{
+namespace smpi{
 
-
-int
-Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
-                                      MPI_Datatype sdtype,
-                                      void *rbuf, int rcount,
-                                      MPI_Datatype rdtype,
-                                      int root,
-                                      MPI_Comm comm
-                                      )
+int Coll_scatter_ompi_binomial::scatter(void* sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount,
+                                        MPI_Datatype rdtype, int root, MPI_Comm comm)
 {
     int line = -1;
     int i;
@@ -44,7 +39,7 @@ Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
     int err;
     ompi_coll_tree_t* bmtree;
     MPI_Status status;
-    MPI_Aint sextent, slb, strue_lb, strue_extent; 
+    MPI_Aint sextent, slb, strue_lb, strue_extent;
     MPI_Aint rextent, rlb, rtrue_lb, rtrue_extent;
 
     size = comm->size();
@@ -54,7 +49,7 @@ Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
                  "Coll_scatter_ompi_binomial::scatter rank %d", rank);
 
     /* create the binomial tree */
-    
+
 //    COLL_TUNED_UPDATE_IN_ORDER_BMTREE( comm, tuned_module, root );
     bmtree =  ompi_coll_tuned_topo_build_in_order_bmtree( comm, root);//ompi_ data->cached_in_order_bmtree;
 
@@ -66,115 +61,121 @@ Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
     vrank = (rank - root + size) % size;
 
     if (rank == root) {
-       if (0 == root) {
-           /* root on 0, just use the send buffer */
-           ptmp = (char *) sbuf;
-           if (rbuf != MPI_IN_PLACE) {
-               /* local copy to rbuf */
-               err = Datatype::copy(sbuf, scount, sdtype,
-                                     rbuf, rcount, rdtype);
-               if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
-           }
-       } else {
-           /* root is not on 0, allocate temp buffer for send */
-           tempbuf = (char *) smpi_get_tmp_sendbuffer(strue_extent + (scount*size - 1) * sextent);
-           if (NULL == tempbuf) {
-               err = MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
-           }
-
-           ptmp = tempbuf - slb;
-
-           /* and rotate data so they will eventually in the right place */
-           err = Datatype::copy((char *) sbuf + sextent*root*scount, scount*(size-root), sdtype,
-            ptmp, scount*(size-root), sdtype);
-           if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
-
-
-           err = Datatype::copy((char*)sbuf, scount*root, sdtype,
-                                                ptmp + sextent*scount*(size - root), scount*root, sdtype);
-           if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
-
-           if (rbuf != MPI_IN_PLACE) {
-               /* local copy to rbuf */
-               err = Datatype::copy(ptmp, scount, sdtype,
-                                     rbuf, rcount, rdtype);
-               if (MPI_SUCCESS != err) { line = __LINE__; goto err_hndl; }
-           }
-       }
-       total_send = scount;
-    } else if (!(vrank % 2)) {
-       /* non-root, non-leaf nodes, allocate temp buffer for recv
-        * the most we need is rcount*size/2 */
-       tempbuf = (char *) smpi_get_tmp_recvbuffer(rtrue_extent + (rcount*size - 1) * rextent);
-       if (NULL == tempbuf) {
-           err= MPI_ERR_OTHER; line = __LINE__; goto err_hndl;
-       }
-
-       ptmp = tempbuf - rlb;
-
-       sdtype = rdtype;
-       scount = rcount;
-       sextent = rextent;
-       total_send = scount;
+      if (0 == root) {
+        /* root on 0, just use the send buffer */
+        ptmp = (char*)sbuf;
+        if (rbuf != MPI_IN_PLACE) {
+          /* local copy to rbuf */
+          err = Datatype::copy(sbuf, scount, sdtype, rbuf, rcount, rdtype);
+          if (MPI_SUCCESS != err) {
+            line = __LINE__;
+            goto err_hndl;
+          }
+        }
+      } else {
+        /* root is not on 0, allocate temp buffer for send */
+        tempbuf = (char*)smpi_get_tmp_sendbuffer(strue_extent + (scount * size - 1) * sextent);
+        if (NULL == tempbuf) {
+          err  = MPI_ERR_OTHER;
+          line = __LINE__;
+          goto err_hndl;
+        }
+
+        ptmp = tempbuf - slb;
+
+        /* and rotate data so they will eventually in the right place */
+        err = Datatype::copy((char*)sbuf + sextent * root * scount, scount * (size - root), sdtype, ptmp,
+                             scount * (size - root), sdtype);
+        if (MPI_SUCCESS != err) {
+          line = __LINE__;
+          goto err_hndl;
+        }
+
+        err = Datatype::copy((char*)sbuf, scount * root, sdtype, ptmp + sextent * scount * (size - root), scount * root,
+                             sdtype);
+        if (MPI_SUCCESS != err) {
+          line = __LINE__;
+          goto err_hndl;
+        }
+
+        if (rbuf != MPI_IN_PLACE) {
+          /* local copy to rbuf */
+          err = Datatype::copy(ptmp, scount, sdtype, rbuf, rcount, rdtype);
+          if (MPI_SUCCESS != err) {
+            line = __LINE__;
+            goto err_hndl;
+          }
+        }
+      }
+      total_send = scount;
+    } else if (not(vrank % 2)) {
+      /* non-root, non-leaf nodes, allocate temp buffer for recv
+       * the most we need is rcount*size/2 */
+      tempbuf = (char*)smpi_get_tmp_recvbuffer(rtrue_extent + (rcount * size - 1) * rextent);
+      if (NULL == tempbuf) {
+        err  = MPI_ERR_OTHER;
+        line = __LINE__;
+        goto err_hndl;
+      }
+
+      ptmp = tempbuf - rlb;
+
+      sdtype     = rdtype;
+      scount     = rcount;
+      sextent    = rextent;
+      total_send = scount;
     } else {
-       /* leaf nodes, just use rbuf */
-       ptmp = (char *) rbuf;
+      /* leaf nodes, just use rbuf */
+      ptmp = (char*)rbuf;
     }
 
-    if (!(vrank % 2)) {
-       if (rank != root) {
-           /* recv from parent on non-root */
-           Request::recv(ptmp, rcount*size, rdtype, bmtree->tree_prev,
-                                   COLL_TAG_SCATTER, comm, &status);
-           /* local copy to rbuf */
-           Datatype::copy(ptmp, scount, sdtype, rbuf, rcount, rdtype);
-       }
-       /* send to children on all non-leaf */
-       for (i = 0; i < bmtree->tree_nextsize; i++) {
-           int mycount = 0, vkid;
-           /* figure out how much data I have to send to this child */
-           vkid = (bmtree->tree_next[i] - root + size) % size;
-           mycount = vkid - vrank;
-           if (mycount > (size - vkid))
-               mycount = size - vkid;
-           mycount *= scount;
-
-           Request::send(ptmp + total_send*sextent, mycount, sdtype,
-                                   bmtree->tree_next[i],
-                                   COLL_TAG_SCATTER,
-                                    comm);
-
-           total_send += mycount;
-       }
-
+    if (not(vrank % 2)) {
+      if (rank != root) {
+        /* recv from parent on non-root */
+        Request::recv(ptmp, rcount * size, rdtype, bmtree->tree_prev, COLL_TAG_SCATTER, comm, &status);
+        /* local copy to rbuf */
+        Datatype::copy(ptmp, scount, sdtype, rbuf, rcount, rdtype);
+      }
+      /* send to children on all non-leaf */
+      for (i = 0; i < bmtree->tree_nextsize; i++) {
+        int mycount = 0, vkid;
+        /* figure out how much data I have to send to this child */
+        vkid    = (bmtree->tree_next[i] - root + size) % size;
+        mycount = vkid - vrank;
+        if (mycount > (size - vkid))
+          mycount = size - vkid;
+        mycount *= scount;
+
+        Request::send(ptmp + total_send * sextent, mycount, sdtype, bmtree->tree_next[i], COLL_TAG_SCATTER, comm);
+
+        total_send += mycount;
+      }
 
     } else {
-       /* recv from parent on leaf nodes */
-       Request::recv(ptmp, rcount, rdtype, bmtree->tree_prev,
-                               COLL_TAG_SCATTER, comm, &status);
+      /* recv from parent on leaf nodes */
+      Request::recv(ptmp, rcount, rdtype, bmtree->tree_prev, COLL_TAG_SCATTER, comm, &status);
     }
 
     if (NULL != tempbuf)
       smpi_free_tmp_buffer(tempbuf);
-    //!FIXME : store the tree, as done in ompi, instead of calculating it each time ?
-    xbt_free(bmtree);
+    // not FIXME : store the tree, as done in ompi, instead of calculating it each time ?
+    ompi_coll_tuned_topo_destroy_tree(&bmtree);
 
     return MPI_SUCCESS;
 
  err_hndl:
     if (NULL != tempbuf)
-       free(tempbuf);
+      smpi_free_tmp_buffer(tempbuf);
 
-    XBT_DEBUG(  "%s:%4d\tError occurred %d, rank %2d",
-                __FILE__, line, err, rank);
+    XBT_DEBUG("%s:%4d\tError occurred %d, rank %2d", __FILE__, line, err, rank);
     return err;
 }
 
 /*
  * Linear functions are copied from the BASIC coll module
  * they do not segment the message and are simple implementations
- * but for some small number of nodes and/or small data sizes they 
- * are just as fast as tuned/tree based segmenting operations 
+ * but for some small number of nodes and/or small data sizes they
+ * are just as fast as tuned/tree based segmenting operations
  * and as such may be selected by the decision functions
  * These are copied into this module due to the way we select modules
  * in V1. i.e. in V2 we will handle this differently and so will not
@@ -184,20 +185,14 @@ Coll_scatter_ompi_binomial::scatter(void *sbuf, int scount,
 
 /* copied function (with appropriate renaming) starts here */
 /*
- *     scatter_intra
+ *  scatter_intra
  *
- *     Function:       - basic scatter operation
- *     Accepts:        - same arguments as MPI_Scatter()
- *     Returns:        - MPI_SUCCESS or error code
+ *  Function:  - basic scatter operation
+ *  Accepts:  - same arguments as MPI_Scatter()
+ *  Returns:  - MPI_SUCCESS or error code
  */
-int
-Coll_scatter_ompi_basic_linear::scatter(void *sbuf, int scount,
-                                          MPI_Datatype sdtype,
-                                          void *rbuf, int rcount,
-                                          MPI_Datatype rdtype,
-                                          int root,
-                                          MPI_Comm comm
-                                          )
+int Coll_scatter_ompi_basic_linear::scatter(void* sbuf, int scount, MPI_Datatype sdtype, void* rbuf, int rcount,
+                                            MPI_Datatype rdtype, int root, MPI_Comm comm)
 {
     int i, rank, size, err;
     char *ptmp;
@@ -249,3 +244,6 @@ Coll_scatter_ompi_basic_linear::scatter(void *sbuf, int scount,
 
     return MPI_SUCCESS;
 }
+
+}
+}