Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot//simgrid/simgrid
[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
101
102 /******************************* Networking ***********************************/
103 typedef struct s_smx_rvpoint *smx_rdv_t;
104
105 /******************************** Context *************************************/
106 typedef struct s_smx_context *smx_context_t;
107 typedef struct s_smx_context_factory *smx_context_factory_t;
108
109 /* Process creation/destruction callbacks */
110 typedef void (*void_pfn_smxprocess_t) (smx_process_t);
111
112
113 /* The following function pointer types describe the interface that any context
114    factory should implement */
115
116
117 typedef smx_context_t(*smx_pfn_context_factory_create_context_t)
118   (xbt_main_func_t, int, char **, void_pfn_smxprocess_t, void* data);
119 typedef int (*smx_pfn_context_factory_finalize_t) (smx_context_factory_t*);
120 typedef void (*smx_pfn_context_free_t) (smx_context_t);
121 typedef void (*smx_pfn_context_start_t) (smx_context_t);
122 typedef void (*smx_pfn_context_stop_t) (smx_context_t);
123 typedef void (*smx_pfn_context_suspend_t) (smx_context_t context);
124 typedef void (*smx_pfn_context_runall_t) (void);
125 typedef smx_context_t (*smx_pfn_context_self_t) (void);
126 typedef void* (*smx_pfn_context_get_data_t) (smx_context_t context);
127
128 /* interface of the context factories */
129 typedef struct s_smx_context_factory {
130   const char *name;
131   smx_pfn_context_factory_create_context_t create_context;
132   smx_pfn_context_factory_finalize_t finalize;
133   smx_pfn_context_free_t free;
134   smx_pfn_context_stop_t stop;
135   smx_pfn_context_suspend_t suspend;
136   smx_pfn_context_runall_t runall;
137   smx_pfn_context_self_t self;
138   smx_pfn_context_get_data_t get_data;
139 } s_smx_context_factory_t;
140
141 /* Hack: let msg load directly the right factory */
142 typedef void (*smx_ctx_factory_initializer_t)(smx_context_factory_t*);
143 XBT_PUBLIC(smx_ctx_factory_initializer_t) smx_factory_initializer_to_use;
144 extern char* smx_context_factory_name;
145 extern int smx_context_stack_size;
146
147 #ifdef HAVE_THREAD_LOCAL_STORAGE
148 extern __thread smx_context_t smx_current_context;
149 #else
150 extern smx_context_t smx_current_context;
151 #endif
152
153 /* *********************** */
154 /* Context type definition */
155 /* *********************** */
156 /* the following function pointers types describe the interface that all context
157    concepts must implement */
158 /* each context type derive from this structure, so they must contain this structure
159  * at their beginning -- OOP in C :/ */
160 typedef struct s_smx_context {
161   s_xbt_swag_hookup_t hookup;
162   xbt_main_func_t code;
163   void_pfn_smxprocess_t cleanup_func;
164   void *data;   /* Here SIMIX stores the smx_process_t containing the context */
165   char **argv;
166   int argc;
167   unsigned iwannadie:1;
168 } s_smx_ctx_base_t;
169
170 /* methods of this class */
171 XBT_PUBLIC(void) smx_ctx_base_factory_init(smx_context_factory_t *factory);
172 XBT_PUBLIC(int) smx_ctx_base_factory_finalize(smx_context_factory_t *factory);
173
174 XBT_PUBLIC(smx_context_t)
175 smx_ctx_base_factory_create_context_sized(size_t size,
176                                           xbt_main_func_t code, int argc,
177                                           char **argv,
178                                           void_pfn_smxprocess_t cleanup,
179                                           void* data);
180 XBT_PUBLIC(void) smx_ctx_base_free(smx_context_t context);
181 XBT_PUBLIC(void) smx_ctx_base_stop(smx_context_t context);
182 XBT_PUBLIC(smx_context_t) smx_ctx_base_self(void);
183 XBT_PUBLIC(void) *smx_ctx_base_get_data(smx_context_t context);
184
185 XBT_PUBLIC(xbt_dynar_t) SIMIX_process_get_runnable(void);
186 XBT_PUBLIC(smx_process_t) SIMIX_process_from_PID(int PID);
187 XBT_PUBLIC(xbt_dynar_t) SIMIX_processes_as_dynar(void);
188
189 /* parallelism */
190 XBT_PUBLIC(int) SIMIX_context_is_parallel(void);
191 XBT_PUBLIC(int) SIMIX_context_get_nthreads(void);
192 XBT_PUBLIC(void) SIMIX_context_set_nthreads(int nb_threads);
193 XBT_PUBLIC(int) SIMIX_context_get_parallel_threshold(void);
194 XBT_PUBLIC(void) SIMIX_context_set_parallel_threshold(int threshold);
195 XBT_PUBLIC(e_xbt_parmap_mode_t) SIMIX_context_get_parallel_mode(void);
196 XBT_PUBLIC(void) SIMIX_context_set_parallel_mode(e_xbt_parmap_mode_t mode);
197
198
199
200 /********************************** Global ************************************/
201 /* Initialization and exit */
202 XBT_PUBLIC(void) SIMIX_global_init(int *argc, char **argv);
203 XBT_PUBLIC(void) SIMIX_clean(void);
204
205
206 XBT_PUBLIC(void) SIMIX_function_register_process_cleanup(void_pfn_smxprocess_t function);
207 XBT_PUBLIC(void) SIMIX_function_register_process_create(smx_creation_func_t function);
208 XBT_PUBLIC(void) SIMIX_function_register_process_kill(void_pfn_smxprocess_t function);
209
210 /* Simulation execution */
211 XBT_PUBLIC(void) SIMIX_run(void);    
212 XBT_PUBLIC(double) SIMIX_get_clock(void);
213
214 /* Timer functions FIXME: should these be public? */
215 XBT_PUBLIC(void) SIMIX_timer_set(double date, void *function, void *arg);
216 XBT_PUBLIC(double) SIMIX_timer_next(void);
217
218 XBT_PUBLIC(void) SIMIX_display_process_status(void);
219
220 /******************************* Environment **********************************/
221 XBT_PUBLIC(void) SIMIX_create_environment(const char *file);
222
223 /******************************** Deployment **********************************/
224
225 XBT_PUBLIC(void) SIMIX_function_register(const char *name, xbt_main_func_t code);
226 XBT_PUBLIC(void) SIMIX_function_register_default(xbt_main_func_t code);
227 XBT_PUBLIC(xbt_main_func_t) SIMIX_get_registered_function(const char *name);
228 XBT_PUBLIC(void) SIMIX_launch_application(const char *file);
229 XBT_PUBLIC(void) SIMIX_process_set_function(const char* process_host,
230                                             const char *process_function,
231                                             xbt_dynar_t arguments,
232                                             double process_start_time,
233                                             double process_kill_time);
234
235 /*********************************** Host *************************************/
236 XBT_PUBLIC(xbt_dict_t) SIMIX_host_get_dict(void);
237 XBT_PUBLIC(smx_host_t) SIMIX_host_get_by_name(const char *name);
238 XBT_PUBLIC(smx_host_t) SIMIX_host_self(void);
239 XBT_PUBLIC(const char*) SIMIX_host_self_get_name(void);
240 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 */
241 XBT_PUBLIC(void) SIMIX_host_self_set_data(void *data);
242 XBT_PUBLIC(void*) SIMIX_host_self_get_data(void);
243 XBT_PUBLIC(void*) SIMIX_host_get_data(smx_host_t host);
244 XBT_PUBLIC(void) SIMIX_host_set_data(smx_host_t host, void *data);
245
246 /********************************* Process ************************************/
247 XBT_PUBLIC(int) SIMIX_process_count(void);
248 XBT_PUBLIC(smx_process_t) SIMIX_process_self(void);
249 XBT_PUBLIC(const char*) SIMIX_process_self_get_name(void);
250 XBT_PUBLIC(void) SIMIX_process_self_set_data(smx_process_t self, void *data);
251 XBT_PUBLIC(void*) SIMIX_process_self_get_data(smx_process_t self);
252 XBT_PUBLIC(smx_context_t) SIMIX_process_get_context(smx_process_t);
253 XBT_PUBLIC(void) SIMIX_process_set_context(smx_process_t p,smx_context_t c);
254 XBT_PUBLIC(int) SIMIX_process_has_pending_comms(smx_process_t process);
255
256 /****************************** Communication *********************************/
257 XBT_PUBLIC(void) SIMIX_comm_set_copy_data_callback(void (*callback) (smx_action_t, void*, size_t));
258 XBT_PUBLIC(void) SIMIX_comm_copy_pointer_callback(smx_action_t comm, void* buff, size_t buff_size);
259 XBT_PUBLIC(void) SIMIX_comm_copy_buffer_callback(smx_action_t comm, void* buff, size_t buff_size);
260 XBT_PUBLIC(void) smpi_comm_copy_data_callback(smx_action_t comm, void* buff, size_t buff_size);
261
262 XBT_PUBLIC(smx_action_t) SIMIX_comm_get_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
263 XBT_PUBLIC(int) SIMIX_comm_has_send_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
264 XBT_PUBLIC(int) SIMIX_comm_has_recv_match(smx_rdv_t rdv, int (*match_fun)(void*, void*), void* data);
265 XBT_PUBLIC(void) SIMIX_comm_finish(smx_action_t action);
266
267 /******************************************************************************/
268 /*                            SIMIX simcalls                                  */
269 /******************************************************************************/
270 /* These functions are a system call-like interface to the simulation kernel. */
271 /* They can also be called from maestro's context, and they are thread safe.  */
272 /******************************************************************************/
273
274 /******************************* Host simcalls ********************************/
275 /* TODO use handlers and keep smx_host_t hidden from higher levels */
276 XBT_PUBLIC(xbt_dict_t) simcall_host_get_dict(void);
277 XBT_PUBLIC(smx_host_t) simcall_host_get_by_name(const char *name);
278 XBT_PUBLIC(const char *) simcall_host_get_name(smx_host_t host);
279 XBT_PUBLIC(xbt_dict_t) simcall_host_get_properties(smx_host_t host);
280 XBT_PUBLIC(double) simcall_host_get_speed(smx_host_t host);
281 XBT_PUBLIC(double) simcall_host_get_available_speed(smx_host_t host);
282 /* Two possible states, 1 - CPU ON and 0 CPU OFF */
283 XBT_PUBLIC(int) simcall_host_get_state(smx_host_t host);
284 XBT_PUBLIC(void *) simcall_host_get_data(smx_host_t host);
285
286 XBT_PUBLIC(void) simcall_host_set_data(smx_host_t host, void *data);
287
288 XBT_PUBLIC(smx_action_t) simcall_host_execute(const char *name, smx_host_t host,
289                                                 double computation_amount,
290                                                 double priority);
291 XBT_PUBLIC(smx_action_t) simcall_host_parallel_execute(const char *name,
292                                                      int host_nb,
293                                                      smx_host_t *host_list,
294                                                      double *computation_amount,
295                                                      double *communication_amount,
296                                                      double amount,
297                                                      double rate);
298 XBT_PUBLIC(void) simcall_host_execution_destroy(smx_action_t execution);
299 XBT_PUBLIC(void) simcall_host_execution_cancel(smx_action_t execution);
300 XBT_PUBLIC(double) simcall_host_execution_get_remains(smx_action_t execution);
301 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_get_state(smx_action_t execution);
302 XBT_PUBLIC(void) simcall_host_execution_set_priority(smx_action_t execution, double priority);
303 XBT_PUBLIC(e_smx_state_t) simcall_host_execution_wait(smx_action_t execution);
304
305
306 /**************************** Process simcalls ********************************/
307 /* Constructor and Destructor */
308 XBT_PUBLIC(void) simcall_process_create(smx_process_t *process,
309                                           const char *name,
310                                           xbt_main_func_t code,
311                                           void *data,
312                                           const char *hostname,
313                                           double kill_time,
314                                           int argc, char **argv,
315                                           xbt_dict_t properties);
316
317 XBT_PUBLIC(void) simcall_process_kill(smx_process_t process);
318 XBT_PUBLIC(void) simcall_process_killall(void);
319
320 /* Process handling */
321 XBT_PUBLIC(void) simcall_process_cleanup(smx_process_t process);
322 XBT_PUBLIC(void) simcall_process_change_host(smx_process_t process,
323                                                smx_host_t dest);
324 XBT_PUBLIC(void) simcall_process_suspend(smx_process_t process);
325 XBT_PUBLIC(void) simcall_process_resume(smx_process_t process);
326
327 /* Getters and Setters */
328 XBT_PUBLIC(int) simcall_process_count(void);
329 XBT_PUBLIC(void *) simcall_process_get_data(smx_process_t process);
330 XBT_PUBLIC(void) simcall_process_set_data(smx_process_t process, void *data);
331 XBT_PUBLIC(smx_host_t) simcall_process_get_host(smx_process_t process);
332 XBT_PUBLIC(const char *) simcall_process_get_name(smx_process_t process);
333 XBT_PUBLIC(int) simcall_process_is_suspended(smx_process_t process);
334 XBT_PUBLIC(xbt_dict_t) simcall_process_get_properties(smx_process_t host);
335 XBT_PUBLIC(void) simcall_process_set_kill_time(smx_process_t process, double kill_time);
336
337 /* Sleep control */
338 XBT_PUBLIC(e_smx_state_t) simcall_process_sleep(double duration);
339
340 /************************** Comunication simcalls *****************************/
341 /***** Rendez-vous points *****/
342
343 XBT_PUBLIC(smx_rdv_t) simcall_rdv_create(const char *name);
344 XBT_PUBLIC(void) simcall_rdv_destroy(smx_rdv_t rvp);
345 XBT_PUBLIC(smx_rdv_t) simcall_rdv_get_by_name(const char *name);
346 XBT_PUBLIC(int) simcall_rdv_comm_count_by_host(smx_rdv_t rdv, smx_host_t host);
347 XBT_PUBLIC(smx_action_t) simcall_rdv_get_head(smx_rdv_t rdv);
348
349 XBT_PUBLIC(xbt_dict_t) SIMIX_get_rdv_points(void);
350
351 /***** Communication simcalls *****/
352
353 XBT_PUBLIC(void) simcall_comm_send(smx_rdv_t rdv, double task_size,
354                                      double rate, void *src_buff,
355                                      size_t src_buff_size,
356                                      int (*match_fun)(void *, void *, smx_action_t),
357                                      void *data, double timeout);
358
359 XBT_PUBLIC(smx_action_t) simcall_comm_isend(smx_rdv_t rdv, double task_size,
360                                               double rate, void *src_buff,
361                                               size_t src_buff_size,
362                                               int (*match_fun)(void *, void *, smx_action_t),
363                                               void (*clean_fun)(void *),
364                                               void *data, int detached);
365
366 XBT_PUBLIC(void) simcall_comm_recv(smx_rdv_t rdv, void *dst_buff,
367                                      size_t * dst_buff_size,
368                                      int (*match_fun)(void *, void *, smx_action_t),
369                                      void *data, double timeout);
370
371 XBT_PUBLIC(smx_action_t) simcall_comm_irecv(smx_rdv_t rdv, void *dst_buff,
372                                               size_t * dst_buff_size,
373                                               int (*match_fun)(void *, void *, smx_action_t),
374                                               void *data);
375
376 XBT_PUBLIC(void) simcall_comm_destroy(smx_action_t comm);
377
378 XBT_PUBLIC(void) simcall_comm_cancel(smx_action_t comm);
379
380 /* FIXME: waitany is going to be a vararg function, and should take a timeout */
381 XBT_PUBLIC(unsigned int) simcall_comm_waitany(xbt_dynar_t comms);
382 XBT_PUBLIC(void) simcall_comm_wait(smx_action_t comm, double timeout);
383 XBT_PUBLIC(int) simcall_comm_test(smx_action_t comm);
384 XBT_PUBLIC(int) simcall_comm_testany(xbt_dynar_t comms);
385
386 /* Getters and setters */
387 XBT_PUBLIC(double) simcall_comm_get_remains(smx_action_t comm);
388 XBT_PUBLIC(e_smx_state_t) simcall_comm_get_state(smx_action_t comm);
389 XBT_PUBLIC(void *) simcall_comm_get_src_data(smx_action_t comm);
390 XBT_PUBLIC(void *) simcall_comm_get_dst_data(smx_action_t comm);
391 XBT_PUBLIC(smx_process_t) simcall_comm_get_src_proc(smx_action_t comm);
392 XBT_PUBLIC(smx_process_t) simcall_comm_get_dst_proc(smx_action_t comm);
393
394 #ifdef HAVE_LATENCY_BOUND_TRACKING
395 XBT_PUBLIC(int) simcall_comm_is_latency_bounded(smx_action_t comm);
396 #endif
397
398 #ifdef HAVE_TRACING
399 /************************** Tracing handling **********************************/
400 XBT_PUBLIC(void) simcall_set_category(smx_action_t action, const char *category);
401 #endif
402
403 /************************** Synchro simcalls **********************************/
404
405 XBT_PUBLIC(smx_mutex_t) simcall_mutex_init(void);
406 XBT_PUBLIC(void) simcall_mutex_destroy(smx_mutex_t mutex);
407 XBT_PUBLIC(void) simcall_mutex_lock(smx_mutex_t mutex);
408 XBT_PUBLIC(int) simcall_mutex_trylock(smx_mutex_t mutex);
409 XBT_PUBLIC(void) simcall_mutex_unlock(smx_mutex_t mutex);
410
411 XBT_PUBLIC(smx_cond_t) simcall_cond_init(void);
412 XBT_PUBLIC(void) simcall_cond_destroy(smx_cond_t cond);
413 XBT_PUBLIC(void) simcall_cond_signal(smx_cond_t cond);
414 XBT_PUBLIC(void) simcall_cond_wait(smx_cond_t cond, smx_mutex_t mutex);
415 XBT_PUBLIC(void) simcall_cond_wait_timeout(smx_cond_t cond,
416                                          smx_mutex_t mutex,
417                                          double max_duration);
418 XBT_PUBLIC(void) simcall_cond_broadcast(smx_cond_t cond);
419
420 XBT_PUBLIC(smx_sem_t) simcall_sem_init(int capacity);
421 XBT_PUBLIC(void) simcall_sem_destroy(smx_sem_t sem);
422 XBT_PUBLIC(void) simcall_sem_release(smx_sem_t sem);
423 XBT_PUBLIC(void) simcall_sem_release_forever(smx_sem_t sem);
424 XBT_PUBLIC(int) simcall_sem_would_block(smx_sem_t sem);
425 XBT_PUBLIC(void) simcall_sem_block_onto(smx_sem_t sem);
426 XBT_PUBLIC(void) simcall_sem_acquire(smx_sem_t sem);
427 XBT_PUBLIC(void) simcall_sem_acquire_timeout(smx_sem_t sem,
428                                            double max_duration);
429 XBT_PUBLIC(unsigned int) simcall_sem_acquire_any(xbt_dynar_t sems);
430 XBT_PUBLIC(int) simcall_sem_get_capacity(smx_sem_t sem);
431
432 XBT_PUBLIC(size_t) simcall_file_read(void* ptr, size_t size, size_t nmemb, smx_file_t stream);
433 XBT_PUBLIC(size_t) simcall_file_write(const void* ptr, size_t size, size_t nmemb, smx_file_t stream);
434 XBT_PUBLIC(smx_file_t) simcall_file_open(const char* storage, const char* path, const char* mode);
435 XBT_PUBLIC(int) simcall_file_close(smx_file_t fp);
436 XBT_PUBLIC(int) simcall_file_stat(smx_file_t fd, s_file_stat_t *buf);
437
438 SG_END_DECL()
439 #endif                          /* _SIMIX_SIMIX_H */