在R中使用Fortran子程序?未定义的符号

时间:2015-07-13 23:57:39

标签: r fortran

这是一个跟进to my previous question。我将Fortran代码包装在一个模块中,现在它在我运行时编译:

R CMD SHLIB ./Fortran/Fpi.f90

这是我的Fortran代码:

Module Fpi 
IMPLICIT NONE
contains 
    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, dboard

    ! 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


end module Fpi 

我试图在R中运行它:

mypi <- function(DARTS, ROUNDS) {
  dyn.load("./Fortran/Fpi.so")
  retvals <- .Fortran("pi", DARTS = as.integer(DARTS) , ROUNDS = as.integer(ROUNDS), avepi = as.numeric(1))
  return(retvals$avepi)
}

mypi(DARTS = 50000, ROUNDS = 10)

我收到此错误:

Error in dyn.load("./Fortran/Fpi.so") : 
  unable to load shared object '/home/ignacio/local/projects/PI/./Fortran/Fpi.so':
  /home/ignacio/local/projects/PI/./Fortran/Fpi.so: undefined symbol: dboard_

3 个答案:

答案 0 :(得分:2)

您的问题归结为dboard

的声明
   double precision                ::  pi_est, homepi, pirecv, pisum, dboard

您要说dboard外部功能,而不是模块程序。这解释了为什么有一个符号dboard_开始发挥作用。你想删除它:

double precision                ::  pi_est, homepi, pirecv, pisum

而是在pi中依赖于dboard的模块过程 - pi已经知道它没有此声明。

现在,除此之外,由于pi在一个模块中,因此该子例程本身会有一些名称重整。我通过使pi本身成为(C)可互操作的过程来解决这个问题。

Module Fpi 
  IMPLICIT NONE
contains 
  subroutine pi(avepi, DARTS, ROUNDS) bind(C)
    use, intrinsic :: iso_c_binding, only : c_double, c_int
    real(c_double), intent(out)   ::  avepi
    integer(c_int), intent(in)    ::  DARTS, ROUNDS
    ...

然后使用.C而不是.Fortran

您可以在模块中保留pidboard,后者甚至不需要可互操作。

答案 1 :(得分:1)

尝试在R函数中修复fortran调用的名称。你键入“pi”,它应该是“Fpi”。另外,为什么不将函数带到路径而不是在函数内传递长路径?

mypi <- function(DARTS, ROUNDS) {
  dyn.load("./Fortran/Fpi.so")
  retvals <- .Fortran("Fpi", DARTS = as.integer(DARTS) , ROUNDS = as.integer(ROUNDS), answer = as.numeric(1))
  return(retvals$answer)
}

mypi(DARTS = 50000,ROUNDS = 10)

答案 2 :(得分:1)

显然我无法将子程序包装在模块中。这个fortran代码正在运行:

subroutine dboard(darts, dartsscore)
  implicit none
  integer, intent(in)           :: darts
  double precision, intent(out) :: dartsscore
  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

dartsscore = 4.0d0*score/darts

end subroutine dboard

subroutine pi(avepi, DARTS, ROUNDS)
  implicit none
  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

interface 
   subroutine dboard(darts, dartsscore)
      implicit none
      integer, intent(in)           :: darts
      double precision, intent(out) :: dartsscore
   end subroutine dboard
end interface

! 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
  call dboard(darts, pi_est)
  ! calculate the average value of pi over all iterations
  avepi = ((avepi*i) + pi_est)/(i + 1)
end do
end subroutine pi    

这是R代码:

system("R CMD SHLIB ./Fortran/Fpi.f90")

mypi <- function(DARTS, ROUNDS) {
  dyn.load("./Fortran/Fpi.so")
  retvals <- .Fortran("pi", avepi = as.numeric(1), DARTS =  as.integer(DARTS), ROUNDS =  as.integer(ROUNDS))
  return(retvals$avepi)
}

mypi(DARTS = 50000, ROUNDS = 10)