3 /* sg_emul - Emulation support (simulation) */
5 /* Copyright (c) 2003-5 Arnaud Legrand, Martin Quinson. All rights reserved.*/
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. */
10 #include <stdio.h> /* sprintf */
11 #include "gras/emul.h"
12 #include "gras/Virtu/virtu_sg.h"
13 #include "gras_modinter.h"
15 #include "xbt/xbt_portability.h" /* timers */
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_emul,gras_virtu,"Emulation support");
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;
28 static char* locbuf = NULL;
29 static int locbufsize;
31 void gras_emul_init(void)
34 benchmark_set = xbt_dict_new();
35 timer = xbt_os_timer_new();
39 void gras_emul_exit(void) {
40 if (locbuf) free(locbuf);
41 xbt_dict_free(&benchmark_set);
42 xbt_os_timer_free(timer);
46 static void store_in_dict(xbt_dict_t dict, const char *key, double value)
50 ir = xbt_dict_get_or_null(dict, key);
52 ir = xbt_new0(double,1);
53 xbt_dict_set(dict, key, ir, xbt_free_f);
58 static double get_from_dict(xbt_dict_t dict, const char *key) {
59 double *ir = xbt_dict_get(dict, key);
64 int gras_bench_always_begin(const char *location,int line)
66 xbt_assert0(!benchmarking,"Already benchmarking");
70 xbt_os_timer_start(timer);
74 int gras_bench_always_end(void)
80 xbt_assert0(benchmarking,"Not benchmarking yet");
82 xbt_os_timer_stop(timer);
83 duration = xbt_os_timer_elapsed(timer);
85 cond = SIMIX_cond_init();
86 mutex = SIMIX_mutex_init();
88 SIMIX_mutex_lock(mutex);
89 act = SIMIX_action_execute(SIMIX_host_self(), (char*) "task", (duration)/reference);
91 SIMIX_register_action_to_condition(act,cond);
92 SIMIX_register_condition_to_action(act,cond);
93 SIMIX_cond_wait(cond, mutex);
95 SIMIX_action_destroy(act);
96 SIMIX_mutex_unlock(mutex);
98 SIMIX_cond_destroy(cond);
99 SIMIX_mutex_destroy(mutex);
104 int gras_bench_once_begin(const char *location,int line) {
106 xbt_assert0(!benchmarking,"Already benchmarking");
109 if (!locbuf || locbufsize < strlen(location) + 64) {
110 locbufsize = strlen(location) + 64;
111 locbuf = xbt_realloc(locbuf,locbufsize);
113 sprintf(locbuf,"%s:%d",location, line);
115 ir = xbt_dict_get_or_null(benchmark_set, locbuf);
119 xbt_os_timer_start(timer);
127 int gras_bench_once_end(void)
133 xbt_assert0(benchmarking,"Not benchmarking yet");
136 xbt_os_timer_stop(timer);
137 duration = xbt_os_timer_elapsed(timer);
138 store_in_dict(benchmark_set, locbuf, duration);
140 duration = get_from_dict(benchmark_set,locbuf);
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();
146 SIMIX_mutex_lock(mutex);
147 act = SIMIX_action_execute(SIMIX_host_self(), (char*)"task", (duration)/reference);
149 SIMIX_register_action_to_condition(act,cond);
150 SIMIX_register_condition_to_action(act,cond);
151 SIMIX_cond_wait(cond, mutex);
153 SIMIX_action_destroy(act);
154 SIMIX_mutex_unlock(mutex);
156 SIMIX_cond_destroy(cond);
157 SIMIX_mutex_destroy(mutex);
162 /*** Conditional execution support ***/
164 int gras_if_RL(void) {
168 int gras_if_SG(void) {
172 void gras_global_init(int *argc,char **argv) {
173 return SIMIX_global_init(argc,argv);
175 void gras_create_environment(const char *file) {
176 return SIMIX_create_environment(file);
178 void gras_function_register(const char *name, void *code) {
179 return SIMIX_function_register(name, (smx_process_code_t)code);
182 smx_cond_t cond = NULL;
183 smx_action_t smx_action;
184 xbt_fifo_t actions_done = xbt_fifo_new();
185 xbt_fifo_t actions_failed = xbt_fifo_new();
188 /* Clean IO before the run */
193 while (SIMIX_solve(actions_done, actions_failed) != -1.0) {
195 while ( (smx_action = xbt_fifo_pop(actions_failed)) ) {
198 DEBUG1("** %s failed **",smx_action->name);
199 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
200 SIMIX_cond_broadcast(cond);
202 /* action finished, destroy it */
203 // SIMIX_action_destroy(smx_action);
206 while ( (smx_action = xbt_fifo_pop(actions_done)) ) {
208 DEBUG1("** %s done **",smx_action->name);
209 while ( (cond = xbt_fifo_pop(smx_action->cond_list)) ) {
210 SIMIX_cond_broadcast(cond);
212 /* action finished, destroy it */
213 //SIMIX_action_destroy(smx_action);
216 xbt_fifo_free(actions_failed);
217 xbt_fifo_free(actions_done);
221 void gras_launch_application(const char *file) {
222 return SIMIX_launch_application(file);
225 return SIMIX_clean();