From: HUSFELD@usmv01.usm.uni-muenchen.de (Husfeld, Dirk)
Subject: Re: For HP and IBM RISC hosts, how to get cpu times from Fortran?
Summary: Improved `second()' function based on `times()' system call (HP-UX)
Keywords: HP-UX
Organization: University Observatory Munich, FRG 
Date: Wed, 14 Oct 1992 15:58:51 GMT
X-News-Reader: VMS NEWS 1.20

In <7555@charon.cwi.nl> dik@cwi.nl writes:

> In article <1992Oct13.155347.15401@news.lrz-muenchen.de> HUSFELD@usmv01.usm.uni-muenchen.de (Husfeld, Dirk) writes:
>  > HPUX Fortran has no function for the CPU time.  You have to write your
>  > own in C.  This is the code I'm using:
>  > 
> What is wrong with the following (which I have in my source tree, although I
> have no access to HP machines quite some time)?
>       REAL FUNCTION GTIME()
>       INTEGER T(4)
>       EXTERNAL TIMES
>       CALL TIMES(T)
>       GTIME = (T(1) + T(2)) / 100.0
>       RETURN
>       END
> -- 

Hmm... there is no subroutine TIMES documented in the Fortran /9000 reference
for HP 9000 Series 700 Computers.  There is, however, a system call named
`times' which is meant to be called from C.  Quite obviously, your code
links in this system call.  It must have worked at some time (I guess) and
possibly still does, but I would recommend putting the system call into
a C function and call that, as I did with `clock()' in my previous post.
The major reason for this suggestion is the "magic" value 100.0 that you
divide into the sum.  It corresponds to the constant CLK_TCK that HP says
is "configurable" (whatever that means exactly), at least it is different
for different HP architectures.  For example, HP series 300 machines have
CLK_TCK = 50, series 800 machines use 100.  A C routine would determine
the actual value of CLK_TCK to convert into seconds.  Moreover, a C routine
would run no risk concerning type compatibilities (which the Fortran code
does).  So, here's my proposal for this function:

/*	second()  --  return CPU time in seconds elapsed in the current
			process and all its children (user and system times)
*/

#include	<unistd.h>
#include	<sys/times.h>

float	second()
{
	struct tms	*t;

	times( t );

	return ( ( (float)( t->tms_utime + t->tms_stime
			  + t->tms_cutime + t->tms_cstime
			  ) ) / sysconf( _SC_CLK_TCK ) );
}


Note that this routine sums the CPU times not only of the current process
but of all its child processes as well, to be compatible with the `clock()'
library function.  To include the child process' times into your Fortran
code, you would have to add all four elements of the array T.  To exclude
the childs from the above function, delete the line with `tms_cutime' and
`tms_cstime' in it.

Compile the second() function with
	cc -Ac -c second.c
(it is -Ac this time for compatibility mode, not -Aa for ANSI mode as in
my previous post).

In fact, I now prefer the above function over the earlier version based on
`clock()' because it doesn't suffer from the wrap-around problem.  Thanks
for bringing my attention to `times()'.


Dirk Husfeld

Institute of Astronomy and Astrophysics     Tel.:(FRG) 89  92 20 94 40
Scheinerstrasse 1                           Fax :(FRG) 89  92 20 94 27
8000 Munich 80, FRG
Email: husfeld@astronomie.physik.uni-muenchen.dbp.de     (X.400)
 or    husfeld@usm.uni-muenchen.de                       (Internet)