The ROme OpTimistic Simulator  2.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
core.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ROOT-Sim.h>
36 #include <stdio.h>
37 #include <limits.h>
38 #include <float.h>
39 #include <math.h>
40 #include <stdint.h>
41 #include <setjmp.h>
42 
43 #include <arch/thread.h>
44 
45 
47 #define master_kernel() (kid == 0)
48 
49 // XXX: This should be moved to state or queues
50 enum {
53 };
54 
56 #define N_KER_MAX 128
57 
59 #define MAX_LPs 250000
60 
61 // XXX: this should be moved somewhere else...
62 enum {
67 };
68 
69 extern jmp_buf exit_jmp;
70 
72 #define likely(exp) __builtin_expect(exp, 1)
73 #define unlikely(exp) __builtin_expect(exp, 0)
75 
76 
77 enum {
81 };
82 
83 // XXX should be moved to a more librarish header
85 #define F_EQUAL(a,b) (fabsf((a) - (b)) < FLT_EPSILON)
86 #define F_EQUAL_ZERO(a) (fabsf(a) < FLT_EPSILON)
88 #define F_DIFFER(a,b) (fabsf((a) - (b)) >= FLT_EPSILON)
90 #define F_DIFFER_ZERO(a) (fabsf(a) >= FLT_EPSILON)
92 
94 #define D_EQUAL(a,b) (fabs((a) - (b)) < DBL_EPSILON)
95 #define D_EQUAL_ZERO(a) (fabs(a) < DBL_EPSILON)
97 #define D_DIFFER(a,b) (fabs((a) - (b)) >= DBL_EPSILON)
99 #define D_DIFFER_ZERO(a) (fabs(a) >= DBL_EPSILON)
101 
103 #ifdef max
104 #undef max
105 #endif
106 #define max(a, b) \
107  ({ __typeof__ (a) _a = (a); \
108  __typeof__ (b) _b = (b); \
109  _a > _b ? _a : _b; })
110 
112 #ifdef min
113 #undef min
114 #endif
115 #define min(a, b) \
116  ({ __typeof__ (a) _a = (a); \
117  __typeof__ (b) _b = (b); \
118  _a < _b ? _a : _b; })
119 
121 #define UNION_CAST(x, destType) (((union {__typeof__(x) a; destType b;})x).b)
122 
123 // GID and LID types
124 
132 typedef struct _gid_t {
133  unsigned int to_int;
134 } GID_t;
135 
136 
144 typedef struct _lid_t {
145  unsigned int to_int;
146 } LID_t;
147 
148 #define is_lid(val) __builtin_types_compatible_p(__typeof__ (val), LID_t)
149 #define is_gid(val) __builtin_types_compatible_p(__typeof__ (val), GID_t)
150 
151 #define set_lid(lid, value) (__builtin_choose_expr(is_lid(lid), lid.to_int, (void)0) = (value))
152 #define set_gid(gid, value) (__builtin_choose_expr(is_gid(gid), gid.to_int, (void)0) = (value))
153 
154 typedef enum { positive, negative, control } message_kind_t;
155 
156 #ifdef HAVE_MPI
157 typedef unsigned char phase_colour;
158 #endif
159 
160 #define MSG_PADDING offsetof(msg_t, sender)
161 #define MSG_META_SIZE (offsetof(msg_t, event_content) - MSG_PADDING)
162 
164 typedef struct _msg_t {
165 
166  /* Place here all memebers of the struct which should not be transmitted over the network */
167 
168  // Pointers to attach messages to chains
169  struct _msg_t *next;
170  struct _msg_t *prev;
171 
172  /* Place here all members which must be transmitted over the network. It is convenient not to reorder the members
173  * of the structure. If new members have to be addedd, place them right before the "Model data" part.*/
174 
175  // Kernel's information
176  GID_t sender;
177  GID_t receiver;
178 #ifdef HAVE_MPI
179  phase_colour colour;
180 #endif
181  int type;
182  message_kind_t message_kind;
183  simtime_t timestamp;
184  simtime_t send_time;
185  unsigned long long mark;
186  unsigned long long rendezvous_mark;
187 
188  // Model data
189  unsigned int size;
190  unsigned char event_content[];
191 } msg_t;
192 
194 typedef struct _msg_hdr_t {
195  // Pointers to attach messages to chains
196  struct _msg_hdr_t *next;
197  struct _msg_hdr_t *prev;
198  // Kernel's information
199  GID_t sender;
200  GID_t receiver;
201  // TODO: non serve davvero, togliere
202  int type;
203  unsigned long long rendezvous_mark;
204  // TODO: fine togliere
206  simtime_t send_time;
207  unsigned long long mark;
208 } msg_hdr_t;
209 
210 
213 
214 // XXX: this should be refactored someway
215 extern unsigned int kid, /* Kernel ID for the local kernel */
216  n_ker, /* Total number of kernel instances */
217  n_cores, /* Total number of cores required for simulation */
218  n_prc, /* Number of LPs hosted by the current kernel instance */
219 *kernel;
220 
221 extern void ProcessEvent_light(unsigned int me, simtime_t now, int event_type, void *event_content, unsigned int size, void *state);
222 bool OnGVT_light(unsigned int me, void *snapshot);
223 extern void ProcessEvent_inc(unsigned int me, simtime_t now, int event_type, void *event_content, unsigned int size, void *state);
224 bool OnGVT_inc(unsigned int me, void *snapshot);
225 
226 extern void base_init(void);
227 extern void base_fini(void);
228 extern unsigned int find_kernel_by_gid(GID_t gid) __attribute__((pure));
229 extern void _rootsim_error(bool fatal, const char *msg, ...);
230 extern void distribute_lps_on_kernels(void);
231 extern void simulation_shutdown(int code) __attribute__((noreturn));
232 extern inline bool user_requested_exit(void);
233 extern inline bool simulation_error(void);
234 extern void initialization_complete(void);
235 
236 #define rootsim_error(fatal, msg, ...) _rootsim_error(fatal, "%s:%d: %s(): " msg, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
Definition of a LID.
Definition: core.h:144
unsigned int n_prc
Number of logical processes hosted by the current kernel instance.
Definition: core.c:67
ROOT-Sim header for model development.
struct _msg_hdr_t msg_hdr_t
Message envelope definition. This is used to handle the output queue and stores information needed to...
simtime_t timestamp
Unique identifier of the message, used for rendez-vous event.
Definition: core.h:205
barrier_t all_thread_barrier
Barrier for all worker threads.
Definition: core.c:49
unsigned int to_int
The LID numerical value.
Definition: core.h:145
void initialization_complete(void)
Definition: core.c:316
unsigned int to_int
The GID numerical value.
Definition: core.h:133
Generic thread management facilities.
struct _lid_t LID_t
Definition of a LID.
jmp_buf exit_jmp
Definition: main.c:62
struct _msg_t msg_t
Message Type definition.
double simtime_t
This defines the type with whom timestamps are represented.
Definition: ROOT-Sim.h:55
void distribute_lps_on_kernels(void)
Definition: core.c:258
Message Type definition.
Definition: core.h:164
unsigned int size
Unique identifier of the message, used for rendez-vous events.
Definition: core.h:189
struct _gid_t GID_t
Definition of a GID.
unsigned long long rendezvous_mark
Unique identifier of the message, used for antimessages.
Definition: core.h:186
void simulation_shutdown(int code)
Definition: core.c:178
Definition of a GID.
Definition: core.h:132
void _rootsim_error(bool fatal, const char *msg,...)
Definition: core.c:220
unsigned int find_kernel_by_gid(GID_t gid)
Definition: core.c:164
unsigned int n_cores
Total number of cores required for simulation.
Definition: core.c:61
void base_fini(void)
Definition: core.c:151
unsigned int n_ker
Total number of simulation kernel instances running.
Definition: core.c:58
void base_init(void)
Definition: core.c:129
Message envelope definition. This is used to handle the output queue and stores information needed to...
Definition: core.h:194
unsigned int kid
Identifier of the local kernel.
Definition: core.c:55
unsigned int * kernel
Mapping between kernel instances and logical processes.
Definition: core.c:52
Thread barrier definition.
Definition: thread.h:102