Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1f4305ecf4bf983b5f25df00b6fa3f261b297e1d
[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_os_time.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 unsigned 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         smx_action_t act;
77         smx_cond_t cond;
78         smx_mutex_t mutex;
79
80   xbt_assert0(benchmarking,"Not benchmarking yet");
81   benchmarking = 0;
82   xbt_os_timer_stop(timer);
83   duration = xbt_os_timer_elapsed(timer);
84
85         cond = SIMIX_cond_init();
86         mutex = SIMIX_mutex_init();
87         
88         SIMIX_mutex_lock(mutex);
89         act = SIMIX_action_execute(SIMIX_host_self(), (char*) "task", (duration)/reference);
90         
91         SIMIX_register_action_to_condition(act,cond);
92         SIMIX_cond_wait(cond, mutex);
93         SIMIX_unregister_action_to_condition(act,cond);
94         
95         SIMIX_action_destroy(act);
96         SIMIX_mutex_unlock(mutex);
97
98         SIMIX_cond_destroy(cond);
99         SIMIX_mutex_destroy(mutex);
100
101   return 0;
102 }
103
104 int gras_bench_once_begin(const char *location,int line) { 
105   double *ir = NULL;
106   xbt_assert0(!benchmarking,"Already benchmarking");
107   benchmarking = 1;
108
109   if (!locbuf || locbufsize < strlen(location) + 64) {
110      locbufsize = strlen(location) + 64;
111      locbuf = xbt_realloc(locbuf,locbufsize);
112   }
113   sprintf(locbuf,"%s:%d",location, line);
114    
115   ir = xbt_dict_get_or_null(benchmark_set, locbuf);
116   if(!ir) {
117     DEBUG1("%s",locbuf); 
118     duration = 1;
119     xbt_os_timer_start(timer);
120     return 1;
121   } else {
122     duration = -1.0;
123     return 0;
124   }
125 }
126
127 int gras_bench_once_end(void)
128 {
129         smx_action_t act;
130         smx_cond_t cond;
131         smx_mutex_t mutex;
132
133   xbt_assert0(benchmarking,"Not benchmarking yet");
134   benchmarking = 0;
135   if(duration>0) {
136     xbt_os_timer_stop(timer);
137     duration = xbt_os_timer_elapsed(timer);
138     store_in_dict(benchmark_set, locbuf, duration);
139   } else {
140     duration = get_from_dict(benchmark_set,locbuf);
141   }
142   DEBUG2("Simulate the run of a task of %f sec for %s",duration,locbuf);
143         cond = SIMIX_cond_init();
144         mutex = SIMIX_mutex_init();
145         
146         SIMIX_mutex_lock(mutex);
147         act = SIMIX_action_execute(SIMIX_host_self(), (char*)"task", (duration)/reference);
148         
149         SIMIX_register_action_to_condition(act,cond);
150         SIMIX_cond_wait(cond, mutex);
151         SIMIX_unregister_action_to_condition(act,cond);
152         
153         SIMIX_action_destroy(act);
154         SIMIX_mutex_unlock(mutex);
155
156         SIMIX_cond_destroy(cond);
157         SIMIX_mutex_destroy(mutex);
158   return 0;
159 }
160
161
162 /*** Conditional execution support ***/
163
164 int gras_if_RL(void) {
165    return 0;
166 }
167
168 int gras_if_SG(void) {
169    return 1;
170 }