-
Notifications
You must be signed in to change notification settings - Fork 108
Have fpm define precision #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Great question. I don't know, I am hoping we will encourage the community to use stdlib to get precision. Fpm has the "power" to do anything, but the question is how it would work and if it makes sense. Like the promoting of single precision to double? |
Yes, the entire situation is kind of messy. I have seen some codes which rely on the compiler flags such as In the Fortran METIS interface I used the C preprocessor: #ifdef REAL64
integer, parameter, public :: real_t = c_double
#else
integer, parameter, public :: real_t = c_float
#endif to allow users to select the precision depending on the version of METIS installed on their system. I think it would be good if we could establish some guidelines for package developers whether such precision choices are responsibility of the package developer or the package user, and whether it should be done by 1) a preprocessor (C, fypp) + build system, or 2) the package manager. |
The Discourse Thread (https://fortran-lang.discourse.group/t/two-modules-with-the-same-name-in-one-program/4116) touched upon the issue of duplication of precision modules. What are the existing practices for defining precision?
For most type of numerical work, I'd argue option 3 is preferred. If one knows the code should interoperate with Python, R, MATLAB, etc. on a specific platform, the precision can also be picked accordingly. In C or C++ we can match our choice of precision with a To achieve a level of homogeneity and composability between packages which provide reusable libraries I propose the following: Each package provider should include in their project a module, such as ! <my_package>_precision.F90
module <my_package>_precision
implicit none
public :: wp
#ifdef __FPM__
include "fpm/precision.inc" ! path reserved/manipulated by fpm
integer, parameter :: wp = fpm_wp
#else
! non-FPM build system, e.g. standalone Make or CMake package
integer, parameter :: wp = kind(1.0d0)
#endif
end module <my_package>_precision When FPM is the build system, it will pass the ! precision.inc (note this file works in both fixed- and free-form)
integer fpm_rp, fpm_wp, fpm_hp
parameter(fpm_rp=kind(1.0e0),fpm_wp=kind(1.0d0))
parameter(fpm_hp=selected_real_kind(33)) I imagine some package authors may prefer their own parameter names, in this case, they can over-ride them: ! <my_package>_kinds.F90
module <my_package>_kinds
! I'm one of those people who conflate storage width and precision, mind your own business!
use, intrinsic :: iso_fortran_env, only: real64
implicit none
private
public :: rk
#ifndef __FPM__
! real programmers use rk, wp is for quiche-eaters,
integer, parameter :: rk = real64
#else
! but since I <3 the Fortran-lang community, let's do it your way too
include "fpm/precision.inc"
integer, parameter :: rk => fpm_wp
#endif
end module <my_package>_kinds C <my_package>_precision.inc
C I'm a fixed-form person, but still like to use fpm
include "fpm/precision.inc"
integer wp
parameter(wp=fpm_wp) Further ideas/questions:
See also: |
If relying on the preprocessor is not desirable (although J3 opened a forum on the preprocessor - https://mailman.j3-fortran.org/pipermail/j3/2022-August/013845.html), we could rely on "standard" filenames instead. |
Recent Fortran language resources usually recommend defining a module with constants defining the precision that are later reused throughout the code, for example:
These kinds of modules are duplicated throughout libraries. This can potentially lead to incompatibilities, i.e. if library 1 uses single precision as default, and library 2 uses double precision. The user is then faced with the problem of either adapting library 1, or making library 2 use the precision module from library 1.
Would it make sense to have some mechanism to give fpm the "power" of enforcing a certain default precision? Hopefully in the long-term most Fortran programmers would default to just using constants from the stdlib precision module. This might not always be enough (i.e. interfacing with C, or perhaps using fpm on some non x86_64 architectures).
The text was updated successfully, but these errors were encountered: