* 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");
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;
}
#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()
/* 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();
}