Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
modernize some SIMIX functions
authorSUTER Frederic <frederic.suter@cc.in2p3.fr>
Wed, 6 Oct 2021 07:31:15 +0000 (09:31 +0200)
committerSUTER Frederic <frederic.suter@cc.in2p3.fr>
Wed, 6 Oct 2021 07:57:34 +0000 (09:57 +0200)
12 files changed:
include/simgrid/simix.h
src/kernel/EngineImpl.cpp
src/kernel/actor/ActorImpl.cpp
src/kernel/context/Context.cpp
src/kernel/context/Context.hpp
src/kernel/context/ContextSwapped.cpp
src/kernel/context/ContextThread.cpp
src/kernel/context/ContextUnix.cpp
src/simgrid/sg_config.cpp
src/simix/smx_context.cpp
teshsuite/xbt/parmap_bench/parmap_bench.cpp
teshsuite/xbt/parmap_test/parmap_test.cpp

index b8cdb77..79b1e1a 100644 (file)
 #endif
 
 /******************************* Networking ***********************************/
-extern unsigned smx_context_stack_size;
-extern unsigned smx_context_guard_size;
-
 SG_BEGIN_DECL
 
 XBT_ATTRIB_DEPRECATED_v331("Please use sg_actor_by_pid() instead.") XBT_PUBLIC smx_actor_t
     SIMIX_process_from_PID(aid_t pid);
 
 /* parallelism */
-XBT_PUBLIC int SIMIX_context_is_parallel();
-XBT_PUBLIC int SIMIX_context_get_nthreads();
-XBT_PUBLIC void SIMIX_context_set_nthreads(int nb_threads);
-XBT_PUBLIC e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode();
-XBT_PUBLIC void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::is_parallel()") XBT_PUBLIC int SIMIX_context_is_parallel();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::get_nthreads()") XBT_PUBLIC int SIMIX_context_get_nthreads();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::set_nthreads()") XBT_PUBLIC
+    void SIMIX_context_set_nthreads(int nb_threads);
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::get_parallel_mode()") XBT_PUBLIC e_xbt_parmap_mode_t
+    SIMIX_context_get_parallel_mode();
+XBT_ATTRIB_DEPRECATED_v333("Please use kernel::context::set_parallel_mode()") XBT_PUBLIC
+    void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
 XBT_PUBLIC int SIMIX_is_maestro();
 
 /********************************** Global ************************************/
index bcb3562..29f737a 100644 (file)
@@ -89,7 +89,7 @@ static void segvhandler(int signum, siginfo_t* siginfo, void* /*context*/)
             "If you think you've found a bug in SimGrid, please report it along with a\n"
             "Minimal Working Example (MWE) reproducing your problem and a full backtrace\n"
             "of the fault captured with gdb or valgrind.\n",
