Skip to content

Use explicit kind parameters #63

Description

@rouson

PR #62 made a global switch from double precision to default real to support running do concurrent on GPUs. As suggested by @certik, a common practice is to use explicit kind parameters to allow for different precisions. As demonstrated by the aforementioned PR, a global precision change touches hundreds of lines across tens of files. For now at least, we should consider adopting the common practice of using defining named kind parameters (constants) for use throughout the code. I'm reasonably confident that if I try to do this myself, I will likely miss some of the required changes. This might be best accomplished in a (semi-)automated way with a script or with AI or with an AI-generated script.

The categories of required work include

  • Define a new module, e.g., formal_constants_m containing
    • kind parameters, e.g, fp, and
    • macros, e.g., FORMAL_REAL_KIND, that facilitate selecting the global precision at compile time
  • Throughout src, example, and test, replace
    • real with real(fp) in all declarations
    • literal constants, such as 1D0, with 1_fp
    • intrinsic conversions, such as real(foo), with real(foo, fp)
    • Array constructors, such as [real::] with [real(fp)::]
  • Define separate CI tests for each supported precision -- probably two precisions: default real and double precision. (This could have the impact of doubling the CI time.)
  • Documentation
    • Consider updating the UML diagrams

I think it's ok to leave procedure names such as scalar_3D_premultiply_real as is.

The biggest downside of the above approach is that it requires recompiling when switching precision and it precludes mixed-precision (as does the current approach merged in the aforementioned PR).

FWIW, my preference for a long-term solution would be to instead use

  1. Parameterized derived types,
  2. Templates, or
  3. Generic subprograms.

At present, however, approach 1 is a lot of work and restricts us to very recent versions of most compilers. Approach 2 is supported only by one compiler experimentally: lfortran. Approach 3 probably has no compiler support yet.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions