A toolkit for working with phylogenetic data.
v0.18.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-2017 Lucas Czech
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 (const unsigned int number)
113 {
114  number_of_threads_ = number;
115 
116  #if defined( GENESIS_OPENMP )
117 
118  // If we use OpenMp, set the thread number there, too.
119  omp_set_num_threads( number );
120 
121  #endif
122 }
123 
125 {
126  #ifdef GENESIS_PTHREADS
127  return true;
128  #else
129  return false;
130  #endif
131 }
132 
134 {
135  #ifdef GENESIS_OPENMP
136  return true;
137  #else
138  return false;
139  #endif
140 }
141 
142 // =================================================================================================
143 // Random Seed & Engine
144 // =================================================================================================
145 
146 void Options::random_seed(const unsigned seed)
147 {
148  random_seed_ = seed;
149  random_engine_.seed( seed );
150 }
151 
152 // =================================================================================================
153 // Run Time Environment
154 // =================================================================================================
155 
157 {
158  // Using http://stackoverflow.com/a/1312957/4184258
159  #if defined( _WIN32 ) || defined( _WIN64 )
160  return _isatty( _fileno( stdin ));
161  #else
162  return isatty( fileno( stdin ));
163  #endif
164 }
165 
167 {
168  #if defined( _WIN32 ) || defined( _WIN64 )
169  return _isatty( _fileno( stdout ));
170  #else
171  return isatty( fileno( stdout ));
172  #endif
173 }
174 
176 {
177  #if defined( _WIN32 ) || defined( _WIN64 )
178  return _isatty( _fileno( stderr ));
179  #else
180  return isatty( fileno( stderr ));
181  #endif
182 }
183 
184 // =================================================================================================
185 // Compile Time Environment
186 // =================================================================================================
187 
189 {
190  static const uint16_t e = 0x1000;
191  return 0 == *reinterpret_cast< uint8_t const* >( &e );
192 }
193 
195 {
196  static const uint16_t e = 0x0001;
197  return 0 == *reinterpret_cast< uint8_t const* >( &e );
198 }
199 
200 std::string Options::platform()
201 {
202  #if defined _WIN64
203  return "Win64";
204  #elif defined _WIN32
205  return "Win32";
206  #elif defined __linux__
207  return "Linux";
208  #elif defined __APPLE__
209  return "Apple";
210  #elif defined __unix__
211  return "Unix";
212  #else
213  return "Unknown";
214  #endif
215 }
216 
218 {
219  #if defined(__clang__)
220  return "clang";
221  #elif defined(__ICC) || defined(__INTEL_COMPILER)
222  return "icc";
223  #elif defined(__GNUC__) || defined(__GNUG__)
224  return "gcc";
225  #elif defined(__HP_cc) || defined(__HP_aCC)
226  return "hp";
227  #elif defined(__IBMCPP__)
228  return "ilecpp";
229  #elif defined(_MSC_VER)
230  return "msvc";
231  #elif defined(__PGI)
232  return "pgcpp";
233  #elif defined(__SUNPRO_CC)
234  return "sunpro";
235  #else
236  return "unknown";
237  #endif
238 }
239 
241 {
242  #if defined(__clang__)
243  return __clang_version__;
244  #elif defined(__ICC) || defined(__INTEL_COMPILER)
245  return __INTEL_COMPILER;
246  #elif defined(__GNUC__) || defined(__GNUG__)
247  return std::to_string(__GNUC__) + "." +
248  std::to_string(__GNUC_MINOR__) + "." +
249  std::to_string(__GNUC_PATCHLEVEL__)
250  ;
251  #elif defined(__HP_cc) || defined(__HP_aCC)
252  return "";
253  #elif defined(__IBMCPP__)
254  return __IBMCPP__;
255  #elif defined(_MSC_VER)
256  return _MSC_VER;
257  #elif defined(__PGI)
258  return __PGI;
259  #elif defined(__SUNPRO_CC)
260  return __SUNPRO_CC;
261  #else
262  return "unknown";
263  #endif
264 }
265 
266 std::string Options::cpp_version()
267 {
268  #ifdef __cplusplus
269  return std::to_string(__cplusplus);
270  #else
271  return "unknown";
272  #endif
273 }
274 
275 // =================================================================================================
276 // Build Type
277 // =================================================================================================
278 
280 {
281  #ifdef DEBUG
282  return true;
283  #else
284  return false;
285  #endif
286 }
287 
289 {
290  #ifdef NDEBUG
291  return true;
292  #else
293  return false;
294  #endif
295 }
296 
297 std::string Options::build_type()
298 {
299  #if defined( DEBUG )
300  return "debug";
301  #elif defined( NDEBUG )
302  return "release";
303  #else
304  return "unknown";
305  #endif
306 }
307 
308 // =================================================================================================
309 // Dump & Overview
310 // =================================================================================================
311 
312 std::string Options::info() const
313 {
314  std::string res = genesis_header();
315 
316  res += "\n";
317  res += "Compile Time Options\n";
318  res += "=============================================\n\n";
319  res += "Platform: " + platform() + "\n";
320  res += "Compiler: " + compiler_family() + " " + compiler_version() + "\n";
321  res += "C++ version: " + cpp_version() + "\n";
322  res += "Build type: " + build_type() + "\n";
323  res += "Endianness: " + std::string( is_little_endian() ? "little endian" : "big endian" ) + "\n";
324  res += "Using Pthreads: " + std::string( using_pthreads() ? "true" : "false" ) + "\n";
325  res += "Using OpenMP: " + std::string( using_openmp() ? "true" : "false" ) + "\n";
326 
327  res += "\n";
328  res += "Run Time Options\n";
329  res += "=============================================\n\n";
330  auto const cli_str = command_line_string();
331  res += "Command line: " + ( cli_str.size() > 0 ? cli_str : "(not available)" ) + "\n";
332  res += "Number of threads: " + std::to_string( number_of_threads() ) + "\n";
333  res += "Random seed: " + std::to_string( random_seed_ ) + "\n";
334  return res;
335 }
336 
337 } // namespace utils
338 } // namespace genesis
static std::string cpp_version()
Return the CPP version that was used to compile genesis.
Definition: options.cpp:266
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:166
static std::string compiler_version()
Return the compiler version that was used to compile genesis.
Definition: options.cpp:240
static std::string platform()
Return the platform under which genesis was compiled.
Definition: options.cpp:200
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:217
bool using_pthreads() const
Return whether the binary was compiled using Pthreads.
Definition: options.cpp:124
static bool is_debug()
Return whether the binary was compiled with build type DEBUG.
Definition: options.cpp:279
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:300
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:288
unsigned random_seed() const
Returns the random seed that was used to initialize the engine.
Definition: options.hpp:127
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:156
std::string info() const
Return a list with compile time and run time options with their values.
Definition: options.cpp:312
static std::string build_type()
Return the build type that was used to compile the binary, i.e., "debug" or "release".
Definition: options.cpp:297
static bool is_big_endian()
Return whether the system uses big endian memory.
Definition: options.cpp:194
bool using_openmp() const
Return whether the binary was compiled using OpenMP.
Definition: options.cpp:133
static bool is_little_endian()
Return whether the system uses little endian memory.
Definition: options.cpp:188
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:175
Some stuff that is totally not imporatant, but nice.
std::string genesis_header()
Return the header for genesis.
Definition: version.hpp:93