Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
don't malloc tons of dynars in mpi_waitany
authorMartin Quinson <martin.quinson@loria.fr>
Wed, 1 Feb 2017 01:10:12 +0000 (02:10 +0100)
committerMartin Quinson <martin.quinson@loria.fr>
Wed, 1 Feb 2017 01:10:12 +0000 (02:10 +0100)
include/xbt/dynar.h
src/smpi/smpi_base.cpp
src/xbt/dynar.cpp

index ef7a555..d9796b5 100644 (file)
@@ -66,6 +66,7 @@ SG_BEGIN_DECL()
 typedef struct xbt_dynar_s *xbt_dynar_t;
 
 XBT_PUBLIC(xbt_dynar_t) xbt_dynar_new(const unsigned long elm_size, void_f_pvoid_t const free_f);
+XBT_PUBLIC(void) xbt_dynar_init(xbt_dynar_t dynar, const unsigned long elmsize, void_f_pvoid_t const free_f);
 XBT_PUBLIC(void) xbt_dynar_free(xbt_dynar_t * dynar);
 XBT_PUBLIC(void) xbt_dynar_free_voidp(void *dynar);
 XBT_PUBLIC(void) xbt_dynar_free_container(xbt_dynar_t * dynar);
index 24f5645..9bae432 100644 (file)
@@ -837,7 +837,7 @@ void smpi_mpi_wait(MPI_Request * request, MPI_Status * status)
 
 int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
 {
-  xbt_dynar_t comms;
+  s_xbt_dynar_t comms; // Keep it on stack to save some extra mallocs
   int i;
   int size = 0;
   int index = MPI_UNDEFINED;
@@ -845,14 +845,14 @@ int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
 
   if(count > 0) {
     // Wait for a request to complete
-    comms = xbt_dynar_new(sizeof(smx_activity_t), nullptr);
+    xbt_dynar_init(&comms, sizeof(smx_activity_t), nullptr);
     map = xbt_new(int, count);
     XBT_DEBUG("Wait for one of %d", count);
     for(i = 0; i < count; i++) {
       if (requests[i] != MPI_REQUEST_NULL && !(requests[i]->flags & PREPARED) && !(requests[i]->flags & FINISHED)) {
         if (requests[i]->action != nullptr) {
           XBT_DEBUG("Waiting any %p ", requests[i]);
-          xbt_dynar_push(comms, &requests[i]->action);
+          xbt_dynar_push(&comms, &requests[i]->action);
           map[size] = i;
           size++;
         }else{
@@ -867,7 +867,7 @@ int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
       }
     }
     if(size > 0) {
-      i = simcall_comm_waitany(comms, -1);
+      i = simcall_comm_waitany(&comms, -1);
 
       // not MPI_UNDEFINED, as this is a simix return code
       if (i != -1) {
@@ -878,7 +878,6 @@ int smpi_mpi_waitany(int count, MPI_Request requests[], MPI_Status * status)
       }
     }
     xbt_free(map);
-    xbt_dynar_free(&comms);
   }
 
   if (index==MPI_UNDEFINED)
index 44ce025..375b1c5 100644 (file)
@@ -102,6 +102,18 @@ xbt_dynar_t xbt_dynar_new(const unsigned long elmsize, void_f_pvoid_t const free
   return dynar;
 }
 
+/** @brief Initialize a dynar structure that was not malloc'ed
+ * This can be useful to keep temporary dynars on the stack
+ */
+void xbt_dynar_init(xbt_dynar_t dynar, const unsigned long elmsize, void_f_pvoid_t const free_f)
+{
+  dynar->size    = 0;
+  dynar->used    = 0;
+  dynar->elmsize = elmsize;
+  dynar->data    = nullptr;
+  dynar->free_f  = free_f;
+}
+
 /** @brief Destructor of the structure not touching to the content
  *
  * \param dynar poor victim