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);
56 /** \brief Creates a new SIMIX action to execute an action.
58 * 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.
59 * \param host SIMIX host where the action will be executed
60 * \param name Action name
61 * \param amount Task amount (in bytes)
62 * \return A new SIMIX action
64 smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
66 /* check if the host is active */
67 if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
68 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
71 /* alloc structures */
72 smx_action_t act = xbt_new0(s_smx_action_t,1);
73 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
74 smx_simdata_action_t simdata = act->simdata;
75 act->cond_list = xbt_fifo_new();
78 simdata->source = host;
79 act-> name = xbt_strdup(name);
81 /* set communication */
82 simdata->surf_action = surf_workstation_resource->extension_public->
83 execute(host->simdata->host, amount);
85 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
90 /** \brief Creates a new sleep SIMIX action.
92 * 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".
93 * \param host SIMIX host where the sleep will run.
94 * \param duration Time duration of the sleep.
95 * \return A new SIMIX action
97 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
99 char name[] = "sleep";
101 /* check if the host is active */
102 if ( surf_workstation_resource->extension_public->get_state(host->simdata->host)!=SURF_CPU_ON) {
103 THROW1(host_error,0,"Host %s failed, you cannot call this function",host->name);
106 /* alloc structures */
107 smx_action_t act = xbt_new0(s_smx_action_t,1);
108 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
109 smx_simdata_action_t simdata = act->simdata;
110 act->cond_list = xbt_fifo_new();
112 /* initialize them */
113 simdata->source = host;
114 act->name = xbt_strdup(name);
116 simdata->surf_action = surf_workstation_resource->extension_public->
117 sleep(host->simdata->host, duration);
119 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
125 * \brief Cancels an action.
127 * This functions stops the execution of an action. It calls a surf functions.
128 * \param action The SIMIX action
130 void SIMIX_action_cancel(smx_action_t action)
132 xbt_assert0((action != NULL), "Invalid parameter");
134 if(action->simdata->surf_action) {
135 surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
141 * \brief Changes the action's priority
143 * This functions changes the priority only. It calls a surf functions.
144 * \param action The SIMIX action
145 * \param priority The new priority
147 void SIMIX_action_set_priority(smx_action_t action, double priority)
149 xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
151 surf_workstation_resource->common_public->
152 set_priority(action->simdata->surf_action, priority);
157 * \brief Destroys an action
159 * Destroys an action, freing its memory. This function cannot be called if there are a conditional waiting for it.
160 * \param action The SIMIX action
162 void SIMIX_action_destroy(smx_action_t action)
165 xbt_assert0((action != NULL), "Invalid parameter");
167 xbt_assert1((xbt_fifo_size(action->cond_list)==0),
168 "Conditional list not empty %d. There is a problem. Cannot destroy it now!", xbt_fifo_size(action->cond_list));
170 if(action->name) xbt_free(action->name);
172 xbt_fifo_free(action->cond_list);
174 if(action->simdata->surf_action)
175 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
177 xbt_free(action->simdata);
183 * \brief Set an action to a condition
185 * 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.
186 * \param action SIMIX action
187 * \param cond SIMIX cond
189 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
191 xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
193 xbt_fifo_push(cond->actions,action);
197 * \brief Return how much remais to be done in the action.
199 * \param action The SIMIX action
200 * \return Remains cost
202 double SIMIX_action_get_remains(smx_action_t action)
204 xbt_assert0((action != NULL), "Invalid parameter");
205 return action->simdata->surf_action->remains;
208 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)
211 /* alloc structures */
212 smx_action_t act = xbt_new0(s_smx_action_t,1);
213 act->simdata = xbt_new0(s_smx_simdata_action_t,1);
214 smx_simdata_action_t simdata = act->simdata;
215 act->cond_list = xbt_fifo_new();
217 /* initialize them */
218 act-> name = xbt_strdup(name);
220 /* set communication */
221 simdata->surf_action = surf_workstation_resource->extension_public->execute_parallel_task(workstation_nb, workstation_list, computation_amount, communication_amount, amount, rate);
223 surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
228 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
230 xbt_assert0((action != NULL), "Invalid parameter");
231 return surf_workstation_resource->common_public->action_get_state(action->simdata->surf_action);