Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clean the links between action and conditions. Now there are two 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, char *name,
29                                       double size, double rate)
30 {
31   /* check if the host is active */
32   if (surf_workstation_resource->extension_public->
33       get_state(sender->simdata->host) != SURF_CPU_ON) {
34     THROW1(network_error, 0,
35            "Host %s failed, you cannot call this function", sender->name);
36   }
37   if (surf_workstation_resource->extension_public->
38       get_state(receiver->simdata->host) != SURF_CPU_ON) {
39     THROW1(network_error, 0,
40            "Host %s failed, you cannot call this function",
41            receiver->name);
42   }
43
44   /* alloc structures */
45   smx_action_t act = xbt_new0(s_smx_action_t, 1);
46   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
47   smx_simdata_action_t simdata = act->simdata;
48   act->cond_list = xbt_fifo_new();
49
50   /* initialize them */
51   act->name = xbt_strdup(name);
52   simdata->source = sender;
53
54
55   simdata->surf_action = surf_workstation_resource->extension_public->
56       communicate(sender->simdata->host,
57                   receiver->simdata->host, size, rate);
58   surf_workstation_resource->common_public->action_set_data(simdata->
59                                                             surf_action,
60                                                             act);
61
62   DEBUG1("Create communicate action %p", act);
63   return act;
64 }
65
66 /** \brief Creates a new SIMIX action to execute an action.
67  *
68  *      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. 
69  *      \param host SIMIX host where the action will be executed
70  *      \param name Action name
71  *      \param amount Task amount (in bytes)
72  *      \return A new SIMIX action
73  * */
74 smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
75                                   double amount)
76 {
77   /* check if the host is active */
78   if (surf_workstation_resource->extension_public->
79       get_state(host->simdata->host) != SURF_CPU_ON) {
80     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
81            host->name);
82   }
83
84   /* alloc structures */
85   smx_action_t act = xbt_new0(s_smx_action_t, 1);
86   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
87   smx_simdata_action_t simdata = act->simdata;
88   act->cond_list = xbt_fifo_new();
89
90   /* initialize them */
91   simdata->source = host;
92   act->name = xbt_strdup(name);
93
94   /* set communication */
95   simdata->surf_action = surf_workstation_resource->extension_public->
96       execute(host->simdata->host, amount);
97
98   surf_workstation_resource->common_public->action_set_data(simdata->
99                                                             surf_action,
100                                                             act);
101
102   DEBUG1("Create execute action %p", act);
103   return act;
104 }
105
106 /** \brief Creates a new sleep SIMIX action.
107  *
108  *      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".  
109  *      \param host SIMIX host where the sleep will run.
110  *      \param duration Time duration of the sleep.
111  *      \return A new SIMIX action
112  * */
113 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
114 {
115   char name[] = "sleep";
116
117   /* check if the host is active */
118   if (surf_workstation_resource->extension_public->
119       get_state(host->simdata->host) != SURF_CPU_ON) {
120     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
121            host->name);
122   }
123
124   /* alloc structures */
125   smx_action_t act = xbt_new0(s_smx_action_t, 1);
126   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
127   smx_simdata_action_t simdata = act->simdata;
128   act->cond_list = xbt_fifo_new();
129
130   /* initialize them */
131   simdata->source = host;
132   act->name = xbt_strdup(name);
133
134   simdata->surf_action = surf_workstation_resource->extension_public->
135       sleep(host->simdata->host, duration);
136
137   surf_workstation_resource->common_public->action_set_data(simdata->
138                                                             surf_action,
139                                                             act);
140
141   DEBUG1("Create sleep action %p", act);
142   return act;
143 }
144
145 /**
146  *      \brief Cancels an action.
147  *
148  *      This functions stops the execution of an action. It calls a surf functions.
149  *      \param action The SIMIX action
150  */
151 void SIMIX_action_cancel(smx_action_t action)
152 {
153   xbt_assert0((action != NULL), "Invalid parameter");
154
155   DEBUG1("Cancel action %p", action);
156   if (action->simdata->surf_action) {
157     surf_workstation_resource->common_public->action_cancel(action->
158                                                             simdata->
159                                                             surf_action);
160   }
161   return;
162 }
163
164 /**
165  *      \brief Changes the action's priority
166  *
167  *      This functions changes the priority only. It calls a surf functions.
168  *      \param action The SIMIX action
169  *      \param priority The new priority
170  */
171 void SIMIX_action_set_priority(smx_action_t action, double priority)
172 {
173   xbt_assert0((action != NULL)
174               && (action->simdata != NULL), "Invalid parameter");
175
176   surf_workstation_resource->common_public->
177       set_priority(action->simdata->surf_action, priority);
178   return;
179 }
180
181 /**
182  *      \brief Destroys an action
183  *
184  *      Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it. 
185  *      \param action The SIMIX action
186  */
187 void SIMIX_action_destroy(smx_action_t action)
188 {
189   xbt_assert0((action != NULL), "Invalid parameter");
190
191   xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
192               "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
193               xbt_fifo_size(action->cond_list));
194
195   DEBUG1("Destroy action %p", action);
196   if (action->name)
197     xbt_free(action->name);
198
199   xbt_fifo_free(action->cond_list);
200
201   if (action->simdata->surf_action)
202     action->simdata->surf_action->resource_type->common_public->
203         action_free(action->simdata->surf_action);
204
205   xbt_free(action->simdata);
206   xbt_free(action);
207   return;
208 }
209
210 /**
211  *      \brief Set an action to a condition
212  *
213  *      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. 
214  *      \param action SIMIX action
215  *      \param cond SIMIX cond
216  */
217 void SIMIX_register_action_to_condition(smx_action_t action,
218                                         smx_cond_t cond)
219 {
220   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
221
222   DEBUG2("Register action %p to cond %p", action, cond);
223   __SIMIX_cond_display_actions(cond);
224   xbt_fifo_push(cond->actions, action);
225   __SIMIX_cond_display_actions(cond);
226   DEBUG2("Register condition %p to action %p", cond, action);
227   __SIMIX_action_display_conditions(action);
228   xbt_fifo_push(action->cond_list, cond);
229   __SIMIX_action_display_conditions(action);
230 }
231
232 /**
233  *      \brief Unset an action to a condition.
234  *
235  *      Destroys the "links" from the condition to this action.
236  *      \param action SIMIX action
237  *      \param cond SIMIX cond
238  */
239 void SIMIX_unregister_action_to_condition(smx_action_t action,
240                                           smx_cond_t cond)
241 {
242   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
243
244   __SIMIX_cond_display_actions(cond);
245   xbt_fifo_remove_all(cond->actions, action);
246   __SIMIX_cond_display_actions(cond);
247   __SIMIX_action_display_conditions(action);
248   xbt_fifo_remove_all(action->cond_list, cond);
249   __SIMIX_action_display_conditions(action);
250 }
251
252 /**
253  *      \brief Return how much remais to be done in the action.
254  *
255  *      \param action The SIMIX action
256  *      \return Remains cost
257  */
258 double SIMIX_action_get_remains(smx_action_t action)
259 {
260   xbt_assert0((action != NULL), "Invalid parameter");
261   return action->simdata->surf_action->remains;
262 }
263
264 smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb,
265                                            void **workstation_list,
266                                            double *computation_amount,
267                                            double *communication_amount,
268                                            double amount, double rate)
269 {
270
271   /* alloc structures */
272   smx_action_t act = xbt_new0(s_smx_action_t, 1);
273   act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
274   smx_simdata_action_t simdata = act->simdata;
275   act->cond_list = xbt_fifo_new();
276
277   /* initialize them */
278   act->name = xbt_strdup(name);
279
280   /* set communication */
281   simdata->surf_action =
282       surf_workstation_resource->extension_public->
283       execute_parallel_task(workstation_nb, workstation_list,
284                             computation_amount, communication_amount,
285                             amount, rate);
286
287   surf_workstation_resource->common_public->action_set_data(simdata->
288                                                             surf_action,
289                                                             act);
290
291   return act;
292 }
293
294 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
295 {
296   xbt_assert0((action != NULL), "Invalid parameter");
297   return surf_workstation_resource->common_public->
298       action_get_state(action->simdata->surf_action);
299
300 }
301
302 void __SIMIX_cond_display_actions(smx_cond_t cond)
303 {
304   xbt_fifo_item_t item = NULL;
305   smx_action_t action = NULL;
306
307   DEBUG1("Actions for condition %p", cond);
308   xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
309       DEBUG1("\t %p", action);
310 }
311
312 void __SIMIX_action_display_conditions(smx_action_t action)
313 {
314   xbt_fifo_item_t item = NULL;
315   smx_cond_t cond = NULL;
316
317   DEBUG1("Conditions for action %p", action);
318   xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
319       DEBUG1("\t %p", cond);
320 }