- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - OpenVMS
- >
- Re: FORTRAN interface to CRTL functions
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 12:43 AM
тАО02-03-2011 12:43 AM
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).
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 05:18 AM
тАО02-03-2011 05:18 AM
Re: FORTRAN interface to CRTL functions
===============================================
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 06:21 AM
тАО02-03-2011 06:21 AM
Re: FORTRAN interface to CRTL functions
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?)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 06:42 AM
тАО02-03-2011 06:42 AM
Re: FORTRAN interface to CRTL functions
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 10:06 AM
тАО02-03-2011 10:06 AM
Re: FORTRAN interface to CRTL functions
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-03-2011 10:14 AM
тАО02-03-2011 10:14 AM
Solutionyes, 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2011 04:47 AM
тАО02-04-2011 04:47 AM
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 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2011 05:39 AM
тАО02-04-2011 05:39 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2011 06:40 AM
тАО02-04-2011 06:40 AM
Re: FORTRAN interface to CRTL functions
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-04-2011 10:02 AM
тАО02-04-2011 10:02 AM
Re: FORTRAN interface to CRTL functions
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.