3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4 All rights reserved. */
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. */
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
14 "Logging specific to SIMIX (action)");
16 /************************************* Actions *********************************/
17 /** \brief Creates a new SIMIX action to communicate two hosts.
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
27 smx_action_t SIMIX_action_communicate(smx_host_t sender,
28 smx_host_t receiver, const char *name,
29 double size, double rate)
33 /* check if the host is active */
34 if (surf_workstation_model->extension.workstation.get_state(sender->host) !=
36 THROW1(network_error, 0, "Host %s failed, you cannot call this function",
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",
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();
51 act->name = xbt_strdup(name);
56 surf_workstation_model->extension.workstation.communicate(sender->host,
59 surf_workstation_model->action_data_set(act->surf_action, act);
61 DEBUG1("Create communicate action %p", act);
65 /** \brief Creates a new SIMIX action to execute an action.
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
73 smx_action_t SIMIX_action_execute(smx_host_t host, const char *name,
78 /* check if the host is active */
79 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
81 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
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();
92 act->name = xbt_strdup(name);
94 /* set communication */
96 surf_workstation_model->extension.workstation.execute(host->host, amount);
98 surf_workstation_model->action_data_set(act->surf_action, act);
100 DEBUG1("Create execute action %p", act);
104 /** \brief Creates a new sleep SIMIX action.
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".
110 * \param host SIMIX host where the sleep will run.
111 * \param duration Time duration of the sleep.
112 * \return A new SIMIX action
114 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
116 char name[] = "sleep";
119 /* check if the host is active */
120 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
122 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
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();
131 /* initialize them */
133 act->name = xbt_strdup(name);
136 surf_workstation_model->extension.workstation.sleep(host->host, duration);
138 surf_workstation_model->action_data_set(act->surf_action, act);
140 DEBUG1("Create sleep action %p", act);
145 * \brief Cancels an action.
147 * This functions stops the execution of an action. It calls a surf functions.
148 * \param action The SIMIX action
150 XBT_INLINE void SIMIX_action_cancel(smx_action_t action)
152 xbt_assert0((action != NULL), "Invalid parameter");
154 DEBUG1("Cancel action %p", action);
155 if (action->surf_action) {
156 surf_workstation_model->action_cancel(action->surf_action);
162 * \brief Changes the action's priority
164 * This functions changes the priority only. It calls a surf functions.
165 * \param action The SIMIX action
166 * \param priority The new priority
168 XBT_INLINE void SIMIX_action_set_priority(smx_action_t action, double priority)
170 xbt_assert0((action != NULL), "Invalid parameter");
172 surf_workstation_model->set_priority(action->surf_action, priority);
177 * \brief Destroys an action
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
182 int SIMIX_action_destroy(smx_action_t action)
184 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
185 xbt_assert0((action != NULL), "Invalid parameter");
188 if (action->refcount > 0)
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));
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));
199 DEBUG1("Destroy action %p", action);
201 xbt_free(action->name);
203 xbt_fifo_free(action->cond_list);
204 xbt_fifo_free(action->sem_list);
206 if (action->surf_action)
207 action->surf_action->model_type->action_unref(action->surf_action);
214 * \brief Increase refcount of anan action
216 * \param action The SIMIX action
218 XBT_INLINE void SIMIX_action_use(smx_action_t action)
220 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
221 xbt_assert0((action != NULL), "Invalid parameter");
229 * \brief Decrease refcount of anan action
231 * \param action The SIMIX action
233 XBT_INLINE void SIMIX_action_release(smx_action_t action)
235 xbt_assert0((action != NULL), "Invalid parameter");
243 * \brief Set an action to a condition
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
249 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
251 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
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);
257 xbt_fifo_push(cond->actions, action);
259 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
260 __SIMIX_cond_display_actions(cond);
262 DEBUG2("Register condition %p to action %p", cond, action);
264 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
265 __SIMIX_action_display_conditions(action);
267 xbt_fifo_push(action->cond_list, cond);
269 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
270 __SIMIX_action_display_conditions(action);
274 * \brief Unset an action to a condition.
276 * Destroys the "links" from the condition to this action.
277 * \param action SIMIX action
278 * \param cond SIMIX cond
280 void SIMIX_unregister_action_to_condition(smx_action_t action,
283 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
285 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
286 __SIMIX_cond_display_actions(cond);
288 xbt_fifo_remove_all(cond->actions, action);
290 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
291 __SIMIX_cond_display_actions(cond);
293 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
294 __SIMIX_action_display_conditions(action);
296 xbt_fifo_remove_all(action->cond_list, cond);
298 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
299 __SIMIX_action_display_conditions(action);
302 * \brief Link an action to a semaphore
304 * When the action terminates, the semaphore gets signaled automatically.
306 XBT_INLINE void SIMIX_register_action_to_semaphore(smx_action_t action, smx_sem_t sem) {
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);
313 * \brief Unset an action to a semaphore.
315 * Destroys the "links" from the semaphore to this action.
317 XBT_INLINE void SIMIX_unregister_action_to_semaphore(smx_action_t action,
320 xbt_fifo_remove_all(sem->actions, action);
321 xbt_fifo_remove_all(action->sem_list, sem);
325 * \brief Return how much remais to be done in the action.
327 * \param action The SIMIX action
328 * \return Remains cost
330 XBT_INLINE double SIMIX_action_get_remains(smx_action_t action)
332 xbt_assert0((action != NULL), "Invalid parameter");
333 return surf_workstation_model->get_remains(action->surf_action);
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)
342 void **workstation_list = NULL;
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();
351 /* initialize them */
352 act->name = xbt_strdup(name);
356 workstation_list = xbt_new0(void *, host_nb);
357 for (i = 0; i < host_nb; i++)
358 workstation_list[i] = host_list[i]->host;
361 surf_workstation_model->extension.workstation.
362 execute_parallel_task(host_nb, workstation_list, computation_amount,
363 communication_amount, amount, rate);
365 surf_workstation_model->action_data_set(act->surf_action, act);
370 XBT_INLINE e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
372 xbt_assert0((action != NULL), "Invalid parameter");
373 return surf_workstation_model->action_state_get(action->surf_action);
376 void __SIMIX_cond_display_actions(smx_cond_t cond)
378 xbt_fifo_item_t item = NULL;
379 smx_action_t action = NULL;
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);
386 void __SIMIX_action_display_conditions(smx_action_t action)
388 xbt_fifo_item_t item = NULL;
389 smx_cond_t cond = NULL;
391 DEBUG1("Conditions for action %p", action);
392 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
393 DEBUG1("\t %p", cond);
396 XBT_INLINE char *SIMIX_action_get_name(smx_action_t action)
398 xbt_assert0((action != NULL), "Invalid parameter");
401 /** @brief Change the name of the action. Warning, the string you provide is not strdup()ed */
402 XBT_INLINE void SIMIX_action_set_name(smx_action_t action,char *name)
404 xbt_free(action->name);
408 /** @brief broadcast any condition and release any semaphore including this action */
409 void SIMIX_action_signal_all(smx_action_t action){
413 while ((cond = xbt_fifo_pop(action->cond_list)))
414 SIMIX_cond_broadcast(cond);
416 while ((sem = xbt_fifo_pop(action->sem_list)))
417 SIMIX_sem_release(sem);