Re: [R] R-Fortran question (multiple subroutines)

2010-10-25 Thread Berwin A Turlach
G'day Remko,

On Mon, 25 Oct 2010 15:33:30 +1100
Remko Duursma remkoduur...@gmail.com wrote:

 apologies if this is somewhere in a manual, I have not been able to
 find anything relevant. 

You probably have to set some appropriate flags and the information
should be somewhere in the manual of your compiler. Though, Writing R
Exensions will tell you now to change/specify flags for compilers.

 I run Windows Vista.

My condolences. :)

 But is it possible to have more than one subroutine in my source file,
 one depending on the other? 

Yes.

 I.e, my code looks something like this:
 
 subroutine f(x,y,z)
 
 call g(x,y,z)
 
 end
 
 subroutine g(x,y,z)
 
 z = x*y
 
 end
 
 calling this from R shows that subroutine g is not called. 

What do you mean with g is not called?  How did you assert this?  

If the code functions correctly, then g has to be called, either
explicitly or implicitly (e.g. if it was inlined).

Do you mean that when you compile the code and load the resulting DLL
from R, you can only call subroutine f via .Fortran but not subroutine
g? 

 The code compiled as executable works fine.

What do you mean with compiled as executable?  The snippet that you
showed would not compile as an executable as there is no main program.
So what do you mean with works fine?  That you can call the
subroutine g from the main program that you use when creating an
executable?

My guess, based on this snippet, is that your compiler notices that
subroutine g is not really needed and replaces the call to g within f
by the body of g and does not create a separate entry point for g in
the compiled object; a process known as in-lining (IIRC) and typically
used by compilers if high levels of optimisations are requested.  The
compiler does not know that you intend to call g later directly from R
via .Fortran, all it sees is the code in the file and, if high
optimisation is requested, it may feel free to rearrange the code to
stream-line it (e.g. by in-lining).  

So possible solutions could be:

1) ask for a lower level of optimisation 
2) tell the compiler not to do in-lining (flag --no-inline??)
3) put the routines into separate files, compile the files separately
   and then link all the resulting object files together into a DLL.
   AFAIK, optimising/inlining across separate files is a tricky issue
   so few, if any, compilers would do so.
4) Check whether there is an option to specify which exportable
   symbols have to be in the DLL in the end.

Cheers,

Berwin

== Full address 
Berwin A Turlach  Tel.: +61 (8) 6488 3338 (secr)
School of Maths and Stats (M019)+61 (8) 6488 3383 (self)
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway   
Crawley WA 6009e-mail: ber...@maths.uwa.edu.au
Australiahttp://www.maths.uwa.edu.au/~berwin

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R-Fortran question (multiple subroutines)

2010-10-25 Thread Duncan Murdoch

Remko Duursma wrote:

Dear R-helpers,


apologies if this is somewhere in a manual, I have not been able to
find anything relevant. I run Windows Vista.

I have some Fortran code in a subroutine, and have no problem calling
this from R with .Fortran, compiling the code either with 'R CMD
SHLIB' or independently with gfortran.

But is it possible to have more than one subroutine in my source file,
one depending on the other? Or is this not supported, or is there a
trick?
Of course, I could rewrite my code, but there are lots of subroutines...

I.e, my code looks something like this:

subroutine f(x,y,z)

call g(x,y,z)

end

subroutine g(x,y,z)

z = x*y

end


calling this from R shows that subroutine g is not called. The code
compiled as executable works fine.


There are no such limitations imposed by R.  I'd suggest your diagnosis 
of the problem is wrong.  If you can't spot the problem, please post a 
real example (simplified if possible, but not as much as the one above).


Duncan Murdoch




thanks,
Remko




-
Remko Duursma
Research Lecturer

Centre for Plants and the Environment
University of Western Sydney
Hawkesbury Campus
Richmond NSW 2753

Mobile: +61 (0)422 096908
www.remkoduursma.com

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R-Fortran question (multiple subroutines)

2010-10-25 Thread Berwin A Turlach
G'day all,

On Mon, 25 Oct 2010 06:52:15 -0400
Duncan Murdoch murdoch.dun...@gmail.com wrote:

 Remko Duursma wrote:
