From sgiannerini at gmail.com  Fri Sep  9 19:04:38 2005
From: sgiannerini at gmail.com (Simone Giannerini)
Date: Fri, 9 Sep 2005 19:04:38 +0200
Subject: [Rd] A question on R memory management in .Fortran() calls under
	Windows
Message-ID: <3c12769c0509091004555400f@mail.gmail.com>

Dear R community,

I have a question on how R manages memory allocation in .Fortran()
calls under Windows.
In brief, apparently, it is not possible to allocate large matrices
inside a Fortran subroutine
  unless you pass them as arguments. If you do not act in this way
RGUI crashes with a stack overflow error and acting  on memory through
vsize nsize ppsize and memory.limit does not help at all.

****************************************************************************************
Details of the configurations on which I performed testing follow:
R 2.1.1 on WinXP Pro SP2 ITA 
PC1: AMD 64 3700+ 1GB RAM 
PC2: AMD AthlonXP 2400+ 512Mb RAM
Compaq Visual Fortran pro 6.6C
****************************************************************************************

To give an idea I attach a brief example on how to reproduce this:
Create two simple subroutines 'foo' and 'foobis' that, say, give the
sum of the elements of a matrix:

*** file foo.f90 starts
***********************************************************************
SUBROUTINE foo(X,M,N,S)
!DEC$ ATTRIBUTES DLLEXPORT,C,REFERENCE,ALIAS:'foo_' :: FOO

    IMPLICIT NONE
    integer:: M,N
    real*8:: X(M,N),S

    S  = sum(X);

END SUBROUTINE foo

SUBROUTINE foobis(M,N,S)
!DEC$ ATTRIBUTES DLLEXPORT,C,REFERENCE,ALIAS:'foobis_' :: FOOBIS

    IMPLICIT NONE
    integer:: M,N
    real*8:: X(M,N),S
    X = 1;
    S  = sum(X);

END SUBROUTINE foobis

*** file foo.f90 ends
***********************************************************************

Notice that the matrix X is an input argument in foo and an internal
variable in foobis.
After compiling and linking turn to R:
**************************************************************************
> dyn.load("foo.dll");
> is.loaded(symbol.For("foo"));
[1] TRUE
> is.loaded(symbol.For("foobis"));
[1] TRUE
> M <- 10;
> N <- 10;
> X <- matrix(1,M,N);
> .Fortran("foo",X,as.integer(M),as.integer(N),S=as.double(0))$S;
[1] 100 
> .Fortran("foobis",as.integer(M),as.integer(N),S=as.double(0))$S;
[1] 100

## no problem here with small matrices, let's increase the size

> M <- 3000;
> N <- 100;
> X <- matrix(1,M,N);
> .Fortran("foo",X,as.integer(M),as.integer(N),S=as.double(0))$S;
[1] 3e+05  ## OK


.Fortran("foobis",as.integer(M),as.integer(N),S=as.double(0))$S;
##  *** R GUI CRASHES WITH A STACK OVERFLOW ERROR ***
****************************************************************************
Any suggestion would be greatly appreciated, I apologize if this
problem has already been addressed previously,  I did not notice it.
 Many thanks in advance,
 
 Simone
______________________________________________________ 

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098248  Fax: +39 051 232153
E-mail: giannerini at stat.unibo.it



[Rd] A question on R memory management in .Fortran() calls under Windows
Simone Giannerini