在R中使用Fortran子例程?错误:返回类型不匹配 [英] Use Fortran subroutine in R? Error: Return type mismatch

查看:112
本文介绍了在R中使用Fortran子例程?错误:返回类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在R中使用fortran代码.我能够遵循

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

何时

$ 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后,我得到了另一个错误.

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

推荐答案

您不使用implicit none.那真是太糟糕了!由于隐式键入dboard,他被认为默认为pi内部的实数.

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

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

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

这篇关于在R中使用Fortran子例程?错误:返回类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