Here is a funny problem I am encountering with R and Fortran.
Consider the following Fortran subroutine:
! foo.f90
subroutine fun(n, x)
implicit none
integer, intent(in) :: n
logical, intent(in) :: x(n)
integer :: i
write(*,*) pack([(i, i=1, n)], x)
end subroutine fun
When compiled with R CMD SHLIB foo.f90 and called in R with
dyn.load("foo.so")
x <- c(TRUE ,FALSE)
f <- .Fortran("fun", 2L, as.logical(x))
I get the error message:
Fortran runtime error: Funny sized logical array
When the same subroutine is called from a Fortran program,
! test.f90
program test
implicit none
logical :: x(2)
x = [.true., .false.]
call fun(2, x)
contains
include "foo.f90"
end program test
no error occurs:
$ gfortran foo.f90 test.f90 -o test
$ ./test
1
Potential culprit: The Fortran compiler I am using for this example. I used GNU Fortran 9.2.1. I noticed the same problem with gfortran 8.3.0, but no errors with earlier versions 7.4.0, 6.5.0 and 5.5.0.
But why would the Fortran program test.f90 with the same subroutine work here? (even when the same flags used by R CMD SHLIB are used to compile foo.f90 and test.f90: -fno-optimize-sibling-calls -fpic -ggdb -O0 -Wall -Wextra -pedantic -fimplicit-none -fcheck=all -fbacktrace).
This looked funny at the beginning, but after some time playing around with this problem, this is getting frustrating...
Thanks in advance for your help.
More details:
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.1