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,smx_host_t receiver,char * name, double size, double rate)
29 /* check if the host is active */
30 if ( surf_workstation_resource->extension_public->get_state(sender->simdata->host)!=SURF_CPU_ON) {
31 THROW1(network_error,0,"Host %s failed, you cannot call this function",sender->name);
33 if ( surf_workstation_resource->extension_public->get_state(receiver->simdata->host)!=SURF_CPU_ON) {
34 THROW1(network_error,0,"Host %s failed, you cannot call this function",receiver->name);
37 /* alloc structures */
38 smx_action_t act = xbt_new0(s_smx_action_t,1);
39 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
40 smx_simdata_action_t simdata = act->simdata;
41 act->cond_list = xbt_fifo_new();
44 act->name = xbt_strdup(name);
45 simdata->source = sender;
48 simdata->surf_action = surf_workstation_resource->extension_public->
49 communicate(sender->simdata->host,
50 receiver->simdata->host, size, rate);
51 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
53 DEBUG1("Create communicate action %p",act);
57 /** \brief Creates a new SIMIX action to execute an action.
59 * 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.
60 * \param host SIMIX host where the action will be executed
61 * \param name Action name
62 * \param amount Task amount (in bytes)
63 * \return A new SIMIX action
65 smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
67 /* check if the host is active */
68 if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
69 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
72 /* alloc structures */
73 smx_action_t act = xbt_new0(s_smx_action_t,1);
74 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
75 smx_simdata_action_t simdata = act->simdata;
76 act->cond_list = xbt_fifo_new();
79 simdata->source = host;
80 act-> name = xbt_strdup(name);
82 /* set communication */
83 simdata->surf_action = surf_workstation_resource->extension_public->
84 execute(host->simdata->host, amount);
86 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
88 DEBUG1("Create execute action %p",act);
92 /** \brief Creates a new sleep SIMIX action.
94 * 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".
95 * \param host SIMIX host where the sleep will run.
96 * \param duration Time duration of the sleep.
97 * \return A new SIMIX action
99 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
101 char name[] = "sleep";
103 /* check if the host is active */
104 if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
105 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
108 /* alloc structures */
109 smx_action_t act = xbt_new0(s_smx_action_t,1);
110 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
111 smx_simdata_action_t simdata = act->simdata;
112 act->cond_list = xbt_fifo_new();
114 /* initialize them */
115 simdata->source = host;
116 act->name = xbt_strdup(name);
118 simdata->surf_action = surf_workstation_resource->extension_public->
119 sleep(host->simdata->host, duration);
121 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
123 DEBUG1("Create sleep action %p",act);
128 * \brief Cancels an action.
130 * This functions stops the execution of an action. It calls a surf functions.
131 * \param action The SIMIX action
133 void SIMIX_action_cancel(smx_action_t action)
135 xbt_assert0((action != NULL), "Invalid parameter");
137 DEBUG1("Cancel action %p",action);
138 if(action->simdata->surf_action) {
139 surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
145 * \brief Changes the action's priority
147 * This functions changes the priority only. It calls a surf functions.
148 * \param action The SIMIX action
149 * \param priority The new priority
151 void SIMIX_action_set_priority(smx_action_t action, double priority)
153 xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
155 surf_workstation_resource->common_public->
156 set_priority(action->simdata->surf_action, priority);
161 * \brief Destroys an action
163 * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
164 * \param action The SIMIX action
166 void SIMIX_action_destroy(smx_action_t action)
169 xbt_assert0((action != NULL), "Invalid parameter");
171 xbt_assert1((xbt_fifo_size(action->cond_list)==0),
172 "Conditional list not empty %d. There is a problem. Cannot destroy it now!", xbt_fifo_size(action->cond_list));
174 DEBUG1("Destroy action %p",action);
175 if(action->name) xbt_free(action->name);
177 xbt_fifo_free(action->cond_list);
179 if(action->simdata->surf_action)
180 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
182 xbt_free(action->simdata);
188 * \brief Set an action to a condition
190 * 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.
191 * \param action SIMIX action
192 * \param cond SIMIX cond
194 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
196 xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
198 DEBUG2("Register action %p to condtion %p",action,cond);
199 xbt_fifo_push(cond->actions,action);
203 * \brief Return how much remais to be done in the action.
205 * \param action The SIMIX action
206 * \return Remains cost
208 double SIMIX_action_get_remains(smx_action_t action)
210 xbt_assert0((action != NULL), "Invalid parameter");
211 return action->simdata->surf_action->remains;
214 smx_action_t SIMIX_action_parallel_execute(char *name, int workstation_nb, void **workstation_list, double *computation_amount, double *communication_amount, double amount, double rate)
217 /* alloc structures */
218 smx_action_t act = xbt_new0(s_smx_action_t,1);
219 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
220 smx_simdata_action_t simdata = act->simdata;
221 act->cond_list = xbt_fifo_new();
223 /* initialize them */
224 act-> name = xbt_strdup(name);
226 /* set communication */
227 simdata->surf_action = surf_workstation_resource->extension_public->execute_parallel_task(workstation_nb, workstation_list, computation_amount, communication_amount, amount, rate);
229 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
234 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
236 xbt_assert0((action != NULL), "Invalid parameter");
237 return surf_workstation_resource->common_public->action_get_state(action->simdata->surf_action);