Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8e7124ad069deac674268a9e34acb065a278b8bd
[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         return act;
54 }
55
56 /** \brief Creates a new SIMIX action to execute an action.
57  *
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
63  * */
64 smx_action_t SIMIX_action_execute(smx_host_t host, char * name, double amount)
65 {
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);
69         }
70
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();
76         
77         /* initialize them */
78         simdata->source = host;
79         act-> name = xbt_strdup(name);
80
81         /* set communication */
82         simdata->surf_action = surf_workstation_resource->extension_public->
83                 execute(host->simdata->host, amount);
84
85         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
86
87         return act;
88 }
89
90 /** \brief Creates a new sleep SIMIX action.
91  *
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
96  * */
97 smx_action_t SIMIX_action_sleep(smx_host_t host,  double duration)
98 {       
99         char name[] = "sleep";
100
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);
104         }
105
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();
111         
112         /* initialize them */
113         simdata->source = host;
114         act->name = xbt_strdup(name);
115
116         simdata->surf_action = surf_workstation_resource->extension_public->
117                 sleep(host->simdata->host, duration);
118
119         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
120
121         return act;
122 }
123
124 /**
125  *      \brief Cancels an action.
126  *
127  *      This functions stops the execution of an action. It calls a surf functions.
128  *      \param action The SIMIX action
129  */
130 void SIMIX_action_cancel(smx_action_t action)
131 {
132   xbt_assert0((action != NULL), "Invalid parameter");
133
134   if(action->simdata->surf_action) {
135     surf_workstation_resource->common_public->action_cancel(action->simdata->surf_action);
136   }
137   return;
138 }
139
140 /**
141  *      \brief Changes the action's priority
142  *
143  *      This functions changes the priority only. It calls a surf functions.
144  *      \param action The SIMIX action
145  *      \param priority The new priority
146  */
147 void SIMIX_action_set_priority(smx_action_t action, double priority)
148 {
149         xbt_assert0( (action != NULL) && (action->simdata != NULL), "Invalid parameter" );
150
151         surf_workstation_resource->common_public->
152                 set_priority(action->simdata->surf_action, priority);
153         return;
154 }
155
156 /**
157  *      \brief Destroys an action
158  *
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
161  */
162 void SIMIX_action_destroy(smx_action_t action)
163 {
164
165         xbt_assert0((action != NULL), "Invalid parameter");
166
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));
169
170         if(action->name) xbt_free(action->name);
171
172         xbt_fifo_free(action->cond_list);
173
174         if(action->simdata->surf_action) 
175                 action->simdata->surf_action->resource_type->common_public->action_free(action->simdata->surf_action);
176
177         xbt_free(action->simdata);
178         xbt_free(action);
179         return;
180 }
181
182 /**
183  *      \brief Set an action to a condition
184  *
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
188  */
189 void SIMIX_register_action_to_condition(smx_action_t action, smx_cond_t cond)
190 {
191         xbt_assert0( (action != NULL) && (cond != NULL), "Invalid parameters");
192
193         xbt_fifo_push(cond->actions,action);
194 }
195
196 /**
197  *      \brief Return how much remais to be done in the action.
198  *
199  *      \param action The SIMIX action
200  *      \return Remains cost
201  */
202 double SIMIX_action_get_remains(smx_action_t action) 
203 {
204   xbt_assert0((action != NULL), "Invalid parameter");
205         return action->simdata->surf_action->remains;
206 }
207
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)
209 {
210
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();
216         
217         /* initialize them */
218         act-> name = xbt_strdup(name);
219
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);
222
223         surf_workstation_resource->common_public->action_set_data(simdata->surf_action,act);
224
225         return act;
226 }
227
228 e_surf_action_state_t SIMIX_action_get_state(smx_action_t action)
229 {
230   xbt_assert0((action != NULL), "Invalid parameter");
231         return surf_workstation_resource->common_public->action_get_state(action->simdata->surf_action);
232
233 }