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();
50 act->name = xbt_strdup(name);
55 surf_workstation_model->extension.workstation.communicate(sender->host,
58 surf_workstation_model->action_data_set(act->surf_action, act);
60 DEBUG1("Create communicate action %p", act);
64 /** \brief Creates a new SIMIX action to execute an action.
66 * 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.
67 * \param host SIMIX host where the action will be executed
68 * \param name Action name
69 * \param amount Task amount (in bytes)
70 * \return A new SIMIX action
72 smx_action_t SIMIX_action_execute(smx_host_t host, const char *name,
77 /* check if the host is active */
78 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
80 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
84 /* alloc structures */
85 act = xbt_new0(s_smx_action_t, 1);
86 act->cond_list = xbt_fifo_new();
90 act->name = xbt_strdup(name);
92 /* set communication */
94 surf_workstation_model->extension.workstation.execute(host->host, amount);
96 surf_workstation_model->action_data_set(act->surf_action, act);
98 DEBUG1("Create execute action %p", act);
102 /** \brief Creates a new sleep SIMIX action.
104 * This function creates a SURF action and allocates the data necessary
105 * to create the SIMIX action. It can raise a host_error exception if the
106 * host crashed. The default SIMIX name of the action is "sleep".
108 * \param host SIMIX host where the sleep will run.
109 * \param duration Time duration of the sleep.
110 * \return A new SIMIX action
112 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
114 char name[] = "sleep";
117 /* check if the host is active */
118 if (surf_workstation_model->extension.workstation.get_state(host->host) !=
120 THROW1(host_error, 0, "Host %s failed, you cannot call this function",
124 /* alloc structures */
125 act = xbt_new0(s_smx_action_t, 1);
126 act->cond_list = xbt_fifo_new();
128 /* initialize them */
130 act->name = xbt_strdup(name);
133 surf_workstation_model->extension.workstation.sleep(host->host, duration);
135 surf_workstation_model->action_data_set(act->surf_action, act);
137 DEBUG1("Create sleep action %p", act);
142 * \brief Cancels an action.
144 * This functions stops the execution of an action. It calls a surf functions.
145 * \param action The SIMIX action
147 void SIMIX_action_cancel(smx_action_t action)
149 xbt_assert0((action != NULL), "Invalid parameter");
151 DEBUG1("Cancel action %p", action);
152 if (action->surf_action) {
153 surf_workstation_model->action_cancel(action->surf_action);
159 * \brief Changes the action's priority
161 * This functions changes the priority only. It calls a surf functions.
162 * \param action The SIMIX action
163 * \param priority The new priority
165 void SIMIX_action_set_priority(smx_action_t action, double priority)
167 xbt_assert0((action != NULL), "Invalid parameter");
169 surf_workstation_model->set_priority(action->surf_action, priority);
174 * \brief Destroys an action
176 * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
177 * \param action The SIMIX action
179 int SIMIX_action_destroy(smx_action_t action)
181 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
182 xbt_assert0((action != NULL), "Invalid parameter");
185 if (action->refcount > 0)
188 xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
189 "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
190 xbt_fifo_size(action->cond_list));
192 DEBUG1("Destroy action %p", action);
194 xbt_free(action->name);
196 xbt_fifo_free(action->cond_list);
198 if (action->surf_action)
199 action->surf_action->model_type->action_unref(action->surf_action);
206 * \brief Increase refcount of anan action
208 * \param action The SIMIX action
210 void SIMIX_action_use(smx_action_t action)
212 XBT_IN3("(%p:'%s',%d)", action, action->name, action->refcount);
213 xbt_assert0((action != NULL), "Invalid parameter");
221 * \brief Decrease refcount of anan action
223 * \param action The SIMIX action
225 void SIMIX_action_release(smx_action_t action)
227 xbt_assert0((action != NULL), "Invalid parameter");
235 * \brief Set an action to a condition
237 * 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.
238 * \param action SIMIX action
239 * \param cond SIMIX cond
241 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
243 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
245 DEBUG2("Register action %p to cond %p", action, cond);
246 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
247 __SIMIX_cond_display_actions(cond);
249 xbt_fifo_push(cond->actions, action);
251 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
252 __SIMIX_cond_display_actions(cond);
254 DEBUG2("Register condition %p to action %p", cond, action);
256 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
257 __SIMIX_action_display_conditions(action);
259 xbt_fifo_push(action->cond_list, cond);
261 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
262 __SIMIX_action_display_conditions(action);
266 * \brief Unset an action to a condition.
268 * Destroys the "links" from the condition to this action.
269 * \param action SIMIX action
270 * \param cond SIMIX cond
272 void SIMIX_unregister_action_to_condition(smx_action_t action,
275 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
277 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
278 __SIMIX_cond_display_actions(cond);
280 xbt_fifo_remove_all(cond->actions, action);
282 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
283 __SIMIX_cond_display_actions(cond);
285 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
286 __SIMIX_action_display_conditions(action);
288 xbt_fifo_remove_all(action->cond_list, cond);
290 if(XBT_LOG_ISENABLED(simix_action, xbt_log_priority_debug))
291 __SIMIX_action_display_conditions(action);
295 * \brief Return how much remais to be done in the action.
297 * \param action The SIMIX action
298 * \return Remains cost
300 double SIMIX_action_get_remains(smx_action_t action)
302 xbt_assert0((action != NULL), "Invalid parameter");
303 return surf_workstation_model->get_remains(action->surf_action);
306 smx_action_t SIMIX_action_parallel_execute(char *name, int host_nb,
307 smx_host_t * host_list,
308 double *computation_amount,
309 double *communication_amount,
310 double amount, double rate)
312 void **workstation_list = NULL;
316 /* alloc structures */
317 act = xbt_new0(s_smx_action_t, 1);
318 act->cond_list = xbt_fifo_new();
320 /* initialize them */
321 act->name = xbt_strdup(name);
325 workstation_list = xbt_new0(void *, host_nb);
326 for (i = 0; i < host_nb; i++)
327 workstation_list[i] = host_list[i]->host;
330 surf_workstation_model->extension.workstation.
331 execute_parallel_task(host_nb, workstation_list, computation_amount,
332 communication_amount, amount, rate);
334 surf_workstation_model->action_data_set(act->surf_action, act);
339 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
341 xbt_assert0((action != NULL), "Invalid parameter");
342 return surf_workstation_model->action_state_get(action->surf_action);
345 void __SIMIX_cond_display_actions(smx_cond_t cond)
347 xbt_fifo_item_t item = NULL;
348 smx_action_t action = NULL;
350 DEBUG1("Actions for condition %p", cond);
351 xbt_fifo_foreach(cond->actions, item, action, smx_action_t)
352 DEBUG2("\t %p [%s]", action, action->name);
355 void __SIMIX_action_display_conditions(smx_action_t action)
357 xbt_fifo_item_t item = NULL;
358 smx_cond_t cond = NULL;
360 DEBUG1("Conditions for action %p", action);
361 xbt_fifo_foreach(action->cond_list, item, cond, smx_cond_t)
362 DEBUG1("\t %p", cond);
365 char *SIMIX_action_get_name(smx_action_t action)
367 xbt_assert0((action != NULL), "Invalid parameter");
370 /** @brief Change the name of the action. Warning, the string you provide is not strdup()ed */
371 void SIMIX_action_set_name(smx_action_t action,char *name)
376 void SIMIX_action_signal_all(smx_action_t action)
380 while ((cond = xbt_fifo_pop(action->cond_list)))
381 SIMIX_cond_broadcast(cond);