Logo AND Algorithmique Numérique Distribuée

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