Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
57dd2379e526bb62e8589c5835e296eb6e9c8bb6
[simgrid.git] / src / gras / Virtu / sg_emul.c
1 /* $Id$ */
2
3 /* sg_emul - Emulation support (simulation)                                 */
4
5 /* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h> /* sprintf */
11 #include "gras/emul.h"
12 #include "gras/Virtu/virtu_sg.h"
13 #include "gras_modinter.h"
14
15 #include "xbt/xbt_portability.h" /* timers */
16 #include "xbt/dict.h"
17 #include "xbt/ex.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul,gras_virtu,"Emulation support");
20
21 /*** Timing macros ***/
22 static xbt_os_timer_t timer;
23 static int benchmarking = 0;
24 static xbt_dict_t benchmark_set = NULL;
25 static double reference = .00000000523066250047108838; /* FIXME: we should benchmark host machine to set this; unit=s/flop */
26 static double duration = 0.0;
27
28 static char* locbuf = NULL;
29 static int locbufsize;
30
31 void gras_emul_init(void)
32 {
33   if(!benchmark_set) {
34     benchmark_set = xbt_dict_new();
35     timer = xbt_os_timer_new();
36   }
37 }
38
39 void gras_emul_exit(void) {
40   if (locbuf) free(locbuf);
41   xbt_dict_free(&benchmark_set);
42   xbt_os_timer_free(timer);
43 }
44
45
46 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
47 {
48   double *ir;
49
50   ir = xbt_dict_get_or_null(dict, key);
51   if (!ir) {
52     ir = xbt_new0(double,1);
53     xbt_dict_set(dict, key, ir, xbt_free_f);
54   }
55   *ir = value;
56 }
57
58 static double get_from_dict(xbt_dict_t dict, const char *key) {
59   double *ir = xbt_dict_get(dict, key);
60
61   return *ir;
62 }
63
64 int gras_bench_always_begin(const char *location,int line)
65 {
66   xbt_assert0(!benchmarking,"Already benchmarking");
67   benchmarking = 1;
68
69   if (!timer)
70   xbt_os_timer_start(timer);
71   return 0;
72 }
73
74 int gras_bench_always_end(void)
75 {
76   m_task_t task = NULL;
77   
78   xbt_assert0(benchmarking,"Not benchmarking yet");
79   benchmarking = 0;
80   xbt_os_timer_stop(timer);
81   duration = xbt_os_timer_elapsed(timer);
82   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
83   MSG_task_execute(task);
84   MSG_task_destroy(task);
85   return 0;
86 }
87
88 int gras_bench_once_begin(const char *location,int line) { 
89   double *ir = NULL;
90   xbt_assert0(!benchmarking,"Already benchmarking");
91   benchmarking = 1;
92
93   if (!locbuf || locbufsize < strlen(location) + 64) {
94      locbufsize = strlen(location) + 64;
95      locbuf = xbt_realloc(locbuf,locbufsize);
96   }
97   sprintf(locbuf,"%s:%d",location, line);
98    
99   ir = xbt_dict_get_or_null(benchmark_set, locbuf);
100   if(!ir) {
101     DEBUG1("%s",locbuf); 
102     duration = 1;
103     xbt_os_timer_start(timer);
104     return 1;
105   } else {
106     duration = -1.0;
107     return 0;
108   }
109 }
110
111 int gras_bench_once_end(void)
112 {
113   m_task_t task = NULL;
114
115   xbt_assert0(benchmarking,"Not benchmarking yet");
116   benchmarking = 0;
117   if(duration>0) {
118     xbt_os_timer_stop(timer);
119     duration = xbt_os_timer_elapsed(timer);
120     store_in_dict(benchmark_set, locbuf, duration);
121   } else {
122     duration = get_from_dict(benchmark_set,locbuf);
123   }
124   DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
125   task = MSG_task_create("task", (duration)/reference, 0 , NULL);
126   MSG_task_execute(task);
127   MSG_task_destroy(task);
128   return 0;
129 }
130
131
132 /*** Conditional execution support ***/
133
134 int gras_if_RL(void) {
135    return 0;
136 }
137
138 int gras_if_SG(void) {
139    return 1;
140 }
141
142