Logo AND Algorithmique Numérique Distribuée

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