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