Operating System - OpenVMS
1751880 Members
5478 Online
108783 Solutions
New Discussion юеВ

FORTRAN interface to CRTL functions

 
SOLVED
Go to solution
Michael Menge
Frequent Advisor

FORTRAN interface to CRTL functions

VMS V8.4, Fortran V8.2 and C V7.3 on Alpha:

I want to call the CRTL function
struc tm *gmtime (const time_t timer);
from a Fortran program. I tried some versions (with/without INTERFACE, EXTERNAL etc.) but didn't find the right one. For debugging purpose I wrote a dummy c-function ggtime
struc tm *ggtime (const time_t timer);
with only a printf() for the argument timer to be able to see what Fortran is passing to the c-function.
Defining in Fortran the c-function as a subroutine or an INT*4 function (only to debug the argument), the printf() shows the right value; Fortran puts the argument in the first position of the argument list and returns the function value per register.
But defining the c-function as a TYPE, the argument list is built different (the return value or its address is put somewhere in the argument list), so the c-function doesn't get the correct argument and printf() show a wrong value. The argument lists for a Fortran TYPE function and a c struc function differs right?
Do I have to build a wrapper c subroutine or can I build the right interface to the c function in Fortran directly (via !DEC$ ATTRIBUTES etc.)?
Calling the CRTL function
time_t mktime (struc tm *timeptr)
from Fortran via decc$mktime works fine, the argument list matches between Fortran and C (tm is passed by reference as the first argument, time_t is passed back as integer*4 in the r0 register).
34 REPLIES 34
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions

This one, using an interface to gmtime_r as a subroutine seems to work:

===============================================
Program test_gmtime
USE tm_type

INTERFACE
! FUNCTION gmtime_f(t,tm_p)
SUBROUTINE gmtime_f(t,tm_p)
USE tm_type
INTEGER , INTENT(IN) :: t
type(TM), INTENT(OUT) :: tm_p
! type(TM) , pointer :: gmtime_f
! END FUNCTION
END subroutine
END INTERFACE

type (TM) :: tm_p
type (TM), pointer :: tm_r

! tm_r=gmtime_f(0,tm_p)
call gmtime_f(0,tm_p)

print *, ' ',tm_p%tm_year,tm_p%tm_mon,tm_p%tm_mday,tm_p%tm_hour,tm_p%tm_min
END program test_gmtime
=============================================
! where gmtime_f.c is:
#include
struct tm *gmtime_f (const time_t *timer, struct tm *result) {
return gmtime_r (timer,result);
}
=============================================
tm_type module is:

MODULE TM_TYPE
PUBLIC
TYPE :: TM
INTEGER tm_sec ! seconds after the minute [0, 61]
INTEGER tm_min ! minutes after the hour [0, 59]
INTEGER tm_hour ! hours since midnight [0, 23]
INTEGER tm_mday ! day of the month [1, 31]
INTEGER tm_mon ! months since January [0, 11]
INTEGER tm_year ! years since 1900
INTEGER tm_wday ! days since Sunday [0, 6]
INTEGER tm_yday ! days since January 1 [0, 365]
INTEGER tm_isdst ! Daylight Saving Time flag
INTEGER tm_gmtoff ! offset from UTC in seconds
INTEGER fill1 !to account for newer struct tm
INTEGER fill2
END TYPE TM
END MODULE TM_TYPE

=============================================

But the version to use gmtime as a function returning a pointer to type TM seems not to work:
Either my pointer assignement syntax is wrong , or F90 pointer types are not compatible with C pointers.

integer,pointer:: probably is not the same as
c_intptr_t in iso_c_binding.

http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions

And to the question if Fortran(90) can use a C function returning a pointer: it apparently is not VMS specific. It needs ISO_C_BINDING, see this discussion:
http://stackoverflow.com/questions/3852790/fortran-interface-to-call-a-c-function-that-return-a-pointer

(and no, VMS Fortran 8.2 does not yet have iso_c_binding, no?)
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions

A Fortran 2003 version would probably look like:

PROGRAM test_gmtime
USE tm_type
USE, intrinsic :: iso_c_binding, only : c_ptr,c_f_pointer

IMPLICIT NONE

INTERFACE
FUNCTION gmtime_f(t,tm_p)
USE, intrinsic :: iso_c_binding, only : c_ptr
USE tm_type
INTEGER , INTENT(IN) :: t
type(TM), INTENT(OUT) :: tm_p
type(c_ptr) :: gmtime_f
END FUNCTION
END INTERFACE

type (TM) :: tm_p
type (TM), pointer :: tm_r
type(c_ptr) :: c_p

