Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Functionnal benchmarking macros :
authoralegrand <alegrand@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 17 Feb 2005 05:49:26 +0000 (05:49 +0000)
committeralegrand <alegrand@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Thu, 17 Feb 2005 05:49:26 +0000 (05:49 +0000)
It seems to be working fine.

surf:~/Work/GRAS/gras/examples/gras/chrono $ time ./test_sg ; time ./test_rl
xbt/module.c:44: [module/INFO] Initialize XBT
gras/gras.c:27: [gras/INFO] Initialize GRAS
chrono.c:32: [Chrono/INFO] Before computation: 0
chrono.c:52: [Chrono/INFO] After computation: 15.7531; Duration: 15.7531
MSG: Congratulations ! Simulation terminated : all process are over
real    0m4.002s
user    0m3.940s
sys     0m0.030s

gras/gras.c:27: [gras/INFO] Initialize GRAS
xbt/module.c:44: [module/INFO] Initialize XBT
chrono.c:32: [Chrono/INFO] Before computation: 1.10862e+09
chrono.c:52: [Chrono/INFO] After computation: 1.10862e+09; Duration: 15.6633

real    0m15.729s
user    0m15.550s
sys     0m0.060s

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@1032 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/gras/chrono/chrono.c
include/gras/chrono.h
src/gras/chrono.c
src/gras/gras.c

index c0c8888..eb31f9a 100644 (file)
@@ -8,6 +8,7 @@
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "gras.h"
+#include "xbt/log.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(Chrono,"Messages specific to this example");
 
@@ -17,29 +18,38 @@ int multiplier (int argc,char *argv[]);
 
 int multiplier (int argc,char *argv[])
 {
-  int i,j,k;
+  int i,j,k,l;
   double *A,*B,*C;
   int n = 500;
+  double start = 0.0;
+
   gras_init(&argc, argv, NULL);
-  
+
   A = malloc(n*n*sizeof(double));
   B = malloc(n*n*sizeof(double));
   C = malloc(n*n*sizeof(double));
 
-  INFO1("Before computation : %lg", gras_os_time());
-  for(i=0; i<n; i++)
-    for(j=0; j<n; j++) {
-      A[i*n+j]=2/n;
-      B[i*n+j]=1/n;
-      C[i*n+j]=0.0;
-    }
-
-  for(i=0; i<n; i++)
-    for(j=0; j<n; j++)
-      for(k=0; k<n; k++)       
-       C[i*n+j] += A[i*n+k]*B[k*n+j];
-
-  INFO1("After computation : %lg", gras_os_time());
+  INFO1("Before computation: %lg", start=gras_os_time());
+
+  for(l=0; l<4; l++) {
+    GRAS_BENCH_ONCE_RUN_ONCE_BEGIN();
+    for(i=0; i<n; i++)
+      for(j=0; j<n; j++) {
+       A[i*n+j]=2/n;
+       B[i*n+j]=1/n;
+       C[i*n+j]=0.0;
+      }
+    
+    for(i=0; i<n; i++)
+      for(j=0; j<n; j++)
+       for(k=0; k<n; k++)      
+         C[i*n+j] += A[i*n+k]*B[k*n+j];
+    
+    GRAS_BENCH_ONCE_RUN_ONCE_END();
+  }
+
+  start = gras_os_time()-start;
+  INFO2("After computation: %lg; Duration: %lg ", gras_os_time(), start);
 
   return 0;
 }
index 9571e0c..11c8ccc 100644 (file)
 #define GRAS_CHRONO_H
 
 #include "xbt/misc.h"
+#include "gras/cond.h"
 
 BEGIN_DECL()
 
-void gras_bench_always_begin(const char *location);
-void gras_bench_always_end(void);
+void gras_chrono_init(void);
+int gras_bench_always_begin(const char *location, int line);
+int gras_bench_always_end(void);
+int gras_bench_once_begin(const char *location, int line);
+int gras_bench_once_end(void);
 
 /** \brief Start benchmark this part of the code
     \hideinitializer */
-#define GRAS_BENCH_ALWAYS_BEGIN gras_bench_always_begin(__FILE__ __LINE__ __FUNCTION__)
+#define GRAS_BENCH_ALWAYS_BEGIN()  do { if(gras_if_SG()) gras_bench_always_begin(__FILE__, __LINE__); } while(0)
 /** \brief Stop benchmark this part of the code
     \hideinitializer */
-#define GRAS_BENCH_ALWAYS_END gras_bench_always_end()
+#define GRAS_BENCH_ALWAYS_END() do { if(gras_if_SG()) gras_bench_always_end(); } while(0)
 
 /** \brief Start benchmark this part of the code if it has never been benchmarked before
     \hideinitializer */
-#define GRAS_BENCH_ONCE_BEGIN
+#define GRAS_BENCH_ONCE_RUN_ALWAYS_BEGIN()  do { if(gras_if_SG()) gras_bench_once_begin(__FILE__, __LINE__); } while(0)
 /** \brief Stop benchmarking this part of the code
     \hideinitializer */
