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)
190 xbt_assert0((action != NULL), "Invalid parameter");
192 xbt_assert1((xbt_fifo_size(action->cond_list) == 0),
193 "Conditional list not empty %d. There is a problem. Cannot destroy it now!",
194 xbt_fifo_size(action->cond_list));
196 DEBUG1("Destroy action %p", action);
198 xbt_free(action->name);
200 xbt_fifo_free(action->cond_list);
202 if (action->simdata->surf_action)
203 action->simdata->surf_action->resource_type->common_public->
204 action_free(action->simdata->surf_action);
206 xbt_free(action->simdata);
212 * \brief Set an action to a condition
214 * 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.
215 * \param action SIMIX action
216 * \param cond SIMIX cond
218 void SIMIX_register_action_to_condition(smx_action_t action,
221 xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
223 DEBUG2("Register action %p to condtion %p", action, cond);
224 xbt_fifo_push(cond->actions, action);
228 * \brief Return how much remais to be done in the action.
230 * \param action The SIMIX action
231 * \return Remains cost
233 double SIMIX_action_get_remains(smx_action_t action)
235 xbt_assert0((action != NULL), "Invalid parameter");
236 return action->simdata->surf_action->remains;
239 smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb,
240 void **workstation_list,
241 double *computation_amount,
242 double *communication_amount,
243 double amount, double rate)
246 /* alloc structures */
247 smx_action_t act = xbt_new0(s_smx_action_t, 1);
248 act->simdata = xbt_new0(s_smx_simdata_action_t, 1);
249 smx_simdata_action_t simdata = act->simdata;
250 act->cond_list = xbt_fifo_new();
252 /* initialize them */
253 act->name = xbt_strdup(name);
255 /* set communication */
256 simdata->surf_action =
257 surf_workstation_resource->extension_public->
258 execute_parallel_task(workstation_nb, workstation_list,
259 computation_amount, communication_amount,
262 surf_workstation_resource->common_public->action_set_data(simdata->
269 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
271 xbt_assert0((action != NULL), "Invalid parameter");
272 return surf_workstation_resource->common_public->
273 action_get_state(action->simdata->surf_action);