xref: /petsc/config/PETSc/options/scalarTypes.py (revision 3fc23ce8277e00a65c4b1be6e2b4535b56ba7978)
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__float128 = 0
10    return
11
12  def __str1__(self):
13    output  = '  Scalar type: ' + self.scalartype + '\n'
14    output += '  Precision: ' + self.precision + '\n'
15    if self.have__float128 and not self.precision == '__float128': output += '  Support for __float128\n'
16    return output
17
18  def setupHelp(self, help):
19    import nargs
20    #  Dec 2016, the __fp16 type is only available with GNU compilers on ARM systems
21    help.addArgument('PETSc', '-with-precision=<__fp16,single,double,__float128>', nargs.Arg(None, 'double', 'Specify numerical precision'))
22    help.addArgument('PETSc', '-with-scalar-type=<real or complex>', nargs.Arg(None, 'real', 'Specify real or complex numbers'))
23    return
24
25  def setupDependencies(self, framework):
26    config.base.Configure.setupDependencies(self, framework)
27    self.types         = framework.require('config.types', self)
28    self.languages     = framework.require('PETSc.options.languages', self)
29    self.compilers     = framework.require('config.compilers', self)
30    self.libraries     = framework.require('config.libraries',self)
31    self.setCompilers  = framework.require('config.setCompilers', self)
32    self.compilerFlags = framework.require('config.compilerFlags', self)
33    self.types         = framework.require('config.types', self)
34    self.headers       = framework.require('config.headers', self)
35    self.libraries     = framework.require('config.libraries', self)
36    return
37
38  def configureScalarType(self):
39    '''Choose between real and complex numbers'''
40    self.scalartype = self.framework.argDB['with-scalar-type'].lower()
41    if self.scalartype == 'complex':
42      self.addDefine('USE_COMPLEX', '1')
43      if self.languages.clanguage == 'C' and not self.types.c99_complex:
44        raise RuntimeError('C Compiler provided does not support C99 complex')
45      if self.languages.clanguage == 'Cxx' and not self.types.cxx_complex:
46        raise RuntimeError('Cxx compiler provided does not support std::complex')
47      if self.languages.clanguage == 'Cxx':
48        self.addDefine('USE_CXXCOMPLEX',1)
49    elif not self.scalartype == 'real':
50      raise RuntimeError('--with-scalar-type must be real or complex')
51    self.logPrint('Scalar type is '+str(self.scalartype))
52    # On apple isinf() and isnan() do not work when <complex> is included
53    self.pushLanguage(self.languages.clanguage)
54    if self.scalartype == 'complex' and self.languages.clanguage == 'Cxx':
55      if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isnormal(b);\n'):
56        self.addDefine('HAVE_ISNORMAL',1)
57      if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isnan(b);\n'):
58        self.addDefine('HAVE_ISNAN',1)
59      elif self.checkLink('#include <float.h>\n#include <complex>\n','double b = 2.0;int a = _isnan(b);\n'):
60        self.addDefine('HAVE__ISNAN',1)
61      if self.checkLink('#include <math.h>\n#include <complex>\n','double b = 2.0;int a = isinf(b);\n'):
62        self.addDefine('HAVE_ISINF',1)
63      elif self.checkLink('#include <float.h>\n#include <complex>\n','double b = 2.0;int a = _finite(b);\n'):
64        self.addDefine('HAVE__FINITE',1)
65    else:
66      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnormal(b);\n'):
67        self.addDefine('HAVE_ISNORMAL',1)
68      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnan(b);\n'):
69        self.addDefine('HAVE_ISNAN',1)
70      elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _isnan(b);\n'):
71        self.addDefine('HAVE__ISNAN',1)
72      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isinf(b);\n'):
73        self.addDefine('HAVE_ISINF',1)
74      elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _finite(b);\n'):
75        self.addDefine('HAVE__FINITE',1)
76    self.popLanguage()
77    return
78
79  def configurePrecision(self):
80    '''Set the default real number precision for PETSc objects'''
81    self.log.write('Checking C compiler works with __float128\n')
82    self.have__float128 = 0
83    if self.libraries.check('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f; logq(f);'):
84      self.log.write('C compiler with quadmath library\n')
85      self.have__float128 = 1
86      if hasattr(self.compilers, 'FC'):
87        self.libraries.pushLanguage('FC')
88        self.log.write('Checking Fortran works with quadmath library\n')
89        if self.libraries.check('quadmath','     ',call = '      real*16 s,w; w = 2.0 ;s = cos(w)'):
90          self.log.write('Fortran works with quadmath library\n')
91        else:
92          self.have__float128 = 0
93          self.log.write('Fortran fails with quadmath library\n')
94        self.libraries.popLanguage()
95      if self.have__float128:
96          self.libraries.add('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f; logq(f);')
97          self.addDefine('HAVE_REAL___FLOAT128', '1')
98
99    self.precision = self.framework.argDB['with-precision'].lower()
100    if self.precision == '__fp16':  # supported by gcc trunk
101      if self.scalartype == 'complex':
102        raise RuntimeError('__fp16 can only be used with real numbers, not complex')
103      if hasattr(self.compilers, 'FC'):
104        raise RuntimeError('__fp16 can only be used with C compiler, not Fortran')
105      self.addDefine('USE_REAL___FP16', '1')
106      self.addMakeMacro('PETSC_SCALAR_SIZE', '16')
107    elif self.precision == 'single':
108      self.addDefine('USE_REAL_SINGLE', '1')
109      self.addMakeMacro('PETSC_SCALAR_SIZE', '32')
110    elif self.precision == 'double':
111      self.addDefine('USE_REAL_DOUBLE', '1')
112      self.addMakeMacro('PETSC_SCALAR_SIZE', '64')
113    elif self.precision == '__float128':  # supported by gcc 4.6/gfortran and later
114      if self.have__float128:
115        self.addDefine('USE_REAL___FLOAT128', '1')
116        self.addMakeMacro('PETSC_SCALAR_SIZE', '128')
117      else:
118        raise RuntimeError('__float128 support not found. --with-precision=__float128 works with gcc-4.6 and newer compilers.')
119    else:
120      raise RuntimeError('--with-precision must be __fp16, single, double, or __float128')
121    self.logPrint('Precision is '+str(self.precision))
122    return
123
124  def configure(self):
125    self.executeTest(self.configureScalarType)
126    self.executeTest(self.configurePrecision)
127    return
128