Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
32ba95aff12e07f4a53bd9b3d635067d1c6dac1a
[simgrid.git] / src / include / surf / surf.h
1 /*      $Id$     */
2
3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved.                  */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #ifndef _SURF_SURF_H
9 #define _SURF_SURF_H
10
11 #include "xbt/swag.h"
12 #include "xbt/dynar.h"
13 #include "xbt/dict.h"
14
15 /* Actions and resources are higly connected structures... */
16
17 /** \brief Action datatype
18  *  \ingroup SURF_actions
19  *  
20  * An action is some working amount on a resource.
21  * It is represented as a cost, a priority, a duration and a state.
22  *
23  * \see e_surf_action_state_t
24  */
25 typedef struct surf_action *surf_action_t;
26
27 /** \brief Action structure
28  * \ingroup SURF_actions
29  *
30  *  Never create s_surf_action_t by yourself ! The actions are created
31  *  on the fly when you call execute or communicate on a resource.
32  *  
33  *  \see e_surf_action_state_t
34  */
35 typedef struct surf_action {
36   s_xbt_swag_hookup_t state_hookup;
37   xbt_swag_t state_set;
38   double cost;                  /**< cost        */
39   double priority;              /**< priority (1.0 by default) */
40   double max_duration;          /**< max_duration (may fluctuate until
41                                    the task is completed) */
42   double remains;               /**< How much of that cost remains to
43                                  * be done in the currently running task */
44   double start;                 /**< start time  */
45   double finish;                /**< finish time : this is modified during the run
46                                  * and fluctuates until the task is completed */
47   void *data;                   /**< for your convenience */
48   int using;
49   surf_resource_t resource_type;
50 } s_surf_action_t;
51
52 /** \brief Action states
53  *  \ingroup SURF_actions
54  *
55  *  Action states.
56  *
57  *  \see surf_action_t, surf_action_state_t
58  */
59 typedef enum {
60   SURF_ACTION_READY = 0,        /**< Ready        */
61   SURF_ACTION_RUNNING,          /**< Running      */
62   SURF_ACTION_FAILED,           /**< Task Failure */
63   SURF_ACTION_DONE,             /**< Completed    */
64   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
65   SURF_ACTION_NOT_IN_THE_SYSTEM /**< Not in the system anymore. Why did you ask ? */
66 } e_surf_action_state_t;
67
68 /** \brief Action state sets
69  *  \ingroup SURF_actions
70  *
71  *  This structure contains some sets of actions.
72  *  It provides a fast access to the actions in each state.
73  *
74  *  \see surf_action_t, e_surf_action_state_t
75  */
76 typedef struct surf_action_state {
77   xbt_swag_t ready_action_set;   /**< Actions in state SURF_ACTION_READY */
78   xbt_swag_t running_action_set; /**< Actions in state SURF_ACTION_RUNNING */
79   xbt_swag_t failed_action_set;  /**< Actions in state SURF_ACTION_FAILED */
80   xbt_swag_t done_action_set;    /**< Actions in state SURF_ACTION_DONE */
81 } s_surf_action_state_t, *surf_action_state_t;
82
83 /***************************/
84 /* Generic resource object */
85 /***************************/
86
87 /** \brief Resource datatype
88  *  \ingroup SURF_resources
89  *  
90  *  Generic data structure for a resource. The workstations,
91  *  the CPUs and the network links are examples of resources.
92  */
93 typedef struct surf_resource *surf_resource_t;
94
95 /** \brief Resource datatype
96  *  \ingroup SURF_resources
97  *  
98  *  Generic data structure for a resource. The workstations,
99  *  the CPUs and the network links are examples of resources.
100  */
101 typedef struct surf_resource {
102   surf_resource_private_t common_private;
103   surf_resource_public_t common_public;
104 } s_surf_resource_t;
105
106 /** \brief Public data available on all resources
107  *  \ingroup SURF_resources
108  *
109  *  These functions are implemented by all resources.
110  */
111 typedef struct surf_resource_public {
112   s_surf_action_state_t states; /**< Any living action on this resource */
113   void *(*name_service) (const char *name); /**< Return a resource given its name */
114   const char *(*get_resource_name) (void *resource_id); /**< Return the name of a resource */
115
116   e_surf_action_state_t(*action_get_state) (surf_action_t action); /**< Return the state of an action */
117   double (*action_get_start_time) (surf_action_t action); /**< Return the start time of an action */
118   double (*action_get_finish_time) (surf_action_t action); /**< Return the finish time of an action */
119   void (*action_use) (surf_action_t action); /**< Set an action used */
120   int  (*action_free) (surf_action_t action); /**< Free an action */
121   void (*action_cancel) (surf_action_t action); /**< Cancel a running action */
122   void (*action_recycle) (surf_action_t action); /**< Recycle an action */
123   void (*action_change_state) (surf_action_t action, /**< Change an action state*/
124                                e_surf_action_state_t state);
125   void (*action_set_data) (surf_action_t action, void *data); /**< Set the user data of an action */
126   void (*suspend) (surf_action_t action); /**< Suspend an action */
127   void (*resume) (surf_action_t action); /**< Resume a suspended action */
128   int (*is_suspended) (surf_action_t action); /**< Return whether an action is suspended */
129   void (*set_max_duration) (surf_action_t action, double duration); /**< Set the max duration of an action*/
130   void (*set_priority) (surf_action_t action, double priority); /**< Set the priority of an action */
131   const char *name; /**< Name of this resource */
132 } s_surf_resource_public_t, *surf_resource_public_t;
133
134 /** \brief Private data available on all resources
135  *  \ingroup SURF_resources
136  */
137 typedef struct surf_resource_private *surf_resource_private_t;
138
139 /**************************************/
140 /* Implementations of resource object */
141 /**************************************/
142
143 /** \brief Timer resource
144  *  \ingroup SURF_resources
145  */
146 typedef struct surf_timer_resource {
147   surf_resource_private_t common_private;
148   surf_resource_public_t common_public;
149   surf_timer_resource_extension_public_t extension_public;
150 } s_surf_timer_resource_t, *surf_timer_resource_t;
151
152 /** \brief Timer resource extension public
153  * \ingroup SURF_resource
154  *
155  * Additionnal functions specific to the timer resource
156  */
157 typedef struct surf_timer_resource_extension_public {
158   void (*set) (double date, void *function, void *arg);
159   int (*get)  (void **function, void **arg);
160 } s_surf_timer_resource_extension_public_t,
161   *surf_timer_resource_extension_public_t;
162
163 /** \brief The timer resource
164  *  \ingroup SURF_resources
165  */
166 extern surf_timer_resource_t surf_timer_resource;
167
168 /** \brief Initializes the timer resource
169  *  \ingroup SURF_resources
170  */
171 void surf_timer_resource_init(const char *filename);
172
173 /* Cpu resource */
174
175 /** \brief CPU state
176  *  \ingroup SURF_resources
177  */
178 typedef enum {
179   SURF_CPU_ON = 1,              /**< Ready        */
180   SURF_CPU_OFF = 0              /**< Running      */
181 } e_surf_cpu_state_t;
182
183 /** \brief CPU resource datatype
184  *  \ingroup SURF_resources
185  */
186 typedef struct surf_cpu_resource {
187   surf_resource_private_t common_private;
188   surf_resource_public_t common_public;
189   surf_cpu_resource_extension_public_t extension_public;
190 } s_surf_cpu_resource_t, *surf_cpu_resource_t;
191
192 /** \brief CPU resource extension public
193  *  \ingroup SURF_resources
194  *  
195  *  Public functions specific to the CPU resource.
196  */
197 typedef struct surf_cpu_resource_extension_public {
198   surf_action_t(*execute) (void *cpu, double size);
199   surf_action_t(*sleep) (void *cpu, double duration);
200   e_surf_cpu_state_t(*get_state) (void *cpu);
201   double (*get_speed) (void *cpu, double load);
202   double (*get_available_speed) (void *cpu);
203 } s_surf_cpu_resource_extension_public_t,
204     *surf_cpu_resource_extension_public_t;
205
206 /** \brief The CPU resource
207  *  \ingroup SURF_resources
208  */
209 extern surf_cpu_resource_t surf_cpu_resource;
210
211 /** \brief Initializes the CPU resource with the model Cas01
212  *  \ingroup SURF_resources
213  *
214  *  This function is called by surf_workstation_resource_init_CLM03
215  *  so you shouldn't have to call it by yourself.
216  *
217  *  \see surf_workstation_resource_init_CLM03()
218  */
219 void surf_cpu_resource_init_Cas01(const char *filename);
220
221 /* Network resource */
222
223 /** \brief Network resource datatype
224  *  \ingroup SURF_resources
225  */
226 typedef struct surf_network_resource {
227   surf_resource_private_t common_private;
228   surf_resource_public_t common_public;
229   surf_network_resource_extension_public_t extension_public;
230 } s_surf_network_resource_t, *surf_network_resource_t;
231
232 /** \brief Network resource extension public
233  *  \ingroup SURF_resources
234  *
235  *  Public functions specific to the network resource
236  */
237 typedef struct surf_network_resource_extension_public {
238   surf_action_t(*communicate) (void *src, void *dst, double size,
239                                double max_rate);
240   const void** (*get_route) (void *src, void *dst);
241   int (*get_route_size) (void *src, void *dst);
242   const char* (*get_link_name) (const void *link);
243   double (*get_link_bandwidth) (const void *link);
244   double (*get_link_latency) (const void *link);
245 } s_surf_network_resource_extension_public_t,
246     *surf_network_resource_extension_public_t;
247
248 /** \brief The network resource
249  *  \ingroup SURF_resources
250  *
251  *  When creating a new API on top on SURF, you shouldn't use the
252  *  network resource unless you know what you are doing. Only the workstation
253  *  resource should be accessed because depending on the platform model,
254  *  the network resource can be NULL.
255  */
256 extern surf_network_resource_t surf_network_resource;
257
258 /** \brief Initializes the platform with the network model CM02
259  *  \ingroup SURF_resources
260  *  \param filename XML platform file name
261  *
262  *  This function is called by surf_workstation_resource_init_CLM03
263  *  so you shouldn't call it by yourself.
264  *
265  *  \see surf_workstation_resource_init_CLM03()
266  */
267 void surf_network_resource_init_CM02(const char *filename);
268
269 /** \brief Workstation resource datatype.
270  *  \ingroup SURF_resources
271  *
272  */
273 typedef struct surf_workstation_resource {
274   surf_resource_private_t common_private;
275   surf_resource_public_t common_public;
276   surf_workstation_resource_extension_public_t extension_public;
277 } s_surf_workstation_resource_t, *surf_workstation_resource_t;
278
279 /** \brief Workstation resource extension public
280  *  \ingroup SURF_resources
281  *
282  *  Public functions specific to the workstation resource.
283  */
284 typedef struct surf_workstation_resource_extension_public {
285   surf_action_t(*execute) (void *workstation, double size);            /**< Execute a computation amount on a workstation
286                                                                         and create the corresponding action */
287   surf_action_t(*sleep) (void *workstation, double duration);          /**< Make a workstation sleep during a given duration */
288   e_surf_cpu_state_t(*get_state) (void *workstation);                  /**< Return the CPU state of a workstation */
289   double (*get_speed) (void *workstation, double load);                /**< Return the speed of a workstation */
290   double (*get_available_speed) (void *workstation);                   /**< Return tha available speed of a workstation */
291   surf_action_t(*communicate) (void *workstation_src,                  /**< Execute a communication amount between two workstations */
292                                void *workstation_dst, double size,
293                                double max_rate);
294   surf_action_t(*execute_parallel_task) (int workstation_nb,           /**< Execute a parallel task on several workstations */
295                                          void **workstation_list,
296                                          double *computation_amount,
297                                          double *communication_amount,
298                                          double amount,
299                                          double rate);
300   const void** (*get_route) (void *src, void *dst);                    /**< Return the network link list between two workstations */
301   int (*get_route_size) (void *src, void *dst);                        /**< Return the route size between two workstations */
302   const char* (*get_link_name) (const void *link);                     /**< Return the name of a network link */
303   double (*get_link_bandwidth) (const void *link);                     /**< Return the current bandwidth of a network link */
304   double (*get_link_latency) (const void *link);                       /**< Return the current latency of a network link */
305 } s_surf_workstation_resource_extension_public_t,
306     *surf_workstation_resource_extension_public_t;
307
308 /** \brief The workstation resource
309  *  \ingroup SURF_resources
310  *
311  *  Note that when you create an API on top of SURF,
312  *  the workstation resource should be the only one you use
313  *  because depending on the platform model, the network resource and the CPU resource
314  *  may not exist.
315  */
316 extern surf_workstation_resource_t surf_workstation_resource;
317
318 /** \brief Initializes the platform with the workstation model CLM03
319  *  \ingroup SURF_resources
320  *  \param filename XML platform file name
321  *
322  *  This platform model seperates the workstation resource and the network resource.
323  *  The workstation resource will be initialized with the model CLM03, the network
324  *  resource with the model CM02 and the CPU resource with the model Cas01.
325  *  In future releases, some other network models will be implemented and will be
326  *  combined with the workstation model CLM03.
327  *
328  *  \see surf_workstation_resource_init_KCCFLN05()
329  */
330 void surf_workstation_resource_init_CLM03(const char *filename);
331
332 /** \brief Initializes the platform with the model KCCFLN05
333  *  \ingroup SURF_resources
334  *  \param filename XML platform file name
335  *
336  *  With this model, the workstations and the network are handled together.
337  *  There is no network resource. This platform model is the default one for
338  *  MSG and SimDag.
339  *
340  *  \see surf_workstation_resource_init_CLM03()
341  */
342 void surf_workstation_resource_init_KCCFLN05(const char *filename);
343
344 /** \brief The network links
345  *  \ingroup SURF_resources
346  *
347  *  This dict contains all network links.
348  *
349  *  \see workstation_set
350  */
351 extern xbt_dict_t network_link_set;
352
353 /** \brief The workstations
354  *  \ingroup SURF_resources
355  *
356  *  This dict contains all workstations.
357  *
358  *  \see network_link_set
359  */
360 extern xbt_dict_t workstation_set;
361
362 /** \brief List of initialized resources
363  *  \ingroup SURF_resources
364  */
365 extern xbt_dynar_t resource_list;
366
367 /*******************************************/
368 /*** SURF Globals **************************/
369 /*******************************************/
370
371 /** \brief Initialize SURF
372  *  \ingroup SURF_simulation
373  *  \param argc argument number
374  *  \param argv arguments
375  *
376  *  This function has to be called to initialize the common structures.
377  *  Then you will have to create the environment by calling surf_timer_resource_init()
378  *  and surf_workstation_resource_init_CLM03() or surf_workstation_resource_init_KCCFLN05().
379  *
380  *  \see surf_timer_resource_init(), surf_workstation_resource_init_CLM03(),
381  *  surf_workstation_resource_init_KCCFLN05(), surf_exit()
382  */
383 void surf_init(int *argc, char **argv); /* initialize common structures */
384
385 /** \brief Performs a part of the simulation
386  *  \ingroup SURF_simulation
387  *  \return the elapsed time, or -1.0 if no event could be executed
388  *
389  *  This function execute all possible events, update the action states
390  *  and returns the time elapsed.
391  *  When you call execute or communicate on a resource, the corresponding actions
392  *  are not executed immediately but only when you call surf_solve.
393  *  Note that the returned elapsed time can be zero.
394  */
395 double surf_solve(void);
396
397 /** \brief Return the current time
398  *  \ingroup SURF_simulation
399  *
400  *  Return the current time in millisecond.
401  */
402 double surf_get_clock(void);
403
404 /** \brief Exit SURF
405  *  \ingroup SURF_simulation
406  *
407  *  Clean everything.
408  *
409  *  \see surf_init()
410  */
411 void surf_exit(void);
412
413 #endif                          /* _SURF_SURF_H */