Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add option "smpi/auto-shared-malloc-thresh"
authorAugustin Degomme <adegomme@gmail.com>
Tue, 19 Nov 2019 12:56:26 +0000 (13:56 +0100)
committerAugustin Degomme <adegomme@gmail.com>
Tue, 19 Nov 2019 14:35:26 +0000 (15:35 +0100)
This applies smpi_shared_malloc automatically to all allocations above a certain size.
Test based on the sample-shared one: malloc >= 8bytes is indeed shared, without using macros.

todo: calloc and co.

include/smpi/sampi.h
include/smpi/smpi.h
include/smpi/smpi_helpers.h
include/smpi/smpi_helpers_internal.h
src/simgrid/sg_config.cpp
src/smpi/internals/smpi_shared.cpp
teshsuite/smpi/CMakeLists.txt
teshsuite/smpi/auto-shared/auto-shared.c [new file with mode: 0644]
teshsuite/smpi/auto-shared/auto-shared.tesh [new file with mode: 0644]

index b51f8ee..5e011d9 100644 (file)
@@ -6,6 +6,7 @@
 #ifndef SAMPI_H_
 #define SAMPI_H_
 
+#define SAMPI_OVERRIDEN_MALLOC
 #include <stdlib.h>
 #include <smpi/smpi.h>
 
@@ -14,6 +15,8 @@
   type _XBT_CONCAT(AP, name) args;
 
 #ifndef HAVE_SMPI
+#undef malloc
+#undef free
 // Internally disable these overrides (HAVE_SMPI is only defined when building the library)
 #define malloc(nbytes) _sampi_malloc(nbytes)
 #define calloc(n_elm, elm_size) _sampi_calloc((n_elm), (elm_size))
index cae3e1f..3407cc7 100644 (file)
@@ -1025,14 +1025,13 @@ XBT_PUBLIC void smpi_trace_set_call_location__(const char* file, int* line);
   SMPI_SAMPLE_LOOP(loop_init, (loop_end), (loop_iter), 1, (iters), (thres))
 #define SMPI_SAMPLE_DELAY(duration) for(smpi_execute(duration); 0; )
 #define SMPI_SAMPLE_FLOPS(flops) for(smpi_execute_flops(flops); 0; )
-
 XBT_PUBLIC void* smpi_shared_malloc(size_t size, const char* file, int line);
 #define SMPI_SHARED_MALLOC(size) smpi_shared_malloc((size), __FILE__, __LINE__)
 XBT_PUBLIC void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int nb_shared_blocks);
 #define SMPI_PARTIAL_SHARED_MALLOC(size, shared_block_offsets, nb_shared_blocks)                                       \
   smpi_shared_malloc_partial((size), (shared_block_offsets), (nb_shared_blocks))
 
-XBT_PUBLIC void smpi_shared_free(void* data);
+
 #define SMPI_SHARED_FREE(data) smpi_shared_free(data)
 
 XBT_PUBLIC int smpi_shared_known_call(const char* func, const char* input);
index b1a12da..55efcf8 100644 (file)
@@ -32,5 +32,8 @@
 #define getopt(x, y, z) smpi_getopt((x), (y), (z))
 #define getopt_long(x, y, z, a, b) smpi_getopt_long((x), (y), (z), (a), (b))
 #define getopt_long_only(x, y, z, a, b) smpi_getopt_long_only((x), (y), (z), (a), (b))
-
+#ifndef SAMPI_OVERRIDEN_MALLOC
+#define malloc(x) smpi_shared_malloc_intercept(x, __FILE__, __LINE__)
+#define free(x) smpi_shared_free(x)
+#endif
 #endif
index 57490ce..59aa0df 100644 (file)
@@ -34,6 +34,8 @@ int smpi_getopt_long(int argc, char* const* argv, const char* options, const str
                      int* opt_index);
 int smpi_getopt(int argc, char* const* argv, const char* options);
 
+void* smpi_shared_malloc_intercept(size_t size, const char* file, int line);
+void smpi_shared_free(void* data);
 #ifdef __cplusplus
 } // extern "C"
 #endif
index e736195..2b8cdc2 100644 (file)
@@ -392,6 +392,10 @@ void sg_config_init(int *argc, char **argv)
   simgrid::config::declare_flag<double>("smpi/shared-malloc-blocksize",
                                         "Size of the bogus file which will be created for global shared allocations",
                                         1UL << 20);
