Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow to instanciate the constant network model. It uses a newly created routing...
[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 share_resources(double now)
63 {
64   surf_action_network_Constant_t action = NULL;
65   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
66   double min = -1.0;
67
68   xbt_swag_foreach(action, running_actions) {
69     if (action->latency > 0) {
70       if (min < 0)
71         min = action->latency;
72       else if (action->latency < min)
73         min = action->latency;
74     }
75   }
76
77   return min;
78 }
79
80 static void update_actions_state(double now, double delta)
81 {
82   surf_action_network_Constant_t action = NULL;
83   surf_action_network_Constant_t next_action = NULL;
84   xbt_swag_t running_actions = surf_network_model->states.running_action_set;
85
86   xbt_swag_foreach_safe(action, next_action, running_actions) {
87     if (action->latency > 0) {
88       if (action->latency > delta) {
89         double_update(&(action->latency), delta);
90       } else {
91         action->latency = 0.0;
92       }
93     }
94     double_update(&(action->generic_action.remains),
95                   action->generic_action.cost * delta / action->lat_init);
96     if (action->generic_action.max_duration != NO_MAX_DURATION)
97       double_update(&(action->generic_action.max_duration), delta);
98
99     if (action->generic_action.remains <= 0) {
100       action->generic_action.finish = surf_get_clock();
101       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
102     } else if ((action->generic_action.max_duration != NO_MAX_DURATION) &&
103                (action->generic_action.max_duration <= 0)) {
104       action->generic_action.finish = surf_get_clock();
105       surf_network_model->action_state_set((surf_action_t) action, SURF_ACTION_DONE);
106     }
107   }
108 }
109
110 static void update_resource_state(void *id,
111                                   tmgr_trace_event_t event_type,
112                                   double value, double time)
113 {
114   DIE_IMPOSSIBLE;
115 }
116
117 static surf_action_t communicate(const char *src_name,const char *dst_name,int src, int dst, double size,
118     double rate)
119 {
120   surf_action_network_Constant_t action = NULL;
121
122   XBT_IN4("(%s,%s,%g,%g)", src_name, dst_name, size, rate);
123
124   action = surf_action_new(sizeof(s_surf_action_network_Constant_t),size,surf_network_model,0);
125
126   action->suspended = 0;
127
128   action->latency = 1;//random_generate(random_latency);
129   action->lat_init = action->latency;
130   INFO1("Latency: %f",action->latency);
131
132   if (action->latency <= 0.0) {
133     action->generic_action.state_set =
134       surf_network_model->states.done_action_set;
135     xbt_swag_insert(action, action->generic_action.state_set);
136   }
137
138   XBT_OUT;
139
140   return (surf_action_t) action;
141 }
142
143 /* returns an array of link_Constant_t */
144 static xbt_dynar_t get_route(void *src, void *dst)
145 {
146   xbt_die("Calling this function does not make any sense");
147 }
148
149 static double get_link_bandwidth(const void *link)
150 {
151   DIE_IMPOSSIBLE;
152 }
153
154 static double get_link_latency(const void *link)
155 {
156   DIE_IMPOSSIBLE;
157 }
158
159 static int link_shared(const void *link)
160 {
161   DIE_IMPOSSIBLE;
162 }
163
164 static void action_suspend(surf_action_t action)
165 {
166   ((surf_action_network_Constant_t) action)->suspended = 1;
167 }
168
169 static void action_resume(surf_action_t action)
170 {
171   if (((surf_action_network_Constant_t) action)->suspended)
172     ((surf_action_network_Constant_t) action)->suspended = 0;
173 }
174
175 static int action_is_suspended(surf_action_t action)
176 {
177   return ((surf_action_network_Constant_t) action)->suspended;
178 }
179
180 static void action_set_max_duration(surf_action_t action, double duration)
181 {
182   action->max_duration = duration;
183 }
184
185 static void finalize(void)
186 {
187   surf_model_exit(surf_network_model);
188   surf_network_model = NULL;
189 }
190
191
192
193 void surf_network_model_init_Constant(const char *filename)
194 {
195   xbt_assert(surf_network_model == NULL);
196   if (surf_network_model)
197     return;
198   surf_network_model = surf_model_init();
199
200   INFO0("Blah");
201   surf_network_model->name = "constant time network";
202   surf_network_model->action_unref = action_unref;
203   surf_network_model->action_cancel = action_cancel;
204   surf_network_model->action_recycle = action_recycle;
205
206   surf_network_model->model_private->resource_used = resource_used;
207   surf_network_model->model_private->share_resources = share_resources;
208   surf_network_model->model_private->update_actions_state =
209     update_actions_state;
210   surf_network_model->model_private->update_resource_state =
211     update_resource_state;
212   surf_network_model->model_private->finalize = finalize;
213
214   surf_network_model->suspend = action_suspend;
215   surf_network_model->resume = action_resume;
216   surf_network_model->is_suspended = action_is_suspended;
217   surf_cpu_model->set_max_duration = action_set_max_duration;
218
219   surf_network_model->extension.network.communicate = communicate;
220   surf_network_model->extension.network.get_link_bandwidth =
221     get_link_bandwidth;
222   surf_network_model->extension.network.get_link_latency = get_link_latency;
223   surf_network_model->extension.network.link_shared = link_shared;
224
225   if (!random_latency)
226     random_latency = random_new(RAND, 100, 0.0, 1.0, .125, .034);
227   define_callbacks(filename);
228   xbt_dynar_push(model_list, &surf_network_model);
229
230   update_model_description(surf_network_model_description,
231                            "Constant", surf_network_model);
232
233   xbt_cfg_set_string(_surf_cfg_set,"routing","none");
234   routing_model_create(sizeof(double),NULL);
235 }
236