Interoperability Concepts
Kind Mapping
Match Data Widths
Interop starts by choosing Fortran kinds that correspond to C-facing data widths. The example models the choice without calling C.
Program
Play the program to choose which integer kind label is selected.
kind_mapping.f90
Replay: real traced execution (multi-file project)
program kind_mapping_demo
use iso_c_binding, only: c_int, c_long
implicit none
integer :: kind_choice
integer :: chosen_kind
character(len=12) :: label
kind_choice = 1
select case (kind_choice)
case (1)
chosen_kind = c_int
label = 'c_int'
case (2)
chosen_kind = c_long
label = 'c_long'
case default
chosen_kind = kind(0)
label = 'default'
end select
print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
program kind_mapping_demo
use iso_c_binding, only: c_int, c_long
implicit none
integer :: kind_choice
integer :: chosen_kind
character(len=12) :: label
kind_choice = 2
select case (kind_choice)
case (1)
chosen_kind = c_int
label = 'c_int'
case (2)
chosen_kind = c_long
label = 'c_long'
case default
chosen_kind = kind(0)
label = 'default'
end select
print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
program kind_mapping_demo
use iso_c_binding, only: c_int, c_long
implicit none
integer :: kind_choice
integer :: chosen_kind
character(len=12) :: label
kind_choice = 3
select case (kind_choice)
case (1)
chosen_kind = c_int
label = 'c_int'
case (2)
chosen_kind = c_long
label = 'c_long'
case default
chosen_kind = kind(0)
label = 'default'
end select
print '(A, 1X, I0)', trim(label), chosen_kind
end program kind_mapping_demo
kind_choice ← 1
8kind_choice = 19select case (kind_choice)values this step1kind_choicematched ← case (1)
8kind_choice = 19select case (kind_choice)10 case (1)values this stepcase (1)matched1kind_choicechosen_kind ← c_int
10case (1)11 chosen_kind = c_int12 label = 'c_int'values this stepc_intchosen_kindlabel ← c_int
11 chosen_kind = c_int12 label = 'c_int'13case (2)values this stepc_intlabelprint '(A, 1X, I0)', trim(label), chosen_kind
19 end select20 print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demooutputc_int 4values this stepc_intlabelc_intchosen_kind
kind_choice ← 2
8kind_choice = 29select case (kind_choice)values this step2kind_choicematched ← case (2)
8kind_choice = 29select case (kind_choice)10 case (1)values this stepcase (2)matched2kind_choicechosen_kind ← c_long
13case (2)14 chosen_kind = c_long15 label = 'c_long'values this stepc_longchosen_kindlabel ← c_long
14 chosen_kind = c_long15 label = 'c_long'16case defaultvalues this stepc_longlabelprint '(A, 1X, I0)', trim(label), chosen_kind
19 end select20 print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demooutputc_long 8values this stepc_longlabelc_longchosen_kind
kind_choice ← 3
8kind_choice = 39select case (kind_choice)values this step3kind_choicematched ← case default
8kind_choice = 39select case (kind_choice)10 case (1)values this stepcase defaultmatched3kind_choicechosen_kind ← default integer
16case default17 chosen_kind = kind(0)18 label = 'default'values this stepdefault integerchosen_kindlabel ← default
17 chosen_kind = kind(0)18 label = 'default'19end selectvalues this stepdefaultlabelprint '(A, 1X, I0)', trim(label), chosen_kind
19 end select20 print '(A, 1X, I0)', trim(label), chosen_kind21end program kind_mapping_demooutputdefault 4values this stepdefaultlabeldefault integerchosen_kind
C kinds
`c_int` and `c_long` name interoperable integer kinds.
selection
`select case` can centralize the mapping decision.
label plus kind
A trace can carry both the human label and the selected kind value.