Logo AND Algorithmique Numérique Distribuée

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