Logo AND Algorithmique Numérique Distribuée

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