1 /* Copyright (c) 2007, 2009, 2010. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
12 "Logging specific to SIMIX (action)");
14 /************************************* Actions *********************************/
15 /** \brief Creates a new SIMIX action to communicate two hosts.
17 * 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.
18 * \param sender SIMIX host sender
19 * \param receiver SIMIX host receiver
20 * \param name Action name
21 * \param size Communication size (in bytes)
22 * \param rate Communication rate between hosts.
23 * \return A new SIMIX action
25 smx_action_t SIMIX_action_communicate(smx_host_t sender,
26 smx_host_t receiver, const char *name,
27 double size, double rate)
31 /* check if the host is active */
32 if (surf_workstation_model->extension.workstation.get_state(sender->host) !=
34 THROW1(network_error, 0, "Host %s failed, you cannot call this function",
37 if (surf_workstation_model->extension.workstation.
38 get_state(receiver->host) != SURF_RESOURCE_ON) {
39 THROW1(network_error, 0, "Host %s failed, you cannot call this function",
43 /* alloc structures */
44 act = xbt_new0(s_smx_action_t, 1);
45 act->cond_list = xbt_fifo_new();
46 act->sem_list = xbt_fifo_new();
49 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);
97 /* set communication */
99 surf_workstation_model->extension.workstation.execute(host->host, amount);
101 surf_workstation_model->action_data_set(act->surf_action, act);
103 DEBUG1("Create execute action %p", act);
105 TRACE_smx_action_execute (act);
110 /** \brief Creates a new sleep SIMIX action.
112 * This function creates a SURF action and allocates the data necessary
113 * to create the SIMIX action. It can raise a host_error exception if the
114 * host crashed. The default SIMIX name of the action is "sleep".
116 * \param host SIMIX host where the sleep will run.
117 * \param duration Time duration of the sleep.
118 * \return A new SIMIX action
120 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
122 char name[] = "sleep";
125 /* check if the host is active */
126 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
128 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
132 /* alloc structures */
133 act = xbt_new0(s_smx_action_t, 1);
134 act->cond_list = xbt_fifo_new();
135 act->sem_list = xbt_fifo_new();
137 /* initialize them */
139 act->name = xbt_strdup(name);
141 act->category = NULL;
145 surf_workstation_model->extension.workstation.sleep(host->host, duration);
147 surf_workstation_model->action_data_set(act->surf_action, act);
149 DEBUG1("Create sleep action %p", act);
154 * \brief Cancels an action.
156 * This functions stops the execution of an action. It calls a surf functions.
157 * \param action The SIMIX action
159 XBT_INLINE void SIMIX_action_cancel(smx_action_t action)
161 xbt_assert0((action != NULL), "Invalid parameter");
163 DEBUG1("Cancel action %p", action);
164 if (action->surf_action) {
165 surf_workstation_model->action_cancel(action->surf_action);
171 * \brief Changes the action's priority
173 * This functions changes the priority only. It calls a surf functions.
174 * \param action The SIMIX action
175 * \param priority The new priority
177 XBT_INLINE void SIMIX_action_set_priority(smx_action_t action, double priority)
179 xbt_assert0((action != NULL), "Invalid parameter");
181 surf_workstation_model->set_priority(action->surf_action, priority);
186 * \brief Resumes the execution of an action.
188 * This functions restarts the execution of an action. It just calls the right SURF function.
189 * \param action The SIMIX action
190 * \param priority The new priority
192 XBT_INLINE void SIMIX_action_resume(smx_action_t action)
194 xbt_assert0((action != NULL), "Invalid parameter");
196 surf_workstation_model->resume(action->surf_action);
201 * \brief Suspends the execution of an action.
203 * This functions suspends the execution of an action. It just calls the right SURF function.
204 * \param action The SIMIX action
205 * \param priority The new priority
207 XBT_INLINE void SIMIX_action_suspend(smx_action_t action)
209 xbt_assert0((action != NULL), "Invalid parameter");
211 surf_workstation_model->suspend(action->surf_action);
216 * \brief Destroys an action
218 * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
219 * \param action The SIMIX action
221 int SIMIX_action_destroy(smx_action_t action)
223 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
224 xbt_assert0((action != NULL), "Invalid parameter");
227 if (action->refcount > 0)
230 xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
231 "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
232 xbt_fifo_size(action->cond_list));
234 xbt_assert1((xbt_fifo_size(action->sem_list) == 0),
235 "Semaphore list not empty %d. There is a problem. Cannot destroy it now!",
236 xbt_fifo_size(action->sem_list));
238 DEBUG1("Destroy action %p", action);
240 xbt_free(action->name);
242 xbt_fifo_free(action->cond_list);
243 xbt_fifo_free(action->sem_list);
245 if (action->surf_action)
246 action->surf_action->model_type->action_unref(action->surf_action);
248 TRACE_smx_action_destroy (action);
255 * \brief Increase refcount of anan action
257 * \param action The SIMIX action
259 XBT_INLINE void SIMIX_action_use(smx_action_t action)
261 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
262 xbt_assert0((action != NULL), "Invalid parameter");
270 * \brief Decrease refcount of anan action
272 * \param action The SIMIX action
274 XBT_INLINE void SIMIX_action_release(smx_action_t action)
276 xbt_assert0((action != NULL), "Invalid parameter");
284 * \brief Set an action to a condition
286 * 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.
287 * \param action SIMIX action
288 * \param cond SIMIX cond
290 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
292 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
294 DEBUG2("Register action %p to cond %p", action, cond);
295 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
296 __SIMIX_cond_display_actions(cond);
298 xbt_fifo_push(cond->actions, action);
300 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
301 __SIMIX_cond_display_actions(cond);
303 DEBUG2("Register condition %p to action %p", cond, action);
305 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
306 __SIMIX_action_display_conditions(action);
308 xbt_fifo_push(action->cond_list, cond);
310 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
311 __SIMIX_action_display_conditions(action);
315 * \brief Unset an action to a condition.
317 * Destroys the "links" from the condition to this action.
318 * \param action SIMIX action
319 * \param cond SIMIX cond
321 void SIMIX_unregister_action_to_condition(smx_action_t action,
324 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
326 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
327 __SIMIX_cond_display_actions(cond);
329 xbt_fifo_remove_all(cond->actions, action);
331 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
332 __SIMIX_cond_display_actions(cond);
334 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
335 __SIMIX_action_display_conditions(action);
337 xbt_fifo_remove_all(action->cond_list, cond);
339 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
340 __SIMIX_action_display_conditions(action);
343 * \brief Link an action to a semaphore
345 * When the action terminates, the semaphore gets signaled automatically.
347 XBT_INLINE void SIMIX_register_action_to_semaphore(smx_action_t action, smx_sem_t sem) {
349 DEBUG2("Register action %p to semaphore %p (and otherwise)", action, sem);
350 xbt_fifo_push(sem->actions, action);
351 xbt_fifo_push(action->sem_list, sem);
354 * \brief Unset an action to a semaphore.
356 * Destroys the "links" from the semaphore to this action.
358 XBT_INLINE void SIMIX_unregister_action_to_semaphore(smx_action_t action,
361 xbt_fifo_remove_all(sem->actions, action);
362 xbt_fifo_remove_all(action->sem_list, sem);
366 * \brief Return how much remais to be done in the action.
368 * \param action The SIMIX action
369 * \return Remains cost
371 XBT_INLINE double SIMIX_action_get_remains(smx_action_t action)
373 xbt_assert0((action != NULL), "Invalid parameter");
374 return surf_workstation_model->get_remains(action->surf_action);
377 smx_action_t SIMIX_action_parallel_execute(char *name, int host_nb,
378 smx_host_t * host_list,
379 double *computation_amount,
380 double *communication_amount,
381 double amount, double rate)
383 void **workstation_list = NULL;
387 /* alloc structures */
388 act = xbt_new0(s_smx_action_t, 1);
389 act->cond_list = xbt_fifo_new();
390 act->sem_list = xbt_fifo_new();
392 /* initialize them */
393 act->name = xbt_strdup(name);
395 act->category = NULL;
400 workstation_list = xbt_new0(void *, host_nb);
401 for (i = 0; i < host_nb; i++)
402 workstation_list[i] = host_list[i]->host;
405 surf_workstation_model->extension.workstation.
406 execute_parallel_task(host_nb, workstation_list, computation_amount,
407 communication_amount, amount, rate);
409 surf_workstation_model->action_data_set(act->surf_action, act);
414 XBT_INLINE e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
416 xbt_assert0((action != NULL), "Invalid parameter");
417 return surf_workstation_model->action_state_get(action->surf_action);
420 void __SIMIX_cond_display_actions(smx_cond_t cond)
422 xbt_fifo_item_t item = NULL;
423 smx_action_t action = NULL;
425 DEBUG1("Actions for condition %p", cond);
426 xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
427 DEBUG2("\t %p [%s]", action, action->name);
430 void __SIMIX_action_display_conditions(smx_action_t action)
432 xbt_fifo_item_t item = NULL;
433 smx_cond_t cond = NULL;
435 DEBUG1("Conditions for action %p", action);
436 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
437 DEBUG1("\t %p", cond);
440 XBT_INLINE char *SIMIX_action_get_name(smx_action_t action)
442 xbt_assert0((action != NULL), "Invalid parameter");
445 /** @brief Change the name of the action. Warning, the string you provide is not strdup()ed */
446 XBT_INLINE void SIMIX_action_set_name(smx_action_t action,char *name)
448 xbt_free(action->name);
452 /** @brief broadcast any condition and release any semaphore including this action */
453 void SIMIX_action_signal_all(smx_action_t action){
457 while ((cond = xbt_fifo_pop(action->cond_list)))
458 SIMIX_cond_broadcast(cond);
460 while ((sem = xbt_fifo_pop(action->sem_list)))
461 SIMIX_sem_release(sem);