[...]
  I.e, my code looks something like this:
  
  subroutine f(x,y,z)
  
  call g(x,y,z)
  
  end
  
  subroutine g(x,y,z)
  
  z = x*y
  
  end
  
  
  calling this from R shows that subroutine g is not called. The code
  compiled as executable works fine.
 
 There are no such limitations imposed by R.  I'd suggest your
 diagnosis of the problem is wrong.  If you can't spot the problem,
 please post a real example (simplified if possible, but not as much
 as the one above).

Actually, it turns out that this example is simplified enough. :)

I put this snippet into a file, compiled it via R CMD SHLIB, loaded
it into R and then was very surprised about the result of .Fortran(f,
x=1.1, y=2.2, z=0.0).  

Eventually it dawned to me, Remko needs to read the table in section
5.2 of Writing R Extensions.  The simple fix in this case is to put
a double precision x, y, z at the beginning of each subroutine.  The
default precision in FORTRAN is not double precision, that's why the
code seems to work when compiled as stand alone but not when called
from R.

In general, I would recommend to start each subroutine with implicit
none (o.k., I know that this is not standard FORTRAN77 but most
compiler support that construct; the alternative, that would confrom
with the FORTRAN77 standard, is to declare everything to be implicitly
of type character and then let the fun begin) and then to explicitly
declare all variables to be of the appropriate type.

HTH.

Cheers,

Berwin

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R-Fortran question (multiple subroutines)

2010-10-25 Thread Berend Hasselman


Berwin A Turlach wrote:
 
 Remko Duursma wrote:
 [...]
  I.e, my code looks something like this:
  
  subroutine f(x,y,z)
  
  call g(x,y,z)
  
  end
  
  subroutine g(x,y,z)
  
  z = x*y
  
  end
  
  
  calling this from R shows that subroutine g is not called. The code
  compiled as executable works fine.
 
 There are no such limitations imposed by R.  I'd suggest your
 diagnosis of the problem is wrong.  If you can't spot the problem,
 please post a real example (simplified if possible, but not as much
 as the one above).
 
 Actually, it turns out that this example is simplified enough. :)
 
 I put this snippet into a file, compiled it via R CMD SHLIB, loaded
 it into R and then was very surprised about the result of .Fortran(f,
 x=1.1, y=2.2, z=0.0).  
 
 Eventually it dawned to me, Remko needs to read the table in section
 5.2 of Writing R Extensions.  The simple fix in this case is to put
 a double precision x, y, z at the beginning of each subroutine.  The
 default precision in FORTRAN is not double precision, that's why the
 code seems to work when compiled as stand alone but not when called
 from R.
 
 In general, I would recommend to start each subroutine with implicit
 none (o.k., I know that this is not standard FORTRAN77 but most
 compiler support that construct; the alternative, that would confrom
 with the FORTRAN77 standard, is to declare everything to be implicitly
 of type character and then let the fun begin) and then to explicitly
 declare all variables to be of the appropriate type.
 

I alway look for a command line option for the fortran compiler to achieve
the implicit none.
gfortran has the option -fimplicit-none. I try to always use it.
For R package building I start out with using the implicit-none option and
comment it out in the makevars only in the final stage when R warns me about
non standard options.

Berend
-- 
View this message in context: 
http://r.789695.n4.nabble.com/R-Fortran-question-multiple-subroutines-tp3009729p3010385.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] R-Fortran question (multiple subroutines)

2010-10-25 Thread Remko Duursma
Hi Berwin and all others who replied:

that did the trick, thanks for your help!


remko


 Actually, it turns out that this example is simplified enough. :)

 I put this snippet into a file, compiled it via R CMD SHLIB, loaded
 it into R and then was very surprised about the result of .Fortran(f,
 x=1.1, y=2.2, z=0.0).

 Eventually it dawned to me, Remko needs to read the table in section
 5.2 of Writing R Extensions.  The simple fix in this case is to put
 a double precision x, y, z at the beginning of each subroutine.  The
 default precision in FORTRAN is not double precision, that's why the
 code seems to work when compiled as stand alone but not when called
 from R.

 In general, I would recommend to start each subroutine with implicit
 none (o.k., I know that this is not standard FORTRAN77 but most
 compiler support that construct; the alternative, that would confrom
 with the FORTRAN77 standard, is to declare everything to be implicitly
 of type character and then let the fun begin) and then to explicitly
 declare all variables to be of the appropriate type.

 HTH.

 Cheers,

        Berwin


__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.