-            smx_context_stack_size / 1024);
+            simgrid::kernel::context::stack_size / 1024);
   } else if (siginfo->si_signo == SIGSEGV) {
     fprintf(stderr, "Segmentation fault.\n");
 #if HAVE_SMPI
index 801eb53..5c1d62e 100644 (file)
@@ -60,7 +60,7 @@ ActorImpl::ActorImpl(xbt::string name, s4u::Host* host) : host_(host), name_(std
 {
   pid_            = maxpid++;
   simcall_.issuer_ = this;
-  stacksize_      = smx_context_stack_size;
+  stacksize_       = context::stack_size;
 }
 
 ActorImpl::~ActorImpl()
index ae03b1b..afacbd3 100644 (file)
@@ -19,6 +19,61 @@ namespace kernel {
 namespace context {
 
 ContextFactoryInitializer factory_initializer = nullptr;
+static e_xbt_parmap_mode_t parallel_synchronization_mode = XBT_PARMAP_DEFAULT;
+static int parallel_contexts                             = 1;
+unsigned stack_size;
+unsigned guard_size;
+
+/** @brief Returns whether some parallel threads are used for the user contexts. */
+int is_parallel()
+{
+  return parallel_contexts > 1;
+}
+
+/**
+ * @brief Returns the number of parallel threads used for the user contexts.
+ * @return the number of threads (1 means no parallelism)
+ */
+int get_nthreads()
+{
+  return parallel_contexts;
+}
+
+/**
+ * @brief Sets the number of parallel threads to use  for the user contexts.
+ *
+ * This function should be called before initializing SIMIX.
+ * A value of 1 means no parallelism (1 thread only).
+ * If the value is greater than 1, the thread support must be enabled.
+ *
+ * @param nb_threads the number of threads to use
+ */
+void set_nthreads(int nb_threads)
+{
+  if (nb_threads <= 0) {
+    nb_threads = std::thread::hardware_concurrency();
+    XBT_INFO("Auto-setting contexts/nthreads to %d", nb_threads);
+  }
+  parallel_contexts = nb_threads;
+}
+
+/**
+ * @brief Sets the synchronization mode to use when actors are run in parallel.
+ * @param mode how to synchronize threads if actors are run in parallel
+ */
+void set_parallel_mode(e_xbt_parmap_mode_t mode)
+{
+  parallel_synchronization_mode = mode;
+}
+
+/**
+ * @brief Returns the synchronization mode used when actors are run in parallel.
+ * @return how threads are synchronized if actors are run in parallel
+ */
+e_xbt_parmap_mode_t get_parallel_mode()
+{
+  return parallel_synchronization_mode;
+}
 
 ContextFactory::~ContextFactory() = default;
 
index 30dd591..bd3e09a 100644 (file)
@@ -6,7 +6,9 @@
 #ifndef SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
 #define SIMGRID_KERNEL_CONTEXT_CONTEXT_HPP
 
-#include "simgrid/forward.h"
+#include <simgrid/forward.h>
+#include <xbt/parmap.h>
+
 #include "src/kernel/activity/ActivityImpl.hpp"
 
 #include <csignal>
@@ -15,6 +17,8 @@
 namespace simgrid {
 namespace kernel {
 namespace context {
+extern unsigned stack_size;
+extern unsigned guard_size;
 
 class XBT_PUBLIC ContextFactory {
 public:
@@ -103,6 +107,11 @@ XBT_PRIVATE ContextFactory* sysv_factory();
 XBT_PRIVATE ContextFactory* raw_factory();
 XBT_PRIVATE ContextFactory* boost_factory();
 
+XBT_PUBLIC int is_parallel();
+XBT_PUBLIC int get_nthreads();
+XBT_PUBLIC void set_nthreads(int nb_threads);
+XBT_PUBLIC void set_parallel_mode(e_xbt_parmap_mode_t mode);
+XBT_PUBLIC e_xbt_parmap_mode_t get_parallel_mode();
 } // namespace context
 } // namespace kernel
 } // namespace simgrid
index 5716b0c..dee917b 100644 (file)
@@ -74,12 +74,12 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
     : Context(std::move(code), actor), factory_(*factory)
 {
   // Save maestro (=first created context) in preparation for run_all
-  if (not SIMIX_context_is_parallel() && factory_.maestro_context_ == nullptr)
+  if (not is_parallel() && factory_.maestro_context_ == nullptr)
     factory_.maestro_context_ = this;
 
   if (has_code()) {
     xbt_assert((actor->get_stacksize() & 0xf) == 0, "Actor stack size should be multiple of 16");
-    if (smx_context_guard_size > 0 && not MC_is_active()) {
+    if (guard_size > 0 && not MC_is_active()) {
 #if PTH_STACKGROWTH != -1
       xbt_die(
           "Stack overflow protection is known to be broken on your system: you stacks grow upwards (or detection is "
@@ -89,7 +89,7 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
        * Protected pages need to be put after the stack when PTH_STACKGROWTH == 1. */
 #endif
 
-      size_t size = actor->get_stacksize() + smx_context_guard_size;
+      size_t size = actor->get_stacksize() + guard_size;
 #if SIMGRID_HAVE_MC
       /* Cannot use posix_memalign when SIMGRID_HAVE_MC. Align stack by hand, and save the
        * pointer returned by xbt_malloc0. */
@@ -107,7 +107,7 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
 #ifndef _WIN32
       /* This is fatal. We are going to fail at some point when we try reusing this. */
       xbt_assert(
-          mprotect(this->stack_, smx_context_guard_size, PROT_NONE) != -1,
+          mprotect(this->stack_, guard_size, PROT_NONE) != -1,
           "Failed to protect stack: %s.\n"
           "If you are running a lot of actors, you may be exceeding the amount of mappings allowed per process.\n"
           "On Linux systems, change this value with sudo sysctl -w vm.max_map_count=newvalue (default value: 65536)\n"
@@ -115,7 +115,7 @@ SwappedContext::SwappedContext(std::function<void()>&& code, smx_actor_t actor,
           "for more information.",
           strerror(errno));
 #endif
-      this->stack_ = this->stack_ + smx_context_guard_size;
+      this->stack_ = this->stack_ + guard_size;
     } else {
       this->stack_ = static_cast<unsigned char*>(xbt_malloc0(actor->get_stacksize()));
     }
@@ -152,9 +152,9 @@ SwappedContext::~SwappedContext()
 #endif
 
 #ifndef _WIN32
-  if (smx_context_guard_size > 0 && not MC_is_active()) {
-    stack_ = stack_ - smx_context_guard_size;
-    if (mprotect(stack_, smx_context_guard_size, PROT_READ | PROT_WRITE) == -1) {
+  if (guard_size > 0 && not MC_is_active()) {
+    stack_ = stack_ - guard_size;
+    if (mprotect(stack_, guard_size, PROT_READ | PROT_WRITE) == -1) {
       XBT_WARN("Failed to remove page protection: %s", strerror(errno));
       /* try to pursue anyway */
     }
@@ -211,11 +211,10 @@ void SwappedContextFactory::run_all()
    * stuff It is much easier to understand what happens if you see the working threads as bodies that swap their soul
    * for the ones of the simulated processes that must run.
    */
-  if (SIMIX_context_is_parallel()) {
+  if (is_parallel()) {
     // We lazily create the parmap so that all options are actually processed when doing so.
     if (parmap_ == nullptr)
-      parmap_ = std::make_unique<simgrid::xbt::Parmap<smx_actor_t>>(SIMIX_context_get_nthreads(),
-                                                                    SIMIX_context_get_parallel_mode());
+      parmap_ = std::make_unique<simgrid::xbt::Parmap<smx_actor_t>>(get_nthreads(), get_parallel_mode());
 
     // Usually, Parmap::apply() executes the provided function on all elements of the array.
     // Here, the executed function does not return the control to the parmap before all the array is processed:
@@ -250,7 +249,7 @@ void SwappedContextFactory::run_all()
 void SwappedContext::resume()
 {
   auto* old = static_cast<SwappedContext*>(self());
-  if (SIMIX_context_is_parallel()) {
+  if (is_parallel()) {
     // Save my current soul (either maestro, or one of the minions) in a thread-specific area
     worker_context_ = old;
   }
@@ -272,7 +271,7 @@ void SwappedContext::resume()
 void SwappedContext::suspend()
 {
   SwappedContext* next_context;
-  if (SIMIX_context_is_parallel()) {
+  if (is_parallel()) {
     // Get some more work to directly swap into the next executable actor instead of yielding back to the parmap
     boost::optional<smx_actor_t> next_work = factory_.parmap_->next();
     if (next_work) {
index 7cb10dd..2b8c1d1 100644 (file)
@@ -26,21 +26,21 @@ namespace context {
 
 ThreadContextFactory::ThreadContextFactory() : ContextFactory()
 {
-  if (smx_context_stack_size != 8 * 1024 * 1024)
+  if (stack_size != 8 * 1024 * 1024)
     XBT_INFO("Stack size modifications are ignored by thread factory.");
-  if (SIMIX_context_is_parallel())
+  if (is_parallel())
     ParallelThreadContext::initialize();
 }
 
 ThreadContextFactory::~ThreadContextFactory()
 {
-  if (SIMIX_context_is_parallel())
+  if (is_parallel())
     ParallelThreadContext::finalize();
 }
 
 ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code, actor::ActorImpl* actor, bool maestro)
 {
-  if (SIMIX_context_is_parallel())
+  if (is_parallel())
     return this->new_context<ParallelThreadContext>(std::move(code), actor, maestro);
   else
     return this->new_context<SerialThreadContext>(std::move(code), actor, maestro);
@@ -48,7 +48,7 @@ ThreadContext* ThreadContextFactory::create_context(std::function<void()>&& code
 
 void ThreadContextFactory::run_all()
 {
-  if (SIMIX_context_is_parallel()) {
+  if (is_parallel()) {
     // Parallel execution
     ParallelThreadContext::run_all();
   } else {
@@ -192,7 +192,7 @@ xbt::OsSemaphore* ParallelThreadContext::thread_sem_ = nullptr;
 
 void ParallelThreadContext::initialize()
 {
-  thread_sem_ = new xbt::OsSemaphore(SIMIX_context_get_nthreads());
+  thread_sem_ = new xbt::OsSemaphore(get_nthreads());
 }
 
 void ParallelThreadContext::finalize()
index 2e0394f..0d29b5a 100644 (file)
@@ -65,7 +65,7 @@ UContext::UContext(std::function<void()>&& code, actor::ActorImpl* actor, Swappe
 
 #if SIMGRID_HAVE_MC
     if (MC_is_active()) {
-      MC_register_stack_area(get_stack(), &(this->uc_), smx_context_stack_size);
+      MC_register_stack_area(get_stack(), &(this->uc_), stack_size);
     }
 #endif
   }
index 43d486f..96121b0 100644 (file)
@@ -12,6 +12,7 @@
 #include "simgrid/sg_config.hpp"
 #include "src/instr/instr_private.hpp"
 #include "src/internal_config.h"
+#include "src/kernel/context/Context.hpp"
 #include "src/kernel/lmm/maxmin.hpp"
 #include "src/mc/mc_config.hpp"
 #include "src/mc/mc_replay.hpp"
@@ -201,11 +202,11 @@ static void _sg_cfg_cb__network_model(const std::string& value)
 static void _sg_cfg_cb_contexts_parallel_mode(const std::string& mode_name)
 {
   if (mode_name == "posix") {
-    SIMIX_context_set_parallel_mode(XBT_PARMAP_POSIX);
+    simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_POSIX);
   } else if (mode_name == "futex") {
-    SIMIX_context_set_parallel_mode(XBT_PARMAP_FUTEX);
+    simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_FUTEX);
   } else if (mode_name == "busy_wait") {
-    SIMIX_context_set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
+    simgrid::kernel::context::set_parallel_mode(XBT_PARMAP_BUSY_WAIT);
   } else {
     xbt_die("Command line setting of the parallel synchronization mode should "
             "be one of \"posix\", \"futex\" or \"busy_wait\"");
@@ -308,7 +309,7 @@ void sg_config_init(int *argc, char **argv)
                                       "no");
 
   simgrid::config::declare_flag<int>("contexts/stack-size", "Stack size of contexts in KiB (not with threads)",
-                                     8 * 1024, [](int value) { smx_context_stack_size = value * 1024; });
+                                     8 * 1024, [](int value) { simgrid::kernel::context::stack_size = value * 1024; });
 
   /* guard size for contexts stacks in memory pages */
 #if defined(_WIN32) || (PTH_STACKGROWTH != -1)
@@ -318,9 +319,9 @@ void sg_config_init(int *argc, char **argv)
 #endif
   simgrid::config::declare_flag<int>("contexts/guard-size", "Guard size for contexts stacks in memory pages",
                                      default_guard_size,
-                                     [](int value) { smx_context_guard_size = value * xbt_pagesize; });
+                                     [](int value) { simgrid::kernel::context::guard_size = value * xbt_pagesize; });
   simgrid::config::declare_flag<int>("contexts/nthreads", "Number of parallel threads used to execute user contexts", 1,
-                                     &SIMIX_context_set_nthreads);
+                                     &simgrid::kernel::context::set_nthreads);
 
   /* synchronization mode for parallel user contexts */
 #if HAVE_FUTEX_H
@@ -360,7 +361,7 @@ void sg_config_init(int *argc, char **argv)
 
   sg_config_cmd_line(argc, argv);
 
-  xbt_mallocator_initialization_is_done(SIMIX_context_is_parallel());
+  xbt_mallocator_initialization_is_done(simgrid::kernel::context::is_parallel());
 }
 
 void sg_config_finalize()
index e7968b8..74f44f0 100644 (file)
@@ -48,58 +48,29 @@ static simgrid::config::Flag<std::string>
     context_factory_name("contexts/factory", (std::string("Possible values: ") + contexts_list()).c_str(),
                          context_factories.begin()->first);
 
-unsigned smx_context_stack_size;
-unsigned smx_context_guard_size;
-static int smx_parallel_contexts = 1;
-static e_xbt_parmap_mode_t smx_parallel_synchronization_mode = XBT_PARMAP_DEFAULT;
-
-/** @brief Returns whether some parallel threads are used for the user contexts. */
-int SIMIX_context_is_parallel() {
-  return smx_parallel_contexts > 1;
+int SIMIX_context_is_parallel() // XBT_ATTRIB_DEPRECATED_v333
+{
+  return simgrid::kernel::context::is_parallel();
 }
 
-/**
- * @brief Returns the number of parallel threads used for the user contexts.
- * @return the number of threads (1 means no parallelism)
- */
-int SIMIX_context_get_nthreads() {
-  return smx_parallel_contexts;
+int SIMIX_context_get_nthreads() // XBT_ATTRIB_DEPRECATED_v333
+{
+  return simgrid::kernel::context::get_nthreads();
 }
 
-/**
- * @brief Sets the number of parallel threads to use
- * for the user contexts.
- *
- * This function should be called before initializing SIMIX.
- * A value of 1 means no parallelism (1 thread only).
- * If the value is greater than 1, the thread support must be enabled.
- *
- * @param nb_threads the number of threads to use
- */
-void SIMIX_context_set_nthreads(int nb_threads) {
-  if (nb_threads<=0) {
-    nb_threads = std::thread::hardware_concurrency();
-    XBT_INFO("Auto-setting contexts/nthreads to %d", nb_threads);
-  }
-  smx_parallel_contexts = nb_threads;
+void SIMIX_context_set_nthreads(int nb_threads) // XBT_ATTRIB_DEPRECATED_v333
+{
+  simgrid::kernel::context::set_nthreads(nb_threads);
 }
 
-/**
- * @brief Returns the synchronization mode used when processes are run in
- * parallel.
- * @return how threads are synchronized if processes are run in parallel
- */
-e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode() {
-  return smx_parallel_synchronization_mode;
+e_xbt_parmap_mode_t SIMIX_context_get_parallel_mode() // XBT_ATTRIB_DEPRECATED_v333
+{
+  return simgrid::kernel::context::get_parallel_mode();
 }
 
-/**
- * @brief Sets the synchronization mode to use when processes are run in
- * parallel.
- * @param mode how to synchronize threads if processes are run in parallel
- */
-void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode) {
-  smx_parallel_synchronization_mode = mode;
+void SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode) // XBT_ATTRIB_DEPRECATED_v333
+{
+  simgrid::kernel::context::set_parallel_mode(mode);
 }
 
 namespace simgrid {
index 8a5dde4..ef2a1b2 100644 (file)
@@ -133,7 +133,7 @@ int main(int argc, char* argv[])
   XBT_INFO("Parmap benchmark with %d workers (modes = %#x)...", nthreads, modes);
   XBT_INFO("%s", "");
 
-  SIMIX_context_set_nthreads(nthreads);
+  simgrid::kernel::context::set_nthreads(nthreads);
 
   XBT_INFO("Benchmark for parmap create+apply+destroy (small comp):");
   bench_all_modes(nthreads, timeout, modes, true, &fun_small_comp);
index a16394d..706284e 100644 (file)
@@ -88,7 +88,7 @@ int main(int argc, char** argv)
   int status = 0;
   xbt_log_control_set("parmap_test.fmt:[%c/%p]%e%m%n");
   simgrid::s4u::Engine e(&argc, argv);
-  SIMIX_context_set_nthreads(16); // dummy value > 1
+  simgrid::kernel::context::set_nthreads(16); // dummy value > 1
 
   XBT_INFO("Basic testing posix");
   status += test_parmap_basic(XBT_PARMAP_POSIX);