Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Insane amount of debugs
[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,smx_host_t receiver,char * name, double size, double rate)
28 {
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);
32         }
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);
35         }
36
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();
42         
43         /* initialize them */
44         act->name = xbt_strdup(name);
45         simdata->source = sender;
46
47
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);
52
53   DEBUG1("Create communicate action %p",act);
54         return act;
55 }
56
57 /** \brief Creates a new SIMIX action to execute an action.
58  *
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
64  * */
65 smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
66 {
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);
70         }
71
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();
77         
78         /* initialize them */
79         simdata->source = host;
80         act-> name = xbt_strdup(name);
81
82         /* set communication */
83         simdata->surf_action = surf_workstation_resource->extension_public->
84                 execute(host->simdata->host, amount);
85
86         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
87
88   DEBUG1("Create execute action %p",act);
89         return act;
90 }
91
92 /** \brief Creates a new sleep SIMIX action.
93  *
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
98  * */
99 smx_action_t SIMIX_action_sleep(smx_host_t host,  double duration)
100 {       
101         char name[] = "sleep";
102
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);
106         }
107
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();
113         
114         /* initialize them */
115         simdata->source = host;
116         act->name = xbt_strdup(name);
117
118         simdata->surf_action = surf_workstation_resource->extension_public->
119                 sleep(host->simdata->host, duration);
120
121         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
122
123   DEBUG1("Create sleep action %p",act);
124         return act;
125 }
126
127 /**
128  *      \brief Cancels an action.
129  *
130  *      This functions stops the execution of an action. It calls a surf functions.
131  *      \param action The SIMIX action
132  */
133 void SIMIX_action_cancel(smx_action_t action)
134 {
135   xbt_assert0((action != NULL), "Invalid parameter");
136
137   DEBUG1("Cancel action %p",action);
138   if(action->simdata->surf_action) {
139     surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
140   }
141   return;
142 }
143
144 /**
145  *      \brief Changes the action's priority
146  *
147  *      This functions changes the priority only. It calls a surf functions.
148  *      \param action The SIMIX action
149  *      \param priority The new priority
150  */
151 void SIMIX_action_set_priority(smx_action_t action, double priority)
152 {
153         xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
154
155         surf_workstation_resource->common_public->
156                 set_priority(action->simdata->surf_action, priority);
157         return;
158 }
159
160 /**
161  *      \brief Destroys an action
162  *
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
165  */
166 void SIMIX_action_destroy(smx_action_t action)
167 {
168
169         xbt_assert0((action != NULL), "Invalid parameter");
170
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));
173
174   DEBUG1("Destroy action %p",action);
175         if(action->name) xbt_free(action->name);
176
177         xbt_fifo_free(action->cond_list);
178
179         if(action->simdata->surf_action) 
180                 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
181
182         xbt_free(action->simdata);
183         xbt_free(action);
184         return;
185 }
186
187 /**
188  *      \brief Set an action to a condition
189  *
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
193  */
194 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
195 {
196         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
197
198   DEBUG2("Register action %p to condtion %p",action,cond);
199         xbt_fifo_push(cond->actions,action);
200 }
201
202 /**
203  *      \brief Return how much remais to be done in the action.
204  *
205  *      \param action The SIMIX action
206  *      \return Remains cost
207  */
208 double SIMIX_action_get_remains(smx_action_t action) 
209 {
210   xbt_assert0((action != NULL), "Invalid parameter");
211         return action->simdata->surf_action->remains;
212 }
213
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)
215 {
216
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();
222         
223         /* initialize them */
224         act-> name = xbt_strdup(name);
225
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);
228
229         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
230
231         return act;
232 }
233
234 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
235 {
236   xbt_assert0((action != NULL), "Invalid parameter");
237         return surf_workstation_resource->common_public->action_get_state(action->simdata->surf_action);
238
239 }