Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ContextBoost: define helper function for jump_fcontext.
[simgrid.git] / src / kernel / context / ContextBoost.cpp
1 /* Copyright (c) 2015-2017. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <cstdint>
7
8 #include <functional>
9 #include <utility>
10 #include <vector>
11
12 #include <boost/context/all.hpp>
13
14 #include <xbt/log.h>
15 #include <xbt/xbt_os_thread.h>
16
17 #include "src/simix/smx_private.h"
18 #include "src/internal_config.h"
19 #include "src/kernel/context/ContextBoost.hpp"
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
22
23 namespace simgrid {
24 namespace kernel {
25 namespace context {
26
27 class BoostSerialContext : public BoostContext {
28 public:
29   BoostSerialContext(std::function<void()> code,
30       void_pfn_smxprocess_t cleanup_func,
31       smx_actor_t process)
32     : BoostContext(std::move(code), cleanup_func, process) {}
33   void stop() override;
34   void suspend() override;
35 };
36
37 #if HAVE_THREAD_CONTEXTS
38 class BoostParallelContext : public BoostContext {
39 public:
40   BoostParallelContext(std::function<void()> code,
41       void_pfn_smxprocess_t cleanup_func,
42       smx_actor_t process)
43     : BoostContext(std::move(code), cleanup_func, process) {}
44   void stop() override;
45   void suspend() override;
46   void resume() override;
47 };
48 #endif
49
50 // BoostContextFactory
51
52 bool BoostContext::parallel_                             = false;
53 simgrid::xbt::Parmap<smx_actor_t>* BoostContext::parmap_ = nullptr;
54 uintptr_t BoostContext::threads_working_                 = 0;
55 xbt_os_thread_key_t BoostContext::worker_id_key_;
56 unsigned long BoostContext::process_index_   = 0;
57 BoostContext* BoostContext::maestro_context_ = nullptr;
58 std::vector<BoostContext*> BoostContext::workers_context_;
59
60 BoostContextFactory::BoostContextFactory()
61   : ContextFactory("BoostContextFactory")
62 {
63   BoostContext::parallel_ = SIMIX_context_is_parallel();
64   if (BoostContext::parallel_) {
65 #if HAVE_THREAD_CONTEXTS
66     int nthreads = SIMIX_context_get_nthreads();
67     BoostContext::parmap_ = new simgrid::xbt::Parmap<smx_actor_t>(nthreads, SIMIX_context_get_parallel_mode());
68     BoostContext::workers_context_.clear();
69     BoostContext::workers_context_.resize(nthreads, nullptr);
70     BoostContext::maestro_context_ = nullptr;
71     xbt_os_thread_key_create(&BoostContext::worker_id_key_);
72 #else
73     xbt_die("No thread support for parallel context execution");
74 #endif
75   }
76 }
77
78 BoostContextFactory::~BoostContextFactory()
79 {
80 #if HAVE_THREAD_CONTEXTS
81   if (BoostContext::parmap_) {
82     delete BoostContext::parmap_;
83     BoostContext::parmap_ = nullptr;
84   }
85   BoostContext::workers_context_.clear();
86 #endif
87 }
88
89 smx_context_t BoostContextFactory::create_context(std::function<void()> code, void_pfn_smxprocess_t cleanup_func,
90                                                   smx_actor_t process)
91 {
92   BoostContext* context = nullptr;
93   if (BoostContext::parallel_)
94 #if HAVE_THREAD_CONTEXTS
95     context = this->new_context<BoostParallelContext>(std::move(code), cleanup_func, process);
96 #else
97     xbt_die("No support for parallel execution");
98 #endif
99   else
100     context = this->new_context<BoostSerialContext>(std::move(code), cleanup_func, process);
101   return context;
102 }
103
104 void BoostContextFactory::run_all()
105 {
106 #if HAVE_THREAD_CONTEXTS
107   if (BoostContext::parallel_) {
108     BoostContext::threads_working_ = 0;
109     BoostContext::parmap_->apply(
110         [](smx_actor_t process) {
111           BoostContext* context = static_cast<BoostContext*>(process->context);
112           return context->resume();
113         },
114         simix_global->process_to_run);
115   } else
116 #endif
117   {
118     if (simix_global->process_to_run.empty())
119       return;
120     smx_actor_t first_process    = simix_global->process_to_run.front();
121     BoostContext::process_index_ = 1;
122     /* execute the first process */
123     static_cast<BoostContext*>(first_process->context)->resume();
124   }
125 }
126
127
128 // BoostContext
129
130 void BoostContext::smx_ctx_boost_wrapper(std::intptr_t arg)
131 {
132   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
133   (*context)();
134   context->stop();
135 }
136
137 inline void BoostContext::smx_ctx_boost_jump_fcontext(BoostContext* from, BoostContext* to)
138 {
139 #if BOOST_VERSION < 105600
140   boost::context::jump_fcontext(from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
141 #else
142   boost::context::jump_fcontext(&from->fc_, to->fc_, reinterpret_cast<intptr_t>(to));
143 #endif
144 }
145
146 BoostContext::BoostContext(std::function<void()> code,
147     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
148   : Context(std::move(code), cleanup_func, process)
149 {
150
151   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
152   if (has_code()) {
153     this->stack_ = SIMIX_context_stack_new();
154 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
155 // or higher address:
156 #if PTH_STACKGROWTH == -1
157     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size - 1;
158 #else
159     void* stack = this->stack_;
160 #endif
161     this->fc_ = boost::context::make_fcontext(
162                       stack,
163                       smx_context_usable_stack_size,
164                       smx_ctx_boost_wrapper);
165   } else {
166 #if BOOST_VERSION < 105600
167     this->fc_ = new boost::context::fcontext_t();
168 #endif
169     if (BoostContext::maestro_context_ == nullptr)
170       BoostContext::maestro_context_ = this;
171   }
172 }
173
174 BoostContext::~BoostContext()
175 {
176 #if BOOST_VERSION < 105600
177   if (not this->stack_)
178     delete this->fc_;
179 #endif
180   if (this == maestro_context_)
181     maestro_context_ = nullptr;
182   SIMIX_context_stack_delete(this->stack_);
183 }
184
185 // BoostSerialContext
186
187 void BoostContext::resume()
188 {
189   SIMIX_context_set_current(this);
190   smx_ctx_boost_jump_fcontext(maestro_context_, this);
191 }
192
193 void BoostSerialContext::suspend()
194 {
195   /* determine the next context */
196   BoostSerialContext* next_context = nullptr;
197   unsigned long int i              = process_index_;
198   process_index_++;
199
200   if (i < simix_global->process_to_run.size()) {
201     /* execute the next process */
202     XBT_DEBUG("Run next process");
203     next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
204   } else {
205     /* all processes were run, return to maestro */
206     XBT_DEBUG("No more process to run");
207     next_context = static_cast<BoostSerialContext*>(maestro_context_);
208   }
209   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
210   smx_ctx_boost_jump_fcontext(this, next_context);
211 }
212
213 void BoostSerialContext::stop()
214 {
215   BoostContext::stop();
216   this->suspend();
217 }
218
219 // BoostParallelContext
220
221 #if HAVE_THREAD_CONTEXTS
222
223 void BoostParallelContext::suspend()
224 {
225   boost::optional<smx_actor_t> next_work = parmap_->next();
226   BoostParallelContext* next_context;
227   if (next_work) {
228     XBT_DEBUG("Run next process");
229     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
230   } else {
231     XBT_DEBUG("No more processes to run");
232     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
233     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
234   }
235
236   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
237   smx_ctx_boost_jump_fcontext(this, next_context);
238 }
239
240 void BoostParallelContext::stop()
241 {
242   BoostContext::stop();
243   this->suspend();
244 }
245
246 void BoostParallelContext::resume()
247 {
248   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
249   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
250
251   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
252   workers_context_[worker_id] = worker_context;
253
254   SIMIX_context_set_current(this);
255   smx_ctx_boost_jump_fcontext(worker_context, this);
256 }
257
258 #endif
259
260 XBT_PRIVATE ContextFactory* boost_factory()
261 {
262   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
263   return new BoostContextFactory();
264 }
265
266 }}} // namespace