Logo AND Algorithmique Numérique Distribuée

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