Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
960f5bdbd5151cb7916fa3b32a51e9bf313fac8e
[simgrid.git] / include / simgrid / simix.h
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef _SIMIX_SIMIX_H
8 #define _SIMIX_SIMIX_H
9
10 #include "xbt/misc.h"
11 #include "xbt/fifo.h"
12 #include "xbt/dict.h"
13 #include "xbt/file_stat.h"
14 #include "xbt/function_types.h"
15 #include "xbt/parmap.h"
16 #include "xbt/swag.h"
17
18 SG_BEGIN_DECL()
19
20
21 /* ******************************** Host ************************************ */
22 /** @defgroup m_datatypes_management_details Details on SIMIX datatypes */
23 /** @brief Host datatype
24     @ingroup m_datatypes_management
25
26     A <em>location</em> (or <em>host</em>) is any possible place where
27     a process may run. Thus it is represented as a <em>physical
28     resource with computing capabilities</em>, some <em>mailboxes</em>
29     to enable running process to communicate with remote ones, and
30     some <em>private data</em> that can be only accessed by local
31     process.
32
33     \see m_host_management
34   @{ */
35 typedef struct s_smx_host *smx_host_t;
36 typedef enum {
37   SIMIX_WAITING,
38   SIMIX_READY,
39   SIMIX_RUNNING,
40   SIMIX_DONE,
41   SIMIX_CANCELED,
42   SIMIX_FAILED,
43   SIMIX_SRC_HOST_FAILURE,
44   SIMIX_DST_HOST_FAILURE,
45   SIMIX_SRC_TIMEOUT,
46   SIMIX_DST_TIMEOUT,
47   SIMIX_LINK_FAILURE
48 } e_smx_state_t;
49 /** @} */
50
51
52 typedef struct s_smx_timer* smx_timer_t;
53
54 /* ******************************** Synchro ************************************ */
55 typedef struct s_smx_mutex *smx_mutex_t;
56 typedef struct s_smx_cond *smx_cond_t;
57 typedef struct s_smx_sem *smx_sem_t;
58
59 /********************************** File *************************************/
60 typedef struct s_smx_file *smx_file_t;
61 typedef struct s_smx_stat *smx_stat_t;
62
63 /********************************** Action *************************************/
64 typedef struct s_smx_action *smx_action_t; /* FIXME: replace by specialized action handlers */
65
66
67
68 /* ****************************** Process *********************************** */
69 /** @brief Process datatype
70     @ingroup m_datatypes_management
71
72     A processt may be defined as a <em>code</em>, with some <em>private
73     data</em>, executing in a <em>location</em>.
74     \see m_process_management
75   @{ */
76 typedef struct s_smx_process *smx_process_t;
77 /** @} */
78
79
80 /*
81  * Type of function that creates a process.
82  * The function must accept the following parameters:
83  * void* process: the process created will be stored there
84  * const char *name: a name for the object. It is for user-level information and can be NULL
85  * xbt_main_func_t code: is a function describing the behavior of the process
86  * void *data: data a pointer to any data one may want to attach to the new object.
87  * smx_host_t host: the location where the new process is executed
88  * int argc, char **argv: parameters passed to code
89  * xbt_dict_t pros: properties
90  */
91 typedef void (*smx_creation_func_t) ( /* process */ smx_process_t*,
92                                       /* name */ const char*,
93                                       /* code */ xbt_main_func_t,
94                                       /* userdata */ void*,
95                                       /* hostname */ const char*,
96                                       /* kill_time */ double,
97                                       /* argc */ int,
98                                       /* argv */ char**,
99                                       /* props */ xbt_dict_t,
100                                       /* auto_restart */ int);
101
102
103 /******************************* Networking ***********************************/
104 typedef struct s_smx_rvpoint *smx_rdv_t;
105
106 /******************************** Context *************************************/
107 typedef struct s_smx_context *smx_context_t;
108 typedef struct s_smx_context_factory *smx_context_factory_t;
109
110 /* Process creation/destruction callbacks */
111 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
112
113 /* for function autorestart */
114 typedef void (*void_pfn_smxhost_t) (smx_host_t);
115
116 /* The following function pointer types describe the interface that any context
117    factory should implement */
118
119
120 typedef smx_context_t(*smx_pfn_context_factory_create_context_t)
121   (xbt_main_func_t, int, char **, void_pfn_smxprocess_t, void* data);
122 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
123 typedef void (*smx_pfn_context_free_t) (smx_context_t);
124 typedef void (*smx_pfn_context_start_t) (smx_context_t);
125 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
126 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
127 typedef void (*smx_pfn_context_runall_t) (void);
128 typedef smx_context_t (*smx_pfn_context_self_t) (void);
129 typedef void* (*smx_pfn_context_get_data_t) (smx_context_t context);
130
131 /* interface of the context factories */
132 typedef struct s_smx_context_factory {
133   const char *name;
134   smx_pfn_context_factory_create_context_t create_context;
135   smx_pfn_context_factory_finalize_t finalize;
136   smx_pfn_context_free_t free;
137   smx_pfn_context_stop_t stop;
138   smx_pfn_context_suspend_t suspend;
139   smx_pfn_context_runall_t runall;
140   smx_pfn_context_self_t self;
141   smx_pfn_context_get_data_t get_data;
142 } s_smx_context_factory_t;
143
144 /* Hack: let msg load directly the right factory */
145 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
146 XBT_PUBLIC(smx_ctx_factory_initializer_t) smx_factory_initializer_to_use;
147 extern char* smx_context_factory_name;
148 extern int smx_context_stack_size;
149
150 #ifdef HAVE_THREAD_LOCAL_STORAGE
151 extern __thread smx_context_t smx_current_context;
152 #else
153 extern smx_context_t smx_current_context;
154 #endif
155
156 /* *********************** */
157 /* Context type definition */
158 /* *********************** */
159 /* the following function pointers types describe the interface that all context
160    concepts must implement */
161 /* each context type derive from this structure, so they must contain this structure
162  * at their beginning -- OOP in C :/ */
163 typedef struct s_smx_context {
164   s_xbt_swag_hookup_t hookup;
165   xbt_main_func_t code;
166   void_pfn_smxprocess_t cleanup_func;
167   void *data;   /* Here SIMIX stores the smx_process_t containing the context */
168   char **argv;
169   int argc;
170   unsigned iwannadie:1;
171 } s_smx_ctx_base_t;
172
173 /* methods of this class */
174 XBT_PUBLIC(void) smx_ctx_base_factory_init(smx_context_factory_t *factory);
175 XBT_PUBLIC(int) smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
176
177 XBT_PUBLIC(smx_context_t)
178 smx_ctx_base_factory_create_context_sized(size_t size,
179                                           xbt_main_func_t code, int argc,
180                                           char **argv,
181                                           void_pfn_smxprocess_t cleanup,
182                                           void* data);
183 XBT_PUBLIC(void) smx_ctx_base_free(smx_context_t context);
184 XBT_PUBLIC(void) smx_ctx_base_stop(smx_context_t context);
185 XBT_PUBLIC(smx_context_t) smx_ctx_base_self(void);
186 XBT_PUBLIC(void) *smx_ctx_base_get_data(smx_context_t context);
187
188 XBT_PUBLIC(xbt_dynar_t) SIMIX_process_get_runnable(void);
189 XBT_PUBLIC(smx_process_t) SIMIX_process_from_PID(int PID);
190 XBT_PUBLIC(xbt_dynar_t) SIMIX_processes_as_dynar(void);
191
192 /* parallelism */
193 XBT_PUBLIC(int) SIMIX_context_is_parallel(void);
194 XBT_PUBLIC(int) SIMIX_context_get_nthreads(void);
195 XBT_PUBLIC(void) SIMIX_context_set_nthreads(int nb_threads);
196 XBT_PUBLIC(int) SIMIX_context_get_parallel_threshold(void);
197 XBT_PUBLIC(void) SIMIX_context_set_parallel_threshold(int threshold);
198 XBT_PUBLIC(e_xbt_parmap_mode_t) SIMIX_context_get_parallel_mode(void);
199 XBT_PUBLIC(void) SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
200
201
202
203 /********************************** Global ************************************/
204 /* Initialization and exit */
205 XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv);
206 XBT_PUBLIC(void) SIMIX_clean(void);
207
208
209 XBT_PUBLIC(void) SIMIX_function_register_process_cleanup(void_pfn_smxprocess_t function);
210 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
211 XBT_PUBLIC(void) SIMIX_function_register_process_kill(void_pfn_smxprocess_t function);
212
213 /* Simulation execution */
214 XBT_PUBLIC(void) SIMIX_run(void);    
215 XBT_PUBLIC(double) SIMIX_get_clock(void);
216
217 /* Timer functions FIXME: should these be public? */
218 XBT_PUBLIC(void) SIMIX_timer_set(double date, void *function, void *arg);
219 XBT_PUBLIC(double) SIMIX_timer_next(void);
220
221 XBT_PUBLIC(void) SIMIX_display_process_status(void);
222
223 /******************************* Environment **********************************/
224 XBT_PUBLIC(void) SIMIX_create_environment(const char *file);
225
226 /******************************** Deployment **********************************/
227
228 XBT_PUBLIC(void) SIMIX_function_register(const char *name, xbt_main_func_t code);
229 XBT_PUBLIC(void) SIMIX_function_register_default(xbt_main_func_t code);
230 XBT_PUBLIC(xbt_main_func_t) SIMIX_get_registered_function(const char *name);
231 XBT_PUBLIC(void) SIMIX_launch_application(const char *file);
232 XBT_PUBLIC(void) SIMIX_process_set_function(const char* process_host,
233                                             const char *process_function,
234                                             xbt_dynar_t arguments,
235                                             double process_start_time,
236                                             double process_kill_time);
237
238 /*********************************** Host *************************************/
239 XBT_PUBLIC(xbt_dict_t) SIMIX_host_get_dict(void);
240 XBT_PUBLIC(smx_host_t) SIMIX_host_get_by_name(const char *name);
241 XBT_PUBLIC(smx_host_t) SIMIX_host_self(void);
242 XBT_PUBLIC(const char*) SIMIX_host_self_get_name(void);
243 XBT_PUBLIC(const char*) SIMIX_host_get_name(smx_host_t host); /* FIXME: make private: only the name of SIMIX_host_self() should be public without request */
244 XBT_PUBLIC(void) SIMIX_host_self_set_data(void *data);
245 XBT_PUBLIC(void*) SIMIX_host_self_get_data(void);
246 XBT_PUBLIC(void*) SIMIX_host_get_data(smx_host_t host);
247 XBT_PUBLIC(void) SIMIX_host_set_data(smx_host_t host, void *data);
248
249 /********************************* Process ************************************/
250 XBT_PUBLIC(int) SIMIX_process_count(void);
251 XBT_PUBLIC(smx_process_t) SIMIX_process_self(void);
252 XBT_PUBLIC(const char*) SIMIX_process_self_get_name(void);
253 XBT_PUBLIC(void) SIMIX_process_self_set_data(smx_process_t self, void *data);
254 XBT_PUBLIC(void*) SIMIX_process_self_get_data(smx_process_t self);
255 XBT_PUBLIC(smx_context_t) SIMIX_process_get_context(smx_process_t);
256 XBT_PUBLIC(void) SIMIX_process_set_context(smx_process_t p,smx_context_t c);
257 XBT_PUBLIC(int) SIMIX_process_has_pending_comms(smx_process_t process);
258 XBT_PUBLIC(void) SIMIX_process_on_exit_runall(smx_process_t process);
259 XBT_PUBLIC(void) SIMIX_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data);
260
261 /****************************** Communication *********************************/
262 XBT_PUBLIC(void) SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, void*, size_t));
263 XBT_PUBLIC(void) SIMIX_comm_copy_pointer_callback(smx_action_t comm, void* buff, size_t buff_size);
264 XBT_PUBLIC(void) SIMIX_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size);
265 XBT_PUBLIC(void) smpi_comm_copy_data_callback(smx_action_t comm, void* buff, size_t buff_size);
266
267 XBT_PUBLIC(smx_action_t) SIMIX_comm_get_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
268 XBT_PUBLIC(int) SIMIX_comm_has_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
269 XBT_PUBLIC(int) SIMIX_comm_has_recv_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
270 XBT_PUBLIC(void) SIMIX_comm_finish(smx_action_t action);
271
272 /******************************************************************************/
273 /*                            SIMIX simcalls                                  */
274 /******************************************************************************/
275 /* These functions are a system call-like interface to the simulation kernel. */
276 /* They can also be called from maestro's context, and they are thread safe.  */
277 /******************************************************************************/
278
279 /******************************* Host simcalls ********************************/
280 /* TODO use handlers and keep smx_host_t hidden from higher levels */
281 XBT_PUBLIC(xbt_dict_t) simcall_host_get_dict(void);
282 XBT_PUBLIC(smx_host_t) simcall_host_get_by_name(const char *name);
283 XBT_PUBLIC(const char *) simcall_host_get_name(smx_host_t host);
284 XBT_PUBLIC(xbt_dict_t) simcall_host_get_properties(smx_host_t host);
285 XBT_PUBLIC(double) simcall_host_get_speed(smx_host_t host);
286 XBT_PUBLIC(double) simcall_host_get_available_speed(smx_host_t host);
287 /* Two possible states, 1 - CPU ON and 0 CPU OFF */
288 XBT_PUBLIC(int) simcall_host_get_state(smx_host_t host);
289 XBT_PUBLIC(void *) simcall_host_get_data(smx_host_t host);
290
291 XBT_PUBLIC(void) simcall_host_set_data(smx_host_t host, void *data);
292
293 XBT_PUBLIC(smx_action_t) simcall_host_execute(const char *name, smx_host_t host,
294                                                 double computation_amount,
295                                                 double priority);
296 XBT_PUBLIC(smx_action_t) simcall_host_parallel_execute(const char *name,
297                                                      int host_nb,
298                                                      smx_host_t *host_list,
299                                                      double *computation_amount,
300                                                      double *communication_amount,
301                                                      double amount,
302                                                      double rate);
303 XBT_PUBLIC(void) simcall_host_execution_destroy(smx_action_t execution);
304 XBT_PUBLIC(void) simcall_host_execution_cancel(smx_action_t execution);
305 XBT_PUBLIC(double) simcall_host_execution_get_remains(smx_action_t execution);
306 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_get_state(smx_action_t execution);
307 XBT_PUBLIC(void) simcall_host_execution_set_priority(smx_action_t execution, double priority);
308 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_wait(smx_action_t execution);
309
310
311 /**************************** Process simcalls ********************************/
312 /* Constructor and Destructor */
313 XBT_PUBLIC(void) simcall_process_create(smx_process_t *process,
314                                           const char *name,
315                                           xbt_main_func_t code,
316                                           void *data,
317                                           const char *hostname,
318                                           double kill_time,
319                                           int argc, char **argv,
320                                           xbt_dict_t properties,
321                                           int auto_restart);
322
323 XBT_PUBLIC(void) simcall_process_kill(smx_process_t process);
324 XBT_PUBLIC(void) simcall_process_killall(void);
325
326 /* Process handling */
327 XBT_PUBLIC(void) simcall_process_cleanup(smx_process_t process);
328 XBT_PUBLIC(void) simcall_process_change_host(smx_process_t process,
329                  smx_host_t dest);
330 XBT_PUBLIC(void) simcall_process_suspend(smx_process_t process);
331 XBT_PUBLIC(void) simcall_process_resume(smx_process_t process);
332
333 /* Getters and Setters */
334 XBT_PUBLIC(int) simcall_process_count(void);
335 XBT_PUBLIC(void *) simcall_process_get_data(smx_process_t process);
336 XBT_PUBLIC(void) simcall_process_set_data(smx_process_t process, void *data);
337 XBT_PUBLIC(smx_host_t) simcall_process_get_host(smx_process_t process);
338 XBT_PUBLIC(const char *) simcall_process_get_name(smx_process_t process);
339 XBT_PUBLIC(int) simcall_process_is_suspended(smx_process_t process);
340 XBT_PUBLIC(xbt_dict_t) simcall_process_get_properties(smx_process_t host);
341 XBT_PUBLIC(void) simcall_process_set_kill_time(smx_process_t process, double kill_time);
342 XBT_PUBLIC(void) simcall_process_on_exit(smx_process_t process, int_f_pvoid_t fun, void *data);
343 XBT_PUBLIC(void) simcall_process_auto_restart_set(smx_process_t process, int auto_restart);
344 /* Sleep control */
345 XBT_PUBLIC(e_smx_state_t) simcall_process_sleep(double duration);
346
347 /************************** Comunication simcalls *****************************/
348 /***** Rendez-vous points *****/
349
350 XBT_PUBLIC(smx_rdv_t) simcall_rdv_create(const char *name);
351 XBT_PUBLIC(void) simcall_rdv_destroy(smx_rdv_t rvp);
352 XBT_PUBLIC(smx_rdv_t) simcall_rdv_get_by_name(const char *name);
353 XBT_PUBLIC(int) simcall_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host);
354 XBT_PUBLIC(smx_action_t) simcall_rdv_get_head(smx_rdv_t rdv);
355
356 XBT_PUBLIC(xbt_dict_t) SIMIX_get_rdv_points(void);
357
358 /***** Communication simcalls *****/
359
360 XBT_PUBLIC(void) simcall_comm_send(smx_rdv_t rdv, double task_size,
361                                      double rate, void *src_buff,
362                                      size_t src_buff_size,
363                                      int (*match_fun)(void *, void *, smx_action_t),
364                                      void *data, double timeout);
365
366 XBT_PUBLIC(smx_action_t) simcall_comm_isend(smx_rdv_t rdv, double task_size,
367                                               double rate, void *src_buff,
368                                               size_t src_buff_size,
369                                               int (*match_fun)(void *, void *, smx_action_t),
370                                               void (*clean_fun)(void *),
371                                               void *data, int detached);
372
373 XBT_PUBLIC(void) simcall_comm_recv(smx_rdv_t rdv, void *dst_buff,
374                                      size_t * dst_buff_size,
375                                      int (*match_fun)(void *, void *, smx_action_t),
376                                      void *data, double timeout);
377
378 XBT_PUBLIC(smx_action_t) simcall_comm_irecv(smx_rdv_t rdv, void *dst_buff,
379                                               size_t * dst_buff_size,
380                                               int (*match_fun)(void *, void *, smx_action_t),
381                                               void *data);
382
383 XBT_PUBLIC(void) simcall_comm_destroy(smx_action_t comm);
384
385 XBT_PUBLIC(void) simcall_comm_cancel(smx_action_t comm);
386
387 /* FIXME: waitany is going to be a vararg function, and should take a timeout */
388 XBT_PUBLIC(unsigned int) simcall_comm_waitany(xbt_dynar_t comms);
389 XBT_PUBLIC(void) simcall_comm_wait(smx_action_t comm, double timeout);
390 XBT_PUBLIC(int) simcall_comm_test(smx_action_t comm);
391 XBT_PUBLIC(int) simcall_comm_testany(xbt_dynar_t comms);
392
393 /* Getters and setters */
394 XBT_PUBLIC(double) simcall_comm_get_remains(smx_action_t comm);
395 XBT_PUBLIC(e_smx_state_t) simcall_comm_get_state(smx_action_t comm);
396 XBT_PUBLIC(void *) simcall_comm_get_src_data(smx_action_t comm);
397 XBT_PUBLIC(void *) simcall_comm_get_dst_data(smx_action_t comm);
398 XBT_PUBLIC(smx_process_t) simcall_comm_get_src_proc(smx_action_t comm);
399 XBT_PUBLIC(smx_process_t) simcall_comm_get_dst_proc(smx_action_t comm);
400
401 #ifdef HAVE_LATENCY_BOUND_TRACKING
402 XBT_PUBLIC(int) simcall_comm_is_latency_bounded(smx_action_t comm);
403 #endif
404
405 #ifdef HAVE_TRACING
406 /************************** Tracing handling **********************************/
407 XBT_PUBLIC(void) simcall_set_category(smx_action_t action, const char *category);
408 #endif
409
410 /************************** Synchro simcalls **********************************/
411
412 XBT_PUBLIC(smx_mutex_t) simcall_mutex_init(void);
413 XBT_PUBLIC(void) simcall_mutex_destroy(smx_mutex_t mutex);
414 XBT_PUBLIC(void) simcall_mutex_lock(smx_mutex_t mutex);
415 XBT_PUBLIC(int) simcall_mutex_trylock(smx_mutex_t mutex);
416 XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex);
417
418 XBT_PUBLIC(smx_cond_t) simcall_cond_init(void);
419 XBT_PUBLIC(void) simcall_cond_destroy(smx_cond_t cond);
420 XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond);
421 XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex);
422 XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond,
423                                          smx_mutex_t mutex,
424                                          double max_duration);
425 XBT_PUBLIC(void) simcall_cond_broadcast(smx_cond_t cond);
426
427 XBT_PUBLIC(smx_sem_t) simcall_sem_init(int capacity);
428 XBT_PUBLIC(void) simcall_sem_destroy(smx_sem_t sem);
429 XBT_PUBLIC(void) simcall_sem_release(smx_sem_t sem);
430 XBT_PUBLIC(void) simcall_sem_release_forever(smx_sem_t sem);
431 XBT_PUBLIC(int) simcall_sem_would_block(smx_sem_t sem);
432 XBT_PUBLIC(void) simcall_sem_block_onto(smx_sem_t sem);
433 XBT_PUBLIC(void) simcall_sem_acquire(smx_sem_t sem);
434 XBT_PUBLIC(void) simcall_sem_acquire_timeout(smx_sem_t sem,
435                                            double max_duration);
436 XBT_PUBLIC(unsigned int) simcall_sem_acquire_any(xbt_dynar_t sems);
437 XBT_PUBLIC(int) simcall_sem_get_capacity(smx_sem_t sem);
438
439 XBT_PUBLIC(double) simcall_file_read(void* ptr, size_t size, size_t nmemb, smx_file_t stream);
440 XBT_PUBLIC(size_t) simcall_file_write(const void* ptr, size_t size, size_t nmemb, smx_file_t stream);
441 XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode);
442 XBT_PUBLIC(int) simcall_file_close(smx_file_t fp);
443 XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf);
444
445 SG_END_DECL()
446 #endif                          /* _SIMIX_SIMIX_H */