Logo AND Algorithmique Numérique Distribuée

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