c_p= gmtime_f(0,tm_p)
call c_f_pointer(c_p, tm_r) !convert C- to Fortran-pointer
print *, ' ',tm_p%tm_year,tm_p%tm_mon,tm_p%tm_mday,tm_p%tm_hour,tm_p%tm_min
print *, ' ',tm_r%tm_year,tm_r%tm_mon,tm_r%tm_mday,tm_r%tm_hour,tm_r%tm_min
END PROGRAM test_gmtime

The c_f_pointer call makes the difference!
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions

I just made a test of the F2003 version on
GNU Fortran (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839]

and it works perfectly, returning the tm structure both in the passed argument structure, and in the returned C pointer to a tm structure.

Remember the difference between C pointers and Fortran POINTER type:
C pointers in practice are simply addresses,
while Fortran POINTER types are opaque structures. They contain the address of the pointee, the association status, and probably in addition some description of type and size.
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor
Solution

Re: FORTRAN interface to CRTL functions

>Do I have to build a wrapper c subroutine or >can I build the right interface to the c >function in Fortran directly (via !DEC$ >ATTRIBUTES etc.)?

yes, that's why I wrapped it in gmtime_f.
The reason is, that decc$gmtime(_r) exists in CRTL, but apparently returns nothing.
Looking into time.h, it shows that gmtime_r is effectively defined as either
__utc_gmtime(_r) or
__utc_localtime_(r)
depending on some macro definitions.
http://www.mpp.mpg.de/~huber
Michael Menge
Frequent Advisor

Re: FORTRAN interface to CRTL functions

For the VMS part we are using VMS Fortran V8.2, so there isn't any ISO_C_BINDING right?
As mentioned the c-function decc$mktime works fine with fortran and returns the time_t value(INTEGER*4) in the same way as a fortran function. I wonder in which way the c-compiler returns the tm pointer as the function result?
For the fortran pointer is an opaque structure and not the same as a c pointer, perhaps the fortran POINTER statement POINTER (tm_p,tm) could be used. This kind of pointer works like in c and is also available on our other platform SLES with the inter compiler.
Also when using the !DEC$ atttribute C directive for the c funtion name in the fortran interface definition, the compiler should be able to handle the return value of the c function in the correct manner.
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions


[
As mentioned the c-function decc$mktime works fine with fortran and returns the time_t value(INTEGER*4) in the same way as a fortran function. I wonder in which way the c-compiler returns the tm pointer as the function result?
]

For scalars (integer,real,logical) it is not a problem, C and Fortran return the value in a register.
C also returns pointers simply in a register (an address).

If You like dirty programming, then You can declare the C function simply as INTEGER*4 or INTEGER*8 and pass the function return value as an argument to another subroutine/function expecting a pointer to a structure or array.
This is basically what iso_c_binding provides in subroutine c_f_pointer.


[
For the fortran pointer is an opaque structure and not the same as a c pointer, perhaps the fortran POINTER statement POINTER (tm_p,tm) could be used. This kind
of pointer works like in c and is also available on our other platform SLES with the inter compiler.
]

This is the old pre-f90 non-standard type of pointer (called "Cray pointers", because it stems from Cray-Fortran).
I doubt is is directly compatable with C pointer return, but if You tested it, so be it...

[
Also when using the !DEC$ atttribute C directive for the c funtion name in the fortran interface definition, the compiler should be able to handle the return value of the c function in the correct manner.
]

Maybe, but then again without iso_c_binding the result is nothing but some integer type containing an address, You can pass the value to procedures as above, but can't use it as a F90 POINTER type without a conversion routine like c_f_pointer().

In the case of gmtime_r it is not necessary.
For other kind of C RTL functions such a kind of bricolage might be necessary; although I find it much more cleaner to write a Fortran wrapper, e.g. in the style of the Posix Fortran IPXF* and PXF* routines, which pass everything as subroutine arguments, not via function return.

Some of my examples:
http://www.mpp.mpg.de/~huber/util/
see *PXF* and *_f source files.
http://www.mpp.mpg.de/~huber
Hoff
Honored Contributor

Re: FORTRAN interface to CRTL functions

I'd suggest implementing this with the Sun or GNU Fortran gmtime intrinsic interface:

http://gcc.gnu.org/onlinedocs/gfortran/GMTIME.html

This if somebody doesn't already have that coded and posted somewhere, given Sun and GNU Fortran code does tend to get ported around to other Fortran compilers.

If this is a more generic request for a time value for logging or such, then there are system services and RTL calls that can and do fetch values useful for timestamps, including sys$getutc and friends.
Joseph Huber_1
Honored Contributor

Re: FORTRAN interface to CRTL functions

[ GNU Fortran GMTIME ]

Yes of course, that's exactly what my gmtime_f is for VMS:
call gmtime_f(0,tm_p)

I just named it differently, because it could conflict with gmtime, if linked to the VAXC RTL.

In my first reply I just added a F90 INTERFACE, one can use it without, and instead of the TM type one can simply pass an integer array in F77.
http://www.mpp.mpg.de/~huber