Logo AND Algorithmique Numérique Distribuée

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