Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
renaming value of presence state to "presence" (to get a good time-slice value later on)
[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.workstation.get_state(sender->host) !=
35       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.workstation.
40       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   act->sem_list = xbt_fifo_new();
49
50   /* initialize them */
51   act->name = xbt_strdup(name);
52   act->source = sender;
53 #ifdef HAVE_TRACING
54   act->category = NULL;
55 #endif
56
57   act->surf_action =
58     surf_workstation_model->extension.workstation.communicate(sender->host,
59                                                               receiver->host,
60                                                               size, rate);
61   surf_workstation_model->action_data_set(act->surf_action, act);
62
63   DEBUG1("Create communicate action %p", act);
64   return act;
65 }
66
67 /** \brief Creates a new SIMIX action to execute an action.
68  *
69  *      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.
70  *      \param host SIMIX host where the action will be executed
71  *      \param name Action name
72  *      \param amount Task amount (in bytes)
73  *      \return A new SIMIX action
74  * */
75 smx_action_t SIMIX_action_execute(smx_host_t host, const char *name,
76                                   double amount)
77 {
78   smx_action_t act;
79
80   /* check if the host is active */
81   if (surf_workstation_model->extension.workstation.get_state(host->host) !=
82       SURF_RESOURCE_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->cond_list = xbt_fifo_new();
90   act->sem_list = xbt_fifo_new();
91
92   /* initialize them */
93   act->source = host;
94   act->name = xbt_strdup(name);
95 #ifdef HAVE_TRACING
96   act->category = NULL;
97 #endif
98
99   /* set communication */
100   act->surf_action =
101     surf_workstation_model->extension.workstation.execute(host->host, amount);
102
103   surf_workstation_model->action_data_set(act->surf_action, act);
104
105   DEBUG1("Create execute action %p", act);
106 #ifdef HAVE_TRACING
107   TRACE_smx_action_execute (act);
108 #endif
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
115  * to create the SIMIX action. It can raise a host_error exception if the
116  * host crashed. The default SIMIX name of the action is "sleep".
117  *
118  *      \param host SIMIX host where the sleep will run.
119  *      \param duration Time duration of the sleep.
120  *      \return A new SIMIX action
121  * */
122 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
123 {
124   char name[] = "sleep";
125   smx_action_t act;
126
127   /* check if the host is active */
128   if (surf_workstation_model->extension.workstation.get_state(host->host) !=
129       SURF_RESOURCE_ON) {
130     THROW1(host_error, 0, "Host %s failed, you cannot call this function",
131            host->name);
132   }
133
134   /* alloc structures */
135   act = xbt_new0(s_smx_action_t, 1);
136   act->cond_list = xbt_fifo_new();
137   act->sem_list = xbt_fifo_new();
138
139   /* initialize them */
140   act->source = host;
141   act->name = xbt_strdup(name);
142 #ifdef HAVE_TRACING
143   act->category = NULL;
144 #endif
145
146   act->surf_action =
147     surf_workstation_model->extension.workstation.sleep(host->host, duration);
148
149   surf_workstation_model->action_data_set(act->surf_action, act);
150
151   DEBUG1("Create sleep action %p", act);
152   return act;
153 }
154
155 /**
156  *      \brief Cancels an action.
157  *
158  *      This functions stops the execution of an action. It calls a surf functions.
159  *      \param action The SIMIX action
160  */
161 XBT_INLINE void SIMIX_action_cancel(smx_action_t action)
162 {
163   xbt_assert0((action != NULL), "Invalid parameter");
164
165   DEBUG1("Cancel action %p", action);
166   if (action->surf_action) {
167     surf_workstation_model->action_cancel(action->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 XBT_INLINE void SIMIX_action_set_priority(smx_action_t action, double priority)
180 {
181   xbt_assert0((action != NULL), "Invalid parameter");
182
183   surf_workstation_model->set_priority(action->surf_action, priority);
184   return;
185 }
186
187 /**
188  *      \brief Resumes the execution of an action.
189  *
190  *  This functions restarts the execution of an action. It just calls the right SURF function.
191  *  \param action The SIMIX action
192  *  \param priority The new priority
193  */
194 XBT_INLINE void SIMIX_action_resume(smx_action_t action)
195 {
196   xbt_assert0((action != NULL), "Invalid parameter");
197
198   surf_workstation_model->resume(action->surf_action);
199   return;
200 }
201
202 /**
203  *  \brief Suspends the execution of an action.
204  *
205  *  This functions suspends the execution of an action. It just calls the right SURF function.
206  *  \param action The SIMIX action
207  *  \param priority The new priority
208  */
209 XBT_INLINE void SIMIX_action_suspend(smx_action_t action)
210 {
211   xbt_assert0((action != NULL), "Invalid parameter");
212
213   surf_workstation_model->suspend(action->surf_action);
214   return;
215 }
216
217 /**
218  *      \brief Destroys an action
219  *
220  *      Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
221  *      \param action The SIMIX action
222  */
223 int SIMIX_action_destroy(smx_action_t action)
224 {
225   XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
226   xbt_assert0((action != NULL), "Invalid parameter");
227
228   action->refcount--;
229   if (action->refcount > 0)
230     return 0;
231
232   xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
233               "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
234               xbt_fifo_size(action->cond_list));
235
236   xbt_assert1((xbt_fifo_size(action->sem_list) == 0),
237               "Semaphore list not empty %d. There is a problem. Cannot destroy it now!",
238               xbt_fifo_size(action->sem_list));
239
240   DEBUG1("Destroy action %p", action);
241   if (action->name)
242     xbt_free(action->name);
243
244   xbt_fifo_free(action->cond_list);
245   xbt_fifo_free(action->sem_list);
246
247   if (action->surf_action)
248     action->surf_action->model_type->action_unref(action->surf_action);
249 #ifdef HAVE_TRACING
250   TRACE_smx_action_destroy (action);
251 #endif
252   xbt_free(action);
253   return 1;
254 }
255
256 /**
257  *      \brief Increase refcount of anan action
258  *
259  *      \param action The SIMIX action
260  */
261 XBT_INLINE void SIMIX_action_use(smx_action_t action)
262 {
263   XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
264   xbt_assert0((action != NULL), "Invalid parameter");
265
266   action->refcount++;
267
268   return;
269 }
270
271 /**
272  *      \brief Decrease refcount of anan action
273  *
274  *      \param action The SIMIX action
275  */
276 XBT_INLINE void SIMIX_action_release(smx_action_t action)
277 {
278   xbt_assert0((action != NULL), "Invalid parameter");
279
280   action->refcount--;
281
282   return;
283 }
284
285 /**
286  *  \brief Set an action to a condition
287  *
288  *  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.
289  *  \param action SIMIX action
290  *  \param cond SIMIX cond
291  */
292 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
293 {
294   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
295
296   DEBUG2("Register action %p to cond %p", action, cond);
297   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
298     __SIMIX_cond_display_actions(cond);
299
300   xbt_fifo_push(cond->actions, action);
301
302   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
303     __SIMIX_cond_display_actions(cond);
304
305   DEBUG2("Register condition %p to action %p", cond, action);
306
307   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
308     __SIMIX_action_display_conditions(action);
309
310   xbt_fifo_push(action->cond_list, cond);
311
312   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
313     __SIMIX_action_display_conditions(action);
314 }
315
316 /**
317  *  \brief Unset an action to a condition.
318  *
319  *  Destroys the "links" from the condition to this action.
320  *  \param action SIMIX action
321  *  \param cond SIMIX cond
322  */
323 void SIMIX_unregister_action_to_condition(smx_action_t action,
324                                           smx_cond_t cond)
325 {
326   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
327
328   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
329     __SIMIX_cond_display_actions(cond);
330
331   xbt_fifo_remove_all(cond->actions, action);
332
333   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
334     __SIMIX_cond_display_actions(cond);
335
336   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
337     __SIMIX_action_display_conditions(action);
338
339   xbt_fifo_remove_all(action->cond_list, cond);
340
341   if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
342     __SIMIX_action_display_conditions(action);
343 }
344 /**
345  *  \brief Link an action to a semaphore
346  *
347  *  When the action terminates, the semaphore gets signaled automatically.
348  */
349 XBT_INLINE void SIMIX_register_action_to_semaphore(smx_action_t action, smx_sem_t sem) {
350
351   DEBUG2("Register action %p to semaphore %p (and otherwise)", action, sem);
352   xbt_fifo_push(sem->actions, action);
353   xbt_fifo_push(action->sem_list, sem);
354 }
355 /**
356  *  \brief Unset an action to a semaphore.
357  *
358  *  Destroys the "links" from the semaphore to this action.
359  */
360 XBT_INLINE void SIMIX_unregister_action_to_semaphore(smx_action_t action,
361                                           smx_sem_t sem)
362 {
363   xbt_fifo_remove_all(sem->actions, action);
364   xbt_fifo_remove_all(action->sem_list, sem);
365 }
366
367 /**
368  *      \brief Return how much remais to be done in the action.
369  *
370  *      \param action The SIMIX action
371  *      \return Remains cost
372  */
373 XBT_INLINE double SIMIX_action_get_remains(smx_action_t action)
374 {
375   xbt_assert0((action != NULL), "Invalid parameter");
376   return surf_workstation_model->get_remains(action->surf_action);
377 }
378
379 smx_action_t SIMIX_action_parallel_execute(char *name, int host_nb,
380                                            smx_host_t * host_list,
381                                            double *computation_amount,
382                                            double *communication_amount,
383                                            double amount, double rate)
384 {
385   void **workstation_list = NULL;
386   smx_action_t act;
387   int i;
388
389   /* alloc structures */
390   act = xbt_new0(s_smx_action_t, 1);
391   act->cond_list = xbt_fifo_new();
392   act->sem_list = xbt_fifo_new();
393
394   /* initialize them */
395   act->name = xbt_strdup(name);
396 #ifdef HAVE_TRACING
397   act->category = NULL;
398 #endif
399
400   /* set action */
401
402   workstation_list = xbt_new0(void *, host_nb);
403   for (i = 0; i < host_nb; i++)
404     workstation_list[i] = host_list[i]->host;
405
406   act->surf_action =
407     surf_workstation_model->extension.workstation.
408     execute_parallel_task(host_nb, workstation_list, computation_amount,
409                           communication_amount, amount, rate);
410
411   surf_workstation_model->action_data_set(act->surf_action, act);
412
413   return act;
414 }
415
416 XBT_INLINE e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
417 {
418   xbt_assert0((action != NULL), "Invalid parameter");
419   return surf_workstation_model->action_state_get(action->surf_action);
420 }
421
422 void __SIMIX_cond_display_actions(smx_cond_t cond)
423 {
424   xbt_fifo_item_t item = NULL;
425   smx_action_t action = NULL;
426
427   DEBUG1("Actions for condition %p", cond);
428   xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
429     DEBUG2("\t %p [%s]", action, action->name);
430 }
431
432 void __SIMIX_action_display_conditions(smx_action_t action)
433 {
434   xbt_fifo_item_t item = NULL;
435   smx_cond_t cond = NULL;
436
437   DEBUG1("Conditions for action %p", action);
438   xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
439     DEBUG1("\t %p", cond);
440 }
441
442 XBT_INLINE char *SIMIX_action_get_name(smx_action_t action)
443 {
444   xbt_assert0((action != NULL), "Invalid parameter");
445   return action->name;
446 }
447 /** @brief Change the name of the action. Warning, the string you provide is not strdup()ed */
448 XBT_INLINE void SIMIX_action_set_name(smx_action_t action,char *name)
449 {
450   xbt_free(action->name);
451   action->name = name;
452 }
453
454 /** @brief broadcast any condition and release any semaphore including this action */
455 void SIMIX_action_signal_all(smx_action_t action){
456   smx_cond_t cond;
457   smx_sem_t sem;
458
459   while ((cond = xbt_fifo_pop(action->cond_list)))
460     SIMIX_cond_broadcast(cond);
461
462   while ((sem = xbt_fifo_pop(action->sem_list)))
463     SIMIX_sem_release(sem);
464 }