Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Direct context switching: clean the semantics of parmap
[simgrid.git] / src / surf / network_constant.c
1 /* Copyright (c) 2008, 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 "surf_private.h"
8 #include "surf/random_mgr.h"
9 #include "xbt/dict.h"
10 #include "xbt/str.h"
11 #include "xbt/log.h"
12
13 typedef struct surf_action_network_Constant {
14   s_surf_action_t generic_action;
15   double latency;
16   double lat_init;
17   int suspended;
18 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
19
20 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
21 static random_data_t random_latency = NULL;
22 static int host_number_int = 0;
23
24 static void netcste_count_hosts(void)
25 {
26   host_number_int++;
27 }
28
29 static void netcste_define_callbacks(const char *file)
30 {
31   surfxml_add_callback(STag_surfxml_host_cb_list, &netcste_count_hosts);
32 }
33
34 static int netcste_resource_used(void *resource_id)
35 {
36   return 0;
37 }
38
39 static int netcste_action_unref(surf_action_t action)
40 {
41   action->refcount--;
42   if (!action->refcount) {
43     xbt_swag_remove(action, action->state_set);
44     surf_action_free(&action);
45     return 1;
46   }
47   return 0;
48 }
49
50 static void netcste_action_cancel(surf_action_t action)
51 {
52   return;
53 }
54
55 static double netcste_share_resources(double now)
56 {
57   surf_action_network_Constant_t action = NULL;
58   xbt_swag_t running_actions =
59       surf_network_model->states.running_action_set;
60   double min = -1.0;
61
62   xbt_swag_foreach(action, running_actions) {
63     if (action->latency > 0) {
64       if (min < 0)
65         min = action->latency;
66       else if (action->latency < min)
67         min = action->latency;
68     }
69   }
70
71   return min;
72 }
73
74 static void netcste_update_actions_state(double now, double delta)
75 {
76   surf_action_network_Constant_t action = NULL;
77   surf_action_network_Constant_t next_action = NULL;
78   xbt_swag_t running_actions =
79       surf_network_model->states.running_action_set;
80
81   xbt_swag_foreach_safe(action, next_action, running_actions) {
82     if (action->latency > 0) {
83       if (action->latency > delta) {
84         double_update(&(action->latency), delta);
85       } else {
86         action->latency = 0.0;
87       }
88     }
89     double_update(&(action->generic_action.remains),
90                   action->generic_action.cost * delta / action->lat_init);
91     if (action->generic_action.max_duration != NO_MAX_DURATION)
92       double_update(&(action->generic_action.max_duration), delta);
93
94     if (action->generic_action.remains <= 0) {
95       action->generic_action.finish = surf_get_clock();
96       surf_network_model->action_state_set((surf_action_t) action,
97                                            SURF_ACTION_DONE);
98     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
99                && (action->generic_action.max_duration <= 0)) {
100       action->generic_action.finish = surf_get_clock();
101       surf_network_model->action_state_set((surf_action_t) action,
102                                            SURF_ACTION_DONE);
103     }
104   }
105 }
106
107 static void netcste_update_resource_state(void *id,
108                                           tmgr_trace_event_t event_type,
109                                           double value, double time)
110 {
111   DIE_IMPOSSIBLE;
112 }
113
114 static surf_action_t netcste_communicate(const char *src_name,
115                                          const char *dst_name, double size,
116                                          double rate)
117 {
118   surf_action_network_Constant_t action = NULL;
119
120   XBT_IN("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
121
122   action =
123       surf_action_new(sizeof(s_surf_action_network_Constant_t), size,
124                       surf_network_model, 0);
125
126   action->suspended = 0;
127
128   action->latency = sg_latency_factor;          //random_generate(random_latency);
129   action->lat_init = action->latency;
130
131   if (action->latency <= 0.0) {
132     action->generic_action.state_set =
133         surf_network_model->states.done_action_set;
134     xbt_swag_insert(action, action->generic_action.state_set);
135   }
136
137   XBT_OUT();
138
139   return (surf_action_t) action;
140 }
141
142 #ifdef HAVE_TRACING
143 static void netcste_action_set_category(surf_action_t action, const char *category)
144 {
145   //ignore completely the categories in constant model, they are not traced
146 }
147 #endif
148
149 /* returns an array of link_Constant_t */
150 static xbt_dynar_t netcste_get_route(void *src, void *dst)
151 {
152   xbt_die("Calling this function does not make any sense");
153 }
154
155 static double netcste_get_link_bandwidth(const void *link)
156 {
157   DIE_IMPOSSIBLE;
158 }
159
160 static double netcste_get_link_latency(const void *link)
161 {
162   DIE_IMPOSSIBLE;
163 }
164
165 static int link_shared(const void *link)
166 {
167   DIE_IMPOSSIBLE;
168 }
169
170 static void netcste_action_suspend(surf_action_t action)
171 {
172   ((surf_action_network_Constant_t) action)->suspended = 1;
173 }
174
175 static void netcste_action_resume(surf_action_t action)
176 {
177   if (((surf_action_network_Constant_t) action)->suspended)
178     ((surf_action_network_Constant_t) action)->suspended = 0;
179 }
180
181 static int netcste_action_is_suspended(surf_action_t action)
182 {
183   return ((surf_action_network_Constant_t) action)->suspended;
184 }
185
186 static void netcste_finalize(void)
187 {
188   surf_model_exit(surf_network_model);
189   surf_network_model = NULL;
190 }
191
192
193
194 void surf_network_model_init_Constant(const char *filename)
195 {
196   xbt_assert(surf_network_model == NULL);
197   if (surf_network_model)
198     return;
199   surf_network_model = surf_model_init();
200
201   surf_network_model->name = "constant time network";
202   surf_network_model->action_unref = netcste_action_unref;
203   surf_network_model->action_cancel = netcste_action_cancel;
204   surf_network_model->action_recycle = net_action_recycle;
205   surf_network_model->get_remains = net_action_get_remains;
206 #ifdef HAVE_LATENCY_BOUND_TRACKING
207   surf_network_model->get_latency_limited = net_get_link_latency_limited;
208 #endif
209
210   surf_network_model->model_private->resource_used = netcste_resource_used;
211   surf_network_model->model_private->share_resources =
212       netcste_share_resources;
213   surf_network_model->model_private->update_actions_state =
214       netcste_update_actions_state;
215   surf_network_model->model_private->update_resource_state =
216       netcste_update_resource_state;
217   surf_network_model->model_private->finalize = netcste_finalize;
218
219   surf_network_model->suspend = netcste_action_suspend;
220   surf_network_model->resume = netcste_action_resume;
221   surf_network_model->is_suspended = netcste_action_is_suspended;
222   surf_cpu_model->set_max_duration = net_action_set_max_duration;
223
224   surf_network_model->extension.network.communicate = netcste_communicate;
225   surf_network_model->extension.network.get_link_bandwidth =
226       netcste_get_link_bandwidth;
227   surf_network_model->extension.network.get_link_latency =
228       netcste_get_link_latency;
229   surf_network_model->extension.network.link_shared = link_shared;
230 #ifdef HAVE_TRACING
231   surf_network_model->set_category = netcste_action_set_category;
232 #endif
233
234   if (!random_latency)
235     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
236   netcste_define_callbacks(filename);
237   xbt_dynar_push(model_list, &surf_network_model);
238
239   update_model_description(surf_network_model_description,
240                            "Constant", surf_network_model);
241
242   xbt_cfg_set_string(_surf_cfg_set, "routing", "none");
243   routing_model_create(sizeof(double), NULL, netcste_get_link_latency);
244 }