Logo AND Algorithmique Numérique Distribuée

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