Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics + one function to rule them all
[simgrid.git] / src / simix / smx_action.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2007 Arnaud Legrand, Bruno Donnassolo.
4    All rights reserved.                                          */
5
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. */
8
9 #include "private.h"
10 #include "xbt/log.h"
11 #include "xbt/ex.h"
12
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(simix_action, simix,
14                                 "Logging specific to SIMIX (action)");
15
16 /************************************* Actions *********************************/
17 /** \brief Creates a new SIMIX action to communicate two hosts.
18  *
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
26  * */
27 smx_action_t SIMIX_action_communicate(smx_host_t sender,
28                                       smx_host_t receiver, char *name,
29                                       double size, double rate)
30 {
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);
36   }
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",
41            receiver->name);
42   }
43
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();
49
50   /* initialize them */
51   act->name = xbt_strdup(name);
52   simdata->source = sender;
53
54
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->
59                                                             surf_action,
60                                                             act);
61
62   DEBUG1("Create communicate action %p", act);
63   return act;
64 }
65
66 /** \brief Creates a new SIMIX action to execute an action.
67  *
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
73  * */
74 smx_action_t SIMIX_action_execute(smx_host_t host, char *name,
75                                   double amount)
76 {
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",
81            host->name);
82   }
83
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();
89
90   /* initialize them */
91   simdata->source = host;
92   act->name = xbt_strdup(name);
93
94   /* set communication */
95   simdata->surf_action = surf_workstation_resource->extension_public->
96       execute(host->simdata->host, amount);
97
98   surf_workstation_resource->common_public->action_set_data(simdata->
99                                                             surf_action,
100                                                             act);
101
102   DEBUG1("Create execute action %p", act);
103   return act;
104 }
105
106 /** \brief Creates a new sleep SIMIX action.
107  *
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
112  * */
113 smx_action_t SIMIX_action_sleep(smx_host_t host, double duration)
114 {
115   char name[] = "sleep";
116
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",
121            host->name);
122   }
123
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();
129
130   /* initialize them */
131   simdata->source = host;
132   act->name = xbt_strdup(name);
133
134   simdata->surf_action = surf_workstation_resource->extension_public->
135       sleep(host->simdata->host, duration);
136
137   surf_workstation_resource->common_public->action_set_data(simdata->
138                                                             surf_action,
139                                                             act);
140
141   DEBUG1("Create sleep action %p", act);
142   return act;
143 }
144
145 /**
146  *      \brief Cancels an action.
147  *
148  *      This functions stops the execution of an action. It calls a surf functions.
149  *      \param action The SIMIX action
150  */
151 void SIMIX_action_cancel(smx_action_t action)
152 {
153   xbt_assert0((action != NULL), "Invalid parameter");
154
155   DEBUG1("Cancel action %p", action);
156   if (action->simdata->surf_action) {
157     surf_workstation_resource->common_public->action_cancel(action->
158                                                             simdata->
159                                                             surf_action);
160   }
161   return;
162 }
163
164 /**
165  *      \brief Changes the action's priority
166  *
167  *      This functions changes the priority only. It calls a surf functions.
168  *      \param action The SIMIX action
169  *      \param priority The new priority
170  */
171 void SIMIX_action_set_priority(smx_action_t action, double priority)
172 {
173   xbt_assert0((action != NULL)
174               && (action->simdata != NULL), "Invalid parameter");
175
176   surf_workstation_resource->common_public->
177       set_priority(action->simdata->surf_action, priority);
178   return;
179 }
180
181 /**
182  *      \brief Destroys an action
183  *
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
186  */
187 void SIMIX_action_destroy(smx_action_t action)
188 {
189
190   xbt_assert0((action != NULL), "Invalid parameter");
191
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));
195
196   DEBUG1("Destroy action %p", action);
197   if (action->name)
198     xbt_free(action->name);
199
200   xbt_fifo_free(action->cond_list);
201
202   if (action->simdata->surf_action)
203     action->simdata->surf_action->resource_type->common_public->
204         action_free(action->simdata->surf_action);
205
206   xbt_free(action->simdata);
207   xbt_free(action);
208   return;
209 }
210
211 /**
212  *      \brief Set an action to a condition
213  *
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
217  */
218 void SIMIX_register_action_to_condition(smx_action_t action,
219                                         smx_cond_t cond)
220 {
221   xbt_assert0((action != NULL) && (cond != NULL), "Invalid parameters");
222
223   DEBUG2("Register action %p to condtion %p", action, cond);
224   xbt_fifo_push(cond->actions, action);
225 }
226
227 /**
228  *      \brief Return how much remais to be done in the action.
229  *
230  *      \param action The SIMIX action
231  *      \return Remains cost
232  */
233 double SIMIX_action_get_remains(smx_action_t action)
234 {
235   xbt_assert0((action != NULL), "Invalid parameter");
236   return action->simdata->surf_action->remains;
237 }
238
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)
244 {
245
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();
251
252   /* initialize them */
253   act->name = xbt_strdup(name);
254
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,
260                             amount, rate);
261
262   surf_workstation_resource->common_public->action_set_data(simdata->
263                                                             surf_action,
264                                                             act);
265
266   return act;
267 }
268
269 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
270 {
271   xbt_assert0((action != NULL), "Invalid parameter");
272   return surf_workstation_resource->common_public->
273       action_get_state(action->simdata->surf_action);
274
275 }