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, char *name,
29 double size, double rate)
31 /* check if the host is active */
32 if (surf_workstation_resource->extension_public->
33 get_state(sender->simdata->host) != SURF_CPU_ON) {
34 THROW1(network_error, 0,
35 "Host %s failed, you cannot call this function", sender->name);
37 if (surf_workstation_resource->extension_public->
38 get_state(receiver->simdata->host) != SURF_CPU_ON) {
39 THROW1(network_error, 0,
40 "Host %s failed, you cannot call this function",
44 /* alloc structures */
45 smx_action_t act = xbt_new0(s_smx_action_t, 1);
46 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
47 smx_simdata_action_t simdata = act->simdata;
48 act->cond_list = xbt_fifo_new();
51 act->name = xbt_strdup(name);
52 simdata->source = sender;
55 simdata->surf_action = surf_workstation_resource->extension_public->
56 communicate(sender->simdata->host,
57 receiver->simdata->host, size, rate);
58 surf_workstation_resource->common_public->action_set_data(simdata->
62 DEBUG1("Create communicate action %p", act);
66 /** \brief Creates a new SIMIX action to execute an action.
68 * 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.
69 * \param host SIMIX host where the action will be executed
70 * \param name Action name
71 * \param amount Task amount (in bytes)
72 * \return A new SIMIX action
74 smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
77 /* check if the host is active */
78 if (surf_workstation_resource->extension_public->
79 get_state(host->simdata->host) != SURF_CPU_ON) {
80 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
84 /* alloc structures */
85 smx_action_t act = xbt_new0(s_smx_action_t, 1);
86 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
87 smx_simdata_action_t simdata = act->simdata;
88 act->cond_list = xbt_fifo_new();
91 simdata->source = host;
92 act->name = xbt_strdup(name);
94 /* set communication */
95 simdata->surf_action = surf_workstation_resource->extension_public->
96 execute(host->simdata->host, amount);
98 surf_workstation_resource->common_public->action_set_data(simdata->
102 DEBUG1("Create execute action %p", act);
106 /** \brief Creates a new sleep SIMIX action.
108 * 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. The default SIMIX name of the action is "sleep".
109 * \param host SIMIX host where the sleep will run.
110 * \param duration Time duration of the sleep.
111 * \return A new SIMIX action
113 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
115 char name[] = "sleep";
117 /* check if the host is active */
118 if (surf_workstation_resource->extension_public->
119 get_state(host->simdata->host) != SURF_CPU_ON) {
120 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
124 /* alloc structures */
125 smx_action_t act = xbt_new0(s_smx_action_t, 1);
126 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
127 smx_simdata_action_t simdata = act->simdata;
128 act->cond_list = xbt_fifo_new();
130 /* initialize them */
131 simdata->source = host;
132 act->name = xbt_strdup(name);
134 simdata->surf_action = surf_workstation_resource->extension_public->
135 sleep(host->simdata->host, duration);
137 surf_workstation_resource->common_public->action_set_data(simdata->
141 DEBUG1("Create sleep action %p", act);
146 * \brief Cancels an action.
148 * This functions stops the execution of an action. It calls a surf functions.
149 * \param action The SIMIX action
151 void SIMIX_action_cancel(smx_action_t action)
153 xbt_assert0((action != NULL), "Invalid parameter");
155 DEBUG1("Cancel action %p", action);
156 if (action->simdata->surf_action) {
157 surf_workstation_resource->common_public->action_cancel(action->
165 * \brief Changes the action's priority
167 * This functions changes the priority only. It calls a surf functions.
168 * \param action The SIMIX action
169 * \param priority The new priority
171 void SIMIX_action_set_priority(smx_action_t action, double priority)
173 xbt_assert0((action != NULL)
174 && (action->simdata != NULL), "Invalid parameter");
176 surf_workstation_resource->common_public->
177 set_priority(action->simdata->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 void SIMIX_action_destroy(smx_action_t action)
189 xbt_assert0((action != NULL), "Invalid parameter");
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 DEBUG1("Destroy action %p", action);
197 xbt_free(action->name);
199 xbt_fifo_free(action->cond_list);
201 if (action->simdata->surf_action)
202 action->simdata->surf_action->resource_type->common_public->
203 action_free(action->simdata->surf_action);
205 xbt_free(action->simdata);
211 * \brief Set an action to a condition
213 * 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.
214 * \param action SIMIX action
215 * \param cond SIMIX cond
217 void SIMIX_register_action_to_condition(smx_action_t action,
220 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
222 DEBUG2("Register action %p to cond %p", action, cond);
223 __SIMIX_cond_display_actions(cond);
224 xbt_fifo_push(cond->actions, action);
225 __SIMIX_cond_display_actions(cond);
226 DEBUG2("Register condition %p to action %p", cond, action);
227 __SIMIX_action_display_conditions(action);
228 xbt_fifo_push(action->cond_list, cond);
229 __SIMIX_action_display_conditions(action);
233 * \brief Unset an action to a condition.
235 * Destroys the "links" from the condition to this action.
236 * \param action SIMIX action
237 * \param cond SIMIX cond
239 void SIMIX_unregister_action_to_condition(smx_action_t action,
242 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
244 __SIMIX_cond_display_actions(cond);
245 xbt_fifo_remove_all(cond->actions, action);
246 __SIMIX_cond_display_actions(cond);
247 __SIMIX_action_display_conditions(action);
248 xbt_fifo_remove_all(action->cond_list, cond);
249 __SIMIX_action_display_conditions(action);
253 * \brief Return how much remais to be done in the action.
255 * \param action The SIMIX action
256 * \return Remains cost
258 double SIMIX_action_get_remains(smx_action_t action)
260 xbt_assert0((action != NULL), "Invalid parameter");
261 return action->simdata->surf_action->remains;
264 smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb,
265 void **workstation_list,
266 double *computation_amount,
267 double *communication_amount,
268 double amount, double rate)
271 /* alloc structures */
272 smx_action_t act = xbt_new0(s_smx_action_t, 1);
273 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
274 smx_simdata_action_t simdata = act->simdata;
275 act->cond_list = xbt_fifo_new();
277 /* initialize them */
278 act->name = xbt_strdup(name);
280 /* set communication */
281 simdata->surf_action =
282 surf_workstation_resource->extension_public->
283 execute_parallel_task(workstation_nb, workstation_list,
284 computation_amount, communication_amount,
287 surf_workstation_resource->common_public->action_set_data(simdata->
294 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
296 xbt_assert0((action != NULL), "Invalid parameter");
297 return surf_workstation_resource->common_public->
298 action_get_state(action->simdata->surf_action);
302 void __SIMIX_cond_display_actions(smx_cond_t cond)
304 xbt_fifo_item_t item = NULL;
305 smx_action_t action = NULL;
307 DEBUG1("Actions for condition %p", cond);
308 xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
309 DEBUG1("\t %p", action);
312 void __SIMIX_action_display_conditions(smx_action_t action)
314 xbt_fifo_item_t item = NULL;
315 smx_cond_t cond = NULL;
317 DEBUG1("Conditions for action %p", action);
318 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
319 DEBUG1("\t %p", cond);