1from __future__ import generators 2import config.base 3 4class Configure(config.base.Configure): 5 def __init__(self, framework): 6 config.base.Configure.__init__(self, framework) 7 self.headerPrefix = '' 8 self.substPrefix = '' 9 self.have__fp16 = 0 10 self.have__float128 = 0 11 return 12 13 def __str1__(self): 14 output = ' Scalar type: ' + self.scalartype + '\n' 15 output += ' Precision: ' + self.precision + '\n' 16 support = [] 17 if self.have__fp16 and not self.precision == '__fp16': support.append('__fp16') 18 if self.have__float128 and not self.precision == '__float128': support.append('__float128') 19 if not len(support) == 0: output += ' Support for ' + ' and '.join(support) + '\n' 20 return output 21 22 def setupHelp(self, help): 23 import nargs 24 # Dec 2016, the __fp16 type is only available with GNU compilers on ARM systems 25 help.addArgument('PETSc', '-with-precision=<__fp16,single,double,__float128>', nargs.Arg(None, 'double', 'Specify numerical precision')) 26 help.addArgument('PETSc', '-with-scalar-type=<real or complex>', nargs.Arg(None, 'real', 'Specify real or complex numbers')) 27 return 28 29 def setupDependencies(self, framework): 30 config.base.Configure.setupDependencies(self, framework) 31 self.types = framework.require('config.types', self) 32 self.languages = framework.require('PETSc.options.languages', self) 33 self.compilers = framework.require('config.compilers', self) 34 self.libraries = framework.require('config.libraries',self) 35 self.setCompilers = framework.require('config.setCompilers', self) 36 self.compilerFlags = framework.require('config.compilerFlags', self) 37 self.types = framework.require('config.types', self) 38 self.headers = framework.require('config.headers', self) 39 self.libraries = framework.require('config.libraries', self) 40 return 41 42 def configureScalarType(self): 43 '''Choose between real and complex numbers''' 44 self.scalartype = self.framework.argDB['with-scalar-type'].lower() 45 if self.scalartype == 'complex': 46 self.addDefine('USE_COMPLEX', '1') 47 if self.languages.clanguage == 'C' and not self.types.c99_complex: 48 raise RuntimeError('C Compiler provided does not support C99 complex') 49 if self.languages.clanguage == 'Cxx' and not self.types.cxx_complex: 50 raise RuntimeError('Cxx compiler provided does not support std::complex') 51 if self.languages.clanguage == 'Cxx': 52 self.addDefine('USE_CXXCOMPLEX',1) 53 elif not self.scalartype == 'real': 54 raise RuntimeError('--with-scalar-type must be real or complex') 55 self.logPrint('Scalar type is '+str(self.scalartype)) 56 # On apple isinf() and isnan() do not work when <complex> is included 57 self.pushLanguage(self.languages.clanguage) 58 if self.scalartype == 'complex' and self.languages.clanguage == 'Cxx': 59 if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isnormal(b);(void)a'): 60 self.addDefine('HAVE_ISNORMAL',1) 61 if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isnan(b);(void)a'): 62 self.addDefine('HAVE_ISNAN',1) 63 elif self.checkLink('#include <float.h>\n#include <complex>\n','double b = 2.0;int a = _isnan(b);(void)a'): 64 self.addDefine('HAVE__ISNAN',1) 65 if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isinf(b);(void)a'): 66 self.addDefine('HAVE_ISINF',1) 67 elif self.checkLink('#include <float.h>\n#include <complex>\n','double b = 2.0;int a = _finite(b);(void)a'): 68 self.addDefine('HAVE__FINITE',1) 69 else: 70 if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnormal(b);(void)a'): 71 self.addDefine('HAVE_ISNORMAL',1) 72 if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnan(b);(void)a'): 73 self.addDefine('HAVE_ISNAN',1) 74 elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _isnan(b);(void)a'): 75 self.addDefine('HAVE__ISNAN',1) 76 if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isinf(b);(void)a'): 77 self.addDefine('HAVE_ISINF',1) 78 elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _finite(b);(void)a'): 79 self.addDefine('HAVE__FINITE',1) 80 self.popLanguage() 81 return 82 83 def configurePrecision(self): 84 '''Set the default real number precision for PETSc objects''' 85 self.log.write('Checking C compiler works with __float128\n') 86 self.have__float128 = 0 87 if self.libraries.check('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f = 0.0; logq(f)'): 88 self.log.write('C compiler works with quadmath library\n') 89 self.have__float128 = 1 90 if hasattr(self.compilers, 'FC'): 91 self.libraries.pushLanguage('FC') 92 self.log.write('Checking Fortran compiler works with quadmath library\n') 93 if self.libraries.check('quadmath',' ',call = ' real*16 s,w; w = 2.0; s = cos(w)'): 94 self.log.write('Fortran compiler works with quadmath library\n') 95 else: 96 self.have__float128 = 0 97 self.log.write('Fortran compiler fails with quadmath library\n') 98 self.libraries.popLanguage() 99 if hasattr(self.compilers, 'CXX'): 100 self.libraries.pushLanguage('Cxx') 101 isGNU = self.setCompilers.isGNU(self.getCompiler(lang='Cxx'), self.log) 102 # need to bypass g++ error: non-standard suffix on floating constant [-Werror=pedantic] 103 # this warning can't be disabled but is actually never triggered by PETSc 104 if isGNU: 105 self.setCompilers.pushLanguage('Cxx') 106 preprocessorFlagsArg = self.setCompilers.getPreprocessorFlagsArg() 107 oldPreprocessorFlags = getattr(self.setCompilers, preprocessorFlagsArg) 108 setattr(self.setCompilers, preprocessorFlagsArg, oldPreprocessorFlags+' -Wno-error') 109 self.log.write('Checking C++ compiler works with quadmath library\n') 110 if self.libraries.check('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f = FLT128_EPSILON; logq(f)'): 111 self.log.write('C++ compiler works with quadmath library\n') 112 else: 113 self.have__float128 = 0 114 self.log.write('C++ compiler fails with quadmath library\n') 115 if isGNU: 116 setattr(self.setCompilers, preprocessorFlagsArg, oldPreprocessorFlags) 117 self.setCompilers.popLanguage() 118 self.libraries.popLanguage() 119 if self.have__float128: 120 self.libraries.add('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f = 0.0; logq(f)') 121 self.addDefine('HAVE_REAL___FLOAT128', '1') 122 123 self.log.write('Checking C compiler works with __fp16\n') 124 self.have__fp16 = 0 125 if self.libraries.check('','',call='__fp16 f = 1.0, g; g = ret___fp16(f); (void)g',prototype='static __fp16 ret___fp16(__fp16 f) { return f; }'): 126 self.have__fp16 = 1 127 self.addDefine('HAVE_REAL___FP16', '1') 128 129 self.precision = self.framework.argDB['with-precision'].lower() 130 if self.precision == '__fp16': # supported by gcc trunk 131 if self.scalartype == 'complex': 132 raise RuntimeError('__fp16 can only be used with real numbers, not complex') 133 if hasattr(self.compilers, 'FC'): 134 raise RuntimeError('__fp16 can only be used with C compiler, not Fortran') 135 if self.have__fp16: 136 self.addDefine('USE_REAL___FP16', '1') 137 self.addMakeMacro('PETSC_SCALAR_SIZE', '16') 138 else: 139 raise RuntimeError('__fp16 support not found, cannot proceed --with-precision=__fp16') 140 elif self.precision == 'single': 141 self.addDefine('USE_REAL_SINGLE', '1') 142 self.addMakeMacro('PETSC_SCALAR_SIZE', '32') 143 elif self.precision == 'double': 144 self.addDefine('USE_REAL_DOUBLE', '1') 145 self.addMakeMacro('PETSC_SCALAR_SIZE', '64') 146 elif self.precision == '__float128': # supported by gcc 4.6/gfortran and later 147 if self.have__float128: 148 self.addDefine('USE_REAL___FLOAT128', '1') 149 self.addMakeMacro('PETSC_SCALAR_SIZE', '128') 150 else: 151 raise RuntimeError('__float128 support not found. --with-precision=__float128 works with gcc-4.6 and newer compilers.') 152 else: 153 raise RuntimeError('--with-precision must be __fp16, single, double, or __float128') 154 self.logPrint('Precision is '+str(self.precision)) 155 return 156 157 def configure(self): 158 self.executeTest(self.configureScalarType) 159 self.executeTest(self.configurePrecision) 160 return 161