Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fixed full routing support for routers, no routing complexity is added routers are...
[simgrid.git] / src / surf / network_constant.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "surf_private.h"
9 #include "surf/random_mgr.h"
10 #include "xbt/dict.h"
11 #include "xbt/str.h"
12 #include "xbt/log.h"
13
14 typedef struct surf_action_network_Constant {
15   s_surf_action_t generic_action;
16   double latency;
17   double lat_init;
18   int suspended;
19 } s_surf_action_network_Constant_t, *surf_action_network_Constant_t;
20
21 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(surf_network);
22 static random_data_t random_latency = NULL;
23 static int host_number = 0;
24
25 static void count_hosts(void)
26 {
27   host_number++;
28 }
29
30 static void define_callbacks(const char *file)
31 {
32   /* Figuring out the network links */
33   surfxml_add_callback(STag_surfxml_host_cb_list, &count_hosts);
34 }
35
36 static int resource_used(void *resource_id)
37 {
38   return 0;
39 }
40
41 static int action_unref(surf_action_t action)
42 {
43   action->refcount--;
44   if (!action->refcount) {
45     xbt_swag_remove(action, action->state_set);
46     free(action);
47     return 1;
48   }
49   return 0;
50 }
51
52 static void action_cancel(surf_action_t action)
53 {
54   return;
55 }
56
57 static void action_recycle(surf_action_t action)
58 {
59   return;
60 }
61
62 static double action_get_remains(surf_action_t action)
63 {
64   return action->remains;
65 }
66
67 static double share_resources(double now)
68 {
69   surf_action_network_Constant_t action = NULL;
70   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
71   double min = -1.0;
72
73   xbt_swag_foreach(action, running_actions) {
74     if (action->latency > 0) {
75       if (min < 0)
76         min = action->latency;
77       else if (action->latency < min)
78         min = action->latency;
79     }
80   }
81
82   return min;
83 }
84
85 static void update_actions_state(double now, double delta)
86 {
87   surf_action_network_Constant_t action = NULL;
88   surf_action_network_Constant_t next_action = NULL;
89   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
90
91   xbt_swag_foreach_safe(action, next_action, running_actions) {
92     if (action->latency > 0) {
93       if (action->latency > delta) {
94         double_update(&(action->latency), delta);
95       } else {
96         action->latency = 0.0;
97       }
98     }
99     double_update(&(action->generic_action.remains),
100                   action->generic_action.cost * delta / action->lat_init);
101     if (action->generic_action.max_duration != NO_MAX_DURATION)
102       double_update(&(action->generic_action.max_duration), delta);
103
104     if (action->generic_action.remains <= 0) {
105       action->generic_action.finish = surf_get_clock();
106       surf_network_model->action_state_set((surf_action_t) action,
107                                            SURF_ACTION_DONE);
108     } else if ((action->generic_action.max_duration != NO_MAX_DURATION)
109                && (action->generic_action.max_duration <= 0)) {
110       action->generic_action.finish = surf_get_clock();
111       surf_network_model->action_state_set((surf_action_t) action,
112                                            SURF_ACTION_DONE);
113     }
114   }
115 }
116
117 static void update_resource_state(void *id,
118                                   tmgr_trace_event_t event_type,
119                                   double value, double time)
120 {
121   DIE_IMPOSSIBLE;
122 }
123
124 static surf_action_t communicate(const char *src_name, const char *dst_name,
125                                  int src, int dst, double size, double rate)
126 {
127   surf_action_network_Constant_t action = NULL;
128
129   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
130
131   action =
132     surf_action_new(sizeof(s_surf_action_network_Constant_t), size,
133                     surf_network_model, 0);
134
135   action->suspended = 0;
136
137   action->latency = 1;          //random_generate(random_latency);
138   action->lat_init = action->latency;
139
140   if (action->latency <= 0.0) {
141     action->generic_action.state_set =
142       surf_network_model->states.done_action_set;
143     xbt_swag_insert(action, action->generic_action.state_set);
144   }
145
146   XBT_OUT;
147
148   return (surf_action_t) action;
149 }
150
151 /* returns an array of link_Constant_t */
152 static xbt_dynar_t get_route(void *src, void *dst)
153 {
154   xbt_die("Calling this function does not make any sense");
155 }
156
157 static double get_link_bandwidth(const void *link)
158 {
159   DIE_IMPOSSIBLE;
160 }
161
162 static double get_link_latency(const void *link)
163 {
164   DIE_IMPOSSIBLE;
165 }
166
167 static int link_shared(const void *link)
168 {
169   DIE_IMPOSSIBLE;
170 }
171
172 static void action_suspend(surf_action_t action)
173 {
174   ((surf_action_network_Constant_t) action)->suspended = 1;
175 }
176
177 static void action_resume(surf_action_t action)
178 {
179   if (((surf_action_network_Constant_t) action)->suspended)
180     ((surf_action_network_Constant_t) action)->suspended = 0;
181 }
182
183 static int action_is_suspended(surf_action_t action)
184 {
185   return ((surf_action_network_Constant_t) action)->suspended;
186 }
187
188 static void action_set_max_duration(surf_action_t action, double duration)
189 {
190   action->max_duration = duration;
191 }
192
193 static void finalize(void)
194 {
195   surf_model_exit(surf_network_model);
196   surf_network_model = NULL;
197 }
198
199
200
201 void surf_network_model_init_Constant(const char *filename)
202 {
203   xbt_assert(surf_network_model == NULL);
204   if (surf_network_model)
205     return;
206   surf_network_model = surf_model_init();
207
208   INFO0("Blah");
209   surf_network_model->name = "constant time network";
210   surf_network_model->action_unref = action_unref;
211   surf_network_model->action_cancel = action_cancel;
212   surf_network_model->action_recycle = action_recycle;
213   surf_network_model->get_remains = action_get_remains;
214
215   surf_network_model->model_private->resource_used = resource_used;
216   surf_network_model->model_private->share_resources = share_resources;
217   surf_network_model->model_private->update_actions_state =
218     update_actions_state;
219   surf_network_model->model_private->update_resource_state =
220     update_resource_state;
221   surf_network_model->model_private->finalize = finalize;
222
223   surf_network_model->suspend = action_suspend;
224   surf_network_model->resume = action_resume;
225   surf_network_model->is_suspended = action_is_suspended;
226   surf_cpu_model->set_max_duration = action_set_max_duration;
227
228   surf_network_model->extension.network.communicate = communicate;
229   surf_network_model->extension.network.get_link_bandwidth =
230     get_link_bandwidth;
231   surf_network_model->extension.network.get_link_latency = get_link_latency;
232   surf_network_model->extension.network.link_shared = link_shared;
233
234   if (!random_latency)
235     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
236   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);
244 }