Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f2affd4a4e27cd5323071c548dad3ffa5d610bb7
[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 "gras_config.h"
16
17 SG_BEGIN_DECL()
18
19
20
21 /* Actions and resources are higly connected structures... */
22
23 /** \brief Action datatype
24  *  \ingroup SURF_actions
25  *  
26  * An action is some working amount on a resource.
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_resources
35  *  
36  *  Generic data structure for a resource. The workstations,
37  *  the CPUs and the network links are examples of resources.
38  */
39 typedef struct surf_resource *surf_resource_t;
40
41 /** \brief Action structure
42  * \ingroup SURF_actions
43  *
44  *  Never create s_surf_action_t by yourself ! The actions are created
45  *  on the fly when you call execute or communicate on a resource.
46  *  
47  *  \see e_surf_action_state_t
48  */
49 typedef struct surf_action {
50   s_xbt_swag_hookup_t state_hookup;
51   xbt_swag_t state_set;
52   double cost;                  /**< cost        */
53   double priority;              /**< priority (1.0 by default) */
54   double max_duration;          /**< max_duration (may fluctuate until
55                                    the task is completed) */
56   double remains;               /**< How much of that cost remains to
57                                  * be done in the currently running task */
58   double start;                 /**< start time  */
59   double finish;                /**< finish time : this is modified during the run
60                                  * and fluctuates until the task is completed */
61   void *data;                   /**< for your convenience */
62   int using;
63   surf_resource_t resource_type;
64 } s_surf_action_t;
65
66 /** \brief Action states
67  *  \ingroup SURF_actions
68  *
69  *  Action states.
70  *
71  *  \see surf_action_t, surf_action_state_t
72  */
73 typedef enum {
74   SURF_ACTION_READY = 0,        /**< Ready        */
75   SURF_ACTION_RUNNING,          /**< Running      */
76   SURF_ACTION_FAILED,           /**< Task Failure */
77   SURF_ACTION_DONE,             /**< Completed    */
78   SURF_ACTION_TO_FREE,          /**< Action to free in next cleanup */
79   SURF_ACTION_NOT_IN_THE_SYSTEM /**< Not in the system anymore. Why did you ask ? */
80 } e_surf_action_state_t;
81
82 /** \brief Action state sets
83  *  \ingroup SURF_actions
84  *
85  *  This structure contains some sets of actions.
86  *  It provides a fast access to the actions in each state.
87  *
88  *  \see surf_action_t, e_surf_action_state_t
89  */
90 typedef struct surf_action_state {
91   xbt_swag_t ready_action_set;   /**< Actions in state SURF_ACTION_READY */
92   xbt_swag_t running_action_set; /**< Actions in state SURF_ACTION_RUNNING */
93   xbt_swag_t failed_action_set;  /**< Actions in state SURF_ACTION_FAILED */
94   xbt_swag_t done_action_set;    /**< Actions in state SURF_ACTION_DONE */
95 } s_surf_action_state_t, *surf_action_state_t;
96
97 /***************************/
98 /* Generic resource object */
99 /***************************/
100
101 /** \brief Public data available on all resources
102  *  \ingroup SURF_resources
103  *
104  *  These functions are implemented by all resources.
105  */
106 typedef struct surf_resource_public {
107   s_surf_action_state_t states; /**< Any living action on this resource */
108   void *(*name_service) (const char *name); /**< Return a resource given its name */
109   const char *(*get_resource_name) (void *resource_id); /**< Return the name of a resource */
110
111   e_surf_action_state_t(*action_get_state) (surf_action_t action); /**< Return the state of an action */
112   double (*action_get_start_time) (surf_action_t action); /**< Return the start time of an action */
113   double (*action_get_finish_time) (surf_action_t action); /**< Return the finish time of an action */
114   void (*action_use) (surf_action_t action); /**< Set an action used */
115   int  (*action_free) (surf_action_t action); /**< Free an action */
116   void (*action_cancel) (surf_action_t action); /**< Cancel a running action */
117   void (*action_recycle) (surf_action_t action); /**< Recycle an action */
118   void (*action_change_state) (surf_action_t action, /**< Change an action state*/
119                                e_surf_action_state_t state);
120   void (*action_set_data) (surf_action_t action, void *data); /**< Set the user data of an action */
121   void (*suspend) (surf_action_t action); /**< Suspend an action */
122   void (*resume) (surf_action_t action); /**< Resume a suspended action */
123   int (*is_suspended) (surf_action_t action); /**< Return whether an action is suspended */
124   void (*set_max_duration) (surf_action_t action, double duration); /**< Set the max duration of an action*/
125   void (*set_priority) (surf_action_t action, double priority); /**< Set the priority of an action */
126   const char *name; /**< Name of this resource */
127 } s_surf_resource_public_t, *surf_resource_public_t;
128
129 /** \brief Private data available on all resources
130  *  \ingroup SURF_resources
131  */
132 typedef struct surf_resource_private *surf_resource_private_t;
133
134 /** \brief Resource datatype
135  *  \ingroup SURF_resources
136  *  
137  *  Generic data structure for a resource. The workstations,
138  *  the CPUs and the network links are examples of resources.
139  */
140 typedef struct surf_resource {
141   surf_resource_private_t common_private;
142   surf_resource_public_t common_public;
143 } s_surf_resource_t;
144
145 /**************************************/
146 /* Implementations of resource object */
147 /**************************************/
148
149 /** \brief Timer resource extension public
150  * \ingroup SURF_resource
151  *
152  * Additionnal functions specific to the timer resource
153  */
154 typedef struct surf_timer_resource_extension_public {
155   void (*set) (double date, void *function, void *arg);
156   int (*get)  (void **function, void **arg);
157 } s_surf_timer_resource_extension_public_t,
158   *surf_timer_resource_extension_public_t;
159
160 /** \brief Timer resource
161  *  \ingroup SURF_resources
162  */
163 typedef struct surf_timer_resource {
164   surf_resource_private_t common_private;
165   surf_resource_public_t common_public;
166   surf_timer_resource_extension_public_t extension_public;
167 } s_surf_timer_resource_t, *surf_timer_resource_t;
168
169 /** \brief The timer resource
170  *  \ingroup SURF_resources
171  */
172 XBT_PUBLIC_DATA(surf_timer_resource_t) surf_timer_resource;
173
174 /** \brief Initializes the timer resource
175  *  \ingroup SURF_resources
176  */
177 XBT_PUBLIC(void) surf_timer_resource_init(const char *filename);
178
179 /* Cpu resource */
180
181 /** \brief CPU state
182  *  \ingroup SURF_resources
183  */
184 typedef enum {
185   SURF_CPU_ON = 1,              /**< Ready        */
186   SURF_CPU_OFF = 0              /**< Running      */
187 } e_surf_cpu_state_t;
188
189 /** \brief CPU resource extension public
190  *  \ingroup SURF_resources
191  *  
192  *  Public functions specific to the CPU resource.
193  */
194 typedef struct surf_cpu_resource_extension_public {
195   surf_action_t(*execute) (void *cpu, double size);
196   surf_action_t(*sleep) (void *cpu, double duration);
197   e_surf_cpu_state_t(*get_state) (void *cpu);
198   double (*get_speed) (void *cpu, double load);
199   double (*get_available_speed) (void *cpu);
200 } s_surf_cpu_resource_extension_public_t,
201     *surf_cpu_resource_extension_public_t;
202
203 /** \brief CPU resource datatype
204  *  \ingroup SURF_resources
205  */
206 typedef struct surf_cpu_resource {
207   surf_resource_private_t common_private;
208   surf_resource_public_t common_public;
209   surf_cpu_resource_extension_public_t extension_public;
210 } s_surf_cpu_resource_t, *surf_cpu_resource_t;
211
212 /** \brief The CPU resource
213  *  \ingroup SURF_resources
214  */
215 XBT_PUBLIC_DATA(surf_cpu_resource_t) surf_cpu_resource;
216
217 /** \brief Initializes the CPU resource with the model Cas01
218  *  \ingroup SURF_resources
219  *
220  *  This function is called by surf_workstation_resource_init_CLM03
221  *  so you shouldn't have to call it by yourself.
222  *
223  *  \see surf_workstation_resource_init_CLM03()
224  */
225 XBT_PUBLIC(void) surf_cpu_resource_init_Cas01(const char *filename);
226
227 /* Network resource */
228
229 /** \brief Network resource extension public
230  *  \ingroup SURF_resources
231  *
232  *  Public functions specific to the network resource
233  */
234 typedef struct surf_network_resource_extension_public {
235   surf_action_t(*communicate) (void *src, void *dst, double size,
236                                double max_rate);
237   const void** (*get_route) (void *src, void *dst);
238   int (*get_route_size) (void *src, void *dst);
239   const char* (*get_link_name) (const void *link);
240   double (*get_link_bandwidth) (const void *link);
241   double (*get_link_latency) (const void *link);
242 } s_surf_network_resource_extension_public_t,
243     *surf_network_resource_extension_public_t;
244
245 /** \brief Network resource datatype
246  *  \ingroup SURF_resources
247  */
248 typedef struct surf_network_resource {
249   surf_resource_private_t common_private;
250   surf_resource_public_t common_public;
251   surf_network_resource_extension_public_t extension_public;
252 } s_surf_network_resource_t, *surf_network_resource_t;
253
254 /** \brief The network resource
255  *  \ingroup SURF_resources
256  *
257  *  When creating a new API on top on SURF, you shouldn't use the
258  *  network resource unless you know what you are doing. Only the workstation
259  *  resource should be accessed because depending on the platform model,
260  *  the network resource can be NULL.
261  */
262 XBT_PUBLIC_DATA(surf_network_resource_t) surf_network_resource;
263
264 /** \brief Initializes the platform with the network model CM02
265  *  \ingroup SURF_resources
266  *  \param filename XML platform file name
267  *
268  *  This function is called by surf_workstation_resource_init_CLM03
269  *  so you shouldn't call it by yourself.
270  *
271  *  \see surf_workstation_resource_init_CLM03()
272  */
273 XBT_PUBLIC(void) surf_network_resource_init_CM02(const char *filename);
274
275 /** \brief Workstation resource extension public
276  *  \ingroup SURF_resources
277  *
278  *  Public functions specific to the workstation resource.
279  */
280 typedef struct surf_workstation_resource_extension_public {
281   surf_action_t(*execute) (void *workstation, double size);            /**< Execute a computation amount on a workstation
282                                                                         and create the corresponding action */
283   surf_action_t(*sleep) (void *workstation, double duration);          /**< Make a workstation sleep during a given duration */
284   e_surf_cpu_state_t(*get_state) (void *workstation);                  /**< Return the CPU state of a workstation */
285   double (*get_speed) (void *workstation, double load);                /**< Return the speed of a workstation */
286   double (*get_available_speed) (void *workstation);                   /**< Return tha available speed of a workstation */
287   surf_action_t(*communicate) (void *workstation_src,                  /**< Execute a communication amount between two workstations */
288                                void *workstation_dst, double size,
289                                double max_rate);
290   surf_action_t(*execute_parallel_task) (int workstation_nb,           /**< Execute a parallel task on several workstations */
291                                          void **workstation_list,
292                                          double *computation_amount,
293                                          double *communication_amount,
294                                          double amount,
295                                          double rate);
296   const void** (*get_route) (void *src, void *dst);                    /**< Return the network link list between two workstations */
297   int (*get_route_size) (void *src, void *dst);                        /**< Return the route size between two workstations */
298   const char* (*get_link_name) (const void *link);                     /**< Return the name of a network link */
299   double (*get_link_bandwidth) (const void *link);                     /**< Return the current bandwidth of a network link */
300   double (*get_link_latency) (const void *link);                       /**< Return the current latency of a network link */
301 } s_surf_workstation_resource_extension_public_t,
302     *surf_workstation_resource_extension_public_t;
303
304 /** \brief Workstation resource datatype.
305  *  \ingroup SURF_resources
306  *
307  */
308 typedef struct surf_workstation_resource {
309   surf_resource_private_t common_private;
310   surf_resource_public_t common_public;
311   surf_workstation_resource_extension_public_t extension_public;
312 } s_surf_workstation_resource_t, *surf_workstation_resource_t;
313
314 /** \brief The workstation resource
315  *  \ingroup SURF_resources
316  *
317  *  Note that when you create an API on top of SURF,
318  *  the workstation resource should be the only one you use
319  *  because depending on the platform model, the network resource and the CPU resource
320  *  may not exist.
321  */
322 XBT_PUBLIC_DATA(surf_workstation_resource_t) surf_workstation_resource;
323
324 /** \brief Initializes the platform with the workstation model CLM03
325  *  \ingroup SURF_resources
326  *  \param filename XML platform file name
327  *
328  *  This platform model seperates the workstation resource and the network resource.
329  *  The workstation resource will be initialized with the model CLM03, the network
330  *  resource with the model CM02 and the CPU resource with the model Cas01.
331  *  In future releases, some other network models will be implemented and will be
332  *  combined with the workstation model CLM03.
333  *
334  *  \see surf_workstation_resource_init_KCCFLN05()
335  */
336 XBT_PUBLIC(void) surf_workstation_resource_init_CLM03(const char *filename);
337
338 /** \brief Initializes the platform with the model KCCFLN05
339  *  \ingroup SURF_resources
340  *  \param filename XML platform file name
341  *
342  *  With this model, the workstations and the network are handled together.
343  *  There is no network resource. This platform model is the default one for
344  *  MSG and SimDag.
345  *
346  *  \see surf_workstation_resource_init_CLM03()
347  */
348 XBT_PUBLIC(void) surf_workstation_resource_init_KCCFLN05(const char *filename);
349
350 /** \brief Initializes the platform with the model KCCFLN05 using the proportional
351  *  approach as described in [TAG03]. 
352  *  
353  *  \ingroup SURF_resources
354  *  \param filename XML platform file name
355  *
356  *  This function implements the proportional fairness known as the maximization
357  *  of sum ( x1*x2*...*xn ).
358  *
359  *  Reference:
360  *
361  *  [TAG03]. Corinne Touati, Eitan Altman, and Jérôme Galtier.  
362  *  Semi-definite programming approach for bandwidth allocation and routing in networks.
363  *  Game Theory and Applications, 9:169-179, December 2003. Nova publisher.
364  *  With this model, the workstations and the network are handled together.
365  *  There is no network resource. This platform model is the default one for
366  *  MSG and SimDag.
367  *
368  *  \see surf_workstation_resource_init_CLM03()
369  */
370 XBT_PUBLIC(void) surf_workstation_resource_init_KCCFLN05_proportionnal(const char *filename);
371
372 /** \brief Initializes the platform with the model KCCFLN05 using a lagrange
373  *  optimization approach to compute the effectivet bandwidth of each flow based
374  *  on the Vegas TCP flavor fairness as shown in [LOW03]. 
375  *  
376  *  \ingroup SURF_resources
377  *  \param filename XML platform file name
378  *
379  *  This problem is related to max( sum( a * Df * ln(xi) ) ) which is equivalent 
380  *  to the proportional fairness.
381  *
382  *  Reference:
383  *  [LOW03] S. H. Low. A duality model of TCP and queue management algorithms.
384  *  IEEE/ACM Transaction on Networking, 11(4):525-536, 2003.
385  *
386  */
387 XBT_PUBLIC(void) surf_workstation_resource_init_KCCFLN05_Vegas(const char *filename);
388
389 /** \brief Initializes the platform with the model KCCFLN05 using a lagrange
390  *  optimization approach to compute the effectivet bandwidth of each flow based
391  *  on the Reno TCP flavor fairness as shown in [LOW03]. 
392  *  
393  *  \ingroup SURF_resources
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  *  \see surf_workstation_resource_init_KCCFLN05_Vegas()
403  */
404 XBT_PUBLIC(void) surf_workstation_resource_init_KCCFLN05_Reno(const char *filename);
405
406
407
408
409 #ifdef USE_GTNETS
410 XBT_PUBLIC(void) surf_workstation_resource_init_GTNETS(const char *filename);
411 #endif
412
413 /** \brief The network links
414  *  \ingroup SURF_resources
415  *
416  *  This dict contains all network links.
417  *
418  *  \see workstation_set
419  */
420 XBT_PUBLIC_DATA(xbt_dict_t) network_link_set;
421
422 /** \brief The workstations
423  *  \ingroup SURF_resources
424  *
425  *  This dict contains all workstations.
426  *
427  *  \see network_link_set
428  */
429 XBT_PUBLIC_DATA(xbt_dict_t)  workstation_set;
430
431 /** \brief List of initialized resources
432  *  \ingroup SURF_resources
433  */
434 XBT_PUBLIC_DATA(xbt_dynar_t)  resource_list;
435
436 /*******************************************/
437 /*** SURF Globals **************************/
438 /*******************************************/
439
440 /** \brief Initialize SURF
441  *  \ingroup SURF_simulation
442  *  \param argc argument number
443  *  \param argv arguments
444  *
445  *  This function has to be called to initialize the common structures.
446  *  Then you will have to create the environment by calling surf_timer_resource_init()
447  *  and surf_workstation_resource_init_CLM03() or surf_workstation_resource_init_KCCFLN05().
448  *
449  *  \see surf_timer_resource_init(), surf_workstation_resource_init_CLM03(),
450  *  surf_workstation_resource_init_KCCFLN05(), surf_exit()
451  */
452 XBT_PUBLIC(void) surf_init(int *argc, char **argv);     /* initialize common structures */
453
454 /** \brief Performs a part of the simulation
455  *  \ingroup SURF_simulation
456  *  \return the elapsed time, or -1.0 if no event could be executed
457  *
458  *  This function execute all possible events, update the action states
459  *  and returns the time elapsed.
460  *  When you call execute or communicate on a resource, the corresponding actions
461  *  are not executed immediately but only when you call surf_solve.
462  *  Note that the returned elapsed time can be zero.
463  */
464 XBT_PUBLIC(double) surf_solve(void);
465
466 /** \brief Return the current time
467  *  \ingroup SURF_simulation
468  *
469  *  Return the current time in millisecond.
470  */
471 XBT_PUBLIC(double)surf_get_clock(void);
472
473 /** \brief Exit SURF
474  *  \ingroup SURF_simulation
475  *
476  *  Clean everything.
477  *
478  *  \see surf_init()
479  */
480 XBT_PUBLIC(void) surf_exit(void);
481
482
483 SG_END_DECL()
484
485 #endif                          /* _SURF_SURF_H */