Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
71d34f1872c2535f0e94ed90d4991ae936762833
[simgrid.git] / src / simix / smx_context_boost.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** @file smx_context_boost.cpp Userspace context switching implementation based on Boost.Context */
8
9 #include <cstdint>
10
11 #include <boost/context/all.hpp>
12
13 #include "xbt/log.h"
14 #include "smx_private.h"
15 #include "internal_config.h"
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
18
19 typedef struct s_smx_ctx_boost {
20   s_smx_ctx_base_t super;       /* Fields of super implementation */
21   boost::context::fcontext_t* fc;
22   void* stack;
23 } s_smx_ctx_boost_t, *smx_ctx_boost_t;
24
25 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory);
26 static smx_context_t
27 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
28     void_pfn_smxprocess_t cleanup_func, smx_process_t process);
29 static void smx_ctx_boost_free(smx_context_t context);
30
31 static void smx_ctx_boost_wrapper(std::intptr_t arg);
32
33 static void smx_ctx_boost_stop_serial(smx_context_t context);
34 static void smx_ctx_boost_suspend_serial(smx_context_t context);
35 static void smx_ctx_boost_resume_serial(smx_process_t first_process);
36 static void smx_ctx_boost_runall_serial(void);
37
38 #ifdef CONTEXT_THREADS
39 static void smx_ctx_boost_stop_parallel(smx_context_t context);
40 static void smx_ctx_boost_suspend_parallel(smx_context_t context);
41 static void smx_ctx_boost_resume_parallel(smx_process_t first_process);
42 static void smx_ctx_boost_runall_parallel(void);
43 #endif
44
45 #ifdef CONTEXT_THREADS
46 static xbt_parmap_t boost_parmap;
47 static smx_ctx_boost_t* boost_workers_context;
48 static unsigned long boost_threads_working;
49 static xbt_os_thread_key_t boost_worker_id_key;
50 #endif
51
52 static unsigned long boost_process_index = 0;
53 static smx_ctx_boost_t boost_maestro_context;
54
55 void SIMIX_ctx_boost_factory_init(smx_context_factory_t *factory)
56 {
57   smx_ctx_base_factory_init(factory);
58   XBT_VERB("Activating boost context factory");
59
60   (*factory)->finalize = smx_ctx_boost_factory_finalize;
61   (*factory)->create_context = smx_ctx_boost_create_context;
62   /* Do not overload that method (*factory)->finalize */
63   (*factory)->free = smx_ctx_boost_free;
64   (*factory)->name = "smx_boost_context_factory";
65
66   if (SIMIX_context_is_parallel()) {
67 #ifndef CONTEXT_THREADS
68     THROWF(arg_error, 0, "No thread support for parallel context execution");
69 #else
70     int nthreads = SIMIX_context_get_nthreads();
71     boost_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
72     boost_workers_context = xbt_new(smx_ctx_boost_t, nthreads);
73     boost_maestro_context = NULL;
74
75     xbt_os_thread_key_create(&boost_worker_id_key);
76
77     (*factory)->stop = smx_ctx_boost_stop_parallel;
78     (*factory)->suspend = smx_ctx_boost_suspend_parallel;
79     (*factory)->runall = smx_ctx_boost_runall_parallel;
80 #endif
81   } else {
82     (*factory)->stop = smx_ctx_boost_stop_serial;
83     (*factory)->suspend = smx_ctx_boost_suspend_serial;
84     (*factory)->runall = smx_ctx_boost_runall_serial;
85   }
86 }
87
88 /* Initialization functions */
89
90 static int smx_ctx_boost_factory_finalize(smx_context_factory_t *factory)
91 {
92 #ifdef CONTEXT_THREADS
93   if (boost_parmap) {
94     xbt_parmap_destroy(boost_parmap);
95     boost_parmap = nullptr;
96   }
97   xbt_free(boost_workers_context);
98   boost_workers_context = nullptr;
99 #endif
100   return smx_ctx_base_factory_finalize(factory);
101 }
102
103 static smx_context_t
104 smx_ctx_boost_create_context(xbt_main_func_t code, int argc, char **argv,
105     void_pfn_smxprocess_t cleanup_func, smx_process_t process)
106 {
107   smx_ctx_boost_t context =
108       (smx_ctx_boost_t) smx_ctx_base_factory_create_context_sized(
109           sizeof(s_smx_ctx_boost_t),
110           code,
111           argc,
112           argv,
113           cleanup_func,
114           process);
115
116   /* if the user provided a function for the process then use it,
117      otherwise it is the context for maestro */
118   if (code) {
119     context->stack = SIMIX_context_stack_new();
120     // We need to pass the bottom of the stack to make_fcontext,
121     // depending on the stack direction it may be the lower or higher address:
122 #if PTH_STACKGROWTH == -1
123     void* stack = (char*) context->stack + smx_context_usable_stack_size - 1;
124 #else
125     void* stack = context->stack;
126 #endif
127     context->fc = boost::context::make_fcontext(
128                       stack,
129                       smx_context_usable_stack_size,
130                       smx_ctx_boost_wrapper);
131   } else {
132     context->stack = nullptr;
133     context->fc = new boost::context::fcontext_t();
134     if (boost_maestro_context == nullptr)
135       boost_maestro_context = context;
136   }
137
138   return (smx_context_t) context;
139 }
140
141 static void smx_ctx_boost_free(smx_context_t c)
142 {
143   smx_ctx_boost_t context = (smx_ctx_boost_t) c;
144   if (!context)
145     return;
146   if (!context->stack)
147     delete context->fc;
148   if ((smx_ctx_boost_t) c == boost_maestro_context)
149     boost_maestro_context = nullptr;
150   SIMIX_context_stack_delete(context->stack);
151   smx_ctx_base_free(c);
152 }
153
154 static void smx_ctx_boost_wrapper(std::intptr_t arg)
155 {
156   smx_context_t context = (smx_context_t) arg;
157   context->code(context->argc, context->argv);
158   smx_ctx_boost_stop_serial(context);
159 }
160
161 static void smx_ctx_boost_stop_serial(smx_context_t context)
162 {
163   smx_ctx_base_stop(context);
164   simix_global->context_factory->suspend(context);
165 }
166
167 static void smx_ctx_boost_suspend_serial(smx_context_t context)
168 {
169   /* determine the next context */
170   smx_ctx_boost_t next_context;
171   unsigned long int i = boost_process_index++;
172
173   if (i < xbt_dynar_length(simix_global->process_to_run)) {
174     /* execute the next process */
175     XBT_DEBUG("Run next process");
176     next_context = (smx_ctx_boost_t) xbt_dynar_get_as(
177         simix_global->process_to_run, i, smx_process_t)->context;
178   }
179   else {
180     /* all processes were run, return to maestro */
181     XBT_DEBUG("No more process to run");
182     next_context = (smx_ctx_boost_t) boost_maestro_context;
183   }
184   SIMIX_context_set_current((smx_context_t) next_context);
185   boost::context::jump_fcontext(
186     ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
187 }
188
189 static void smx_ctx_boost_resume_serial(smx_process_t first_process)
190 {
191   smx_ctx_boost_t context = (smx_ctx_boost_t) first_process->context;
192   SIMIX_context_set_current((smx_context_t) context);
193   boost::context::jump_fcontext(boost_maestro_context->fc, context->fc,
194     (intptr_t)context);
195 }
196
197 static void smx_ctx_boost_runall_serial(void)
198 {
199   smx_process_t first_process =
200       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
201   boost_process_index = 1;
202
203   /* execute the first process */
204   smx_ctx_boost_resume_serial(first_process);
205 }
206
207 // **** Parallel code
208
209 #ifdef CONTEXT_THREADS
210
211 static void smx_ctx_boost_stop_parallel(smx_context_t context)
212 {
213   smx_ctx_base_stop(context);
214   smx_ctx_boost_suspend_parallel(context);
215 }
216
217 static void smx_ctx_boost_suspend_parallel(smx_context_t context)
218 {
219   smx_process_t next_work = (smx_process_t) xbt_parmap_next(boost_parmap);
220   smx_ctx_boost_t next_context;
221
222   if (next_work != NULL) {
223     XBT_DEBUG("Run next process");
224     next_context = (smx_ctx_boost_t) next_work->context;
225   }
226   else {
227     XBT_DEBUG("No more processes to run");
228     unsigned long worker_id =
229         (unsigned long) xbt_os_thread_get_specific(boost_worker_id_key);
230     next_context = boost_workers_context[worker_id];
231   }
232
233   SIMIX_context_set_current((smx_context_t) next_context);
234   boost::context::jump_fcontext(
235     ((smx_ctx_boost_t)context)->fc, next_context->fc, (intptr_t)next_context);
236 }
237
238 static void smx_ctx_boost_resume_parallel(smx_process_t process)
239 {
240   unsigned long worker_id = __sync_fetch_and_add(&boost_threads_working, 1);
241   xbt_os_thread_set_specific(boost_worker_id_key, (void*) worker_id);
242
243   smx_ctx_boost_t worker_context = (smx_ctx_boost_t)SIMIX_context_self();
244   boost_workers_context[worker_id] = worker_context;
245   smx_ctx_boost_t context = (smx_ctx_boost_t) process->context;
246
247   SIMIX_context_set_current((smx_context_t) context);
248   boost::context::jump_fcontext(worker_context->fc, context->fc,
249     (intptr_t)context);
250 }
251
252 static void smx_ctx_boost_runall_parallel(void)
253 {
254   boost_threads_working = 0;
255   xbt_parmap_apply(boost_parmap, (void_f_pvoid_t) smx_ctx_boost_resume_parallel,
256     simix_global->process_to_run);
257 }
258
259 #endif