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);
95 /* set communication */
97 surf_workstation_model->extension.workstation.execute(host->host, amount);
99 surf_workstation_model->action_data_set(act->surf_action, act);
101 DEBUG1("Create execute action %p", act);
103 TRACE_smx_action_execute (act);
108 /** \brief Creates a new sleep SIMIX action.
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".
114 * \param host SIMIX host where the sleep will run.
115 * \param duration Time duration of the sleep.
116 * \return A new SIMIX action
118 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
120 char name[] = "sleep";
123 /* check if the host is active */
124 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
126 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
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();
135 /* initialize them */
137 act->name = xbt_strdup(name);
138 act->category = NULL;
141 surf_workstation_model->extension.workstation.sleep(host->host, duration);
143 surf_workstation_model->action_data_set(act->surf_action, act);
145 DEBUG1("Create sleep action %p", act);
150 * \brief Cancels an action.
152 * This functions stops the execution of an action. It calls a surf functions.
153 * \param action The SIMIX action
155 XBT_INLINE void SIMIX_action_cancel(smx_action_t action)
157 xbt_assert0((action != NULL), "Invalid parameter");
159 DEBUG1("Cancel action %p", action);
160 if (action->surf_action) {
161 surf_workstation_model->action_cancel(action->surf_action);
167 * \brief Changes the action's priority
169 * This functions changes the priority only. It calls a surf functions.
170 * \param action The SIMIX action
171 * \param priority The new priority
173 XBT_INLINE void SIMIX_action_set_priority(smx_action_t action, double priority)
175 xbt_assert0((action != NULL), "Invalid parameter");
177 surf_workstation_model->set_priority(action->surf_action, priority);
182 * \brief Destroys an action
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
187 int SIMIX_action_destroy(smx_action_t action)
189 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
190 xbt_assert0((action != NULL), "Invalid parameter");
193 if (action->refcount > 0)
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));
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));
204 DEBUG1("Destroy action %p", action);
206 xbt_free(action->name);
208 xbt_fifo_free(action->cond_list);
209 xbt_fifo_free(action->sem_list);
211 if (action->surf_action)
212 action->surf_action->model_type->action_unref(action->surf_action);
214 TRACE_smx_action_destroy (action);
221 * \brief Increase refcount of anan action
223 * \param action The SIMIX action
225 XBT_INLINE void SIMIX_action_use(smx_action_t action)
227 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
228 xbt_assert0((action != NULL), "Invalid parameter");
236 * \brief Decrease refcount of anan action
238 * \param action The SIMIX action
240 XBT_INLINE void SIMIX_action_release(smx_action_t action)
242 xbt_assert0((action != NULL), "Invalid parameter");
250 * \brief Set an action to a condition
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
256 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
258 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
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);
264 xbt_fifo_push(cond->actions, action);
266 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
267 __SIMIX_cond_display_actions(cond);
269 DEBUG2("Register condition %p to action %p", cond, action);
271 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
272 __SIMIX_action_display_conditions(action);
274 xbt_fifo_push(action->cond_list, cond);
276 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
277 __SIMIX_action_display_conditions(action);
281 * \brief Unset an action to a condition.
283 * Destroys the "links" from the condition to this action.
284 * \param action SIMIX action
285 * \param cond SIMIX cond
287 void SIMIX_unregister_action_to_condition(smx_action_t action,
290 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
292 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
293 __SIMIX_cond_display_actions(cond);
295 xbt_fifo_remove_all(cond->actions, action);
297 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
298 __SIMIX_cond_display_actions(cond);
300 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
301 __SIMIX_action_display_conditions(action);
303 xbt_fifo_remove_all(action->cond_list, cond);
305 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
306 __SIMIX_action_display_conditions(action);
309 * \brief Link an action to a semaphore
311 * When the action terminates, the semaphore gets signaled automatically.
313 XBT_INLINE void SIMIX_register_action_to_semaphore(smx_action_t action, smx_sem_t sem) {
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);
320 * \brief Unset an action to a semaphore.
322 * Destroys the "links" from the semaphore to this action.
324 XBT_INLINE void SIMIX_unregister_action_to_semaphore(smx_action_t action,
327 xbt_fifo_remove_all(sem->actions, action);
328 xbt_fifo_remove_all(action->sem_list, sem);
332 * \brief Return how much remais to be done in the action.
334 * \param action The SIMIX action
335 * \return Remains cost
337 XBT_INLINE double SIMIX_action_get_remains(smx_action_t action)
339 xbt_assert0((action != NULL), "Invalid parameter");
340 return surf_workstation_model->get_remains(action->surf_action);
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)
349 void **workstation_list = NULL;
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();
358 /* initialize them */
359 act->name = xbt_strdup(name);
360 act->category = NULL;
364 workstation_list = xbt_new0(void *, host_nb);
365 for (i = 0; i < host_nb; i++)
366 workstation_list[i] = host_list[i]->host;
369 surf_workstation_model->extension.workstation.
370 execute_parallel_task(host_nb, workstation_list, computation_amount,
371 communication_amount, amount, rate);
373 surf_workstation_model->action_data_set(act->surf_action, act);
378 XBT_INLINE e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
380 xbt_assert0((action != NULL), "Invalid parameter");
381 return surf_workstation_model->action_state_get(action->surf_action);
384 void __SIMIX_cond_display_actions(smx_cond_t cond)
386 xbt_fifo_item_t item = NULL;
387 smx_action_t action = NULL;
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);
394 void __SIMIX_action_display_conditions(smx_action_t action)
396 xbt_fifo_item_t item = NULL;
397 smx_cond_t cond = NULL;
399 DEBUG1("Conditions for action %p", action);
400 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
401 DEBUG1("\t %p", cond);
404 XBT_INLINE char *SIMIX_action_get_name(smx_action_t action)
406 xbt_assert0((action != NULL), "Invalid parameter");
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)
412 xbt_free(action->name);
416 /** @brief broadcast any condition and release any semaphore including this action */
417 void SIMIX_action_signal_all(smx_action_t action){
421 while ((cond = xbt_fifo_pop(action->cond_list)))
422 SIMIX_cond_broadcast(cond);
424 while ((sem = xbt_fifo_pop(action->sem_list)))
425 SIMIX_sem_release(sem);