Logo AND Algorithmique Numérique Distribuée

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