Logo AND Algorithmique Numérique Distribuée

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