+  simgrid::config::declare_flag<double>("smpi/auto-shared-malloc-thresh",
+                                        "Threshold size for the automatic sharing of memory",
+                                        0);
+
   simgrid::config::declare_flag<std::string>("smpi/shared-malloc-hugepage",
                                              "Path to a mounted hugetlbfs, to use huge pages with shared malloc.", "");
 
index defbbba..2fb5a57 100644 (file)
@@ -342,6 +342,14 @@ void* smpi_shared_malloc_partial(size_t size, size_t* shared_block_offsets, int
   return mem;
 }
 
+
+void *smpi_shared_malloc_intercept(size_t size, const char *file, int line) {
+  if( simgrid::config::get_value<double>("smpi/auto-shared-malloc-thresh") == 0 || size < simgrid::config::get_value<double>("smpi/auto-shared-malloc-thresh"))
+    return ::operator new(size);
+  else
+    return smpi_shared_malloc(size, file, line);
+}
+
 void *smpi_shared_malloc(size_t size, const char *file, int line) {
   if (size > 0 && smpi_cfg_shared_malloc == SharedMallocType::LOCAL) {
     return smpi_shared_malloc_local(size, file, line);
@@ -427,7 +435,10 @@ void smpi_shared_free(void *ptr)
     snprintf(loc, PTR_STRLEN, "%p", ptr);
     auto meta = allocs_metadata.find(ptr);
     if (meta == allocs_metadata.end()) {
-      XBT_WARN("Cannot free: %p was not shared-allocated by SMPI - maybe its size was 0?", ptr);
+      if (simgrid::config::get_value<double>("smpi/auto_shared_malloc_thresh") > 0)//this free belongs to a malloc under the threshold.
+        ::operator delete(ptr);
+      else
+        XBT_WARN("Cannot free: %p was not shared-allocated by SMPI - maybe its size was 0?", ptr);
       return;
     }
     shared_data_t* data = &meta->second.data->second;
index 178c155..d71b18f 100644 (file)
@@ -17,7 +17,7 @@ if(enable_smpi)
   endforeach()
 
   if(NOT WIN32)
-    foreach(x macro-shared macro-partial-shared macro-partial-shared-communication )
+    foreach(x macro-shared auto-shared macro-partial-shared macro-partial-shared-communication )
       add_executable       (${x}  EXCLUDE_FROM_ALL ${x}/${x}.c)
       target_link_libraries(${x}  simgrid)
       set_target_properties(${x}  PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${x})
@@ -38,7 +38,7 @@ endif()
 foreach(x coll-allgather coll-allgatherv coll-allreduce coll-alltoall coll-alltoallv coll-barrier coll-bcast
     coll-gather coll-reduce coll-reduce-scatter coll-scatter macro-sample pt2pt-dsend pt2pt-pingpong
     type-hvector type-indexed type-struct type-vector bug-17132 gh-139 timers privatization
-    macro-shared macro-partial-shared macro-partial-shared-communication
+    macro-shared auto-shared macro-partial-shared macro-partial-shared-communication
     io-simple io-simple-at io-all io-all-at io-shared io-ordered)
   set(tesh_files    ${tesh_files}    ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.tesh)
   set(teshsuite_src ${teshsuite_src} ${CMAKE_CURRENT_SOURCE_DIR}/${x}/${x}.c)
@@ -62,6 +62,7 @@ set(bin_files       ${bin_files}    ${CMAKE_CURRENT_SOURCE_DIR}/hostfile
 if(enable_smpi)
   if(NOT WIN32)
     ADD_TESH_FACTORIES(tesh-smpi-macro-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-shared macro-shared.tesh)
+    ADD_TESH_FACTORIES(tesh-smpi-auto-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/auto-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/auto-shared auto-shared.tesh)
     ADD_TESH_FACTORIES(tesh-smpi-macro-partial-shared "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-partial-shared --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-partial-shared macro-partial-shared.tesh)
     ADD_TESH_FACTORIES(tesh-smpi-macro-partial-shared-communication "thread;ucontext;raw;boost" --setenv platfdir=${CMAKE_HOME_DIRECTORY}/examples/platforms --setenv bindir=${CMAKE_BINARY_DIR}/teshsuite/smpi/macro-partial-shared-communication --cd ${CMAKE_HOME_DIRECTORY}/teshsuite/smpi/macro-partial-shared-communication macro-partial-shared-communication.tesh)
   endif()
diff --git a/teshsuite/smpi/auto-shared/auto-shared.c b/teshsuite/smpi/auto-shared/auto-shared.c
new file mode 100644 (file)
index 0000000..8966cc8
--- /dev/null
@@ -0,0 +1,61 @@
+/* Copyright (c) 2009-2019. 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. */
+
+/* This example should be instructive to learn about SMPI_SHARED_CALL */
+
+#include <stdio.h>
+#include <mpi.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+static void* hash(const char *str, uint64_t* ans)
+{
+  const char *tohash = str;
+  *ans=5381;
+  printf("hashing !\n");
+  int c = *tohash;
+  while (c != 0) {
+    *ans = ((*ans << 5) + *ans) + c; /* hash * 33 + c */
+    tohash++;
+    c = *tohash;
+  }
+  return NULL;
+}
+
+int main(int argc, char *argv[])
+{
+  MPI_Init(&argc, &argv);
+  int rank;
+  int size;
+  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+  MPI_Comm_size(MPI_COMM_WORLD, &size);
+  //Let's Allocate a memory buffer
+  uint64_t* buf = malloc(sizeof(uint64_t));
+  //one writes data in it
+  if(rank==0){
+    *buf=size;
+  }
+
+  MPI_Barrier(MPI_COMM_WORLD);
+  //everyone reads from it.
+  printf("[%d] The value in the shared buffer is: %" PRIu64"\n", rank, *buf);
+
+  MPI_Barrier(MPI_COMM_WORLD);
+  //Try SMPI_SHARED_CALL function, which should call hash only once and for all.
+  static const char str[] = "onceandforall";
+  if(rank==size-1){
+    SMPI_SHARED_CALL(hash,str,str,buf);
+  }
+
+  MPI_Barrier(MPI_COMM_WORLD);
+
+  printf("[%d] After change, the value in the shared buffer is: %" PRIu64"\n", rank, *buf);
+
+  free(buf);
+
+  MPI_Finalize();
+  return 0;
+}
diff --git a/teshsuite/smpi/auto-shared/auto-shared.tesh b/teshsuite/smpi/auto-shared/auto-shared.tesh
new file mode 100644 (file)
index 0000000..cfadf9f
--- /dev/null
@@ -0,0 +1,28 @@
+p Test compute
+! output sort
+! timeout 5
+$ ${bindir:=.}/../../../smpi_script/bin/smpirun -hostfile ../hostfile -platform ../../../examples/platforms/small_platform.xml -np 4 ${bindir:=.}/auto-shared --log=smpi_kernel.thres:warning --log=xbt_cfg.thres:warning --cfg=smpi/auto-shared-malloc-thresh:8
+> [0] After change, the value in the shared buffer is: 16053117601147974045
+> [0] The value in the shared buffer is: 4
+> [1] After change, the value in the shared buffer is: 16053117601147974045
+> [1] The value in the shared buffer is: 4
+> [2] After change, the value in the shared buffer is: 16053117601147974045
+> [2] The value in the shared buffer is: 4
+> [3] After change, the value in the shared buffer is: 16053117601147974045
+> [3] The value in the shared buffer is: 4
+> hashing !
+
+! output sort
+! timeout 5
+$ ${bindir:=.}/../../../smpi_script/bin/smpirun -hostfile ../hostfile -platform ../../../examples/platforms/small_platform.xml -np 4 ${bindir:=.}/auto-shared --log=smpi_kernel.thres:warning --log=xbt_cfg.thres:warning --cfg=smpi/shared-malloc:local --cfg=smpi/auto-shared-malloc-thresh:8
+> [0] After change, the value in the shared buffer is: 16053117601147974045
+> [0] The value in the shared buffer is: 4
+> [1] After change, the value in the shared buffer is: 16053117601147974045
+> [1] The value in the shared buffer is: 4
+> [2] After change, the value in the shared buffer is: 16053117601147974045
+> [2] The value in the shared buffer is: 4
+> [3] After change, the value in the shared buffer is: 16053117601147974045
+> [3] The value in the shared buffer is: 4
+> hashing !
+
+