Re: [Rd] Calling Fortran90 code from R

From: Simone Giannerini <sgiannerini_at_gmail.com>
Date: Tue, 29 Dec 2009 16:51:44 +0100

Dear Hartwig,

glad to know you workarounded your problem, please see my comments

  1. if put in a module, the exported symbol of the subroutines may change with different compilers. Actually, they change for the same compiler in different platforms. Please see the following
begin  file try.f90 *******************

MODULE TRY
CONTAINS
SUBROUTINE foo(X,M,N,S)

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

   S = sum(X);

END SUBROUTINE foo

END MODULE TRY

end  file try.f90 *******************

under Win32 with the gfortran of Rtools 2.10 I get the following

gfortran try.f90 -shared -o try.so
nm try.so

[...]
6e841280 T ___try__foo
[...]

the exported symbol is different from yours

> dyn.load("try.so")
> is.loaded("__try__foo")

[1] TRUE hence, I might be wrong but I am afraid that your code will not be portable.

2. you can avoid using modules by specifying explicit interfaces into the subroutines that use other subroutines.This is what I do in my packages. I understand it can be painful if you have to put your hands on big modules.

Kind regards

Simone

On Tue, Dec 29, 2009 at 1:44 PM, Hartwig Deneke <hartwig.deneke_at_gmail.com>wrote:

> 2009/12/29 Simone Giannerini <sgiannerini_at_gmail.com>:
> > Dear Hartwig,
> >
> > I think there are many packages that use F90 code and you should really
> be
> > able to call it by following the advices in the R-exts
> > manual. There are no major differences with F77 and you do not need a C
> > wrapper. Maybe you might start with a simple example like this one.
> >
> ....(helpful example removed)...
> > open R ************************
> > dyn.load("foo.so")
> > is.loaded("foo")
> >
> > TRUE
> Dear Simone (and others reading),
>
> first of all thanks for your helpful example. While I did something
> similar yesterday, it did give me a simple example to start from which
> worked, and find a nice approach to solving my problems. A brief
> summary of the origin of my problems, which are related to the use of
> Fortran90 module declarations.
>
> It is perfectly possible to call Fortran90 code compiled with
> gfortran. Compiling a subroutine/function "foo" will produce a symbol
> "foo_" in the dynamic library. If, however, the subroutine is
> contained in a module named "modname", the symbol will be called
> "__modname_MOD_foo". In this case, it will not be found by the
> ".Fortran" call or a call to "is.loaded", at least I have not found
> any way to do this. However, for my purposes, I have simply added a
> set of wrapper subroutines to the library (which I planned to add
> anyway for other reasons, in particular to reduce the number of
> arguments), which forwards the calls to the module functions, and
> everything seems to work all right.
>
> Kind Regards,
> Hartwig
>

-- 
______________________________________________________

Simone Giannerini
Dipartimento di Scienze Statistiche "Paolo Fortunati"
Universita' di Bologna
Via delle belle arti 41 - 40126  Bologna,  ITALY
Tel: +39 051 2098262  Fax: +39 051 232153
http://www2.stat.unibo.it/giannerini/
______________________________________________________

	[[alternative HTML version deleted]]

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 29 Dec 2009 - 15:55:27 GMT

This archive was generated by hypermail 2.2.0 : Wed 30 Dec 2009 - 09:21:18 GMT