Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Declaration of the variables at the beginning of the functions
[simgrid.git] / src / simix / smx_action.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11 #include "xbt/ex.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
14                                 "Logging specific to SIMIX (action)");
15
16 /************************************* Actions *********************************/
17 /** \brief Creates a new SIMIX action to communicate two hosts.
18  *
19  *      This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a network_error exception if the host is unavailable. 
20  *      \param sender SIMIX host sender
21  *      \param receiver SIMIX host receiver
22  *      \param name Action name
23  *      \param size Communication size (in bytes)
24  *      \param rate Communication rate between hosts.
25  *      \return A new SIMIX action
26  * */
27 smx_action_t SIMIX_action_communicate(smx_host_t sender,
28                                       smx_host_t receiver, const char *name,
29                                       double size, double rate)
30 {
31          smx_action_t act;
32          smx_simdata_action_t simdata;
33
34   /* check if the host is active */
35   if (surf_workstation_model->extension_public->
36       get_state(sender->simdata->host) != SURF_CPU_ON) {
37     THROW1(network_error, 0,
38            "Host %s failed, you cannot call this function", sender->name);
39   }
40   if (surf_workstation_model->extension_public->
41       get_state(receiver->simdata->host) != SURF_CPU_ON) {
42     THROW1(network_error, 0,
43            "Host %s failed, you cannot call this function",
44            receiver->name);
45   }
46
47   /* alloc structures */
48   act = xbt_new0(s_smx_action_t, 1);
49   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
50   simdata = act->simdata;
51   act->cond_list = xbt_fifo_new();
52
53   /* initialize them */
54   act->name = xbt_strdup(name);
55   simdata->source = sender;
56
57
58   simdata->surf_action = surf_workstation_model->extension_public->
59       communicate(sender->simdata->host,
60                   receiver->simdata->host, size, rate);
61   surf_workstation_model->common_public->action_set_data(simdata->
62                                                             surf_action,
63                                                             act);
64
65   DEBUG1("Create communicate action %p", act);
66   return act;
67 }
68
69 /** \brief Creates a new SIMIX action to execute an action.
70  *
71  *      This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a host_error exception if the host crashed. 
72  *      \param host SIMIX host where the action will be executed
73  *      \param name Action name
74  *      \param amount Task amount (in bytes)
75  *      \return A new SIMIX action
76  * */
77 smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
78                                   double amount)
79 {
80          smx_action_t act;
81          smx_simdata_action_t simdata;
82
83   /* check if the host is active */
84   if (surf_workstation_model->extension_public->
85       get_state(host->simdata->host) != SURF_CPU_ON) {
86     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
87            host->name);
88   }
89
90   /* alloc structures */
91   act = xbt_new0(s_smx_action_t, 1);
92   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
93   simdata = act->simdata;
94   act->cond_list = xbt_fifo_new();
95
96   /* initialize them */
97   simdata->source = host;
98   act->name = xbt_strdup(name);
99
100   /* set communication */
101   simdata->surf_action = surf_workstation_model->extension_public->
102       execute(host->simdata->host, amount);
103
104   surf_workstation_model->common_public->action_set_data(simdata->
105                                                             surf_action,
106                                                             act);
107
108   DEBUG1("Create execute action %p", act);
109   return act;
110 }
111
112 /** \brief Creates a new sleep SIMIX action.
113  *
114  *      This function creates a SURF action and allocates the data necessary to create the SIMIX action. It can raise a host_error exception if the host crashed. The default SIMIX name of the action is "sleep".  
115  *      \param host SIMIX host where the sleep will run.
116  *      \param duration Time duration of the sleep.
117  *      \return A new SIMIX action
118  * */
119 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
120 {
121   char name[] = "sleep";
122   smx_simdata_action_t simdata;
123   smx_action_t act;
124
125   /* check if the host is active */
126   if (surf_workstation_model->extension_public->
127       get_state(host->simdata->host) != SURF_CPU_ON) {
128     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
129            host->name);
130   }
131
132   /* alloc structures */
133   act = xbt_new0(s_smx_action_t, 1);
134   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
135   simdata = act->simdata;
136   act->cond_list = xbt_fifo_new();
137
138   /* initialize them */
139   simdata->source = host;
140   act->name = xbt_strdup(name);
141
142   simdata->surf_action = surf_workstation_model->extension_public->
143       sleep(host->simdata->host, duration);
144
145   surf_workstation_model->common_public->action_set_data(simdata->
146                                                             surf_action,
147                                                             act);
148
149   DEBUG1("Create sleep action %p", act);
150   return act;
151 }
152
153 /**
154  *      \brief Cancels an action.
155  *
156  *      This functions stops the execution of an action. It calls a surf functions.
157  *      \param action The SIMIX action
158  */
159 void SIMIX_action_cancel(smx_action_t action)
160 {
161   xbt_assert0((action != NULL), "Invalid parameter");
162
163   DEBUG1("Cancel action %p", action);
164   if (action->simdata->surf_action) {
165     surf_workstation_model->common_public->action_cancel(action->
166                                                             simdata->
167                                                             surf_action);
168   }
169   return;
170 }
171
172 /**
173  *      \brief Changes the action's priority
174  *
175  *      This functions changes the priority only. It calls a surf functions.
176  *      \param action The SIMIX action
177  *      \param priority The new priority
178  */
179 void SIMIX_action_set_priority(smx_action_t action, double priority)
180 {
181   xbt_assert0((action != NULL)
182               && (action->simdata != NULL), "Invalid parameter");
183
184   surf_workstation_model->common_public->
185       set_priority(action->simdata->surf_action, priority);
186   return;
187 }
188
189 /**
190  *      \brief Destroys an action
191  *
192  *      Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it. 
193  *      \param action The SIMIX action
194  */
195 void SIMIX_action_destroy(smx_action_t action)
196 {
197   xbt_assert0((action != NULL), "Invalid parameter");
198
199   xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
200               "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
201               xbt_fifo_size(action->cond_list));
202
203   DEBUG1("Destroy action %p", action);
204   if (action->name)
205     xbt_free(action->name);
206
207   xbt_fifo_free(action->cond_list);
208
209   if (action->simdata->surf_action)
210     action->simdata->surf_action->model_type->common_public->
211         action_free(action->simdata->surf_action);
212
213   xbt_free(action->simdata);
214   xbt_free(action);
215   return;
216 }
217
218 /**
219  *      \brief Set an action to a condition
220  *
221  *      Creates the "link" between an action and a condition. You have to call this function when you create an action and want to wait its ending. 
222  *      \param action SIMIX action
223  *      \param cond SIMIX cond
224  */
225 void SIMIX_register_action_to_condition(smx_action_t action,
226                                         smx_cond_t cond)
227 {
228   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
229
230   DEBUG2("Register action %p to cond %p", action, cond);
231   __SIMIX_cond_display_actions(cond);
232   xbt_fifo_push(cond->actions, action);
233   __SIMIX_cond_display_actions(cond);
234   DEBUG2("Register condition %p to action %p", cond, action);
235   __SIMIX_action_display_conditions(action);
236   xbt_fifo_push(action->cond_list, cond);
237   __SIMIX_action_display_conditions(action);
238 }
239
240 /**
241  *      \brief Unset an action to a condition.
242  *
243  *      Destroys the "links" from the condition to this action.
244  *      \param action SIMIX action
245  *      \param cond SIMIX cond
246  */
247 void SIMIX_unregister_action_to_condition(smx_action_t action,
248                                           smx_cond_t cond)
249 {
250   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
251
252   __SIMIX_cond_display_actions(cond);
253   xbt_fifo_remove_all(cond->actions, action);
254   __SIMIX_cond_display_actions(cond);
255   __SIMIX_action_display_conditions(action);
256   xbt_fifo_remove_all(action->cond_list, cond);
257   __SIMIX_action_display_conditions(action);
258 }
259
260 /**
261  *      \brief Return how much remais to be done in the action.
262  *
263  *      \param action The SIMIX action
264  *      \return Remains cost
265  */
266 double SIMIX_action_get_remains(smx_action_t action)
267 {
268   xbt_assert0((action != NULL), "Invalid parameter");
269   return action->simdata->surf_action->remains;
270 }
271
272 smx_action_t SIMIX_action_parallel_execute(const char *name, int host_nb,
273                                            const smx_host_t *host_list,
274                                            double *computation_amount,
275                                            double *communication_amount,
276                                            double amount, double rate)
277 {
278   void **workstation_list = NULL;
279   smx_simdata_action_t simdata;
280   smx_action_t act;
281   int i;
282
283   /* alloc structures */
284   act = xbt_new0(s_smx_action_t, 1);
285   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
286   simdata = act->simdata;
287   act->cond_list = xbt_fifo_new();
288
289   /* initialize them */
290   act->name = xbt_strdup(name);
291
292   /* set action */
293
294   workstation_list = xbt_new0(void *,host_nb);
295   for (i = 0; i < host_nb; i++)
296     workstation_list[i] = host_list[i]->simdata->host;
297
298   simdata->surf_action =
299       surf_workstation_model->extension_public->
300       execute_parallel_task(host_nb, workstation_list,
301                             computation_amount, communication_amount,
302                             amount, rate);
303
304   surf_workstation_model->common_public->action_set_data(simdata->
305                                                          surf_action,
306                                                          act);
307
308   return act;
309 }
310
311 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
312 {
313   xbt_assert0((action != NULL), "Invalid parameter");
314   return surf_workstation_model->common_public->
315       action_get_state(action->simdata->surf_action);
316
317 }
318
319 void __SIMIX_cond_display_actions(smx_cond_t cond)
320 {
321   xbt_fifo_item_t item = NULL;
322   smx_action_t action = NULL;
323
324   DEBUG1("Actions for condition %p", cond);
325   xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
326       DEBUG1("\t %p", action);
327 }
328
329 void __SIMIX_action_display_conditions(smx_action_t action)
330 {
331   xbt_fifo_item_t item = NULL;
332   smx_cond_t cond = NULL;
333
334   DEBUG1("Conditions for action %p", action);
335   xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
336       DEBUG1("\t %p", cond);
337 }