【问题标题】:Use Fortran subroutine in R? Error: Return type mismatch在 R 中使用 Fortran 子程序?错误:返回类型不匹配
【发布时间】:2015-07-13 22:45:12
【问题描述】:

我正在尝试学习如何在 R 中使用 fortran 代码。我能够关注 this tutorial。现在,我正在尝试将其用作基础加上this fortran program to calculate pi。我用这段代码创建了一个文件 Fpi.f90:

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

什么时候

$ 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

我做错了什么?


double precision :: dboard 添加到pi 后,我得到一个不同的错误。

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

【问题讨论】:

    标签: r fortran gfortran


    【解决方案1】:

    您不使用implicit none。这是非常糟糕的!由于隐式键入 dboard 被认为是他在 pi 中的默认真实值。

    将其声明为双精度,或者如果可能使用 R,请使用模块。接口块也可用于在pi 中声明dboard

    【讨论】:

    • 谢谢。我不确定 R 是否支持 fortran 模块,我试图让一些比 f77 教程更复杂的东西工作并失败:( 在 pi 中声明 dboard 后,我得到一个不同的错误
    • 我创建了一个后续问题stackoverflow.com/questions/31395435/…。您的回答使我朝着正确的方向迈出了一步,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-09-12
    • 2013-04-16
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多