Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compute the user code execution time
[simgrid.git] / src / simix / smx_context_raw.c
index 5282293..766d7e1 100644 (file)
@@ -159,10 +159,16 @@ static xbt_parmap_t parmap;
 
 static smx_context_factory_t raw_factory;
 
+#include "xbt/xbt_os_time.h"
+static xbt_os_timer_t timer;
+static double totaltime;
+
+
 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context);
 
 static int smx_ctx_raw_factory_finalize(smx_context_factory_t *factory)
 { 
+  INFO1("Total User Time: %lf", totaltime);
   if(parmap)
       xbt_parmap_destroy(parmap);
   return smx_ctx_base_factory_finalize(factory);
@@ -223,9 +229,11 @@ static void smx_ctx_raw_free(smx_context_t context)
 static void smx_ctx_raw_suspend(smx_context_t context)
 {
   smx_current_context = (smx_context_t)maestro_raw_context;
+  xbt_os_timer_stop(timer);
   raw_swapcontext(
       &((smx_ctx_raw_t) context)->stack_top,
       ((smx_ctx_raw_t) context)->old_stack_top);
+  xbt_os_timer_start(timer);
 }
 
 static void smx_ctx_raw_stop(smx_context_t context)
@@ -236,6 +244,7 @@ static void smx_ctx_raw_stop(smx_context_t context)
 
 static void smx_ctx_raw_wrapper(smx_ctx_raw_t context)
 { 
+  xbt_os_timer_start(timer);
   (context->super.code) (context->super.argc, context->super.argv);
 
   smx_ctx_raw_stop((smx_context_t) context);
@@ -248,6 +257,7 @@ static void smx_ctx_raw_resume(smx_process_t process)
   raw_swapcontext(
       &((smx_ctx_raw_t) context)->old_stack_top,
       ((smx_ctx_raw_t) context)->stack_top);
+  totaltime += xbt_os_timer_elapsed(timer);
 }
 
 
@@ -280,12 +290,12 @@ static int smx_ctx_raw_get_thread_id(){
 
 static void smx_ctx_raw_runall(xbt_dynar_t processes)
 {
-  if(xbt_dynar_length(processes) > 10){
+  if (xbt_dynar_length(processes) >= SIMIX_context_get_parallel_threshold()) {
     DEBUG1("Runall // %lu", xbt_dynar_length(processes));
     raw_factory->self = smx_ctx_raw_self_parallel;
     raw_factory->get_thread_id = smx_ctx_raw_get_thread_id;
     smx_ctx_raw_runall_parallel(processes);
-  }else{
+  } else {
     DEBUG1("Runall serial %lu", xbt_dynar_length(processes));
     raw_factory->self = smx_ctx_base_self;
     raw_factory->get_thread_id = smx_ctx_base_get_thread_id;
@@ -305,19 +315,28 @@ void SIMIX_ctx_raw_factory_init(smx_context_factory_t *factory)
   (*factory)->stop = smx_ctx_raw_stop;
   (*factory)->suspend = smx_ctx_raw_suspend;
   (*factory)->name = "smx_raw_context_factory";
-  (*factory)->runall = smx_ctx_raw_runall;
+
+  parmap = xbt_parmap_new(2);
 
   if (SIMIX_context_is_parallel()) {
-#ifdef CONTEXT_THREADS  /* To use parallel ucontexts a thread pool is needed */
-    parmap = xbt_parmap_new(2);
-    (*factory)->self = smx_ctx_raw_self_parallel;
-    (*factory)->get_thread_id = smx_ctx_raw_get_thread_id;
-#else
-    THROW0(arg_error, 0, "No thread support for parallel context execution");
-#endif
-  } else {
-    (*factory)->runall = smx_ctx_raw_runall;
+    if (SIMIX_context_get_parallel_threshold() > 1) {
+      /* choose dynamically */
+      (*factory)->runall = smx_ctx_raw_runall;
+    }
+    else {
+      /* always parallel */
+      (*factory)->self = smx_ctx_raw_self_parallel;
+      (*factory)->get_thread_id = smx_ctx_raw_get_thread_id;
+      (*factory)->runall = smx_ctx_raw_runall_parallel;
+    }
+  }
+  else {
+    /* always serial */
+    (*factory)->self = smx_ctx_base_self;
+    (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
+    (*factory)->runall = smx_ctx_raw_runall_serial;
   }
-
   raw_factory = *factory;
+
+  timer = xbt_os_timer_new();
 }