E3SM-Project / E3SM-Project/scorpio
Handling of real kind in chunk_cache routines in pio_nf.F90
- Dominant language
- C++
- Stars
- 22
- Forks
- 20
- Avg merge
- 12d 19m
- Merged PRs (30d)
- 1
Description
There are several instances in the Scorpio Fortran interface source code where Fortran types without kind specifications are passed directly to the C interface functions. The types of the arguments match when default kind specifications are used for the types (e.g. default kind for a REAL is REAL*4 - 4 bytes, this type is compatible with a REAL(C_FLOAT) ). However they do not match, and results in a compiler error, when the default kind of the types are changed (e.g. when a user specifies via compiler flags that by default REALs without a specific KIND specification need to be 8 bytes) .
An example:
```
integer function set_chunk_cache(iosysid, iotype, chunk_cache_size, chunk_cache_nelems, &
chunk_cache_preemption) result(ierr)
integer, intent(in) :: iosysid
integer, intent(in) :: iotype
integer(kind=PIO_OFFSET_KIND), intent(in) :: chunk_cache_size
integer(kind=PIO_OFFSET_KIND), intent(in) :: chunk_cache_nelems
real, intent(in) :: chunk_cache_preemption
interface
integer (C_INT) function PIOc_set_chunk_cache(iosysid, iotype, chunk_cache_size, &
chunk_cache_nelems, chunk_cache_preemption) &
bind(c,name="PIOc_set_chunk_cache")
use iso_c_binding
integer(c_int), value :: iosysid
integer(c_int), value :: iotype
integer(c_size_t), value :: chunk_cache_size
integer(c_size_t), value :: chunk_cache_nelems
real(c_float), value :: chunk_cache_preemption
end function PIOc_set_chunk_cache
end interface
ierr = PIOc_set_chunk_cache(iosysid, iotype, chunk_cache_size, chunk_cache_nelems, &
chunk_cache_preemption)
end function set_chunk_cache
```
The interface function expects a c_float (i.e., kind=4). but the variable passed to it has unspecified kind. If the user asks the compiler to default reals to kind=8, this generates a compiler error:
```
/home/luca/workdir/scream/scream-src/branch/externals/scorpio/src/flib/pio_nf.F90:1734:11:
ierr = PIOc_get_chunk_cache(iosysid, iotype, chunk_cache_size, chunk_cache_nelems, &
1
Error: Type mismatch in argument ‘chunk_cache_preemption’ at (1); passed REAL(8) to REAL(4)
```
For the `set_blah` functions, one can simply cast on the fly, like this
```
ierr = PIOc_set_chunk_cache(iosysid, iotype, chunk_cache_size, chunk_cache_nelems, &
real(chunk_cache_preemption,kind=c_float))
```
while for the get functions, I think you need a temporary:
```
real(kind=c_float) :: tmp
ierr = PIOc_get_chunk_cache(iosysid, iotype, chunk_cache_size, chunk_cache_nelems, &
tmp)
chunk_cache_preemption = tmp
```
I can put up a quick PR, if this seems reasonable.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.