The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
init.c
Go to the documentation of this file.
1 
11 #include <core/init.h>
12 
13 #include <arch/io.h>
14 #include <arch/thread.h>
15 #include <core/arg_parse.h>
16 #include <core/core.h>
17 
18 #include <inttypes.h>
19 #include <limits.h>
20 #include <memory.h>
21 #include <stdlib.h>
22 
23 #ifndef ROOTSIM_VERSION
24 #define ROOTSIM_VERSION "debugging_version"
25 #endif
26 
28 
30 enum option_key {
31  OPT_NPRC,
32  OPT_LOG,
33  OPT_CLOG,
34  OPT_SIMT,
35  OPT_GVT,
36  OPT_NP,
37  OPT_BIND,
38  OPT_SERIAL,
39  OPT_SEED,
40  OPT_LAST
41 };
42 
44 static struct ap_option ap_options[] = {
45  {"lp", OPT_NPRC, "VALUE", "Total number of Logical Processes being launched at simulation startup"},
46  {"log-level", OPT_LOG, "TYPE", "Logging level"},
47  {"time", OPT_SIMT, "VALUE", "Logical time at which the simulation will be considered completed"},
48  {"gvt-period", OPT_GVT, "VALUE", "Time between two GVT reductions (in milliseconds)"},
49  {"serial", OPT_SERIAL, NULL, "Runs a simulation with the serial runtime"},
50  {"wt", OPT_NP, "VALUE", "Number of total cores being used by the simulation"},
51  {"no-bind", OPT_BIND, NULL, "Disables thread to core binding"},
52  {"seed", OPT_SEED, "VALUE", "The seed value for the PRNG"},
53  {0}
54 };
55 
59 static void print_config(void)
60 {
61  // TODO
62 }
63 
72 static unsigned long long parse_ullong_limits(const char *str,
73  unsigned long long low, unsigned long long high, bool *err_chk)
74 {
75  char *end_p;
76  unsigned long long ret = strtoull(str, &end_p, 10);
77  *err_chk = *str == '\0' || *end_p != '\0' || ret < low || ret > high;
78  return ret;
79 }
80 
89 static long double parse_ldouble_limits(const char *str, long double low,
90  long double high, bool *err_chk)
91 {
92  char *end_p;
93  long double ret = strtold(str, &end_p);
94  *err_chk = *str == '\0' || *end_p != '\0' || ret < low || ret > high;
95  return ret;
96 }
97 
105 static void parse_opt(int key, const char *arg)
106 {
107  bool parse_err = false;
108  switch (key) {
109 
110  case OPT_NPRC:
111  n_lps = parse_ullong_limits(arg, 1, UINT_MAX, &parse_err);
112  break;
113 
114  case OPT_LOG:
115  // TODO
116  break;
117 
118  case OPT_SIMT:
120  SIMTIME_MAX, &parse_err);
121  break;
122 
123  case OPT_GVT:
125  &parse_err) * 1000;
126  break;
127 
128  case OPT_NP:
129  n_threads = parse_ullong_limits(arg, 1, UINT_MAX, &parse_err);
130  break;
131 
132  case OPT_BIND:
133  global_config.core_binding = false;
134  break;
135 
136  case OPT_SERIAL:
137  global_config.is_serial = true;
138  break;
139 
140  case OPT_SEED:
142  UINT64_MAX, &parse_err);
143  break;
144 
145  case AP_KEY_INIT:
146  n_lps = 0;
147  n_threads = 0;
149  global_config.gvt_period = 200000;
151  global_config.is_serial = false;
154  // Store the predefined values, before reading any overriding one
155  // TODO
156  break;
157 
158  case AP_KEY_FINI:
159  // if the threads count has not been supplied, the other checks
160  // are superfluous: the serial runtime simply will ignore the
161  // field while the parallel one will use the set count of cores
162  // (spitting a warning if it must use less cores than available)
163  if (n_threads == 0) {
166  } else if (global_config.is_serial) {
167  arg_parse_error("requested a serial simulation with %u threads",
168  n_threads);
169  } else if (n_threads > n_lps) {
170  arg_parse_error("requested a simulation with %u threads and %" PRIu64 " LPs",
171  n_threads, n_lps);
172  } else if(n_threads > thread_cores_count()) {
173  arg_parse_error("demanding %u cores, which are more than available (%u)",
175  }
176 
177  if(n_lps == 0)
178  arg_parse_error("number of LPs was not provided \"--lp\"");
179 
180  log_logo_print();
181  print_config();
182  }
183 
184  if (unlikely(parse_err)) {
185  size_t i;
186  for (i = 0; ap_options[i].key != key; ++i);
187  arg_parse_error("invalid value \"%s\" in the %s option.", arg,
188  ap_options[i].name);
189  }
190 }
191 
192 __attribute__((weak)) struct ap_option model_options[] = {0};
193 __attribute__((weak)) void model_parse(int key, const char *arg){(void) key; (void) arg;}
194 
196 struct ap_section ap_sects[] = {
197  {NULL, ap_options, parse_opt},
198  {"Model specific options", model_options, model_parse},
199  {0}
200 };
201 
204  "ROOT-Sim", // TODO properly fill these fields
205  ROOTSIM_VERSION,
206  "rootsim@googlegroups.com",
207  ap_sects
208 };
209 
217 void init_args_parse(int argc, char **argv)
218 {
219  (void) argc;
220  arg_parse_run(&ap_sets, argv);
221 }
ap_option::arg
const char * arg
The argument name for this option, shown in the --usage text.
Definition: arg_parse.h:22
n_lps
lp_id_t n_lps
The total number of LPs in the simulation.
Definition: core.c:13
ap_option
A single parsable command line option.
Definition: arg_parse.h:14
option_key
option_key
This is the list of arg_parse.h mnemonics for command line arguments.
Definition: init.c:30
simulation_configuration::core_binding
bool core_binding
If set, worker threads are bound to physical cores.
Definition: init.h:30
simulation_configuration::termination_time
simtime_t termination_time
The target termination logical time.
Definition: init.h:22
simulation_configuration::gvt_period
unsigned gvt_period
The gvt period expressed in microseconds.
Definition: init.h:24
AP_KEY_INIT
@ AP_KEY_INIT
Signals the start of the parsing process.
Definition: arg_parse.h:31
n_threads
rid_t n_threads
The total number of MPI nodes in the simulation.
Definition: core.c:15
ap_sets
struct ap_settings ap_sets
The struct ap_settings with the ROOT-Sim command line parsing configuration.
Definition: init.c:203
ap_section
A set of options organized and parsed together.
Definition: arg_parse.h:37
print_config
static void print_config(void)
Pretty prints ROOT-Sim current configuration.
Definition: init.c:59
log_logo_print
void log_logo_print(void)
Prints a fancy ROOT-Sim logo on the terminal.
Definition: log.c:82
SIMTIME_MAX
#define SIMTIME_MAX
The maximum value of the logical simulation time, semantically never.
Definition: core.h:64
ap_option::key
int key
The key passed to the parsing function when encountering this option.
Definition: arg_parse.h:19
AP_KEY_FINI
@ AP_KEY_FINI
Signals the end of the parsing process.
Definition: arg_parse.h:33
simulation_configuration::is_serial
bool is_serial
If set, the simulation will run on the serial runtime.
Definition: init.h:32
ap_sects
struct ap_section ap_sects[]
The struct ap_section containing ROOT-Sim internal parser and the model one.
Definition: init.c:196
log_colored
bool log_colored
If set, uses color codes to color the log outputs.
Definition: log.c:21
init.h
Initialization routines.
ap_settings
A complete command line option parsing setup.
Definition: arg_parse.h:49
arg_parse_run
void arg_parse_run(struct ap_settings *ap_s, char **argv)
Parses the command line options.
Definition: arg_parse.c:382
parse_ldouble_limits
static long double parse_ldouble_limits(const char *str, long double low, long double high, bool *err_chk)
Parses a string into a long double value with bounds checks.
Definition: init.c:89
parse_ullong_limits
static unsigned long long parse_ullong_limits(const char *str, unsigned long long low, unsigned long long high, bool *err_chk)
Parses a string into a unsigned long long value with bounds checks.
Definition: init.c:72
simulation_configuration
A set of configurable values used by other modules.
Definition: init.h:20
io_terminal_can_colorize
bool io_terminal_can_colorize(void)
Determines if stdout supports colored text.
Definition: init_test.c:16
parse_opt
static void parse_opt(int key, const char *arg)
Parses a single ROOT-Sim option, also handles parsing events.
Definition: init.c:105
thread_cores_count
unsigned thread_cores_count(void)
Computes the count of available cores on the machine.
Definition: init_test.c:23
io.h
Generic input-output facilities.
arg_parse.h
Command line option parser.
core.h
Core ROOT-Sim functionalities.
unlikely
#define unlikely(exp)
Optimize the branch as likely not taken.
Definition: core.h:59
ap_option::name
const char * name
The long option name.
Definition: arg_parse.h:16
global_config
struct simulation_configuration global_config
The configuration filled in by init_args_parse()
Definition: init.c:27
init_args_parse
void init_args_parse(int argc, char **argv)
Parses ROOT-Sim command line arguments.
Definition: init.c:217
arg_parse_error
void arg_parse_error(const char *fmt,...)
Prints a parsing related error message and exits with a bad exit code.
Definition: arg_parse.c:432
thread.h
Generic architecture management facilities.
simulation_configuration::verbosity
int verbosity
The log verbosity level.
Definition: init.h:26
ap_options
static struct ap_option ap_options[]
The array of ROOT-Sim supported command line options.
Definition: init.c:44
simulation_configuration::prng_seed
uint64_t prng_seed
The seed used to initialize the pseudo random numbers.
Definition: init.h:28