Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2b0bc634bb899b0e85546aee8b9c0ea1a6836d48
[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 static void smx_ctx_boost_wrapper(std::intptr_t arg)
131 {
132   BoostContext* context = reinterpret_cast<BoostContext*>(arg);
133   (*context)();
134   context->stop();
135 }
136
137 BoostContext::BoostContext(std::function<void()> code,
138     void_pfn_smxprocess_t cleanup_func, smx_actor_t process)
139   : Context(std::move(code), cleanup_func, process)
140 {
141
142   /* if the user provided a function for the process then use it, otherwise it is the context for maestro */
143   if (has_code()) {
144     this->stack_ = SIMIX_context_stack_new();
145 // We need to pass the bottom of the stack to make_fcontext, depending on the stack direction it may be the lower
146 // or higher address:
147 #if PTH_STACKGROWTH == -1
148     void* stack = static_cast<char*>(this->stack_) + smx_context_usable_stack_size - 1;
149 #else
150     void* stack = this->stack_;
151 #endif
152     this->fc_ = boost::context::make_fcontext(
153                       stack,
154                       smx_context_usable_stack_size,
155                       smx_ctx_boost_wrapper);
156   } else {
157 #if HAVE_BOOST_CONTEXTS == 1
158     this->fc_ = new boost::context::fcontext_t();
159 #endif
160     if (BoostContext::maestro_context_ == nullptr)
161       BoostContext::maestro_context_ = this;
162   }
163 }
164
165 BoostContext::~BoostContext()
166 {
167 #if HAVE_BOOST_CONTEXTS == 1
168   if (not this->stack_)
169     delete this->fc_;
170 #endif
171   if (this == maestro_context_)
172     maestro_context_ = nullptr;
173   SIMIX_context_stack_delete(this->stack_);
174 }
175
176 // BoostSerialContext
177
178 void BoostContext::resume()
179 {
180   SIMIX_context_set_current(this);
181 #if HAVE_BOOST_CONTEXTS == 1
182   boost::context::jump_fcontext(maestro_context_->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
183 #else
184   boost::context::jump_fcontext(&maestro_context_->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
185 #endif
186 }
187
188 void BoostSerialContext::suspend()
189 {
190   /* determine the next context */
191   BoostSerialContext* next_context = nullptr;
192   unsigned long int i              = process_index_;
193   process_index_++;
194
195   if (i < simix_global->process_to_run.size()) {
196     /* execute the next process */
197     XBT_DEBUG("Run next process");
198     next_context = static_cast<BoostSerialContext*>(simix_global->process_to_run[i]->context);
199   } else {
200     /* all processes were run, return to maestro */
201     XBT_DEBUG("No more process to run");
202     next_context = static_cast<BoostSerialContext*>(maestro_context_);
203   }
204   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
205 #if HAVE_BOOST_CONTEXTS == 1
206   boost::context::jump_fcontext(this->fc_, next_context->fc_, reinterpret_cast<pintptr_t>(next_context));
207 #else
208   boost::context::jump_fcontext(&this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
209 #endif
210 }
211
212 void BoostSerialContext::stop()
213 {
214   BoostContext::stop();
215   this->suspend();
216 }
217
218 // BoostParallelContext
219
220 #if HAVE_THREAD_CONTEXTS
221
222 void BoostParallelContext::suspend()
223 {
224   boost::optional<smx_actor_t> next_work = parmap_->next();
225   BoostParallelContext* next_context;
226   if (next_work) {
227     XBT_DEBUG("Run next process");
228     next_context = static_cast<BoostParallelContext*>(next_work.get()->context);
229   } else {
230     XBT_DEBUG("No more processes to run");
231     uintptr_t worker_id = reinterpret_cast<uintptr_t>(xbt_os_thread_get_specific(worker_id_key_));
232     next_context = static_cast<BoostParallelContext*>(workers_context_[worker_id]);
233   }
234
235   SIMIX_context_set_current(static_cast<smx_context_t>(next_context));
236 #if HAVE_BOOST_CONTEXTS == 1
237   boost::context::jump_fcontext(this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
238 #else
239   boost::context::jump_fcontext(&this->fc_, next_context->fc_, reinterpret_cast<intptr_t>(next_context));
240 #endif
241 }
242
243 void BoostParallelContext::stop()
244 {
245   BoostContext::stop();
246   this->suspend();
247 }
248
249 void BoostParallelContext::resume()
250 {
251   uintptr_t worker_id = __sync_fetch_and_add(&threads_working_, 1);
252   xbt_os_thread_set_specific(worker_id_key_, reinterpret_cast<void*>(worker_id));
253
254   BoostParallelContext* worker_context = static_cast<BoostParallelContext*>(SIMIX_context_self());
255   workers_context_[worker_id] = worker_context;
256
257   SIMIX_context_set_current(this);
258 #if HAVE_BOOST_CONTEXTS == 1
259   boost::context::jump_fcontext(worker_context->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
260 #else
261   boost::context::jump_fcontext(&worker_context->fc_, this->fc_, reinterpret_cast<intptr_t>(this));
262 #endif
263 }
264
265 #endif
266
267 XBT_PRIVATE ContextFactory* boost_factory()
268 {
269   XBT_VERB("Using Boost contexts. Welcome to the 21th century.");
270   return new BoostContextFactory();
271 }
272
273 }}} // namespace