A toolkit for working with phylogenetic data.
v0.19.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
options.cpp
Go to the documentation of this file.
1 /*
2  Genesis - A toolkit for working with phylogenetic data.
3  Copyright (C) 2014-2018 Lucas Czech and HITS gGmbH
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  Contact:
19  Lucas Czech <lucas.czech@h-its.org>
20  Exelixis Lab, Heidelberg Institute for Theoretical Studies
21  Schloss-Wolfsbrunnenweg 35, D-69118 Heidelberg, Germany
22 */
23 
32 
34 
35 #include <chrono>
36 #include <cstdint>
37 #include <cstdio>
38 
39 #if defined( _WIN32 ) || defined( _WIN64 )
40 # include <io.h>
41 #else
42 # include <unistd.h>
43 #endif
44 
45 #ifdef GENESIS_OPENMP
46 # include <omp.h>
47 #endif
48 
49 #ifdef GENESIS_PTHREADS
50 # include <thread>
51 #endif
52 
53 namespace genesis {
54 namespace utils {
55 
56 // =================================================================================================
57 // Initialization
58 // =================================================================================================
59 
60 Options::Options()
61 {
62 
63  #if defined( GENESIS_OPENMP )
64 
65  // Initialize threads to number of OpenMP threads, which might be set through the
66  // `OMP_NUM_THREADS` environment variable.
67  number_of_threads( omp_get_num_threads() );
68 
69  #elif defined( GENESIS_PTHREADS )
70 
71  // Initialize threads with actual number of cores.
72  number_of_threads( std::thread::hardware_concurrency() );
73 
74  #else
75 
76  // Set to single threaded.
77  number_of_threads( 1 );
78 
79  #endif
80 
81  // Initialize random seed with time.
82  random_seed( std::chrono::system_clock::now().time_since_epoch().count() );
83 }
84 
85 // =================================================================================================
86 // Command Line
87 // =================================================================================================
88 
89 std::string Options::command_line_string () const
90 {
91  std::string ret = "";
92  for (size_t i = 0; i < command_line_.size(); ++i) {
93  std::string a = command_line_[i];
94  ret += (i==0 ? "" : " ") + a;
95  }
96  return ret;
97 }
98 
99 void Options::command_line( int const argc, char const* const* argv )
100 {
101  // Store all arguments in the array.
102  command_line_.clear();
103  for (int i = 0; i < argc; i++) {
104  command_line_.push_back(argv[i]);
105  }
106 }
107 
108 // =================================================================================================
109 // Number of Threads
110 // =================================================================================================
111 
112 void Options::number_of_threads ( unsigned int number )
113 {
114  if( number == 0 ) {
115  #ifdef GENESIS_PTHREADS
116  number = std::thread::hardware_concurrency();
117  #else
118  number = 1;
119  #endif
120  }
121  number_of_threads_ = number;
122 
123  #if defined( GENESIS_OPENMP )
124 
125  // If we use OpenMp, set the thread number there, too.
126  omp_set_num_threads( number );
127 
128  #endif
129 }
130 
132 {
133  #ifdef GENESIS_PTHREADS
134  return true;
135  #else
136  return false;
137  #endif
138 }
139 
141 {
142  #ifdef GENESIS_OPENMP
143  return true;
144  #else
145  return false;
146  #endif
147 }
148 
149 // =================================================================================================
150 // Random Seed & Engine
151 // =================================================================================================
152 
153 void Options::random_seed(const unsigned seed)
154 {
155  random_seed_ = seed;
156  random_engine_.seed( seed );
157 }
158 
159 // =================================================================================================
160 // Run Time Environment
161 // =================================================================================================
162 
164 {
165  // Using http://stackoverflow.com/a/1312957/4184258
166  #if defined( _WIN32 ) || defined( _WIN64 )
167  return _isatty( _fileno( stdin ));
168  #else
169  return isatty( fileno( stdin ));
170  #endif
171 }
172 
174 {
175  #if defined( _WIN32 ) || defined( _WIN64 )
176  return _isatty( _fileno( stdout ));
177  #else
178  return isatty( fileno( stdout ));
179  #endif
180 }
181 
183 {
184  #if defined( _WIN32 ) || defined( _WIN64 )
185  return _isatty( _fileno( stderr ));
186  #else
187  return isatty( fileno( stderr ));
188  #endif
189 }
190 
191 // =================================================================================================
192 // Compile Time Environment
193 // =================================================================================================
194 
196 {
197  static const uint16_t e = 0x1000;
198  return 0 == *reinterpret_cast< uint8_t const* >( &e );
199 }
200 
202 {
203  static const uint16_t e = 0x0001;
204  return 0 == *reinterpret_cast< uint8_t const* >( &e );
205 }
206 
207 std::string Options::platform()
208 {
209  #if defined _WIN64
210  return "Win64";
211  #elif defined _WIN32
212  return "Win32";
213  #elif defined __linux__
214  return "Linux";
215  #elif defined __APPLE__
216  return "Apple";
217  #elif defined __unix__
218  return "Unix";
219  #else
220  return "Unknown";
221  #endif
222 }
223 
225 {
226  #if defined(__clang__)
227  return "clang";
228  #elif defined(__ICC) || defined(__INTEL_COMPILER)
229  return "icc";
230  #elif defined(__GNUC__) || defined(__GNUG__)
231  return "gcc";
232  #elif defined(__HP_cc) || defined(__HP_aCC)
233  return "hp";
234  #elif defined(__IBMCPP__)
235  return "ilecpp";
236  #elif defined(_MSC_VER)
237  return "msvc";
238  #elif defined(__PGI)
239  return "pgcpp";
240  #elif defined(__SUNPRO_CC)
241  return "sunpro";
242  #else
243  return "unknown";
244  #endif
245 }
246 
248 {
249  #if defined(__clang__)
250  return __clang_version__;
251  #elif defined(__ICC) || defined(__INTEL_COMPILER)
252  return __INTEL_COMPILER;
253  #elif defined(__GNUC__) || defined(__GNUG__)
254  return std::to_string(__GNUC__) + "." +
255  std::to_string(__GNUC_MINOR__) + "." +
256  std::to_string(__GNUC_PATCHLEVEL__)
257  ;
258  #elif defined(__HP_cc) || defined(__HP_aCC)
259  return "";
260  #elif defined(__IBMCPP__)
261  return __IBMCPP__;
262  #elif defined(_MSC_VER)
263  return _MSC_VER;
264  #elif defined(__PGI)
265  return __PGI;
266  #elif defined(__SUNPRO_CC)
267  return __SUNPRO_CC;
268  #else
269  return "unknown";
270  #endif
271 }
272 
273 std::string Options::cpp_version()
274 {
275  #ifdef __cplusplus
276  return std::to_string(__cplusplus);
277  #else
278  return "unknown";
279  #endif
280 }
281 
282 // =================================================================================================
283 // Build Type
284 // =================================================================================================
285 
287 {
288  #ifdef DEBUG
289  return true;
290  #else
291  return false;
292  #endif
293 }
294 
296 {
297  #ifdef NDEBUG
298  return true;
299  #else
300  return false;
301  #endif
302 }
303 
304 std::string Options::build_type()
305 {
306  #if defined( DEBUG )
307  return "debug";
308  #elif defined( NDEBUG )
309  return "release";
310  #else
311  return "unknown";
312  #endif
313 }
314 
315 // =================================================================================================
316 // Dump & Overview
317 // =================================================================================================
318 
319 std::string Options::info() const
320 {
321  std::string res = genesis_header();
322 
323  res += "\n";
324  res += "Compile Time Options\n";
325  res += "=============================================\n\n";
326  res += "Platform: " + platform() + "\n";
327  res += "Compiler: " + compiler_family() + " " + compiler_version() + "\n";
328  res += "C++ version: " + cpp_version() + "\n";
329  res += "Build type: " + build_type() + "\n";
330  res += "Endianness: " + std::string( is_little_endian() ? "little endian" : "big endian" ) + "\n";
331  res += "Using Pthreads: " + std::string( using_pthreads() ? "true" : "false" ) + "\n";
332  res += "Using OpenMP: " + std::string( using_openmp() ? "true" : "false" ) + "\n";
333 
334  res += "\n";
335  res += "Run Time Options\n";
336  res += "=============================================\n\n";
337  auto const cli_str = command_line_string();
338  res += "Command line: " + ( cli_str.size() > 0 ? cli_str : "(not available)" ) + "\n";
339  res += "Number of threads: " + std::to_string( number_of_threads() ) + "\n";
340  res += "Random seed: " + std::to_string( random_seed_ ) + "\n";
341  return res;
342 }
343 
344 } // namespace utils
345 } // namespace genesis
static std::string cpp_version()
Return the CPP version that was used to compile genesis.
Definition: options.cpp:273
bool stdout_is_terminal() const
Return true iff the standard output stream is a terminal, and false if not, i.e., if it is a file or ...
Definition: options.cpp:173
static std::string compiler_version()
Return the compiler version that was used to compile genesis.
Definition: options.cpp:247
static std::string platform()
Return the platform under which genesis was compiled.
Definition: options.cpp:207
unsigned int number_of_threads() const
Returns the number of threads.
Definition: options.hpp:97
static std::string compiler_family()
Return the compiler family (name) that was used to compile genesis.
Definition: options.cpp:224
bool using_pthreads() const
Return whether the binary was compiled using Pthreads.
Definition: options.cpp:131
static bool is_debug()
Return whether the binary was compiled with build type DEBUG.
Definition: options.cpp:286
std::vector< std::string > command_line() const
Returns an array of strings containing the program's command line arguments.
Definition: options.hpp:72
std::string to_string(T const &v)
Return a string representation of a given value.
Definition: string.hpp:373
std::string command_line_string() const
Returns a string containing the program's command line arguments.
Definition: options.cpp:89
static bool is_release()
Return whether the binary was compiled with build type RELEASE.
Definition: options.cpp:295
unsigned random_seed() const
Returns the random seed that was used to initialize the engine.
Definition: options.hpp:130
bool stdin_is_terminal() const
Return true iff the standard input stream is a terminal, and false if not, i.e., if it is a file or a...
Definition: options.cpp:163
std::string info() const
Return a list with compile time and run time options with their values.
Definition: options.cpp:319
static std::string build_type()
Return the build type that was used to compile the binary, i.e., "debug" or "release".
Definition: options.cpp:304
static bool is_big_endian()
Return whether the system uses big endian memory.
Definition: options.cpp:201
bool using_openmp() const
Return whether the binary was compiled using OpenMP.
Definition: options.cpp:140
static bool is_little_endian()
Return whether the system uses little endian memory.
Definition: options.cpp:195
bool stderr_is_terminal() const
Return true iff the standard error stream is a terminal, and false if not, i.e., if it is a file or a...
Definition: options.cpp:182
Some stuff that is totally not imporatant, but nice.
std::string genesis_header()
Return the header for genesis.
Definition: version.hpp:93