Logo AND Algorithmique Numérique Distribuée

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