Logo AND Algorithmique Numérique Distribuée

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