Skip to content

memory limitation with Fortran interface

2 messages · Guillot Gilles, Göran Broström

#
Hi,

I'm using R 7.0 under Linux as a programming interface to Fortran (g77 
v0.5.24).
Basically, what I want to do is to call a fortran subroutine of mine 
which performs MCMC computations.
Apparently I'm getting into memory management problems.

To track the problem I wrote  the following small Fortran subroutine 
(saved as test.f) : 

      subroutine test(n,p)
      implicit none
      integer n,p,i,j
      real x(n,p)
      do i=1,n
         do j=1,p
            x(i,j) = 0
            enddo
         enddo
      end

I compiled it by : 
g77 -c test.f

then I called it from R with :

n <- 10000
p <- 1000
system("R CMD SHLIB ~/test.o")
dyn.load("~/test.so")
out.res<- .Fortran("test",
                   as.integer(n),
                   as.integer(p)
                   )

causing R breakdown with the following message : 

Segmentation fault

I don't understand why this subroutine causes segmentartion fault 
as its execution requires much less memory than what is existing  on my 
machine (RAM 512 Mo RAM + swap 512 Mo ).

More generally, it seems that memory limitations are stronger
when calling fortran code from R than when executing 
the same Fortran subroutine from a bash command line.


Gilles
#
On Mon, Jan 19, 2004 at 12:19:06PM +0100, Guillot Gilles wrote:
^^^^^^^^^^^

Two errors here:

1. You must allocate memory for x in the calling R function and have x as
an argument to 'test'

2. Use double precision ('double' in R)

Another recommendation is to write an  R  package for tasks like yours.
It is easier than you may think; see 'Writing R extensions'.

G?ran