Logo AND Algorithmique Numérique Distribuée

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