-#define GRAS_BENCH_ONCE_END
+#define GRAS_BENCH_ONCE_RUN_ALWAYS_END()    do { if(gras_if_SG()) gras_bench_once_end(); } while(0)
+
+/** \brief Start benchmark this part of the code if it has never been benchmarked before
+    \hideinitializer */
+#define GRAS_BENCH_ONCE_RUN_ONCE_BEGIN()  if((gras_if_SG()&&(gras_bench_once_begin(__FILE__, __LINE__)))||(gras_if_RL())) { 
+/** \brief Stop benchmarking this part of the code
+    \hideinitializer */
+#define GRAS_BENCH_ONCE_RUN_ONCE_END()    } GRAS_BENCH_ONCE_RUN_ALWAYS_END();
 
 END_DECL()
 
index 8a07a71..e0c46c0 100644 (file)
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "xbt/sysdep.h"
+#include "xbt/dict.h"
 #include "gras/chrono.h"
+#include "msg/msg.h"
+#include "portable.h"
 
-void gras_bench_always_begin(const char *location)
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(chrono,gras,"Benchmarking used code");
+
+static double sys_time(void) {
+#ifdef HAVE_GETTIMEOFDAY
+  struct timeval tv;
+
+  gettimeofday(&tv, NULL);
+
+  return (double)(tv.tv_sec + tv.tv_usec / 1000000.0);
+#else
+  /* Poor resolution */
+  return (double)(time(NULL)); 
+#endif /* HAVE_GETTIMEOFDAY? */ 
+       
+}
+
+static double timer = 0.0;
+static int benchmarking = 0;
+static xbt_dict_t benchmark_set = NULL;
+static double reference = .01019;
+static double duration = 0.0;
+static const char* __location__ = NULL;
+
+static void store_in_dict(xbt_dict_t dict, const char *key, double value)
+{
+  double *ir = NULL;
+
+  xbt_dict_get(dict, key, (void *) &ir);
+  if (!ir) {
+    ir = calloc(1, sizeof(double));
+    xbt_dict_set(dict, key, ir, free);
+  }
+  *ir = value;
+}
+
+static double get_from_dict(xbt_dict_t dict, const char *key)
 {
+  double *ir = NULL;
+
+  xbt_dict_get(dict, key, (void *) &ir);
+
+  return *ir;
+}
+
+int gras_bench_always_begin(const char *location, int line)
+{
+  xbt_assert0(!benchmarking,"Already benchmarking");
+  benchmarking = 1;
+
+  timer = sys_time();
+  return 0;
+}
+
+int gras_bench_always_end(void)
+{
+  m_task_t task = NULL;
+  
+  xbt_assert0(benchmarking,"Not benchmarking yet");
+  benchmarking = 0;
+  duration = sys_time()-timer;
+  task = MSG_task_create("task", (duration)/reference, 0 , NULL);
+  MSG_task_execute(task);
+  /*   printf("---> %lg <--- \n", sys_time()-timer); */
+  MSG_task_destroy(task);
+  return 0;
+}
+
+int gras_bench_once_begin(const char *location, int line)
+{
+  double *ir = NULL;
+  xbt_assert0(!benchmarking,"Already benchmarking");
+  benchmarking = 1;
+
+  __location__=location;
+  xbt_dict_get(benchmark_set, __location__, (void *) &ir);
+  if(!ir) {
+/*     printf("%s:%d\n",location,line); */
+    duration = sys_time();
+    return 1;
+  } else {
+    duration = -1.0;
+    return 0;
+  }
+}
+
+int gras_bench_once_end(void)
+{
+  m_task_t task = NULL;
+
+  xbt_assert0(benchmarking,"Not benchmarking yet");
+  benchmarking = 0;
+  if(duration>0) {
+    duration = sys_time()-duration;
+    store_in_dict(benchmark_set, __location__, duration);
+  } else {
+    duration = get_from_dict(benchmark_set,__location__);
+  }
+  task = MSG_task_create("task", (duration)/reference, 0 , NULL);
+  MSG_task_execute(task);
+  MSG_task_destroy(task);
+  return 0;
 }
 
-void gras_bench_always_end(void)
+void gras_chrono_init(void)
 {
+  if(!benchmark_set)
+    benchmark_set = xbt_dict_new();
 }
index d3b2288..03af67b 100644 (file)
@@ -17,6 +17,7 @@
 #include "gras/core.h"
 #include "gras/cond.h"    /* gras_if_RL() => FIXME: killme when gras/sg works */
 #include "gras/process.h" /* FIXME: killme and put process_init in modinter */
+#include "gras/chrono.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras,XBT_LOG_ROOT_CAT,"All GRAS categories (cf. section \ref GRAS_API)");
 static int gras_running_process = 0;
@@ -28,7 +29,7 @@ void gras_init(int *argc,char **argv, const char *defaultlog) {
   /* First initialize the XBT */
   xbt_init_defaultlog(argc,argv,defaultlog);
    
-  
+  gras_chrono_init();
   /* module registrations: 
    *    - declare process specific data we need (without creating them) 
    */