From 87c589b94e563580e8e0585079cdba97914d791d Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sat, 5 Jan 2019 23:12:40 +0100 Subject: [PATCH] Move Parallel{Boost,Raw,U}Context::{initialize,finalize} to SwappedContext --- src/kernel/context/ContextBoost.cpp | 21 +-------------------- src/kernel/context/ContextBoost.hpp | 8 -------- src/kernel/context/ContextRaw.cpp | 22 +--------------------- src/kernel/context/ContextRaw.hpp | 8 -------- src/kernel/context/ContextSwapped.cpp | 22 ++++++++++++++++++++++ src/kernel/context/ContextSwapped.hpp | 13 ++++++++++++- src/kernel/context/ContextUnix.cpp | 22 +--------------------- src/kernel/context/ContextUnix.hpp | 8 -------- 8 files changed, 37 insertions(+), 87 deletions(-) diff --git a/src/kernel/context/ContextBoost.cpp b/src/kernel/context/ContextBoost.cpp index ac3313465f..b2e23315d8 100644 --- a/src/kernel/context/ContextBoost.cpp +++ b/src/kernel/context/ContextBoost.cpp @@ -136,25 +136,6 @@ void BoostContext::stop() // ParallelBoostContext -simgrid::xbt::Parmap* ParallelBoostContext::parmap_; -std::atomic ParallelBoostContext::threads_working_; -thread_local uintptr_t ParallelBoostContext::worker_id_; -std::vector ParallelBoostContext::workers_context_; - -void ParallelBoostContext::initialize() -{ - parmap_ = nullptr; - workers_context_.clear(); - workers_context_.resize(SIMIX_context_get_nthreads(), nullptr); -} - -void ParallelBoostContext::finalize() -{ - delete parmap_; - parmap_ = nullptr; - workers_context_.clear(); -} - void ParallelBoostContext::run_all() { threads_working_ = 0; @@ -171,7 +152,7 @@ void ParallelBoostContext::run_all() void ParallelBoostContext::suspend() { boost::optional next_work = parmap_->next(); - ParallelBoostContext* next_context; + SwappedContext* next_context; if (next_work) { XBT_DEBUG("Run next process"); next_context = static_cast(next_work.get()->context_); diff --git a/src/kernel/context/ContextBoost.hpp b/src/kernel/context/ContextBoost.hpp index 643549cd31..580ec7ec61 100644 --- a/src/kernel/context/ContextBoost.hpp +++ b/src/kernel/context/ContextBoost.hpp @@ -69,15 +69,7 @@ public: void suspend() override; void resume() override; - static void initialize(); - static void finalize(); static void run_all(); - -private: - static simgrid::xbt::Parmap* parmap_; - static std::vector workers_context_; - static std::atomic threads_working_; - static thread_local uintptr_t worker_id_; }; class BoostContextFactory : public ContextFactory { diff --git a/src/kernel/context/ContextRaw.cpp b/src/kernel/context/ContextRaw.cpp index 31b2aafe59..6898c5f7e2 100644 --- a/src/kernel/context/ContextRaw.cpp +++ b/src/kernel/context/ContextRaw.cpp @@ -277,26 +277,6 @@ void RawContext::stop() // ParallelRawContext -simgrid::xbt::Parmap* ParallelRawContext::parmap_; -std::atomic ParallelRawContext::threads_working_; /* number of threads that have started their work */ -uintptr_t thread_local ParallelRawContext::worker_id_; /* thread-specific storage for the thread id */ -std::vector ParallelRawContext::workers_context_; /* space to save the worker context - in each thread */ - -void ParallelRawContext::initialize() -{ - parmap_ = nullptr; - workers_context_.clear(); - workers_context_.resize(SIMIX_context_get_nthreads(), nullptr); -} - -void ParallelRawContext::finalize() -{ - delete parmap_; - parmap_ = nullptr; - workers_context_.clear(); -} - void ParallelRawContext::run_all() { threads_working_ = 0; @@ -314,7 +294,7 @@ void ParallelRawContext::suspend() { /* determine the next context */ boost::optional next_work = parmap_->next(); - ParallelRawContext* next_context; + SwappedContext* next_context; if (next_work) { /* there is a next process to resume */ XBT_DEBUG("Run next process"); diff --git a/src/kernel/context/ContextRaw.hpp b/src/kernel/context/ContextRaw.hpp index f1b7914011..e14877c9ea 100644 --- a/src/kernel/context/ContextRaw.hpp +++ b/src/kernel/context/ContextRaw.hpp @@ -56,15 +56,7 @@ public: void suspend() override; void resume() override; - static void initialize(); - static void finalize(); static void run_all(); - -private: - static simgrid::xbt::Parmap* parmap_; - static std::vector workers_context_; - static std::atomic threads_working_; - static uintptr_t thread_local worker_id_; }; class RawContextFactory : public ContextFactory { diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index 437fce0600..be10ff854b 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -8,6 +8,7 @@ #include "src/kernel/context/context_private.hpp" #include "src/simix/ActorImpl.hpp" #include "src/simix/smx_private.hpp" +#include "xbt/parmap.hpp" #include "src/kernel/context/ContextSwapped.hpp" @@ -33,8 +34,29 @@ namespace simgrid { namespace kernel { namespace context { +/* Sequential execution */ unsigned long SwappedContext::process_index_; +/* Parallel execution */ +simgrid::xbt::Parmap* SwappedContext::parmap_; +std::atomic SwappedContext::threads_working_; /* number of threads that have started their work */ +thread_local uintptr_t SwappedContext::worker_id_; /* thread-specific storage for the thread id */ +std::vector SwappedContext::workers_context_; /* space to save the worker's context in each thread */ + +void SwappedContext::initialize() +{ + parmap_ = nullptr; + workers_context_.clear(); + workers_context_.resize(SIMIX_context_get_nthreads(), nullptr); +} + +void SwappedContext::finalize() +{ + delete parmap_; + parmap_ = nullptr; + workers_context_.clear(); +} + SwappedContext* SwappedContext::maestro_context_ = nullptr; SwappedContext::SwappedContext(std::function code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process) diff --git a/src/kernel/context/ContextSwapped.hpp b/src/kernel/context/ContextSwapped.hpp index e132621984..9c15dc6483 100644 --- a/src/kernel/context/ContextSwapped.hpp +++ b/src/kernel/context/ContextSwapped.hpp @@ -8,6 +8,8 @@ #include "src/kernel/context/Context.hpp" +#include + namespace simgrid { namespace kernel { namespace context { @@ -17,12 +19,15 @@ public: SwappedContext(std::function code, void_pfn_smxprocess_t cleanup_func, smx_actor_t process); virtual ~SwappedContext(); + static void initialize(); // Initialize the module, using the options + static void finalize(); // Finalize the module + virtual void suspend(); virtual void resume(); static void run_all(); - virtual void swap_into(SwappedContext* to) = 0; + virtual void swap_into(SwappedContext* to) = 0; // Defined in subclasses static SwappedContext* get_maestro() { return maestro_context_; } static void set_maestro(SwappedContext* maestro) { maestro_context_ = maestro; } @@ -30,6 +35,12 @@ public: protected: void* stack_ = nullptr; /* the thread stack */ + /* For the parallel execution */ + static simgrid::xbt::Parmap* parmap_; + static std::vector workers_context_; + static std::atomic threads_working_; + static thread_local uintptr_t worker_id_; + private: static unsigned long process_index_; static SwappedContext* maestro_context_; diff --git a/src/kernel/context/ContextUnix.cpp b/src/kernel/context/ContextUnix.cpp index 2d4bf3b129..4e204b0f71 100644 --- a/src/kernel/context/ContextUnix.cpp +++ b/src/kernel/context/ContextUnix.cpp @@ -148,26 +148,6 @@ void UContext::stop() // ParallelUContext -simgrid::xbt::Parmap* ParallelUContext::parmap_; -std::atomic ParallelUContext::threads_working_; /* number of threads that have started their work */ -thread_local uintptr_t ParallelUContext::worker_id_; /* thread-specific storage for the thread id */ -std::vector ParallelUContext::workers_context_; /* space to save the worker's context - * in each thread */ - -void ParallelUContext::initialize() -{ - parmap_ = nullptr; - workers_context_.clear(); - workers_context_.resize(SIMIX_context_get_nthreads(), nullptr); -} - -void ParallelUContext::finalize() -{ - delete parmap_; - parmap_ = nullptr; - workers_context_.clear(); -} - void ParallelUContext::run_all() { threads_working_ = 0; @@ -198,7 +178,7 @@ void ParallelUContext::suspend() /* determine the next context */ // Get the next soul to embody now: boost::optional next_work = parmap_->next(); - ParallelUContext* next_context; + SwappedContext* next_context; if (next_work) { // There is a next soul to embody (ie, a next process to resume) XBT_DEBUG("Run next process"); diff --git a/src/kernel/context/ContextUnix.hpp b/src/kernel/context/ContextUnix.hpp index 1fcbc338d5..70852d0eb7 100644 --- a/src/kernel/context/ContextUnix.hpp +++ b/src/kernel/context/ContextUnix.hpp @@ -55,15 +55,7 @@ public: void suspend() override; void resume() override; - static void initialize(); - static void finalize(); static void run_all(); - -private: - static simgrid::xbt::Parmap* parmap_; - static std::vector workers_context_; - static std::atomic threads_working_; - static thread_local uintptr_t worker_id_; }; class UContextFactory : public ContextFactory { -- 2.20.1