xref: /petsc/config/PETSc/options/scalarTypes.py (revision d5b43468fb8780a8feea140ccd6fa3e6a50411cc)
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);\n'):
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);\n'):
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);\n'):
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);\n'):
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);\n'):
68        self.addDefine('HAVE__FINITE',1)
69    else:
70      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnormal(b);\n'):
71        self.addDefine('HAVE_ISNORMAL',1)
72      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isnan(b);\n'):
73        self.addDefine('HAVE_ISNAN',1)
74      elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _isnan(b);\n'):
75        self.addDefine('HAVE__ISNAN',1)
76      if self.checkLink('#include <math.h>\n','double b = 2.0; int a = isinf(b);\n'):
77        self.addDefine('HAVE_ISINF',1)
78      elif self.checkLink('#include <float.h>\n','double b = 2.0;int a = _finite(b);\n'):
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; logq(f);'):
88      self.log.write('C compiler 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 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 works with quadmath library\n')
95        else:
96          self.have__float128 = 0
97          self.log.write('Fortran fails with quadmath library\n')
98        self.libraries.popLanguage()
99      if self.have__float128:
100          self.libraries.add('quadmath','logq',prototype='#include <quadmath.h>',call='__float128 f; logq(f);')
101          self.addDefine('HAVE_REAL___FLOAT128', '1')
102
103    self.log.write('Checking C compiler works with __fp16\n')
104    self.have__fp16 = 0
105    if self.libraries.check('','',call='__fp16 f = 1.0, g; g = ret___fp16(f);',prototype='static __fp16 ret___fp16(__fp16 f) { return f; }'):
106      self.have__fp16 = 1
107      self.addDefine('HAVE_REAL___FP16', '1')
108
109    self.precision = self.framework.argDB['with-precision'].lower()
110    if self.precision == '__fp16':  # supported by gcc trunk
111      if self.scalartype == 'complex':
112        raise RuntimeError('__fp16 can only be used with real numbers, not complex')
113      if hasattr(self.compilers, 'FC'):
114        raise RuntimeError('__fp16 can only be used with C compiler, not Fortran')
115      if self.have__fp16:
116        self.addDefine('USE_REAL___FP16', '1')
117        self.addMakeMacro('PETSC_SCALAR_SIZE', '16')
118      else:
119        raise RuntimeError('__fp16 support not found, cannot proceed --with-precision=__fp16')
120    elif self.precision == 'single':
121      self.addDefine('USE_REAL_SINGLE', '1')
122      self.addMakeMacro('PETSC_SCALAR_SIZE', '32')
123    elif self.precision == 'double':
124      self.addDefine('USE_REAL_DOUBLE', '1')
125      self.addMakeMacro('PETSC_SCALAR_SIZE', '64')
126    elif self.precision == '__float128':  # supported by gcc 4.6/gfortran and later
127      if self.have__float128:
128        self.addDefine('USE_REAL___FLOAT128', '1')
129        self.addMakeMacro('PETSC_SCALAR_SIZE', '128')
130      else:
131        raise RuntimeError('__float128 support not found. --with-precision=__float128 works with gcc-4.6 and newer compilers.')
132    else:
133      raise RuntimeError('--with-precision must be __fp16, single, double, or __float128')
134    self.logPrint('Precision is '+str(self.precision))
135    return
136
137  def configure(self):
138    self.executeTest(self.configureScalarType)
139    self.executeTest(self.configurePrecision)
140    return
141