[Rd] Calling Fortran90 code from R

2009-12-29 Thread Hartwig Deneke
Dear all,

I am currently trying to create a package wrapping Fortran90 code, the
RRTMG radiative transfer model( http://rtweb.aer.com/rrtm_frame.html).
I am doing this on a Linux workstation, with gcc/gfortran, in case
this matters. The code heavily relies on F90 features, in particular
modules, which seem to clash with R's assumptions for calling compiled
code.

What I have done:
-compiled the files to mod/object files using gfortran
-am able to create a dynamic link library "librrtmg.so"

Further experiments:
- dyn.load('librrtmg.so') works
-. with "objdump -T librrtmg.so", I have found the following function
symbols to call:
1. __rrtmg_lw_init_MOD_rrtmg_lw_ini (the initialization of the code)
2. __rrtmg_öw_rad_MOD_rrtmg_lw (the actual main function I want
to call to do the calculations)
- These symbols are found via "is.loaded", but only without specifying
"type='FORTRAN'" (I have experimented with upper/lowercase letters as
well as dropping the leading underscores)
- I have not been able to call these routines via the ".Fortran" interface.
- I do managed to call a simple F90 subroutine (with similar argument
list, for experimentation) via the ".C" interface. However, I am not
able to  pass 2D field of type "DOUBLE", but always get a crash.

Is there any official way for calling Fortran90 libraries which I
missed (I have both searched through the documentation and this list)?
Any advice how to go about this? My best idea at the moment seems to
be to write a simple C wrapper for the Fortran routines, and call the
wrapper from R. If needed, I can also post some code from my
experiments. I'd actually like to get something which could be
submitted to CRAN out of this (are there any other packages depending
on Fortran90 code?)

Thanks in advance for any help,
Hartwig Deneke

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Calling Fortran90 code from R

2009-12-29 Thread Simone Giannerini
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.

start file foo.f90 ***

SUBROUTINE foo(X,M,N,S)

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

   S  = sum(X);

END SUBROUTINE foo

end  file foo.f90 ***

compile:
gfortran  -shared foo.f90 -o foo.so

open R 
dyn.load("foo.so")
is.loaded("foo")

TRUE

m <- matrix(1,nrow=4,ncol=3)
storage.mode(m) <- "double"
S <- double(1)
 .Fortran("foo",m,nrow(m),ncol(m),S)

[[1]]
 [,1] [,2] [,3]
[1,]111
[2,]111
[3,]111
[4,]111

[[2]]
[1] 4

[[3]]
[1] 3

[[4]]
[1] 12


ciao

Simone


On Tue, Dec 29, 2009 at 10:26 AM, Hartwig Deneke
wrote:

> Dear all,
>
> I am currently trying to create a package wrapping Fortran90 code, the
> RRTMG radiative transfer model( http://rtweb.aer.com/rrtm_frame.html).
> I am doing this on a Linux workstation, with gcc/gfortran, in case
> this matters. The code heavily relies on F90 features, in particular
> modules, which seem to clash with R's assumptions for calling compiled
> code.
>
> What I have done:
> -compiled the files to mod/object files using gfortran
> -am able to create a dynamic link library "librrtmg.so"
>
> Further experiments:
> - dyn.load('librrtmg.so') works
> -. with "objdump -T librrtmg.so", I have found the following function
> symbols to call:
>1. __rrtmg_lw_init_MOD_rrtmg_lw_ini (the initialization of the code)
>2. __rrtmg_öw_rad_MOD_rrtmg_lw (the actual main function I want
> to call to do the calculations)
> - These symbols are found via "is.loaded", but only without specifying
> "type='FORTRAN'" (I have experimented with upper/lowercase letters as
> well as dropping the leading underscores)
> - I have not been able to call these routines via the ".Fortran" interface.
> - I do managed to call a simple F90 subroutine (with similar argument
> list, for experimentation) via the ".C" interface. However, I am not
> able to  pass 2D field of type "DOUBLE", but always get a crash.
>
> Is there any official way for calling Fortran90 libraries which I
> missed (I have both searched through the documentation and this list)?
> Any advice how to go about this? My best idea at the moment seems to
> be to write a simple C wrapper for the Fortran routines, and call the
> wrapper from R. If needed, I can also post some code from my
> experiments. I'd actually like to get something which could be
> submitted to CRAN out of this (are there any other packages depending
> on Fortran90 code?)
>
> Thanks in advance for any help,
> Hartwig Deneke
>
> __
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



-- 
__

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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] seq.int broken (seq as well) (PR#14169)

2009-12-29 Thread oehl_list
Full_Name: Jens Oehlschlägel
Version: 2.10.1
OS: Windows XP
Submission from: (NULL) (156.109.18.2)


# fine as expected from help page: 
# "from+by, ..., up to the sequence value less than or equal to to"
# thus 1+10=11 is not in
> seq.int(1L, 10L, by=10L)
[1] 1

# of course 1+1e7 should also not be in
# but is: wrong
> seq.int(1L, 1e7L, by=1e7L)
[1] 1e+00 1e+07

# since we use seq.int AND are within integer range, rounding should not be an
issue


> version
   _
platform   i386-pc-mingw32  
arch   i386 
os mingw32  
system i386, mingw32
status  
major  2
minor  10.1 
year   2009 
month  12   
day14   
svn rev50720
language   R
version.string R version 2.10.1 (2009-12-14)

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Calling Fortran90 code from R

2009-12-29 Thread Hartwig Deneke
2009/12/29 Simone Giannerini :
> 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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Calling Fortran90 code from R

2009-12-29 Thread Simone Giannerini
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 wrote:

> 2009/12/29 Simone Giannerini :
> > 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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Outage in Copenhagen tomorrow

2009-12-29 Thread Peter Dalgaard

This came in at 11:21 today:

"From 30/12-2009 at 15.30 untill 31/12-2009 at 08.00, ALL IT services at 
SUND-IT will be shut down!


ALL forms of electronic communication will be affected at SUND; VPN, 
Webmail, Mail, Print, Network drives, Servers ie.
Furthermore, Panum, CSS, Teilum, Museion and Biocenteret will have no 
access to the Internet.


The time of restoration of normal IT services, will be updated at 
http://it.sund.ku.dk the 30. December at 20.00 and 22.00.


This is a planned upgrade of firewalls, which can’t be implemented 
without impacting IT services."


(Timings are CET)

Apart from my personal activities (mail and remote access to servers and 
desktop systems), this is also going to block access to 
bugs.r-project.org. Hopefully, things will return to normal afterwards.


--
   O__   Peter Dalgaard Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark  Ph:  (+45) 35327918
~~ - (p.dalga...@biostat.ku.dk)  FAX: (+45) 35327907

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel