Logo AND Algorithmique Numérique Distribuée

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