Use Fortran subroutine in R? Error: Return type mismatch

删除回忆录丶 提交于 2019-12-16 18:03:34

问题


I'm trying to learn how to use fortran code inside R. I was able to follow this tutorial. Now, I'm trying to use that as a base plus this fortran program to calculate pi. I create a file Fpi.f90 with this code:

subroutine pi(avepi, DARTS, ROUNDS)
double precision, intent(out)   ::  avepi
integer, intent(in)             ::  DARTS, ROUNDS
integer                         ::  MASTER, rank, i, n
integer, allocatable            ::  seed(:)
double precision                ::  pi_est, homepi, pirecv, pisum

! we set it to zero in the sequential run
rank = 0
! initialize the random number generator
! we make sure the seed is different for each task
call random_seed()
call random_seed(size = n)
allocate(seed(n))
seed = 12 + rank*11
call random_seed(put=seed(1:n))
deallocate(seed)

avepi = 0
do i = 0, ROUNDS-1
  pi_est = dboard(DARTS)
  ! calculate the average value of pi over all iterations
  avepi = ((avepi*i) + pi_est)/(i + 1)
end do
end subroutine pi


double precision function dboard(darts)
integer, intent(in)           :: darts
double precision              :: x_coord, y_coord
integer                       :: score, n

score = 0
do n = 1, darts
  call random_number(x_coord)
  call random_number(y_coord)

  if ((x_coord**2 + y_coord**2) <= 1.0d0) then
  score = score + 1
  end if
end do
dboard = 4.0d0*score/darts

end function

When

$ R CMD SHLIB ./Fortran/Fpi.f90
gfortran  -fpic -g -O2 -fstack-protector-strong  -c  Fortran/Fpi.f90 -o Fortran/Fpi.o
Fortran/Fpi.f90:22.15:

pi_est = dboard(DARTS)
               1
Error: Return type mismatch of function 'dboard' at (1) (REAL(4)/REAL(8))
/usr/lib/R/etc/Makeconf:161: recipe for target 'Fortran/Fpi.o' failed
make: *** [Fortran/Fpi.o] Error 1

What am I doing wrong?


After adding double precision :: dboard to pi I get a different error.

R CMD SHLIB ./Fortran/Fpi.f90
gfortran -shared -L/usr/lib/R/lib -Wl,-z,relro -o Fortran/Fpi.so ./Fortran/Fpi.o -L/usr/lib/R/lib -lR
/usr/bin/ld: ./Fortran/Fpi.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
./Fortran/Fpi.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
/usr/share/R/share/make/shlib.mk:6: recipe for target 'Fortran/Fpi.so' failed
make: *** [Fortran/Fpi.so] Error 1

回答1:


You do not use implicit none. That is very bad! Due to implicit typing dboard is thought to he default real inside pi.

Declare it as double precision, or if possible with R, use modules. An interface block can also be used to declare dboard inside pi.



来源:https://stackoverflow.com/questions/31394755/use-fortran-subroutine-in-r-error-return-type-mismatch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!