Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
SURF: Unify the types of models in a uniq s_surf_model_t (using an union) +reindent...
[simgrid.git] / src / smpi / smpi_bench.c
1 #include "private.h"
2 #include <string.h>
3
4 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
5                                 "Logging specific to SMPI (benchmarking)");
6
7 void smpi_execute(double duration)
8 {
9   smx_host_t host = SIMIX_host_self();
10   smx_mutex_t mutex = smpi_process_mutex();
11   smx_cond_t cond = smpi_process_cond();
12   smx_action_t action;
13   e_surf_action_state_t state;
14
15   SIMIX_mutex_lock(mutex);
16
17   action =
18     SIMIX_action_execute(host, "execute",
19                          duration * smpi_global->reference_speed);
20
21   SIMIX_register_action_to_condition(action, cond);
22   for (state = SIMIX_action_get_state(action);
23        state == SURF_ACTION_READY ||
24        state == SURF_ACTION_RUNNING; state = SIMIX_action_get_state(action)
25     ) {
26     SIMIX_cond_wait(cond, mutex);
27   }
28   SIMIX_unregister_action_to_condition(action, cond);
29   SIMIX_action_destroy(action);
30
31   SIMIX_mutex_unlock(mutex);
32
33   return;
34 }
35
36 void smpi_start_timer()
37 {
38   SIMIX_mutex_lock(smpi_global->timer_mutex);
39   xbt_os_timer_start(smpi_global->timer);
40 }
41
42 double smpi_stop_timer()
43 {
44   double duration;
45   xbt_os_timer_stop(smpi_global->timer);
46   duration = xbt_os_timer_elapsed(smpi_global->timer);
47   SIMIX_mutex_unlock(smpi_global->timer_mutex);
48   return duration;
49 }
50
51 void smpi_bench_begin()
52 {
53   smpi_start_timer();
54 }
55
56 void smpi_bench_end()
57 {
58   smpi_execute(smpi_stop_timer());
59 }
60
61 void smpi_do_once_1(const char *file, int line)
62 {
63   smpi_do_once_duration_node_t curr, prev;
64   smpi_bench_end();
65   SIMIX_mutex_lock(smpi_global->do_once_mutex);
66   prev = NULL;
67   for (curr = smpi_global->do_once_duration_nodes;
68        NULL != curr && (strcmp(curr->file, file) || curr->line != line);
69        curr = curr->next) {
70     prev = curr;
71   }
72   if (NULL == curr) {
73     curr = xbt_new(s_smpi_do_once_duration_node_t, 1);
74     curr->file = xbt_strdup(file);
75     curr->line = line;
76     curr->duration = -1;
77     curr->next = NULL;
78     if (NULL == prev) {
79       smpi_global->do_once_duration_nodes = curr;
80     } else {
81       prev->next = curr;
82     }
83   }
84   smpi_global->do_once_duration = &curr->duration;
85 }
86
87 int smpi_do_once_2()
88 {
89   double duration = *(smpi_global->do_once_duration);
90   if (0 > duration) {
91     smpi_start_timer();
92     return 1;
93   }
94   SIMIX_mutex_unlock(smpi_global->do_once_mutex);
95   smpi_execute(duration);
96   smpi_bench_begin();
97   return 0;
98 }
99
100 void smpi_do_once_3()
101 {
102   *(smpi_global->do_once_duration) = smpi_stop_timer();
103 }