Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into mc-process
[simgrid.git] / src / simix / smx_context_raw.c
1 /* context_raw - fast context switching inspired from System V ucontexts   */
2
3 /* Copyright (c) 2009-2014. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8 #include <math.h>
9 #include "smx_private.h"
10 #include "xbt/parmap.h"
11 #include "xbt/dynar.h"
12 #include "mc/mc.h"
13
14 typedef char * raw_stack_t;
15 typedef void (*rawctx_entry_point_t)(void *);
16
17 typedef struct s_smx_ctx_raw {
18   s_smx_ctx_base_t super;         /* Fields of super implementation */
19   char *malloced_stack;           /* malloced area containing the stack */
20   raw_stack_t stack_top;          /* pointer to stack top (within previous area) */
21 #ifdef TIME_BENCH_PER_SR
22   unsigned int thread;            /* Just for measuring purposes */
23 #endif
24 } s_smx_ctx_raw_t, *smx_ctx_raw_t;
25
26 #ifdef CONTEXT_THREADS
27 static xbt_parmap_t raw_parmap;
28 static smx_ctx_raw_t* raw_workers_context;    /* space to save the worker context in each thread */
29 static unsigned long raw_threads_working;     /* number of threads that have started their work */
30 static xbt_os_thread_key_t raw_worker_id_key; /* thread-specific storage for the thread id */
31 #endif 
32 #ifdef ADAPTIVE_THRESHOLD
33 #define SCHED_ROUND_LIMIT 5
34 xbt_os_timer_t round_time;
35 double par_time,seq_time;
36 double par_ratio,seq_ratio;
37 int reached_seq_limit, reached_par_limit;
38 static unsigned int par_proc_that_ran = 0,seq_proc_that_ran = 0;  /* Counters of processes that have run in SCHED_ROUND_LIMIT scheduling rounds */
39 static unsigned int seq_sched_round=0, par_sched_round=0; /* Amount of SR that ran serial/parallel*/
40 /*Varables used to calculate running variance and mean*/
41 double prev_avg_par_proc=0,prev_avg_seq_proc=0;
42 double delta=0;
43 double s_par_proc=0,s_seq_proc=0; /*Standard deviation of number of processes computed in par/seq during the current simulation*/
44 double avg_par_proc=0,sd_par_proc=0;
45 double avg_seq_proc=0,sd_seq_proc=0;
46 long long par_window=(long long)HUGE_VAL,seq_window=0;
47 #endif
48
49 static unsigned long raw_process_index = 0;   /* index of the next process to run in the
50                                                * list of runnable processes */
51 static smx_ctx_raw_t raw_maestro_context;
52 extern raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
53                                    rawctx_entry_point_t entry_point, void* arg);
54 extern void raw_swapcontext(raw_stack_t* old, raw_stack_t new);
55
56 #if PROCESSOR_x86_64
57 __asm__ (
58 #if defined(APPLE)
59    ".text\n"
60    ".globl _raw_makecontext\n"
61    "_raw_makecontext:\n"
62 #elif defined(_WIN32)
63    ".text\n"
64    ".globl raw_makecontext\n"
65    "raw_makecontext:\n"
66 #else
67    ".text\n"
68    ".globl raw_makecontext\n"
69    ".type raw_makecontext,@function\n"
70    "raw_makecontext:\n"/* Calling convention sets the arguments in rdi, rsi, rdx and rcx, respectively */
71 #endif
72    "   mov %rdi,%rax\n"      /* stack */
73    "   add %rsi,%rax\n"      /* size  */
74    "   andq $-16, %rax\n"    /* align stack */
75    "   movq $0,   -8(%rax)\n" /* @return for func */
76    "   mov %rdx,-16(%rax)\n" /* func */
77    "   mov %rcx,-24(%rax)\n" /* arg/rdi */
78    "   movq $0,  -32(%rax)\n" /* rsi */
79    "   movq $0,  -40(%rax)\n" /* rdx */
80    "   movq $0,  -48(%rax)\n" /* rcx */
81    "   movq $0,  -56(%rax)\n" /* r8  */
82    "   movq $0,  -64(%rax)\n" /* r9  */
83    "   movq $0,  -72(%rax)\n" /* rbp */
84    "   movq $0,  -80(%rax)\n" /* rbx */
85    "   movq $0,  -88(%rax)\n" /* r12 */
86    "   movq $0,  -96(%rax)\n" /* r13 */
87    "   movq $0, -104(%rax)\n" /* r14 */
88    "   movq $0, -112(%rax)\n" /* r15 */
89    "   sub $112,%rax\n"
90    "   ret\n"
91 );
92
93 __asm__ (
94 #if defined(APPLE)
95    ".text\n"
96    ".globl _raw_swapcontext\n"
97    "_raw_swapcontext:\n"
98 #elif defined(_WIN32)
99    ".text\n"
100    ".globl raw_swapcontext\n"
101    "raw_swapcontext:\n"
102 #else
103    ".text\n"
104    ".globl raw_swapcontext\n"
105    ".type raw_swapcontext,@function\n"
106    "raw_swapcontext:\n" /* Calling convention sets the arguments in rdi and rsi, respectively */
107 #endif
108    "   push %rdi\n"
109    "   push %rsi\n"
110    "   push %rdx\n"
111    "   push %rcx\n"
112    "   push %r8\n"
113    "   push %r9\n"
114    "   push %rbp\n"
115    "   push %rbx\n"
116    "   push %r12\n"
117    "   push %r13\n"
118    "   push %r14\n"
119    "   push %r15\n"
120    "   mov %rsp,(%rdi)\n" /* old */
121    "   mov %rsi,%rsp\n" /* new */
122    "   pop %r15\n"
123    "   pop %r14\n"
124    "   pop %r13\n"
125    "   pop %r12\n"
126    "   pop %rbx\n"
127    "   pop %rbp\n"
128    "   pop %r9\n"
129    "   pop %r8\n"
130    "   pop %rcx\n"
131    "   pop %rdx\n"
132    "   pop %rsi\n"
133    "   pop %rdi\n"
134    "   ret\n"
135 );
136 #elif PROCESSOR_i686
137 __asm__ (
138 #if defined(APPLE) || defined(_WIN32)
139    ".text\n"
140    ".globl _raw_makecontext\n"
141    "_raw_makecontext:\n"
142 #else
143    ".text\n"
144    ".globl raw_makecontext\n"
145    ".type raw_makecontext,@function\n"
146    "raw_makecontext:\n"
147 #endif
148    "   movl 4(%esp),%eax\n"   /* stack */
149    "   addl 8(%esp),%eax\n"   /* size  */
150    "   andl $-16, %eax\n"     /* align stack */
151    "   movl 12(%esp),%ecx\n"  /* func  */
152    "   movl 16(%esp),%edx\n"  /* arg   */
153    "   movl %edx, -4(%eax)\n"
154    "   movl $0,   -8(%eax)\n" /* @return for func */
155    "   movl %ecx,-12(%eax)\n"
156    "   movl $0,  -16(%eax)\n" /* ebp */
157    "   movl $0,  -20(%eax)\n" /* ebx */
158    "   movl $0,  -24(%eax)\n" /* esi */
159    "   movl $0,  -28(%eax)\n" /* edi */
160    "   subl $28,%eax\n"
161    "   retl\n"
162 );
163
164 __asm__ (
165 #if defined(APPLE) || defined(_WIN32)
166    ".text\n"
167    ".globl _raw_swapcontext\n"
168    "_raw_swapcontext:\n"
169 #else
170    ".text\n"
171    ".globl raw_swapcontext\n"
172    ".type raw_swapcontext,@function\n"
173    "raw_swapcontext:\n"
174 #endif
175    "   movl 4(%esp),%eax\n" /* old */
176    "   movl 8(%esp),%edx\n" /* new */
177    "   pushl %ebp\n"
178    "   pushl %ebx\n"
179    "   pushl %esi\n"
180    "   pushl %edi\n"
181    "   movl %esp,(%eax)\n"
182    "   movl %edx,%esp\n"
183    "   popl %edi\n"
184    "   popl %esi\n"
185    "   popl %ebx\n"
186    "   popl %ebp\n"
187    "   retl\n"
188 );
189 #else
190
191
192 /* If you implement raw contexts for other processors, don't forget to
193    update the definition of HAVE_RAWCTX in buildtools/Cmake/CompleteInFiles.cmake */
194
195 raw_stack_t raw_makecontext(char* malloced_stack, int stack_size,
196                             rawctx_entry_point_t entry_point, void* arg) {
197    THROW_UNIMPLEMENTED;
198 }
199
200 void raw_swapcontext(raw_stack_t* old, raw_stack_t new) {
201    THROW_UNIMPLEMENTED;
202 }
203
204 #endif
205
206 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
207
208 #ifdef TIME_BENCH_PER_SR
209 #include "xbt/xbt_os_time.h"
210 #define NUM_THREADS 4
211 static xbt_os_timer_t timer;
212 static double time_thread_sr[NUM_THREADS];
213 static double time_thread_ssr[NUM_THREADS];
214 static double time_wasted_sr = 0;
215 static double time_wasted_ssr = 0;
216 static unsigned int sr_count = 0;
217 static unsigned int ssr_count = 0;
218 static char new_sr = 0;
219 #endif
220
221 #ifdef TIME_BENCH_ENTIRE_SRS
222 static unsigned int sr_count = 0;
223 static xbt_os_timer_t timer;
224 #endif
225
226 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
227 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory);
228 static smx_context_t smx_ctx_raw_create_context(xbt_main_func_t code, int argc,
229     char **argv, void_pfn_smxprocess_t cleanup_func, smx_process_t process);
230 static void smx_ctx_raw_free(smx_context_t context);
231 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
232 static void smx_ctx_raw_stop(smx_context_t context);
233 static void smx_ctx_raw_suspend_serial(smx_context_t context);
234 static void smx_ctx_raw_resume_serial(smx_process_t first_process);
235 #ifdef TIME_BENCH_PER_SR
236 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes);
237 void smx_ctx_raw_new_sr(void);
238 #else
239 static void smx_ctx_raw_runall_serial(void);
240 #endif
241 static void smx_ctx_raw_suspend_parallel(smx_context_t context);
242 static void smx_ctx_raw_resume_parallel(smx_process_t first_process);
243 static void smx_ctx_raw_runall_parallel(void);
244 static void smx_ctx_raw_runall(void);
245
246 /**
247  * \brief Initializes the raw context factory.
248  * \param factory where to initialize the factory
249  */
250 void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
251 {
252
253   XBT_VERB("Using raw contexts. Because the glibc is just not good enough for us.");
254   smx_ctx_base_factory_init(factory);
255
256   (*factory)->finalize  = smx_ctx_raw_factory_finalize;
257   (*factory)->create_context = smx_ctx_raw_create_context;
258   /* Do not overload that method (*factory)->finalize */
259   (*factory)->free = smx_ctx_raw_free;
260   (*factory)->stop = smx_ctx_raw_stop;
261   (*factory)->name = "smx_raw_context_factory";
262
263   if (SIMIX_context_is_parallel()) {
264 #ifdef CONTEXT_THREADS
265     int nthreads = SIMIX_context_get_nthreads();
266     xbt_os_thread_key_create(&raw_worker_id_key);
267     raw_parmap = xbt_parmap_new(nthreads, SIMIX_context_get_parallel_mode());
268     raw_workers_context = xbt_new(smx_ctx_raw_t, nthreads);
269     raw_maestro_context=NULL;
270
271 #endif
272     if (SIMIX_context_get_parallel_threshold() > 1) {
273       /* choose dynamically */
274       (*factory)->runall = smx_ctx_raw_runall;
275       (*factory)->suspend = NULL;
276     }
277     else {
278       /* always parallel */
279       (*factory)->runall = smx_ctx_raw_runall_parallel;
280       (*factory)->suspend = smx_ctx_raw_suspend_parallel;
281     }
282   }
283   else {
284     /* always serial */
285     (*factory)->runall = smx_ctx_raw_runall_serial;
286     (*factory)->suspend = smx_ctx_raw_suspend_serial;
287   }
288 #ifdef TIME_BENCH_ENTIRE_SRS
289   (*factory)->runall = smx_ctx_raw_runall;
290   (*factory)->suspend = NULL;
291   timer = xbt_os_timer_new();
292 #endif  
293
294 #ifdef ADAPTIVE_THRESHOLD
295   round_time = xbt_os_timer_new(); 
296   reached_seq_limit = 0;
297   reached_par_limit = 0;
298 #endif
299
300 #ifdef TIME_BENCH_PER_SR
301   timer = xbt_os_timer_new();
302 #endif
303 }
304
305 /**
306  * \brief Finalizes the raw context factory.
307  * \param factory the raw context factory
308  */
309 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
310 {
311 #ifdef TIME_BENCH_PER_SR
312   XBT_VERB("Total wasted time in %u SR: %f", sr_count, time_wasted_sr);
313   XBT_VERB("Total wasted time in %u SSR: %f", ssr_count, time_wasted_ssr);
314 #endif
315
316 #ifdef CONTEXT_THREADS
317   if (raw_parmap)
318     xbt_parmap_destroy(raw_parmap);
319   xbt_free(raw_workers_context);
320 #endif
321   return smx_ctx_base_factory_finalize(factory);
322 }
323
324 /**
325  * \brief Creates a new raw context.
326  * \param code main function of this context or NULL to create the maestro
327  * context
328  * \param argc argument number
329  * \param argv arguments to pass to the main function
330  * \param cleanup_func a function to call to free the user data when the
331  * context finished
332  * \param process SIMIX process
333  */
334 static smx_context_t
335 smx_ctx_raw_create_context(xbt_main_func_t code, int argc, char **argv,
336                            void_pfn_smxprocess_t cleanup_func,
337                            smx_process_t process)
338 {
339
340   smx_ctx_raw_t context =
341       (smx_ctx_raw_t) smx_ctx_base_factory_create_context_sized(
342           sizeof(s_smx_ctx_raw_t),
343           code,
344           argc,
345           argv,
346           cleanup_func,
347           process);
348
349   /* if the user provided a function for the process then use it,
350      otherwise it is the context for maestro */
351      if (code) {
352        context->malloced_stack = SIMIX_context_stack_new();
353        context->stack_top =
354            raw_makecontext(context->malloced_stack,
355                            smx_context_usable_stack_size,
356                            (void_f_pvoid_t)smx_ctx_raw_wrapper, context);
357
358      } else {
359        if(process != NULL && raw_maestro_context==NULL)
360          raw_maestro_context = context;
361
362        if(MC_is_active())
363          MC_ignore_heap(&(raw_maestro_context->stack_top), sizeof(raw_maestro_context->stack_top));
364
365      }
366
367      return (smx_context_t) context;
368 }
369
370 /**
371  * \brief Destroys a raw context.
372  * \param context a raw context
373  */
374 static void smx_ctx_raw_free(smx_context_t context)
375 {
376   if (context) {
377     SIMIX_context_stack_delete(((smx_ctx_raw_t) context)->malloced_stack);
378   }
379   smx_ctx_base_free(context);
380 }
381
382 /**
383  * \brief Wrapper for the main function of a context.
384  * \param context a raw context
385  */
386 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
387 {
388   (context->super.code) (context->super.argc, context->super.argv);
389
390   smx_ctx_raw_stop((smx_context_t) context);
391 }
392
393 /**
394  * \brief Stops a raw context.
395  *
396  * This function is called when the main function of the context if finished.
397  *
398  * \param context the current context
399  */
400 static void smx_ctx_raw_stop(smx_context_t context)
401 {
402   smx_ctx_base_stop(context);
403   simix_global->context_factory->suspend(context);
404 }
405
406 /**
407  * \brief Suspends a running context and resumes another one or returns to
408  * maestro.
409  * \param context the current context
410  */
411 static void smx_ctx_raw_suspend_serial(smx_context_t context)
412 {
413   /* determine the next context */
414   smx_context_t next_context;
415   unsigned long int i; 
416 #ifdef TIME_BENCH_PER_SR
417   i = ++raw_process_index;
418 #else
419   i = raw_process_index++;
420 #endif
421   if (i < xbt_dynar_length(simix_global->process_to_run)) {
422     /* execute the next process */
423     XBT_DEBUG("Run next process");
424     next_context = xbt_dynar_get_as(
425         simix_global->process_to_run, i, smx_process_t)->context;
426   }
427   else {
428     /* all processes were run, return to maestro */
429     XBT_DEBUG("No more process to run");
430     next_context = (smx_context_t) raw_maestro_context;
431   }
432   SIMIX_context_set_current(next_context);
433   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top,
434       ((smx_ctx_raw_t) next_context)->stack_top);
435 }
436
437 /**
438  * \brief Resumes sequentially all processes ready to run.
439  * \param first_process the first process to resume
440  */
441 static void smx_ctx_raw_resume_serial(smx_process_t first_process)
442 {
443   smx_ctx_raw_t context = (smx_ctx_raw_t) first_process->context;
444   SIMIX_context_set_current((smx_context_t) context);
445   raw_swapcontext(&raw_maestro_context->stack_top,
446       ((smx_ctx_raw_t) context)->stack_top);
447 }
448
449 #ifdef TIME_BENCH_PER_SR
450 static void smx_ctx_raw_runall_serial(xbt_dynar_t processes)
451 {
452   smx_process_t process;
453   unsigned int cursor;
454   double elapsed = 0;
455   double tmax = 0;
456   unsigned long num_proc = xbt_dynar_length(simix_global->process_to_run);
457   unsigned int t=0;
458   unsigned int data_size = (num_proc / NUM_THREADS) + ((num_proc % NUM_THREADS) ? 1 : 0);
459
460   ssr_count++;
461   time_thread_ssr[0] = 0;
462   xbt_dynar_foreach(processes, cursor, process){ 
463         XBT_VERB("Schedule item %u of %lu",cursor,num_proc);
464         if(cursor >= t * data_size + data_size){
465           if(time_thread_ssr[t] > tmax)
466             tmax = time_thread_ssr[t];
467           t++;
468           time_thread_ssr[t] = 0;
469         }
470
471         if(new_sr){
472           ((smx_ctx_raw_t)process->context)->thread = t;
473           time_thread_sr[t] = 0;
474         }
475
476         xbt_os_cputimer_start(timer);
477         smx_ctx_raw_resume_serial(process);
478         xbt_os_cputimer_stop(timer);
479         elapsed = xbt_os_timer_elapsed(timer);
480         time_thread_ssr[t] += elapsed;
481         time_thread_sr[((smx_ctx_raw_t)process->context)->thread] += elapsed;
482   }
483
484   if(new_sr)
485     new_sr = FALSE;
486
487   if(time_thread_ssr[t] > tmax)
488     tmax = time_thread_ssr[t];
489
490   for(cursor=0; cursor <= t; cursor++){
491     XBT_VERB("Time SSR thread %u = %f (max %f)", cursor, time_thread_ssr[cursor], tmax);
492     time_wasted_ssr += tmax - time_thread_ssr[cursor];
493   }
494 }
495
496 void smx_ctx_raw_new_sr(void)
497 {
498   int i;
499   double tmax = 0;
500   new_sr = TRUE;
501   sr_count++;
502   for(i=0; i < NUM_THREADS; i++){
503     if(time_thread_sr[i] > tmax)
504       tmax = time_thread_sr[i];
505   }
506
507   for(i=0; i < NUM_THREADS; i++){
508     XBT_CRITICAL("Time SR thread %u = %f (max %f)", i, time_thread_sr[i], tmax);
509     time_wasted_sr += tmax - time_thread_sr[i];
510   }
511
512   XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, tmax, xbt_dynar_length(simix_global->process_that_ran));
513   XBT_CRITICAL("New scheduling round");
514 }
515 #else
516 /**
517  * \brief Resumes sequentially all processes ready to run.
518  */
519 static void smx_ctx_raw_runall_serial(void)
520 {
521   smx_process_t first_process =
522       xbt_dynar_get_as(simix_global->process_to_run, 0, smx_process_t);
523   raw_process_index = 1;
524
525   /* execute the first process */
526   smx_ctx_raw_resume_serial(first_process);
527 }
528 #endif
529
530 /**
531  * \brief Suspends a running context and resumes another one or returns to
532  * the main function of the current worker thread.
533  * \param context the context of the current worker thread
534  */
535 static void smx_ctx_raw_suspend_parallel(smx_context_t context)
536 {
537 #ifdef CONTEXT_THREADS
538   /* determine the next context */
539   smx_process_t next_work = xbt_parmap_next(raw_parmap);
540   smx_context_t next_context;
541   raw_stack_t next_stack;
542
543   if (next_work != NULL) {
544     /* there is a next process to resume */
545     XBT_DEBUG("Run next process");
546     next_context = next_work->context;
547     next_stack = ((smx_ctx_raw_t) next_context)->stack_top;
548   }
549   else {
550     /* all processes were run, go to the barrier */
551     XBT_DEBUG("No more processes to run");
552
553     unsigned long worker_id =
554         (unsigned long) xbt_os_thread_get_specific(raw_worker_id_key);
555
556     next_context = (smx_context_t)raw_workers_context[worker_id];
557     XBT_DEBUG("Restoring worker stack %lu (working threads = %lu)",
558         worker_id, raw_threads_working);
559     next_stack = ((smx_ctx_raw_t)next_context)->stack_top;
560   }
561
562   SIMIX_context_set_current(next_context);
563   raw_swapcontext(&((smx_ctx_raw_t) context)->stack_top, next_stack);
564 #endif
565 }
566
567 /**
568  * \brief Resumes sequentially in the current worker thread the processes ready
569  * to run.
570  * \param first_process the first process to resume
571  */
572 static void smx_ctx_raw_resume_parallel(smx_process_t first_process)
573 {
574 #ifdef CONTEXT_THREADS
575   unsigned long worker_id = __sync_fetch_and_add(&raw_threads_working, 1);
576   xbt_os_thread_set_specific(raw_worker_id_key, (void*) worker_id);
577   smx_ctx_raw_t worker_context = (smx_ctx_raw_t)SIMIX_context_self();
578   raw_workers_context[worker_id] = worker_context;
579   XBT_DEBUG("Saving worker stack %lu", worker_id);
580   raw_stack_t* worker_stack = &(worker_context)->stack_top;
581
582
583   smx_context_t context = first_process->context;
584   SIMIX_context_set_current(context);
585   raw_swapcontext(worker_stack, ((smx_ctx_raw_t) context)->stack_top);
586 #endif
587 }
588
589 /**
590  * \brief Resumes in parallel all processes ready to run.
591  */
592 static void smx_ctx_raw_runall_parallel(void)
593 {
594 #ifdef CONTEXT_THREADS
595   raw_threads_working = 0;
596   xbt_parmap_apply(raw_parmap, (void_f_pvoid_t) smx_ctx_raw_resume_parallel,
597       simix_global->process_to_run);
598 #else
599   xbt_die("You asked for a parallel execution, but you don't have any threads.");
600 #endif
601 }
602
603 /**
604  * \brief Resumes all processes ready to run.
605  */
606 #ifdef ADAPTIVE_THRESHOLD
607 static void smx_ctx_raw_runall(void)
608 {
609   unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
610   unsigned long threshold = SIMIX_context_get_parallel_threshold();
611   reached_seq_limit = (seq_sched_round % SCHED_ROUND_LIMIT == 0); 
612   reached_par_limit = (par_sched_round % SCHED_ROUND_LIMIT == 0);
613
614   if(reached_seq_limit && reached_par_limit){
615     par_ratio = (par_proc_that_ran != 0) ? (par_time / (double)par_proc_that_ran) : 0;
616     seq_ratio = (seq_proc_that_ran != 0) ? (seq_time / (double)seq_proc_that_ran) : 0; 
617     if(seq_ratio > par_ratio){
618        if(nb_processes < avg_par_proc) {
619           threshold = (threshold>2) ? threshold - 1 : threshold ;
620           SIMIX_context_set_parallel_threshold(threshold);
621         }
622     } else {
623         if(nb_processes > avg_seq_proc){
624           SIMIX_context_set_parallel_threshold(threshold+1);
625         }
626     }
627   }
628
629   //XBT_CRITICAL("Thresh: %d", SIMIX_context_get_parallel_threshold());
630   if (nb_processes >= SIMIX_context_get_parallel_threshold()) {
631     simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
632     if(nb_processes < par_window){ 
633       par_sched_round++;
634       xbt_os_walltimer_start(round_time);
635       smx_ctx_raw_runall_parallel();
636       xbt_os_walltimer_stop(round_time);
637       par_time += xbt_os_timer_elapsed(round_time);
638
639       prev_avg_par_proc = avg_par_proc;
640       delta = nb_processes - avg_par_proc;
641       avg_par_proc = (par_sched_round==1) ? nb_processes : avg_par_proc + delta / (double) par_sched_round;
642
643       if(par_sched_round>=2){
644         s_par_proc = s_par_proc + (nb_processes - prev_avg_par_proc) * delta; 
645         sd_par_proc = sqrt(s_par_proc / (par_sched_round-1));
646         par_window = (int) (avg_par_proc + sd_par_proc);
647       }else{
648         sd_par_proc = 0;
649       }
650
651       par_proc_that_ran += nb_processes;
652     } else{
653       smx_ctx_raw_runall_parallel();
654     }
655   } else {
656     simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
657     if(nb_processes > seq_window){ 
658       seq_sched_round++;
659       xbt_os_walltimer_start(round_time);
660       smx_ctx_raw_runall_serial();
661       xbt_os_walltimer_stop(round_time);
662       seq_time += xbt_os_timer_elapsed(round_time);
663
664       prev_avg_seq_proc = avg_seq_proc;
665       delta = (nb_processes-avg_seq_proc);
666       avg_seq_proc = (seq_sched_round==1) ? nb_processes : avg_seq_proc + delta / (double) seq_sched_round;
667
668       if(seq_sched_round>=2){
669         s_seq_proc = s_seq_proc + (nb_processes - prev_avg_seq_proc)*delta; 
670         sd_seq_proc = sqrt(s_seq_proc / (seq_sched_round-1));
671         seq_window = (int) (avg_seq_proc - sd_seq_proc);
672       } else {
673         sd_seq_proc = 0;
674       }
675
676       seq_proc_that_ran += nb_processes;
677     } else {
678       smx_ctx_raw_runall_serial();
679     }
680   }
681 }
682
683 #else
684
685 static void smx_ctx_raw_runall(void)
686 {
687 #ifdef TIME_BENCH_ENTIRE_SRS
688   sr_count++;
689   timer = xbt_os_timer_new();
690   double elapsed = 0;
691 #endif
692   unsigned long nb_processes = xbt_dynar_length(simix_global->process_to_run);
693   if (SIMIX_context_is_parallel() && SIMIX_context_get_parallel_threshold()<nb_processes) {
694         XBT_DEBUG("Runall // %lu", nb_processes);
695         simix_global->context_factory->suspend = smx_ctx_raw_suspend_parallel;
696
697      #ifdef TIME_BENCH_ENTIRE_SRS
698         xbt_os_walltimer_start(timer);
699      #endif
700
701         smx_ctx_raw_runall_parallel();
702
703      #ifdef TIME_BENCH_ENTIRE_SRS
704         xbt_os_walltimer_stop(timer);
705         elapsed = xbt_os_timer_elapsed(timer);
706      #endif
707     } else {
708         XBT_DEBUG("Runall serial %lu", nb_processes);
709         simix_global->context_factory->suspend = smx_ctx_raw_suspend_serial;
710
711       #ifdef TIME_BENCH_PER_SR
712         smx_ctx_raw_runall_serial(simix_global->process_to_run);
713       #else
714
715         #ifdef TIME_BENCH_ENTIRE_SRS
716           xbt_os_walltimer_start(timer);
717         #endif
718
719         smx_ctx_raw_runall_serial();
720
721         #ifdef TIME_BENCH_ENTIRE_SRS
722           xbt_os_walltimer_stop(timer);
723           elapsed = xbt_os_timer_elapsed(timer);
724         #endif
725       #endif
726     }
727
728 #ifdef TIME_BENCH_ENTIRE_SRS
729   XBT_CRITICAL("Total time SR %u = %f, %d", sr_count, elapsed, nb_processes);
730 #endif
731 }
732 #endif