Skip to content

.Fortran interface error

4 messages · Javier Garcia-Pintado, Brian Ripley, Charlie Sharpsteen

#
Hi all,
I'm preparing a package which uses .Fortran to interface a Fortran 95
function. This F95 function simply receives the name of a file from R,
opens this file and forwards its content to a F95 module, which, in turn,
makes the real computation.  The F95 module is a pre-existing one and I'm
trying to use it in its actual state.

Thus, data transfer between R and this F95 module is made through scratch
system files (binary or ASCII), and R-F95 interface simply forwards a
pathed name to this module.

OS: openSUSE 10.3 (x86_64)
R version 2.10.1 (2009-12-14)

Then, I've put a demo which makes use of the function, but after model
execution (the F95 code) finishes correctly, and all output is read  back
properly by R (again through system files), R halts, and the following
message appears:

*** glibc detected *** /usr/local/lib64/R/bin/exec/R: free(): invalid
pointer: 0x00000000008a1aa0 ***

Could you give me any hint about where to look for errors?

Just to say that if instead of the R-f95 interface,

I compile the F95 code as executable "runhydro" an call it as:
everything is OK, while compilation as a dynamic library and access to the
F95 "big" module through the simple R-F95 interface:
runfile = as.character(mainfile[1]),
                 charsfile = nchar(mainfile[1]),
                 PACKAGE = "hydroarid"
               )

is what is giving the error (I could handle you the package)

Thanks and best regards,

Javier
---
#
Please try running this under valgrind (see 'Writing R Extensions'). 
The most likely cause is that the Fortran code is corrupting its or 
R's memory.  You may need a build of R set up to instrument R 
allocations (see the manual for details).
On Tue, 13 Apr 2010, jgarcia at ija.csic.es wrote:

            

  
    
#
Hi,
I've stripped all the code, and it seems that any simple attempt to
open/close a file from fortran is the cause of the error, and the error
appears in f77 as well as in f95 code. Please, find attached a foo package
that reproduce the errors, it should build/check/install without any
problem (it does in my computer). If the code

 OPEN( UNIT=5, FILE=runfile, STATUS='OLD', ACTION='READ', &
       IOSTAT=status)
 WRITE (*,*) 'status:   ', status
 IF ( status == 0 ) THEN
   READ(5,'(A)') modelsel
   READ(5,'(A)') modelfile
   READ(5,'(A)') datdirpid
   CLOSE(5)

   WRITE (*,*) 'selected model:   ', modelsel
   WRITE (*,*) 'model input file: ', modelfile
   WRITE (*,*) 'datdirpid: ', datdirpid
 ELSE
   WRITE (*,1002) status
    1002 FORMAT (1X,'open runfile failed--status = ', I6)
   STOP
 END IF

is commented out in any of the two sources files (the f77 or the f95 one),
none of the two corresponding wrappers (as they appear in the examples)
give an error. Thus it seems clear that the operation causing it is the
OPEN/CLOSE. Without commenting out these pieces of code, everything seems
to work right but R halts after a call to the wrapper functions. I cannot
find any comment about this in the documentation. Please, could you tell
me if this is a known error?

Thanks,

Javier
---
-------------- next part --------------
A non-text attachment was scrubbed...
Name: foofortran.tar.gz
Type: application/x-gzip
Size: 2908 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20100413/90c9e4a4/attachment.gz>
#
jgarcia-2 wrote:
It looks like you may have some I/O collisions occurring.  Some Fortran
compilers treat unit 5 as being STDIN, so attempting to read from that
stream when the Fortran program is embedded may be giving R a headache.  I
would recommend either passing "modelsel", "modelfile" and "datadirpid" as
parameters from R, or reading from a file using a different unit number.

For output, instead of using write(*,*) you may need to use some of the R
output callbacks described in section 6.5.1 of "Writing R Extensions".

Hope this helps!

-Charlie



-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University