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)
32 smx_simdata_action_t simdata;
34 /* check if the host is active */
35 if (surf_workstation_model->
36 extension_public->get_state(sender->simdata->host) != SURF_CPU_ON) {
37 THROW1(network_error, 0, "Host %s failed, you cannot call this function",
40 if (surf_workstation_model->
41 extension_public->get_state(receiver->simdata->host) != SURF_CPU_ON) {
42 THROW1(network_error, 0, "Host %s failed, you cannot call this function",
46 /* alloc structures */
47 act = xbt_new0(s_smx_action_t, 1);
48 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
49 simdata = act->simdata;
50 act->cond_list = xbt_fifo_new();
53 act->name = xbt_strdup(name);
54 simdata->source = sender;
57 simdata->surf_action =
58 surf_workstation_model->extension_public->communicate(sender->simdata->
62 surf_workstation_model->common_public->action_set_data(simdata->surf_action,
65 DEBUG1("Create communicate action %p", act);
69 /** \brief Creates a new SIMIX action to execute an action.
71 * 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.
72 * \param host SIMIX host where the action will be executed
73 * \param name Action name
74 * \param amount Task amount (in bytes)
75 * \return A new SIMIX action
77 smx_action_t SIMIX_action_execute(smx_host_t host, const char *name,
81 smx_simdata_action_t simdata;
83 /* check if the host is active */
84 if (surf_workstation_model->
85 extension_public->get_state(host->simdata->host) != SURF_CPU_ON) {
86 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
90 /* alloc structures */
91 act = xbt_new0(s_smx_action_t, 1);
92 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
93 simdata = act->simdata;
94 act->cond_list = xbt_fifo_new();
97 simdata->source = host;
98 act->name = xbt_strdup(name);
100 /* set communication */
101 simdata->surf_action =
102 surf_workstation_model->extension_public->execute(host->simdata->host,
105 surf_workstation_model->common_public->action_set_data(simdata->surf_action,
108 DEBUG1("Create execute action %p", act);
112 /** \brief Creates a new sleep SIMIX action.
114 * 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".
115 * \param host SIMIX host where the sleep will run.
116 * \param duration Time duration of the sleep.
117 * \return A new SIMIX action
119 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
121 char name[] = "sleep";
122 smx_simdata_action_t simdata;
125 /* check if the host is active */
126 if (surf_workstation_model->
127 extension_public->get_state(host->simdata->host) != SURF_CPU_ON) {
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->simdata = xbt_new0(s_smx_simdata_action_t, 1);
135 simdata = act->simdata;
136 act->cond_list = xbt_fifo_new();
138 /* initialize them */
139 simdata->source = host;
140 act->name = xbt_strdup(name);
142 simdata->surf_action =
143 surf_workstation_model->extension_public->sleep(host->simdata->host,
146 surf_workstation_model->common_public->action_set_data(simdata->surf_action,
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 void SIMIX_action_cancel(smx_action_t action)
161 xbt_assert0((action != NULL), "Invalid parameter");
163 DEBUG1("Cancel action %p", action);
164 if (action->simdata->surf_action) {
165 surf_workstation_model->common_public->action_cancel(action->
173 * \brief Changes the action's priority
175 * This functions changes the priority only. It calls a surf functions.
176 * \param action The SIMIX action
177 * \param priority The new priority
179 void SIMIX_action_set_priority(smx_action_t action, double priority)
181 xbt_assert0((action != NULL)
182 && (action->simdata != NULL), "Invalid parameter");
184 surf_workstation_model->common_public->set_priority(action->simdata->
185 surf_action, priority);
190 * \brief Destroys an action
192 * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
193 * \param action The SIMIX action
195 int SIMIX_action_destroy(smx_action_t action)
197 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
198 xbt_assert0((action != NULL), "Invalid parameter");
201 if (action->refcount > 0)
204 xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
205 "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
206 xbt_fifo_size(action->cond_list));
208 DEBUG1("Destroy action %p", action);
210 xbt_free(action->name);
212 xbt_fifo_free(action->cond_list);
214 if (action->simdata->surf_action)
215 action->simdata->surf_action->model_type->
216 common_public->action_free(action->simdata->surf_action);
218 xbt_free(action->simdata);
224 * \brief Increase refcount of anan action
226 * \param action The SIMIX action
228 void SIMIX_action_use(smx_action_t action)
230 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
231 xbt_assert0((action != NULL), "Invalid parameter");
239 * \brief Decrease refcount of anan action
241 * \param action The SIMIX action
243 void SIMIX_action_release(smx_action_t action)
245 xbt_assert0((action != NULL), "Invalid parameter");
253 * \brief Set an action to a condition
255 * 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.
256 * \param action SIMIX action
257 * \param cond SIMIX cond
259 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
261 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
263 DEBUG2("Register action %p to cond %p", action, cond);
264 __SIMIX_cond_display_actions(cond);
265 xbt_fifo_push(cond->actions, action);
266 __SIMIX_cond_display_actions(cond);
267 DEBUG2("Register condition %p to action %p", cond, action);
268 __SIMIX_action_display_conditions(action);
269 xbt_fifo_push(action->cond_list, cond);
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 __SIMIX_cond_display_actions(cond);
286 xbt_fifo_remove_all(cond->actions, action);
287 __SIMIX_cond_display_actions(cond);
288 __SIMIX_action_display_conditions(action);
289 xbt_fifo_remove_all(action->cond_list, cond);
290 __SIMIX_action_display_conditions(action);
294 * \brief Return how much remais to be done in the action.
296 * \param action The SIMIX action
297 * \return Remains cost
299 double SIMIX_action_get_remains(smx_action_t action)
301 xbt_assert0((action != NULL), "Invalid parameter");
302 return action->simdata->surf_action->remains;
305 smx_action_t SIMIX_action_parallel_execute(char *name, int host_nb,
306 smx_host_t * host_list,
307 double *computation_amount,
308 double *communication_amount,
309 double amount, double rate)
311 void **workstation_list = NULL;
312 smx_simdata_action_t simdata;
316 /* alloc structures */
317 act = xbt_new0(s_smx_action_t, 1);
318 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
319 simdata = act->simdata;
320 act->cond_list = xbt_fifo_new();
322 /* initialize them */
323 act->name = xbt_strdup(name);
327 workstation_list = xbt_new0(void *, host_nb);
328 for (i = 0; i < host_nb; i++)
329 workstation_list[i] = host_list[i]->simdata->host;
331 simdata->surf_action =
332 surf_workstation_model->extension_public->execute_parallel_task(host_nb,
335 communication_amount,
339 surf_workstation_model->common_public->action_set_data(simdata->surf_action,
345 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
347 xbt_assert0((action != NULL), "Invalid parameter");
348 return surf_workstation_model->common_public->action_get_state(action->
354 void __SIMIX_cond_display_actions(smx_cond_t cond)
356 xbt_fifo_item_t item = NULL;
357 smx_action_t action = NULL;
359 DEBUG1("Actions for condition %p", cond);
360 xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
361 DEBUG2("\t %p [%s]", action, action->name);
364 void __SIMIX_action_display_conditions(smx_action_t action)
366 xbt_fifo_item_t item = NULL;
367 smx_cond_t cond = NULL;
369 DEBUG1("Conditions for action %p", action);
370 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
371 DEBUG1("\t %p", cond);