[Pw_forum] portability of SCNDS subroutine in clib/cptimer.c

Axel Kohlmeyer akohlmey at cmm.chem.upenn.edu
Sun Nov 26 22:47:12 CET 2006


hi everybody,

just tried some testing on a fedora core 6 machine
using the latest QE cvs version and timings do not
work (again). after some searching, i found out that
this is due to glibc following the POSIX specs, which
have declared CLK_TCK to be obsolete and CLOCKS_PER_SEC
to always return 1000000. hence the use of times(2)
will not fly in the long run unless the subroutine is
changed e.g. as follows.

double F77_FUNC(scnds,SCNDS) ( )

/* Return the cpu time associated to the current process 
*/
{
        static struct tms T;
        static int first = 1;
        static double init_cputime = 0.0;
        static double ticks_per_second;
        double cputime;

        if( first ) {
                ticks_per_second = sysconf(_SC_CLK_TCK);
        }

        times(&T);

        cputime   = (double)(T.tms_utime) / ticks_per_second;

        if( first ) {
                first = 0;
                init_cputime = cputime;
        }
        return cputime - init_cputime;
}

furthermore, times are supposed to overflow rather frequently
(about every 72min on a current 32bit machine...), so perhaps
another source for time used, e.g. getrusage(2) might be
an alternative. according to the linux manpage the fields
ru_utime and ru_stime are specified by POSIX (apart from 
getrusage being conforming to SVr4 and 4.3BSD, i.e. rather
widely in the *nix world). the corresponding code could be:

#include <sys/time.h>
#include <sys/resource.h>
double F77_FUNC(scnds,SCNDS) ( )

/* Return the cpu time associated to the current process 
*/
{
        static struct rusage T;

        getrusage(RUSAGE_SELF, &T);

        return ((double)T.ru_utime.tv_sec + ((double)T.ru_utime.tv_usec)/1000000.0);
}

this would have the additional advantage, that out of
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>

only <sys/time.h> would be needed and potential conflicts
(as it is know to happen on some OSes) from including 
overlapping headers. 

it would be nice, if people running on non-linux machines
could verify that this change is portable across all of 
their platforms. any comments or suggestions are appreciated.

ciao,
   axel.

-- 
=======================================================================
Axel Kohlmeyer   akohlmey at cmm.chem.upenn.edu   http://www.cmm.upenn.edu
   Center for Molecular Modeling   --   University of Pennsylvania
Department of Chemistry, 231 S.34th Street, Philadelphia, PA 19104-6323
tel: 1-215-898-1582,  fax: 1-215-573-6233,  office-tel: 1-215-898-5425
=======================================================================
If you make something idiot-proof, the universe creates a better idiot.




More information about the Pw_forum mailing list