Logo AND Algorithmique Numérique Distribuée

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