Ignacio
Ignacio

Reputation: 7948

Writing R package that call Fortran library

I'm trying to write an R package that calls a Fortran subroutine. I'm using Rstudio package template that creates a bunch of files and directories automatically.

In ./R/ I have a single file, Fpi.R

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

In ./src/ I have Fpi.f90

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

I also have the generic DESCRIPTION and NAMESPACE files that rstudio generates.

I can build and load the library but when I try to use it I get this error:

> library(MyPi)
> Fpi(DARTS = 100, ROUNDS = 100)
Error in .Fortran("pi", avepi = as.numeric(1), DARTS = as.integer(DARTS),  : 
  "pi" not resolved from current namespace (MyPi)

How can I fix this? Thanks!

Upvotes: 4

Views: 959

Answers (1)

Ignacio
Ignacio

Reputation: 7948

I just had to add one line to my NAMESPACE

useDynLib(Fpi)

Upvotes: 3

Related Questions