Help to a FORTRAN newbie with KIND, please

  • Context: Fortran 
  • Thread starter Thread starter balthamossa2b
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary

Discussion Overview

The discussion revolves around the use of the KIND attribute in FORTRAN, particularly in relation to defining integer types with specific sizes. Participants explore the limitations and options available for specifying integer kinds in different compilers.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty using KIND with integers greater than 4 and seeks clarification on the correct usage.
  • Another participant suggests that the KIND keyword may only apply to REAL variables and mentions specific storage sizes for integers using INTEGER* notation.
  • A third participant shares an example from a CFD code, illustrating the use of SELECTED_INT_KIND and how it is implemented in a module for defining precision.
  • There is a mention that KIND values are compiler specific, which some participants find frustrating, but they acknowledge that this is part of the standard.
  • Another participant highlights the advantage of using a module for defining precision, allowing for easier adjustments when porting code to different architectures.

Areas of Agreement / Disagreement

Participants express differing views on the applicability of the KIND keyword to integers, with some suggesting it is limited while others provide examples of its use. The discussion does not reach a consensus on the best approach or the limitations of KIND for integers.

Contextual Notes

Participants note that the behavior of KIND may vary by compiler, and there are unresolved questions regarding the specifics of integer kind declarations.

balthamossa2b
Messages
1
Reaction score
0
Hi, I'm starting with FORTRAN (I have Microsoft Powerstation) and I was experimenting with KIND. Specifically, what I wanted was:

integer (kind=8):: list of variables



But the compiler doesn't let me use KIND with integers if kind>4, and I really need to use long integers. Can someone point out what I'm doing wrong, please?

Thanks in advance.
 
Technology news on Phys.org
I don't know this for a fact, but I believe the KIND keyword applies only to REAL variables, and the choices are 1, 2, and 3. These represent 4-byte, 8-byte, and 10-byte reals.

For integers, I believe you can specify the size of storage as INTEGER*1, INTEGER*2, INTEGER*4, and INTEGER*8. You'll need to look at your compiler documentation to get a definitive answer.
 
In a large CFD code that I use, we have a module that looks like this:
Code:
MODULE Precision_Def
  IMPLICIT NONE

  PRIVATE

  PUBLIC :: i_def,      r_def,      & ! precision for calculation
            i_def_sp,   r_def_sp,   & ! single precision (4 byte)
            i_def_dp,   r_def_dp,   & ! double precision (8 byte)
            intDef,     realDef

  INTEGER, PARAMETER :: i_def    = SELECTED_INT_KIND(8),   &
                        r_def    = SELECTED_REAL_KIND(12), &
                        i_def_sp = SELECTED_INT_KIND(9),   &
                        r_def_sp = SELECTED_REAL_KIND(6),  &
                        i_def_dp = SELECTED_INT_KIND(16),  &
                        r_def_dp = SELECTED_REAL_KIND(12), &
                        intDef   = i_def,                  &
                        realDef  = r_def


END MODULE Precision_Def
Then, when we creat variables in subroutines, etc, they look like this:
Code:
INTEGER(KIND=i_def) :: ...
REAL(KIND=r_def) :: ...
Again as mentioned, it amy be compiler specific, although we've compiled and ran using both g95 and ifort.
 
KIND values are compiler specific (which seems pretty silly but that is what the standard says). minger's answer seems like the way to go -- although I quite like the real*8 and integer*8 type declarations.
 
The nice thing about a module defining it, is e.g. if you decide you want to port over to a 64-bit machine and want to run a higher precision, you can just change the one module, rather than every single subroutine where variables are declared.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 14 ·
Replies
14
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K