ROOT-Sim core  3.0.0-rc.2
A General-Purpose Multi-threaded Parallel/Distributed Simulation Library
ROOT-Sim.h
Go to the documentation of this file.
1 
16 #pragma once
17 
18 #include <limits.h>
19 #include <float.h>
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 
27 typedef double simtime_t;
28 
30 #define SIMTIME_MAX DBL_MAX
31 
33 typedef uint64_t lp_id_t;
34 
52 typedef void (*ProcessEvent_t)(lp_id_t me, simtime_t now, unsigned event_type, const void *event_content,
53  unsigned event_size, void *st);
54 
69 typedef bool (*CanEnd_t)(lp_id_t me, const void *snapshot);
70 
71 enum rootsim_event {LP_INIT = 65534, LP_FINI};
72 
86 extern void ScheduleNewEvent(lp_id_t receiver, simtime_t timestamp, unsigned event_type, const void *event_content,
87  unsigned event_size);
88 
89 extern void SetState(void *new_state);
90 
91 extern void *rs_malloc(size_t req_size);
92 extern void *rs_calloc(size_t nmemb, size_t size);
93 extern void rs_free(void *ptr);
94 extern void *rs_realloc(void *ptr, size_t req_size);
95 
96 extern double Random(void);
97 extern uint64_t RandomU64(void);
98 extern double Poisson(void);
99 #define Expent(mean) ((mean) * Poisson())
100 extern double Normal(void);
101 extern int RandomRange(int min, int max);
102 extern int RandomRangeNonUniform(int x, int min, int max);
103 extern double Gamma(unsigned ia);
104 extern unsigned Zipf(double skew, unsigned limit);
105 
106 
107 enum log_level {
114  LOG_SILENT
115 };
116 
117 /********* TOPOLOGY LIBRARY ************/
118 struct topology;
119 
129 };
130 
140 
142 };
143 
145 #define INVALID_DIRECTION UINT64_MAX
146 
147 extern lp_id_t CountRegions(struct topology *topology);
148 extern lp_id_t CountDirections(lp_id_t from, struct topology *topology);
149 extern lp_id_t GetReceiver(lp_id_t from, struct topology *topology, enum topology_direction direction);
150 
151 extern void ReleaseTopology(struct topology *topology);
152 extern bool AddTopologyLink(struct topology *topology, lp_id_t from, lp_id_t to, double probability);
153 extern bool IsNeighbor(lp_id_t from, lp_id_t to, struct topology *topology);
154 
155 
156 // The following trick belongs to Laurent Deniau at CERN.
157 // https://groups.google.com/g/comp.std.c/c/d-6Mj5Lko_s?pli=1
158 
160 #define PP_NARG(...) \
161  PP_NARG_(__VA_ARGS__,PP_RSEQ_N())
163 #define PP_NARG_(...) \
164  PP_128TH_ARG(__VA_ARGS__)
166 #define PP_128TH_ARG( \
167  _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
168  _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
169  _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
170  _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
171  _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
172  _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
173  _61,_62,_63,_64,_65,_66,_67,_68,_69,_70, \
174  _71,_72,_73,_74,_75,_76,_77,_78,_79,_80, \
175  _81,_82,_83,_84,_85,_86,_87,_88,_89,_90, \
176  _91,_92,_93,_94,_95,_96,_97,_98,_99,_100, \
177  _101,_102,_103,_104,_105,_106,_107,_108,_109,_110, \
178  _111,_112,_113,_114,_115,_116,_117,_118,_119,_120, \
179  _121,_122,_123,_124,_125,_126,_127,N,...) N
181 #define PP_RSEQ_N() \
182  127,126,125,124,123,122,121,120, \
183  119,118,117,116,115,114,113,112,111,110, \
184  109,108,107,106,105,104,103,102,101,100, \
185  99,98,97,96,95,94,93,92,91,90, \
186  89,88,87,86,85,84,83,82,81,80, \
187  79,78,77,76,75,74,73,72,71,70, \
188  69,68,67,66,65,64,63,62,61,60, \
189  59,58,57,56,55,54,53,52,51,50, \
190  49,48,47,46,45,44,43,42,41,40, \
191  39,38,37,36,35,34,33,32,31,30, \
192  29,28,27,26,25,24,23,22,21,20, \
193  19,18,17,16,15,14,13,12,11,10, \
194  9,8,7,6,5,4,3,2,1,0
195 
196 extern struct topology *vInitializeTopology(enum topology_geometry geometry, int argc, ...);
197 
198 
206 #define InitializeTopology(geometry, ...) vInitializeTopology(geometry, PP_NARG(__VA_ARGS__), __VA_ARGS__)
207 /********* TOPOLOGY LIBRARY ************/
208 
214  unsigned n_threads;
218  unsigned gvt_period;
220  enum log_level log_level;
222  FILE *logfile;
224  const char *stats_file;
226  unsigned ckpt_interval;
228  uint64_t prng_seed;
232  bool serial;
237 };
238 
239 extern int RootsimInit(const struct simulation_configuration *conf);
240 extern int RootsimRun(void);
241 extern void RootsimStop(void);
void ScheduleNewEvent(lp_id_t receiver, simtime_t timestamp, unsigned event_type, const void *event_content, unsigned event_size)
API to inject a new event in the simulation.
Definition: process.c:38
int RootsimRun(void)
Start the simulation.
Definition: init.c:134
void(* ProcessEvent_t)(lp_id_t me, simtime_t now, unsigned event_type, const void *event_content, unsigned event_size, void *st)
ProcessEvent callback function.
Definition: ROOT-Sim.h:52
bool(* CanEnd_t)(lp_id_t me, const void *snapshot)
Determine if simulation can be halted.
Definition: ROOT-Sim.h:69
topology_geometry
Definition: ROOT-Sim.h:120
@ TOPOLOGY_SQUARE
square grid topology
Definition: ROOT-Sim.h:122
@ TOPOLOGY_STAR
a star shaped topology
Definition: ROOT-Sim.h:126
@ TOPOLOGY_GRAPH
a (weighted) graph topology
Definition: ROOT-Sim.h:128
@ TOPOLOGY_TORUS
a torus shaped grid topology (a wrapping around square topology)
Definition: ROOT-Sim.h:123
@ TOPOLOGY_FCMESH
a fully-connected mesh
Definition: ROOT-Sim.h:127
@ TOPOLOGY_HEXAGON
hexagonal grid topology
Definition: ROOT-Sim.h:121
@ TOPOLOGY_BIDRING
a ring shaped topology
Definition: ROOT-Sim.h:125
@ TOPOLOGY_RING
a ring shaped topology walkable in a single direction
Definition: ROOT-Sim.h:124
double simtime_t
Simulation time data type.
Definition: ROOT-Sim.h:27
uint64_t RandomU64(void)
Return a random 64-bit value.
Definition: random.c:41
unsigned Zipf(double skew, unsigned limit)
Return a random sample from a Zipf distribution Based on the rejection method by Luc Devroye for samp...
Definition: random.c:152
double Poisson(void)
Return a random number according to an Exponential distribution with unit mean Corresponds to the wai...
Definition: random.c:138
void SetState(void *new_state)
Set the LP simulation state main pointer.
Definition: lp.c:128
topology_direction
Definition: ROOT-Sim.h:131
@ DIRECTION_N
North direction.
Definition: ROOT-Sim.h:134
@ DIRECTION_NE
North-east direction.
Definition: ROOT-Sim.h:136
@ DIRECTION_SW
South-west direction.
Definition: ROOT-Sim.h:137
@ DIRECTION_RANDOM
Get a random direction, depending on the topology.
Definition: ROOT-Sim.h:141
@ DIRECTION_SE
South-east direction.
Definition: ROOT-Sim.h:139
@ DIRECTION_E
East direction.
Definition: ROOT-Sim.h:132
@ DIRECTION_W
West direction.
Definition: ROOT-Sim.h:133
@ DIRECTION_NW
North-west direction.
Definition: ROOT-Sim.h:138
@ DIRECTION_S
South direction.
Definition: ROOT-Sim.h:135
log_level
Definition: ROOT-Sim.h:107
@ LOG_ERROR
The logging level reserved to unexpected, problematic conditions.
Definition: ROOT-Sim.h:112
@ LOG_SILENT
Emit no message during the simulation.
Definition: ROOT-Sim.h:114
@ LOG_TRACE
The logging level reserved to very low priority messages.
Definition: ROOT-Sim.h:108
@ LOG_INFO
The logging level reserved to useful runtime messages.
Definition: ROOT-Sim.h:110
@ LOG_DEBUG
The logging level reserved to useful debug messages.
Definition: ROOT-Sim.h:109
@ LOG_FATAL
The logging level reserved to unexpected, fatal conditions.
Definition: ROOT-Sim.h:113
@ LOG_WARN
The logging level reserved to unexpected, non deal breaking conditions.
Definition: ROOT-Sim.h:111
double Normal(void)
Return a pair of independent random numbers according to a Standard Normal Distribution.
Definition: random.c:73
double Gamma(unsigned ia)
Return a number in according to a Gamma Distribution of Integer Order ia Corresponds to the waiting t...
Definition: random.c:104
int RootsimInit(const struct simulation_configuration *conf)
Initialize the core library.
Definition: init.c:91
uint64_t lp_id_t
Logical Process ID data type.
Definition: ROOT-Sim.h:33
double Random(void)
Return a random value in [0,1] according to a uniform distribution.
Definition: random.c:51
void RootsimStop(void)
Force termination of the simulation.
Definition: termination.c:86
struct topology * vInitializeTopology(enum topology_geometry geometry, int argc,...)
Initialize a topology region.
Definition: topology.c:587
#define min(a, b)
Statement expression to compute the min between two values with no double evaluation side effect.
Definition: core.h:35
#define max(a, b)
Statement expression to compute the max between two values with no double evaluation side effect.
Definition: core.h:24
A set of configurable values used by other modules.
Definition: ROOT-Sim.h:210
enum log_level log_level
The logger verbosity level.
Definition: ROOT-Sim.h:220
unsigned gvt_period
The gvt period expressed in microseconds.
Definition: ROOT-Sim.h:218
CanEnd_t committed
Function pointer to the termination detection function.
Definition: ROOT-Sim.h:236
const char * stats_file
Path to the statistics file. If NULL, no statistics are produced.
Definition: ROOT-Sim.h:224
lp_id_t lps
The number of LPs to be used in the simulation.
Definition: ROOT-Sim.h:212
uint64_t prng_seed
The seed used to initialize the pseudo random numbers.
Definition: ROOT-Sim.h:228
bool serial
If set, the simulation will run on the serial runtime.
Definition: ROOT-Sim.h:232
FILE * logfile
File where to write logged information: if not NULL, output is redirected to this file.
Definition: ROOT-Sim.h:222
unsigned ckpt_interval
The checkpointing interval.
Definition: ROOT-Sim.h:226
ProcessEvent_t dispatcher
Function pointer to the dispatching function.
Definition: ROOT-Sim.h:234
simtime_t termination_time
The target termination logical time. Setting this value to zero means that LVT-based termination is d...
Definition: ROOT-Sim.h:216
bool core_binding
If set, worker threads are bound to physical cores.
Definition: ROOT-Sim.h:230
unsigned n_threads
The number of threads to be used in the simulation. If zero, it defaults to the amount of available c...
Definition: ROOT-Sim.h:214
The structure describing a topology.
Definition: topology.c:28
enum topology_geometry geometry
Definition: topology.c:32