|
[Sponsors] |
Switching between single and double precision in Fortran |
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
November 8, 2020, 19:43 |
Switching between single and double precision in Fortran
|
#1 |
Senior Member
Sayan Bhattacharjee
Join Date: Mar 2020
Posts: 495
Rep Power: 8 |
I want to write fortran 90 or fortran 2003 code and be able to select between either single/double precision at compile time.
In C++ it is extremely easy to do this using template meta-programming. Code:
template<typename T> T sum(T a, T b) { return a+b; } However I have very little idea on how to implement such a generic function in Fortran. I can select between single and double precision for basic arithmetic using selected_real_kind method and using real(xp) to define the required precision. Code:
! Define single & double precision integer, parameter :: sp = selected_real_kind(6, 37) integer, parameter :: dp = selected_real_kind(15, 307) ! The selected precision level integer, parameter :: xp = selected_real_kind(precision(1.0_dp)) Code:
! Constant fractions real(xp), parameter :: & half = 0.5_xp, & third = 1.0_xp / 3.0_xp, & fourth = 1.0_xp / 4.0_xp, & fifth = 1.0_xp / 5.0_xp, & sixth = 1.0_xp / 6.0_xp So, I tried to define generic interfaces to mathematical functions like : Code:
!==-------------------------------------------------------------------------==! !{{{ > MODULE : NCX_GENERIC_MATH !==-------------------------------------------------------------------------==! ! - Generic interfaces to mathematical functions !==---- -----==! module ncx_generic_math use ncx_precision private public :: sqrt_gn, abs_gn !==----------------- -----------------==! ! > INTERFACE : SQRT_GN !==---- -----==! ! - Generic interface for SQRT !==---- -----==! interface sqrt_gn pure function sqrt_dp(x) use ncx_precision, only : dp implicit none real(dp), intent(in) :: x real(dp) :: sqrt_dp end function pure function sqrt_sp(x) use ncx_precision, only : sp implicit none real(sp), intent(in) :: x real(sp) :: sqrt_sp end function end interface Code:
pure function sqrt_dp(x) use ncx_precision, only : dp implicit none real(dp), intent(in) :: x real(dp) :: sqrt_dp sqrt_dp = dsqrt(x) end function pure function sqrt_sp(x) use ncx_precision, only : sp implicit none real(sp), intent(in) :: x real(sp) :: sqrt_sp sqrt_sp = sqrt(x) end function I can use sqrt_gn(9.0_xp) to get the correct square root value for my preferred precision and the code works just fine. But I have some questions.... QUESTIONS : + Is there a better way to do this? <-- MOST IMPORTANT REQUEST + Is there any performance loss due to using this indirection through the interface and then to the pure functions? |
|
Tags |
fortran 90, solver deveopment |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Warning message C4133 while compiling | Arminius | Fluent UDF and Scheme Programming | 0 | October 2, 2017 11:44 |
Interpret-Real Gas UDF | ndabir | Fluent UDF and Scheme Programming | 3 | November 18, 2015 07:29 |
Single precision better than double precision? | 140raiders | CFX | 1 | July 30, 2015 02:32 |
Parallel User Defined Real Gas Model | aeroman | FLUENT | 4 | July 1, 2015 06:09 |
Double precision & User Fortran | Martijn | CFX | 3 | April 4, 2009 05:43 |