Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4792346912533764673d0e34dbcd11982271eb26
[simgrid.git] / src / smpi / smpi_bench.c
1 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "private.h"
8 #include "xbt/dict.h"
9 #include "xbt/sysdep.h"
10
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_bench, smpi,
12                                 "Logging specific to SMPI (benchmarking)");
13
14 xbt_dict_t allocs = NULL; /* Allocated on first use */
15
16 typedef struct {
17    int count;
18    char data[];
19 } shared_data_t;
20
21 static void free_shared_data(void* ptr) {
22    free(ptr);
23 }
24
25 void smpi_bench_destroy(void) {
26    if (allocs) {
27       xbt_dict_free(&allocs);
28    }
29 }
30
31 static void smpi_execute(double duration) {
32   smx_host_t host;
33   smx_action_t action;
34   smx_mutex_t mutex;
35   smx_cond_t cond;
36   e_surf_action_state_t state;
37
38   if(duration >= xbt_cfg_get_double(_surf_cfg_set, "smpi/cpu_threshold")) {
39     host = SIMIX_host_self();
40     mutex = SIMIX_mutex_init();
41     cond = SIMIX_cond_init();
42     DEBUG1("Sleep for %f to handle real computation time", duration);
43     duration *= xbt_cfg_get_double(_surf_cfg_set, "smpi/running_power");
44     action = SIMIX_action_execute(host, "computation", duration);
45     SIMIX_mutex_lock(mutex);
46     SIMIX_register_action_to_condition(action, cond);
47     for(state = SIMIX_action_get_state(action);
48         state == SURF_ACTION_READY ||
49         state == SURF_ACTION_RUNNING; state = SIMIX_action_get_state(action)) {
50       SIMIX_cond_wait(cond, mutex);
51     }
52     SIMIX_unregister_action_to_condition(action, cond);
53     SIMIX_mutex_unlock(mutex);
54     SIMIX_action_destroy(action);
55     SIMIX_cond_destroy(cond);
56     SIMIX_mutex_destroy(mutex);
57   }
58 }
59
60 void smpi_bench_begin(int rank, const char* mpi_call) {
61   if(mpi_call && rank >= 0 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
62     INFO3("SMPE: ts=%f rank=%d type=end et=%s", SIMIX_get_clock(), rank, mpi_call);
63   }
64   xbt_os_timer_start(smpi_process_timer());
65 }
66
67 void smpi_bench_end(int rank, const char* mpi_call) {
68   xbt_os_timer_t timer = smpi_process_timer();
69
70   xbt_os_timer_stop(timer);
71   smpi_execute(xbt_os_timer_elapsed(timer));
72   if(mpi_call && rank >= 0 && xbt_cfg_get_int(_surf_cfg_set, "smpi/log_events")) {
73     INFO3("SMPE: ts=%f rank=%d type=begin et=%s", SIMIX_get_clock(), rank, mpi_call);
74   }
75 }
76
77 /*
78 TODO
79 void smpi_do_once_1(const char *file, int line)
80 {
81   smpi_do_once_duration_node_t curr, prev;
82
83   smpi_bench_end();
84   SIMIX_mutex_lock(smpi_global->do_once_mutex);
85   prev = NULL;
86   for(curr = smpi_global->do_once_duration_nodes;
87       NULL != curr && (strcmp(curr->file, file) || curr->line != line);
88       curr = curr->next) {
89     prev = curr;
90   }
91   if(NULL == curr) {
92     curr = xbt_new(s_smpi_do_once_duration_node_t, 1);
93     curr->file = xbt_strdup(file);
94     curr->line = line;
95     curr->duration = -1;
96     curr->next = NULL;
97     if(NULL == prev) {
98       smpi_global->do_once_duration_nodes = curr;
99     } else {
100       prev->next = curr;
101     }
102   }
103   smpi_global->do_once_duration = &curr->duration;
104 }
105
106 int smpi_do_once_2()
107 {
108   double duration = *(smpi_global->do_once_duration);
109
110   if(0 > duration) {
111     smpi_start_timer();
112     return 1;
113   }
114   SIMIX_mutex_unlock(smpi_global->do_once_mutex);
115   smpi_execute(duration);
116   smpi_bench_begin();
117   return 0;
118 }
119
120 void smpi_do_once_3()
121 {
122   *(smpi_global->do_once_duration) = smpi_stop_timer();
123 }
124 */
125
126 void* smpi_shared_malloc(size_t size, const char* file, int line) {
127    char* loc = bprintf("%s:%d", file, line);
128    shared_data_t* data;
129
130    if (!allocs) {
131       allocs = xbt_dict_new();
132    }
133    data = xbt_dict_get_or_null(allocs, loc);
134    if (!data) {
135       data = (shared_data_t*)xbt_malloc0(sizeof(int) + size);
136       data->count = 1;
137       xbt_dict_set(allocs, loc, data, &free_shared_data);
138    } else {
139       data->count++;
140    }
141    free(loc);
142    return data->data;
143 }
144
145 void smpi_shared_free(void* ptr) {
146    shared_data_t* data = (shared_data_t*)((int*)ptr - 1);
147    char* loc;
148
149    if (!allocs) {
150       WARN0("Cannot free: nothing was allocated");
151       return;
152    }
153    loc = xbt_dict_get_key(allocs, data);
154    if (!loc) {
155       WARN1("Cannot free: %p was not shared-allocated by SMPI", ptr);
156       return;
157    }
158    data->count--;
159    if (data->count <= 0) {
160       xbt_dict_remove(allocs, loc);
161    }
162 }