|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_CORE_OPTIONS_H_
2 #define GENESIS_UTILS_CORE_OPTIONS_H_
93 for (
size_t i = 0; i < command_line_.size(); ++i) {
94 std::string a = command_line_[i];
95 ret += (i==0 ?
"" :
" ") + a;
109 command_line_.clear();
110 for (
int i = 0; i < argc; i++) {
111 command_line_.push_back(argv[i]);
150 throw std::domain_error(
151 "Cannot re-seed global random engine after having spawned the global thread pool, "
152 "as the threads would not be aware of the re-seeding. "
153 "Call Options::get().random_seed() before Options::get().init_global_thread_pool() "
158 thread_local_random_engine_().seed( seed );
170 return thread_local_random_engine_();
180 return seed_counter_.load();
185 inline unsigned long generate_thread_seed_()
187 return random_seed_ + seed_counter_.fetch_add( 1, std::memory_order_relaxed );
190 inline std::default_random_engine& thread_local_random_engine_()
193 thread_local std::default_random_engine engine( generate_thread_seed_() );
220 assert( num_threads > 0 );
221 if( num_threads == 0 ) {
247 throw std::runtime_error(
248 "Global thread pool has already been initialized. "
249 "Cannot call Options::get().init_global_thread_pool() multiple times."
252 thread_pool_ = std::make_shared<utils::ThreadPool>( num_threads, max_queue_size );
270 if( ! thread_pool_ ) {
271 throw std::runtime_error(
272 "Global thread pool has not been initialized. "
273 "Call Options::get().init_global_thread_pool() first."
286 if( ! thread_pool_ ) {
289 return thread_pool_->size() + 1;
339 input_reading_thread_policy_ = policy;
347 return input_reading_thread_policy_;
361 return allow_file_overwriting_;
379 allow_file_overwriting_ = value;
389 std::vector<std::string> command_line_;
392 unsigned long random_seed_;
393 std::atomic<size_t> seed_counter_{0};
396 std::shared_ptr<ThreadPool> thread_pool_;
400 bool allow_file_overwriting_ =
false;
416 random_seed( std::chrono::system_clock::now().time_since_epoch().count() );
419 ~Options() =
default;
421 Options(
const Options& ) =
delete;
422 Options( Options&& ) =
delete;
423 Options& operator= (
const Options& ) =
delete;
424 Options& operator= ( Options&& ) =
delete;
431 #endif // include guard
void allow_file_overwriting(bool value)
Set whether Genesis is allowed to overwrite files when outputting data.
void init_global_thread_pool(size_t num_threads, size_t max_queue_size=0)
Initialize the global thread pool to be used for parallel computations.
void command_line(int const argc, char const *const *argv)
Set arguments to the program's command line options.
size_t guess_number_of_threads(bool use_openmp, bool use_slurm, bool physical_cores)
Make an educated guess on the number of threads to use for multi-threaded functionality.
unsigned long random_seed() const
Returns the random seed that was used to initialize the engine.
InputReadingThreadPolicy
Decide how to use threads for input reading.
std::shared_ptr< ThreadPool > global_thread_pool() const
Return a global thread pool to be used for parallel computations.
std::vector< std::string > const & command_line() const
Returns an array of strings containing the program's command line arguments.
void input_reading_thread_policy(InputReadingThreadPolicy policy)
Set the policy for the threading of input source reading.
size_t seed_counter() const
Return the number of seeds used to initialize thread-local random engines.
std::string command_line_string() const
Returns a string containing the program's command line arguments.
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
Simple Options class for application-wide configuration and settings.
@ kAllAsync
All reading from input sources gets their own thread.
size_t global_thread_pool_size() const
Get the number of threads allocatd in the pool, plus one for the main thread.
std::default_random_engine & random_engine()
Return a thread-local engine for random number generation.
void init_global_thread_pool()
Initialize the global thread pool to be used for parallel computations.
InputReadingThreadPolicy input_reading_thread_policy()
Get the policy for the threading of input source reading.
@ kTrivialAsync
Use async threads for trivial input sources, such as simple files, but use the global thread pool for...
@ kStrict
All input reading uses the global thread pool.
static Options & get()
Returns a single instance of this class.
bool allow_file_overwriting() const
Get whether Genesis is allowed to overwrite files when outputting data.
void random_seed(unsigned long const seed)
Set a specific global seed for the random engine initialization.