Logo AND Algorithmique Numérique Distribuée

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