cloudy trunk
Loading...
Searching...
No Matches
cpu.h
Go to the documentation of this file.
1/* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2 * others. For conditions of distribution and use see copyright notice in license.txt */
3
4#ifndef CPU_H_
5#define CPU_H_
6
8
9/* disable conditional expressions is constant */
10#ifdef _MSC_VER
11#pragma warning( disable : 4127 )
12#endif
13
14#define UNUSED /*@unused@*/
15#ifdef __GNUC__
16#undef UNUSED
17#define UNUSED __attribute__ ((unused)) /*@unused@*/
18#endif
19
21#ifndef INT16_MAX
22#define INT16_MAX 32767
23#endif
24#ifndef INT16_MIN
25#define INT16_MIN (-INT16_MAX - 1)
26#endif
27
28#if SHRT_MAX == INT16_MAX
29typedef short int int16;
30#elif INT_MAX == INT16_MAX
31typedef int int16;
32#else
33#error failed to define int16, please report this to gary@pa.uky.edu
34#endif
35
36#ifndef UINT16_MAX
37#define UINT16_MAX 65535
38#endif
39
40#if USHRT_MAX == UINT16_MAX
41typedef unsigned short int uint16;
42#elif UINT_MAX == UINT16_MAX
43typedef unsigned int uint16;
44#else
45#error failed to define uint16, please report this to gary@pa.uky.edu
46#endif
47
48#ifndef INT32_MAX
49#define INT32_MAX 2147483647L
50#endif
51#ifndef INT32_MIN
52#define INT32_MIN (-INT32_MAX - 1)
53#endif
54
55#if INT_MAX == INT32_MAX
56typedef int int32;
57#elif LONG_MAX == INT32_MAX
58typedef long int int32;
59#else
60#error failed to define int32, please report this to gary@pa.uky.edu
61#endif
62
63#ifndef UINT32_MAX
64#define UINT32_MAX 4294967295UL
65#endif
66
67#if UINT_MAX == UINT32_MAX
68typedef unsigned int uint32;
69#elif ULONG_MAX == UINT32_MAX
70typedef unsigned long int uint32;
71#else
72#error failed to define uint32, please report this to gary@pa.uky.edu
73#endif
74
75#if LONG_MAX > INT32_MAX
76
77/* this will only be defined on LP64 systems
78 * ILP32 systems may have long long support, but that is not
79 * part of the ISO/ANSI standard, so we don't use it here... */
80
81/* INT64_MAX, etc, may be defined as long long, so avoid system definitions! */
82
83#undef INT64_MAX
84#define INT64_MAX 9223372036854775807L
85
86#undef INT64_MIN
87#define INT64_MIN (-INT64_MAX - 1L)
88
89#if LONG_MAX == INT64_MAX
90#define HAVE_INT64 1
91typedef long int int64;
92#endif
93
94#endif
95
96#if ULONG_MAX > UINT32_MAX
97
98#undef UINT64_MAX
99#define UINT64_MAX 18446744073709551615UL
100
101#if ULONG_MAX == UINT64_MAX
102#define HAVE_UINT64 1
103typedef unsigned long int uint64;
104#endif
105
106#endif
107
129#ifdef cray
130#ifndef __cray
131#define __cray 1
132#endif
133#endif
134
136#ifdef __x86_64
137#ifndef __amd64
138#define __amd64 1
139#endif
140#endif
141
142#if defined(_ARCH_PPC) || defined(__POWERPC__) || defined(__powerpc__) || defined(PPC)
143#ifndef __ppc__
144#define __ppc__ 1
145#endif
146#endif
147
153#if defined(unix) || defined(__unix__)
154#ifndef __unix
155#define __unix 1
156#endif
157#endif
158
160#if defined(linux) || defined(__linux)
161#ifndef __linux__
162#define __linux__ 1
163#endif
164#endif
165
167#ifdef __ECC
168#ifndef __ICC
169#define __ICC __ECC
170#endif
171#endif
172
174#undef __GNUC_EXCL__
175#if defined(__GNUC__) && ! ( defined(__ICC) || defined(__PATHSCALE__) || defined(__OPENCC__) || defined(__clang__) )
176#define __GNUC_EXCL__ 1
177#endif
178
179/* make sure __func__ is defined, this can be removed once C++0x is in effect */
180#ifndef HAVE_FUNC
181#undef __func__
182#define __func__ DEBUG_ENTRY.name()
183#endif
184
185/* safe, small, numbers for the float and double */
188/*FLT_MAX is 3.40e38 on wintel, so BIGFLOAT is 3.40e36 */
189UNUSED const realnum BIGFLOAT = numeric_limits<realnum>::max()/realnum(100.f);
191const realnum SMALLFLOAT = numeric_limits<realnum>::min()*realnum(100.f);
192
194const double BIGDOUBLE = DBL_MAX/100.;
195const double SMALLDOUBLE = DBL_MIN*100.;
196
197const int STDLEN = 32;
198
209
210// the C++ openmodes below give the exact equivalent of the C modes "r", "w", "a", etc.
211// the "+" sign in the C mode has been replaced by "p", so, e.g., mode_rpb is equivalent to "r+b"
212const ios_base::openmode mode_r = ios_base::in;
213const ios_base::openmode mode_w = ios_base::out | ios_base::trunc;
214const ios_base::openmode mode_a = ios_base::out | ios_base::app;
215const ios_base::openmode mode_rp = ios_base::in | ios_base::out;
216const ios_base::openmode mode_wp = ios_base::in | ios_base::out | ios_base::trunc;
217const ios_base::openmode mode_ap = ios_base::in | ios_base::out | ios_base::app;
218
219const ios_base::openmode UNUSED mode_rb = mode_r | ios_base::binary;
220const ios_base::openmode UNUSED mode_wb = mode_w | ios_base::binary;
221const ios_base::openmode UNUSED mode_ab = mode_a | ios_base::binary;
222const ios_base::openmode UNUSED mode_rpb = mode_rp | ios_base::binary;
223const ios_base::openmode UNUSED mode_wpb = mode_wp | ios_base::binary;
224const ios_base::openmode UNUSED mode_apb = mode_ap | ios_base::binary;
225
226FILE* open_data( const char* fname, const char* mode, access_scheme scheme=AS_DATA_ONLY );
227void open_data( fstream& stream, const char* fname, ios_base::openmode mode, access_scheme scheme=AS_DATA_ONLY );
228
229/* this class is deliberately kept global so that the constructor is executed before
230 * any of the user code; this assures a correct FP environment right from the start */
232{
235 union
236 {
237 char c[4];
238 int32 i;
240
243
245# ifdef HAVE_INT64
246 int64 Double_SNaN_Value;
247# else
249# endif
250
251# ifdef __unix
252 struct sigaction p_action;
253 struct sigaction p_default;
254# endif
255
258
269 long n_rank;
273 vector<string> chSearchPath;
277
278 void enable_traps() const;
279 static void signal_handler(int sig);
280
281 vector<string> p_exit_status;
282
283 void getPathList( const char* fname, vector<string>& PathList, access_scheme scheme ) const;
284public:
285 t_cpu_i();
286
287 bool big_endian() const { return ( endian.i == 0x12345678 ); }
288 bool little_endian() const { return ( endian.i == 0x78563412 ); }
289
290 sys_float min_float() const { return test_float; }
291 double min_double() const { return test_double; }
292
293# ifdef __unix
294 const struct sigaction* action() const { return &p_action; }
295 const struct sigaction* deflt() const { return &p_default; }
296# endif
297
298 void set_signal_handlers();
299
300 void setAssertAbort(bool val)
301 {
302 p_lgAssertAbort = val;
303#ifdef CATCH_SIGNAL
304# ifdef __unix
305 if( val )
306 sigaction( SIGABRT, deflt(), NULL );
307 else
308 sigaction( SIGABRT, action(), NULL );
309# endif
310# ifdef _MSC_VER
311 if( val )
312 signal( SIGABRT, SIG_DFL );
313 else
314 signal( SIGABRT, &signal_handler );
315# endif
316#endif
317 }
318 bool lgAssertAbort() const { return p_lgAssertAbort; }
319
320 void set_nCPU(long n) { n_avail_CPU = n; }
321 long nCPU() const { return n_avail_CPU; }
322 bool lgMPI() const { return p_lgMPI; }
323 void set_MPISingleRankMode( bool mode ) { p_lgMPISingleRankMode = mode; }
325 void set_nRANK(long n) { n_rank = n; }
326 long nRANK() const { return n_rank; }
327 bool lgMaster() const { return ( n_rank == 0 ); }
328 bool lgMPI_talk() const { return lgMaster() || lgMPISingleRankMode(); }
329 const char *host_name() const { return HostName; }
330 void printDataPath() const;
331 char chDirSeparator() const { return p_chDirSeparator; }
332 bool firstOpen() const { return ( nFileDone == 0 ); }
333 const string& chExitStatus(exit_type s) const { return p_exit_status[s]; }
334
335 friend FILE* open_data( const char* fname, const char* mode, access_scheme scheme );
336 friend void open_data( fstream& stream, const char* fname, ios_base::openmode mode, access_scheme scheme );
337
338 friend void set_NaN(sys_float &x);
339 friend void set_NaN(sys_float x[], long n);
340 friend void set_NaN(double &x);
341 friend void set_NaN(double x[], long n);
342};
343class t_cpu
344{
345 static t_cpu_i *m_i;
346public:
348 {
349 return *m_i;
350 }
351 t_cpu();
352 ~t_cpu();
353};
354// Generate a (static) instance of this variable in *every* file.
355static t_cpu cpu;
356
357// The static (class) pointer is set in the first of these. Obviously
358// this is not thread safe...
359
360// Better engineered variants are available in Alexandrescu's book;
361// better yet to reduce the number of globals and file-statics so
362// this can just be initialized at the start of main().
363
365void set_NaN(sys_float &x);
366void set_NaN(sys_float x[], /* x[n] */
367 long n);
368void set_NaN(double &x);
369void set_NaN(double x[], /* x[n] */
370 long n);
371
373bool MyIsnan(const sys_float &x);
374bool MyIsnan(const double &x);
375
376/* Apply compiler directive saying that current routine does not
377 return as modifier, as in "NORETURN void MyExit() { ... }" */
378#ifdef _MSC_VER
379#define NORETURN __declspec(noreturn) /*@noreturn@*/
380#elif defined(__GNUC__) || ( defined(__INTEL_COMPILER) && defined(__linux__) )
381#define NORETURN __attribute__ ((noreturn)) /*@noreturn@*/
382#else
383#define NORETURN /*@noreturn@*/
384#endif
385
386#define RESTRICT
387#define UNLIKELY(x) (x)
388#ifdef __GNUC__
389#if (__GNUC__ >= 3)
390#undef UNLIKELY
391#define UNLIKELY(x) __builtin_expect((x),0)
392#endif
393#undef RESTRICT
394#define RESTRICT __restrict
395#else
396#endif
397
398/* Some hackery needed to test if a preprocessor macro is empty. Use as follows:
399 * #if EXPAND(SOME_DODGY_MACRO) == 1
400 * get here if SOME_DODGY_MACRO expands to empty string
401 * #endif */
402#define DO_EXPAND(VAL) VAL ## 1
403#define EXPAND(VAL) DO_EXPAND(VAL)
404
405/* Define __COMP and __COMP_VER macros for all systems */
406/* the Intel compiler */
407/* this needs to be before g++ since icc also sets __GNUC__ */
408#if defined __INTEL_COMPILER
409# define __COMP "icc"
410# define __COMP_VER __INTEL_COMPILER
411
412/* PathScale EKOPath compiler */
413/* this needs to be before g++ since pathCC also sets __GNUC__ */
414#elif defined __PATHSCALE__
415# define __COMP "pathCC"
416# define __COMP_VER __PATHCC__ * 100 + __PATHCC_MINOR__ * 10 + __PATHCC_PATCHLEVEL__
417
418/* Open64 compiler */
419/* this needs to be before g++ since openCC also sets __GNUC__ */
420#elif defined __OPENCC__
421# define __COMP "Open64"
422# if EXPAND(__OPENCC_PATCHLEVEL__) == 1
423# define __COMP_VER __OPENCC__ * 100 + __OPENCC_MINOR__ * 10
424# else
425# define __COMP_VER __OPENCC__ * 100 + __OPENCC_MINOR__ * 10 + __OPENCC_PATCHLEVEL__
426# endif
427
428/* LLVM clang++ */
429/* this needs to be before g++ since clang++ also sets __GNUC__ */
430#elif defined __clang__
431# define __COMP "clang++"
432# define __COMP_VER __clang_major__ * 100 + __clang_minor__ * 10 + __clang_patchlevel__
433
434/* g++ */
435#elif defined __GNUC__
436# define __COMP "g++"
437# if defined(__GNUC_PATCHLEVEL__)
438# define __COMP_VER (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
439# else
440# define __COMP_VER (__GNUC__ * 10000 + __GNUC_MINOR__ * 100)
441# endif
442
443#elif defined __PGI
444# define __COMP "Portland Group"
445# if defined(__PGIC__)
446# define __COMP_VER (__PGIC__ * 100 + __PGIC_MINOR__ * 10 + __PGIC_PATCHLEVEL__)
447# else
448# define __COMP_VER 0
449# endif
450
451/* SGI MIPSpro */
452/* this needs to be after g++, since g++ under IRIX also sets _COMPILER_VERSION */
453#elif defined(__sgi) && defined(_COMPILER_VERSION)
454# define __COMP "MIPSpro"
455# define __COMP_VER _COMPILER_VERSION
456
457/* HP */
458#elif defined __HP_aCC
459# define __COMP "HP aCC"
460# define __COMP_VER __HP_aCC
461
462/* DEC - this one may be broken for C++, no way to test it... */
463#elif defined __DECC
464# define __COMP "DEC CC"
465# define __COMP_VER __DECC_VER
466
467/* MS VS */
468#elif defined _MSC_VER
469# define __COMP "vs"
470# define __COMP_VER _MSC_VER
471
472/* Oracle Solaris Studio */
473#elif defined __SUNPRO_CC
474# define __COMP "Solaris Studio"
475# define __COMP_VER __SUNPRO_CC
476
477/* unknown */
478#else
479# define __COMP "unknown"
480# define __COMP_VER 0
481#endif
482
483/* ---------------------------- OS ---------------------------- */
484/* linux */
485#if defined __linux__
486# if defined __i386
487# define __OS "Linux (IA32)"
488# elif defined __amd64
489# define __OS "Linux (AMD64)"
490# elif defined __ia64
491# define __OS "Linux (IA64)"
492# elif defined __ppc__
493# define __OS "Linux (PowerPC)"
494# else
495# define __OS "Linux (other)"
496# endif
497
498/* macintosh */
499#elif defined macintosh
500# define __OS "Mac OS 9"
501
502/* macintosh */
503#elif defined __MACOSX__
504# define __OS "Mac OS X"
505
506/* apple mac, ... */
507#elif defined __APPLE__
508# define __OS "Apple MacOS"
509
510/* HP */
511#elif defined hpux
512# define __OS "HP-UX"
513
514/* Oracle Solaris */
515#elif defined __sun
516# define __OS "Solaris"
517
518/* IBM AIX */
519#elif defined _AIX
520# define __OS "AIX"
521
522/* Compaq alpha */
523#elif defined ultrix
524# define __OS "Ultrix"
525
526/* the BSD variants */
527#elif defined __FreeBSD__
528# define __OS "FreeBSD"
529
530#elif defined __NetBSD__
531# define __OS "NetBSD"
532
533#elif defined __OpenBSD__
534# define __OS "OpenBSD"
535
536/* Windows64 */
537/* this needs to be before _WIN32 since Windows64 also sets _WIN32 */
538#elif defined _WIN64
539# define __OS "Win64"
540
541/* Windows */
542#elif defined _WIN32
543# define __OS "Win32"
544
545/* Cygwin */
546#elif defined __CYGWIN__
547# define __OS "Cygwin"
548
549/* SGI */
550#elif defined __sgi
551# define __OS "IRIX"
552
553/* unknown */
554#else
555# define __OS "unknown"
556#endif
557
558/* don't perform this check when we are generating dependencies */
559#ifndef MM
560/* bomb out if the compiler is broken.... */
561# if defined(__GNUC_EXCL__) && ((__GNUC__ == 2 && __GNUC_MINOR__ == 96) || (__GNUC__ == 3 && __GNUC_MINOR__ == 4))
562# error "This g++ version cannot compile Cloudy and must not be used! Please update g++ to a functional version. See http://wiki.nublado.org/wiki/CompilingCloudyC13 for more details."
563# endif
564
565# if __INTEL_COMPILER >= 1200 && __INTEL_COMPILER < 1300
566# error "This icc version cannot compile Cloudy and must not be used! Please use a functional version of icc, or g++. See http://wiki.nublado.org/wiki/CompilingCloudyC13 for more details."
567# endif
568#endif
569
570#ifdef _MSC_VER
571#pragma warning( default : 4127 )/* disable warning that conditional expression is constant*/
572#endif
573
574#endif /* CPU_H_ */
float sys_float
Definition cddefines.h:106
exit_type
Definition cddefines.h:115
float realnum
Definition cddefines.h:103
Definition cpu.h:232
bool lgMPI_talk() const
Definition cpu.h:328
char HostName[STDLEN]
Definition cpu.h:271
void set_MPISingleRankMode(bool mode)
Definition cpu.h:323
bool firstOpen() const
Definition cpu.h:332
double test_double
Definition cpu.h:242
int32 i
Definition cpu.h:238
bool lgMPI() const
Definition cpu.h:322
const string & chExitStatus(exit_type s) const
Definition cpu.h:333
int32 Double_SNaN_Value[2]
Definition cpu.h:248
void setAssertAbort(bool val)
Definition cpu.h:300
const char * host_name() const
Definition cpu.h:329
sys_float min_float() const
Definition cpu.h:290
void set_nRANK(long n)
Definition cpu.h:325
void set_signal_handlers()
Definition cpu.cpp:477
char p_chDirSeparator
Definition cpu.h:275
bool p_lgMPI
Definition cpu.h:262
bool lgMaster() const
Definition cpu.h:327
union t_cpu_i::@054004306043002077260303305210015065171251205067 endian
bool p_lgAssertAbort
Definition cpu.h:257
bool lgAssertAbort() const
Definition cpu.h:318
sys_float test_float
Definition cpu.h:241
double min_double() const
Definition cpu.h:291
bool big_endian() const
Definition cpu.h:287
bool lgMPISingleRankMode() const
Definition cpu.h:324
int32 Float_SNaN_Value
Definition cpu.h:244
static void signal_handler(int sig)
Definition cpu.cpp:512
long nCPU() const
Definition cpu.h:321
t_cpu_i()
Definition cpu.cpp:72
bool little_endian() const
Definition cpu.h:288
void getPathList(const char *fname, vector< string > &PathList, access_scheme scheme) const
Definition cpu.cpp:532
void enable_traps() const
Definition cpu.cpp:293
int nFileDone
Definition cpu.h:276
long nRANK() const
Definition cpu.h:326
friend void set_NaN(sys_float &x)
Definition cpu.cpp:682
bool p_lgMPISingleRankMode
Definition cpu.h:267
void set_nCPU(long n)
Definition cpu.h:320
friend FILE * open_data(const char *fname, const char *mode, access_scheme scheme)
Definition cpu.cpp:625
vector< string > p_exit_status
Definition cpu.h:281
void printDataPath() const
Definition cpu.cpp:524
long n_rank
Definition cpu.h:269
char chDirSeparator() const
Definition cpu.h:331
char c[4]
Definition cpu.h:237
long n_avail_CPU
Definition cpu.h:260
vector< string > chSearchPath
Definition cpu.h:273
Definition cpu.h:344
~t_cpu()
Definition cpu.cpp:63
t_cpu()
Definition cpu.cpp:56
t_cpu_i & i()
Definition cpu.h:347
static t_cpu_i * m_i
Definition cpu.h:345
static t_cpu cpu
Definition cpu.h:355
const int STDLEN
Definition cpu.h:197
const ios_base::openmode mode_r
Definition cpu.h:212
const ios_base::openmode mode_a
Definition cpu.h:214
UNUSED const realnum BIGFLOAT
Definition cpu.h:189
access_scheme
Definition cpu.h:207
@ AS_LOCAL_ONLY
Definition cpu.h:208
@ AS_DATA_OPTIONAL
Definition cpu.h:208
@ AS_DATA_ONLY_TRY
Definition cpu.h:207
@ AS_DATA_ONLY
Definition cpu.h:207
@ AS_SILENT_TRY
Definition cpu.h:208
@ AS_DATA_LOCAL
Definition cpu.h:208
@ AS_LOCAL_DATA
Definition cpu.h:208
@ AS_LOCAL_ONLY_TRY
Definition cpu.h:207
@ AS_LOCAL_DATA_TRY
Definition cpu.h:207
@ AS_DATA_LOCAL_TRY
Definition cpu.h:207
const double BIGDOUBLE
Definition cpu.h:194
const ios_base::openmode UNUSED mode_apb
Definition cpu.h:224
bool MyIsnan(const sys_float &x)
Definition cpu.cpp:753
const ios_base::openmode UNUSED mode_wpb
Definition cpu.h:223
const ios_base::openmode UNUSED mode_rpb
Definition cpu.h:222
const ios_base::openmode mode_rp
Definition cpu.h:215
const ios_base::openmode mode_w
Definition cpu.h:213
const ios_base::openmode UNUSED mode_rb
Definition cpu.h:219
void set_NaN(sys_float &x)
Definition cpu.cpp:682
FILE * open_data(const char *fname, const char *mode, access_scheme scheme=AS_DATA_ONLY)
Definition cpu.cpp:625
const ios_base::openmode UNUSED mode_wb
Definition cpu.h:220
const double SMALLDOUBLE
Definition cpu.h:195
const ios_base::openmode mode_wp
Definition cpu.h:216
const ios_base::openmode UNUSED mode_ab
Definition cpu.h:221
const ios_base::openmode mode_ap
Definition cpu.h:217
#define UNUSED
Definition cpu.h:14
const realnum SMALLFLOAT
Definition cpu.h:191