Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[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 models are higly connected structures... */
21
22 /** \brief Action datatype
23  *  \ingroup SURF_actions
24  *  
25  * An action is some working amount on a model.
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_models
34  *  
35  *  Generic data structure for a model. The workstations,
36  *  the CPUs and the network links are examples of models.
37  */
38 typedef struct surf_model *surf_model_t;
39
40 /** \brief Resource model description
41  */
42 typedef struct surf_model_description {
43   const char *name;
44   surf_model_t model;
45   void (* model_init) (const char *filename);
46   void (* create_ws) (void);
47 } s_surf_model_description_t, *surf_model_description_t;
48
49 XBT_PUBLIC(void) update_model_description(s_surf_model_description_t *table,
50                                           const char* name, 
51                                           surf_model_t model
52                                           );
53 XBT_PUBLIC(int) find_model_description(s_surf_model_description_t *table,
54                                        const char* name);
55
56 /** \brief Action structure
57  * \ingroup SURF_actions
58  *
59  *  Never create s_surf_action_t by yourself ! The actions are created
60  *  on the fly when you call execute or communicate on a model.
61  *  
62  *  \see e_surf_action_state_t
63  */
64 typedef struct surf_action {
65   s_xbt_swag_hookup_t state_hookup;
66   xbt_swag_t state_set;
67   double cost;                  /**< cost        */
68   double priority;              /**< priority (1.0 by default) */
69   double max_duration;          /**< max_duration (may fluctuate until
70                                    the task is completed) */
71   double remains;               /**< How much of that cost remains to
72                                  * be done in the currently running task */
73   double start;                 /**< start time  */
74   double finish;                /**< finish time : this is modified during the run
75                                  * and fluctuates until the task is completed */
76   void *data;                   /**< for your convenience */
77   int using;
78   surf_model_t model_type;
79 } s_surf_action_t;
80
81 /** \brief Action states
82  *  \ingroup SURF_actions
83  *
84  *  Action states.
85  *
86  *  \see surf_action_t, surf_action_state_t
87  */
88 typedef enum {
89   SURF_ACTION_READY = 0,        /**< Ready        */
90   SURF_ACTION_RUNNING,          /**< Running      */
91   SURF_ACTION_FAILED,           /**< Task Failure */
92   SURF_ACTION_DONE,             /**< Completed    */
93   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
94   SURF_ACTION_NOT_IN_THE_SYSTEM /**< Not in the system anymore. Why did you ask ? */
95 } e_surf_action_state_t;
96
97 /** \brief Action state sets
98  *  \ingroup SURF_actions
99  *
100  *  This structure contains some sets of actions.
101  *  It provides a fast access to the actions in each state.
102  *
103  *  \see surf_action_t, e_surf_action_state_t
104  */
105 typedef struct surf_action_state {
106   xbt_swag_t ready_action_set;   /**< Actions in state SURF_ACTION_READY */
107   xbt_swag_t running_action_set; /**< Actions in state SURF_ACTION_RUNNING */
108   xbt_swag_t failed_action_set;  /**< Actions in state SURF_ACTION_FAILED */
109   xbt_swag_t done_action_set;    /**< Actions in state SURF_ACTION_DONE */
110 } s_surf_action_state_t, *surf_action_state_t;
111
112 /***************************/
113 /* Generic model object */
114 /***************************/
115
116 /** \brief Public data available on all models
117  *  \ingroup SURF_models
118  *
119  *  These functions are implemented by all models.
120  */
121 typedef struct surf_model_public {
122   s_surf_action_state_t states; /**< Any living action on this model */
123   void *(*name_service) (const char *name); /**< Return a model given its name */
124   const char *(*get_resource_name) (void *resource_id); /**< Return the name of a resource */
125
126   e_surf_action_state_t(*action_get_state) (surf_action_t action); /**< Return the state of an action */
127   double (*action_get_start_time) (surf_action_t action); /**< Return the start time of an action */
128   double (*action_get_finish_time) (surf_action_t action); /**< Return the finish time of an action */
129   void (*action_use) (surf_action_t action); /**< Set an action used */
130   int  (*action_free) (surf_action_t action); /**< Free an action */
131   void (*action_cancel) (surf_action_t action); /**< Cancel a running action */
132   void (*action_recycle) (surf_action_t action); /**< Recycle an action */
133   void (*action_change_state) (surf_action_t action, /**< Change an action state*/
134                                e_surf_action_state_t state);
135   void (*action_set_data) (surf_action_t action, void *data); /**< Set the user data of an action */
136   void (*suspend) (surf_action_t action); /**< Suspend an action */
137   void (*resume) (surf_action_t action); /**< Resume a suspended action */
138   int (*is_suspended) (surf_action_t action); /**< Return whether an action is suspended */
139   void (*set_max_duration) (surf_action_t action, double duration); /**< Set the max duration of an action*/
140   void (*set_priority) (surf_action_t action, double priority); /**< Set the priority of an action */
141   xbt_dict_t (*get_properties) (void* resource_id); /**< Return the properties dictionary */
142   const char *name; /**< Name of this model */
143 } s_surf_model_public_t, *surf_model_public_t;
144
145 /** \brief Private data available on all models
146  *  \ingroup SURF_models
147  */
148 typedef struct surf_model_private *surf_model_private_t;
149
150 /** \brief Resource datatype
151  *  \ingroup SURF_models
152  *  
153  *  Generic data structure for a model. The workstations,
154  *  the CPUs and the network links are examples of models.
155  */
156 typedef struct surf_model {
157   surf_model_private_t common_private;
158   surf_model_public_t common_public;
159 } s_surf_model_t;
160
161 /**************************************/
162 /* Implementations of model object */
163 /**************************************/
164
165 /** \brief Timer model extension public
166  * \ingroup SURF_model
167  *
168  * Additionnal functions specific to the timer model
169  */
170 typedef struct surf_timer_model_extension_public {
171   void (*set) (double date, void *function, void *arg);
172   int (*get)  (void **function, void **arg);
173 } s_surf_timer_model_extension_public_t,
174   *surf_timer_model_extension_public_t;
175
176 /** \brief Timer model
177  *  \ingroup SURF_models
178  */
179 typedef struct surf_timer_model {
180   surf_model_private_t common_private;
181   surf_model_public_t common_public;
182   surf_timer_model_extension_public_t extension_public;
183 } s_surf_timer_model_t, *surf_timer_model_t;
184
185 /** \brief The timer model
186  *  \ingroup SURF_models
187  */
188 XBT_PUBLIC_DATA(surf_timer_model_t) surf_timer_model;
189
190 /** \brief Initializes the timer model
191  *  \ingroup SURF_models
192  */
193 XBT_PUBLIC(void) surf_timer_model_init(const char *filename);
194
195 /* Cpu model */
196
197 /** \brief CPU state
198  *  \ingroup SURF_models
199  */
200 typedef enum {
201   SURF_CPU_ON = 1,              /**< Up & ready        */
202   SURF_CPU_OFF = 0              /**< Down & broken     */
203 } e_surf_cpu_state_t;
204
205 /** \brief CPU model extension public
206  *  \ingroup SURF_models
207  *  
208  *  Public functions specific to the CPU model.
209  */
210 typedef struct surf_cpu_model_extension_public {
211   surf_action_t(*execute) (void *cpu, double size);
212   surf_action_t(*sleep) (void *cpu, double duration);
213   e_surf_cpu_state_t(*get_state) (void *cpu);
214   double (*get_speed) (void *cpu, double load);
215   double (*get_available_speed) (void *cpu);
216 } s_surf_cpu_model_extension_public_t,
217     *surf_cpu_model_extension_public_t;
218
219 /** \brief CPU model datatype
220  *  \ingroup SURF_models
221  */
222 typedef struct surf_cpu_model {
223   surf_model_private_t common_private;
224   surf_model_public_t common_public;
225   surf_cpu_model_extension_public_t extension_public;
226 } s_surf_cpu_model_t, *surf_cpu_model_t;
227
228 /** \brief The CPU model
229  *  \ingroup SURF_models
230  */
231 XBT_PUBLIC_DATA(surf_cpu_model_t) surf_cpu_model;
232
233 /** \brief Initializes the CPU model with the model Cas01
234  *  \ingroup SURF_models
235  *
236  *  This function is called by surf_workstation_model_init_CLM03
237  *  so you shouldn't have to call it by yourself.
238  *
239  *  \see surf_workstation_model_init_CLM03()
240  */
241 XBT_PUBLIC(void) surf_cpu_model_init_Cas01(const char *filename);
242
243 /** \brief The list of all available cpu model models
244  *  \ingroup SURF_models
245  */
246 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_cpu_model_description[];
247
248 /* Network model */
249
250 /** \brief Network model extension public
251  *  \ingroup SURF_models
252  *
253  *  Public functions specific to the network model
254  */
255 typedef struct surf_network_model_extension_public {
256   surf_action_t(*communicate) (void *src, void *dst, double size,
257                                double max_rate);
258   const void** (*get_route) (void *src, void *dst);
259   int (*get_route_size) (void *src, void *dst);
260   const char* (*get_link_name) (const void *link);
261   double (*get_link_bandwidth) (const void *link);
262   double (*get_link_latency) (const void *link);
263 } s_surf_network_model_extension_public_t,
264     *surf_network_model_extension_public_t;
265
266 /** \brief Network model datatype
267  *  \ingroup SURF_models
268  */
269 typedef struct surf_network_model {
270   surf_model_private_t common_private;
271   surf_model_public_t common_public;
272   surf_network_model_extension_public_t extension_public;
273 } s_surf_network_model_t, *surf_network_model_t;
274
275 XBT_PUBLIC(void) create_workstations(void);
276
277 /** \brief The network model
278  *  \ingroup SURF_models
279  *
280  *  When creating a new API on top on SURF, you shouldn't use the
281  *  network model unless you know what you are doing. Only the workstation
282  *  model should be accessed because depending on the platform model,
283  *  the network model can be NULL.
284  */
285 XBT_PUBLIC_DATA(surf_network_model_t) surf_network_model;
286
287
288 /** \brief Initializes the platform with the network model 'Constant'
289  *  \ingroup SURF_models
290  *  \param filename XML platform file name
291  *
292  *  In this model, the communication time between two network cards is
293  *  constant, hence no need for a routing table. This is particularly
294  *  usefull when simulating huge distributed algorithms where
295  *  scalability is really an issue. This function is called in
296  *  conjunction with surf_workstation_model_init_compound.
297  *
298  *  \see surf_workstation_model_init_compound()
299  */
300 XBT_PUBLIC(void) surf_network_model_init_Constant(const char *filename);
301
302 /** \brief Initializes the platform with the network model CM02
303  *  \ingroup SURF_models
304  *  \param filename XML platform file name
305  *
306  *  This function is called by surf_workstation_model_init_CLM03
307  *  or by yourself only if you plan using surf_workstation_model_init_compound
308  *
309  *  \see surf_workstation_model_init_CLM03()
310  */
311 XBT_PUBLIC(void) surf_network_model_init_CM02(const char *filename);
312
313 #ifdef HAVE_GTNETS
314 /** \brief Initializes the platform with the network model GTNETS
315  *  \ingroup SURF_models
316  *  \param filename XML platform file name
317  *
318  *  This function is called by surf_workstation_model_init_GTNETS
319  *  or by yourself only if you plan using surf_workstation_model_init_compound
320  *
321  *  \see surf_workstation_model_init_GTNETS()
322  */
323 XBT_PUBLIC(void) surf_network_model_init_GTNETS(const char *filename);
324 #endif
325
326 /** \brief Initializes the platform with the network model Reno
327  *  \ingroup SURF_models
328  *  \param filename XML platform file name
329  *
330  *  The problem is related to max( sum( arctan(C * Df * xi) ) ).
331  *
332  *  Reference:
333  *  [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
334  *  IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
335  *
336  *  Call this function only if you plan using surf_workstation_model_init_compound.
337  *
338  */
339 XBT_PUBLIC(void) surf_network_model_init_Reno(const char *filename);
340
341 /** \brief Initializes the platform with the network model Reno2
342  *  \ingroup SURF_models
343  *  \param filename XML platform file name
344  *
345  *  The problem is related to max( sum( arctan(C * Df * xi) ) ).
346  *
347  *  Reference:
348  *  [LOW01] S. H. Low. A duality model of TCP and queue management algorithms.
349  *  IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
350  *
351  *  Call this function only if you plan using surf_workstation_model_init_compound.
352  *
353  */
354 XBT_PUBLIC(void) surf_network_model_init_Reno2(const char *filename);
355
356 /** \brief Initializes the platform with the network model Vegas
357  *  \ingroup SURF_models
358  *  \param filename XML platform file name
359  *
360  *  This problem is related to max( sum( a * Df * ln(xi) ) ) which is equivalent 
361  *  to the proportional fairness.
362  *
363  *  Reference:
364  *  [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
365  *  IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
366  *
367  *  Call this function only if you plan using surf_workstation_model_init_compound.
368  *
369  */
370 XBT_PUBLIC(void) surf_network_model_init_Vegas(const char *filename);
371
372 #ifdef HAVE_SDP
373 /** \brief Initializes the platform with the network model based on SDP
374  *  \ingroup SURF_models
375  *  \param filename XML platform file name
376  *
377  *  This function implements the proportional fairness known as the maximization
378  *  of x1*x2*...*xn .
379  *
380  *  Reference:
381  *
382  *  [TAG03]. Corinne Touati, Eitan Altman, and Jerome Galtier.  
383  *  Semi-definite programming approach for bandwidth allocation and routing in networks.
384  *  Game Theory and Applications, 9:169-179, December 2003. Nova publisher.
385  *
386  *  Call this function only if you plan using surf_workstation_model_init_compound.
387  */
388 XBT_PUBLIC(void) surf_network_model_init_SDP(const char *filename);
389 #endif
390
391 /** \brief The list of all available network model models
392  *  \ingroup SURF_models
393  */
394 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_network_model_description[];
395
396 /** \brief Workstation model extension public
397  *  \ingroup SURF_models
398  *
399  *  Public functions specific to the workstation model.
400  */
401 typedef struct surf_workstation_model_extension_public {
402   surf_action_t(*execute) (void *workstation, double size);            /**< Execute a computation amount on a workstation
403                                                                         and create the corresponding action */
404   surf_action_t(*sleep) (void *workstation, double duration);          /**< Make a workstation sleep during a given duration */
405   e_surf_cpu_state_t(*get_state) (void *workstation);                  /**< Return the CPU state of a workstation */
406   double (*get_speed) (void *workstation, double load);                /**< Return the speed of a workstation */
407   double (*get_available_speed) (void *workstation);                   /**< Return tha available speed of a workstation */
408   surf_action_t(*communicate) (void *workstation_src,                  /**< Execute a communication amount between two workstations */
409                                void *workstation_dst, double size,
410                                double max_rate);
411
412   surf_action_t(*execute_parallel_task) (int workstation_nb,           /**< Execute a parallel task on several workstations */
413                                          void **workstation_list,
414                                          double *computation_amount,
415                                          double *communication_amount,
416                                          double amount,
417                                          double rate);
418   const void** (*get_route) (void *src, void *dst);                    /**< Return the network link list between two workstations */
419   int (*get_route_size) (void *src, void *dst);                        /**< Return the route size between two workstations */
420   const char* (*get_link_name) (const void *link);                     /**< Return the name of a network link */
421   double (*get_link_bandwidth) (const void *link);                     /**< Return the current bandwidth of a network link */
422   double (*get_link_latency) (const void *link);                       /**< Return the current latency of a network link */
423 } s_surf_workstation_model_extension_public_t,
424     *surf_workstation_model_extension_public_t;
425
426 /** \brief Workstation model datatype.
427  *  \ingroup SURF_models
428  *
429  */
430 typedef struct surf_workstation_model {
431   surf_model_private_t common_private;
432   surf_model_public_t common_public;
433   surf_workstation_model_extension_public_t extension_public;
434 } s_surf_workstation_model_t, *surf_workstation_model_t;
435
436 /** \brief The workstation model
437  *  \ingroup SURF_models
438  *
439  *  Note that when you create an API on top of SURF,
440  *  the workstation model should be the only one you use
441  *  because depending on the platform model, the network model and the CPU model
442  *  may not exist.
443  */
444 XBT_PUBLIC_DATA(surf_workstation_model_t) surf_workstation_model;
445
446 /** \brief Initializes the platform with a compound workstation model
447  *  \ingroup SURF_models
448  *  \param filename XML platform file name
449  *
450  *  This function should be called after a cpu_model and a
451  *  network_model have been set up.
452  *
453  */
454 XBT_PUBLIC(void) surf_workstation_model_init_compound(const char *filename);
455
456 /** \brief Initializes the platform with the workstation model CLM03
457  *  \ingroup SURF_models
458  *  \param filename XML platform file name
459  *
460  *  This platform model seperates the workstation model and the network model.
461  *  The workstation model will be initialized with the model CLM03, the network
462  *  model with the model CM02 and the CPU model with the model Cas01.
463  *  In future releases, some other network models will be implemented and will be
464  *  combined with the workstation model CLM03.
465  *
466  *  \see surf_workstation_model_init_KCCFLN05()
467  */
468 XBT_PUBLIC(void) surf_workstation_model_init_CLM03(const char *filename);
469
470 /** \brief Initializes the platform with the model KCCFLN05
471  *  \ingroup SURF_models
472  *  \param filename XML platform file name
473  *
474  *  With this model, the workstations and the network are handled
475  *  together. The network model is roughly the same as in CM02 but
476  *  interference between computations and communications can be taken
477  *  into account. This platform model is the default one for MSG and
478  *  SimDag.
479  *
480  */
481 XBT_PUBLIC(void) surf_workstation_model_init_KCCFLN05(const char *filename);
482
483 /** \brief Initializes the platform with the model KCCFLN05
484  *  \ingroup SURF_models
485  *  \param filename XML platform file name
486  *
487  *  With this model, only parallel tasks can be used. Resource sharing
488  *  is done by identifying bottlenecks and giving an equal share of
489  *  the model to each action.
490  *
491  */
492 XBT_PUBLIC(void) surf_workstation_model_init_ptask_L07(const char *filename);
493
494 /** \brief The list of all available workstation model models
495  *  \ingroup SURF_models
496  */
497 XBT_PUBLIC_DATA(s_surf_model_description_t) surf_workstation_model_description[];
498
499 /** \brief The network links
500  *  \ingroup SURF_models
501  *
502  *  This dict contains all network links.
503  *
504  *  \see workstation_set
505  */
506 XBT_PUBLIC_DATA(xbt_dict_t) link_set;
507
508 /** \brief The workstations
509  *  \ingroup SURF_models
510  *
511  *  This dict contains all workstations.
512  *
513  *  \see link_set
514  */
515 XBT_PUBLIC_DATA(xbt_dict_t)  workstation_set;
516 XBT_PUBLIC_DATA(xbt_dict_t)  cpu_set;
517 /** \brief List of initialized models
518  *  \ingroup SURF_models
519  */
520 XBT_PUBLIC_DATA(xbt_dynar_t)  model_list;
521
522 /*******************************************/
523 /*** SURF Globals **************************/
524 /*******************************************/
525
526 /** \brief Initialize SURF
527  *  \ingroup SURF_simulation
528  *  \param argc argument number
529  *  \param argv arguments
530  *
531  *  This function has to be called to initialize the common
532  *  structures.  Then you will have to create the environment by
533  *  calling surf_timer_model_init() and
534  *  e.g. surf_workstation_model_init_CLM03() or
535  *  surf_workstation_model_init_KCCFLN05().
536  *
537  *  \see surf_timer_model_init(), surf_workstation_model_init_CLM03(),
538  *  surf_workstation_model_init_KCCFLN05(), surf_workstation_model_init_compound(), surf_exit()
539  */
540 XBT_PUBLIC(void) surf_init(int *argc, char **argv);     /* initialize common structures */
541
542 /** \brief Finish simulation initialization
543  *  \ingroup SURF_simulation
544  *
545  *  This function must be called before the first call to surf_solve()
546  */
547 XBT_PUBLIC(void) surf_presolve(void);
548
549 /** \brief Performs a part of the simulation
550  *  \ingroup SURF_simulation
551  *  \return the elapsed time, or -1.0 if no event could be executed
552  *
553  *  This function execute all possible events, update the action states
554  *  and returns the time elapsed.
555  *  When you call execute or communicate on a model, the corresponding actions
556  *  are not executed immediately but only when you call surf_solve.
557  *  Note that the returned elapsed time can be zero.
558  */
559 XBT_PUBLIC(double) surf_solve(void);
560
561 /** \brief Return the current time
562  *  \ingroup SURF_simulation
563  *
564  *  Return the current time in millisecond.
565  */
566 XBT_PUBLIC(double)surf_get_clock(void);
567
568 /** \brief Exit SURF
569  *  \ingroup SURF_simulation
570  *
571  *  Clean everything.
572  *
573  *  \see surf_init()
574  */
575 XBT_PUBLIC(void) surf_exit(void);
576
577 /* Prototypes of the functions that handle the properties */
578 XBT_PUBLIC_DATA(xbt_dict_t) current_property_set; /* the prop set for the currently parsed element (also used in SIMIX) */
579 XBT_PUBLIC_DATA(void) parse_properties(void);
580
581 /* surf parse file related (public because called from a test suite) */
582 XBT_PUBLIC(void) parse_platform_file(const char* file);
583
584 /* Stores the sets */
585 XBT_PUBLIC_DATA(xbt_dict_t) set_list;
586
587 XBT_PUBLIC_DATA(void) manage_route(xbt_dict_t route_table, const char* route_name, int action, int isMultiRoute);
588 XBT_PUBLIC_DATA(int) route_action;
589
590 /* This is used by all models when creating the routing table while parsing */
591 XBT_PUBLIC_DATA(xbt_dict_t) route_table;
592 XBT_PUBLIC_DATA(xbt_dict_t) route_multi_table;
593 XBT_PUBLIC_DATA(xbt_dict_t) route_multi_table;
594
595 /* For the trace and trace:connect tag (store their content till the end of the parsing) */
596 XBT_PUBLIC_DATA(xbt_dict_t) traces_set_list;
597 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_host_avail;
598 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_power;
599 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_link_avail;
600 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_bandwidth;
601 XBT_PUBLIC_DATA(xbt_dict_t) trace_connect_list_latency;
602
603
604 XBT_PUBLIC_DATA(double) get_cpu_power(const char* power);
605
606
607 SG_END_DECL()
608
609 #endif                          /* _SURF_SURF_H */