system_clock only returns count=0

33 views
Skip to first unread message

John McFarland

unread,
Jun 12, 2009, 3:23:33 PM6/12/09
to gg95
Hi, I'm having trouble using the system_clock intrinsic with g95: it
is only returning count=0. I get the same behavior on Linux x86 and
on Windows x86 under MinGW. Here is my test program:

--------------------------------------------------------------
[sys_clock]$ g95 --version | head -n1
G95 (GCC 4.0.3 (g95 0.92!) May 5 2009)
[sys_clock]$ cat prog.f90
PROGRAM prog
INTEGER :: clock, rate, cmax

CALL SYSTEM_CLOCK(clock,count_rate=rate,count_max=cmax)
PRINT*, 'size of int:', sizeof(clock)
PRINT*, 'count:', clock
PRINT*, 'rate:', rate
PRINT*, 'max:', cmax
END PROGRAM prog
[sys_clock]$ g95 prog.f90
[sys_clock]$ a.out
size of int: 4
count: 0
rate: 10000
max: 2147483647
----------------------------------------------------------------------

Am I doing something wrong here?

Thanks,
John

jjt-fds

unread,
Jun 12, 2009, 3:34:39 PM6/12/09
to gg95
Try the following.

PROGRAM prog
INTEGER :: clock, rate, cmax, i, j
REAL(8) :: D

DO i = 1, 5
CALL SYSTEM_CLOCK(clock,count_rate=rate,count_max=cmax)
PRINT*, 'size of int:', sizeof(clock)
PRINT*, 'count:', clock
PRINT*, 'rate:', rate
PRINT*, 'max:', cmax
!!!!!!!!!!!!!!!!!!!
!! WORK A LITTLE !!
!!!!!!!!!!!!!!!!!!!
DO j = 1, 999
D = SIN(D) +0.25
END DO
END DO
END PROGRAM prog


-Javier

John McFarland

unread,
Jun 12, 2009, 3:47:26 PM6/12/09
to gg95
Thanks, I tied that, and my output looks like:
[sys_clock]$ ./a.out
count: 0
count: 1
count: 2
count: 2
count: 3

So it looks like it is counting. However, I am trying to use this to
randomize the random number seed as described at
http://gcc.gnu.org/onlinedocs/gfortran/RANDOM_005fSEED.html, in which
case this will not work. I am aware that with g95, calling random_seed
() without arguments will randomize the seed, but this does not work
on certain other compilers (e.g. gfortran, which just sets the seed to
a "default" value).

I guess my real question is whether or not there is a compiler-
independent mechanism for randomizing the seed?

John

John McFarland

unread,
Jun 12, 2009, 4:02:50 PM6/12/09
to gg95
Also from reading here:
http://www.ncsa.uiuc.edu/UserInfo/Resources/Hardware/IBMp690/IBM/usr/share/man/info/en_US/xlf/html/lr371.HTM,
it states that "The initial value of COUNT depends on the current
value of the processor clock in a range from 0 to COUNT_MAX." It
seems like that by always starting the count at 0 when you make the
first call to system_clock, g95 is not producing the correct behavior.

jjt-fds

unread,
Jun 12, 2009, 4:15:18 PM6/12/09
to gg95
You can search among system files to get a "number" that changes from
run to run. Use INQUIRE, if the file is found, open it, read a
number, then return with this number. For example, you can search for
the file

/proc/uptime

If it's there then it will contain different numbers at different
times. With enough research you can write a subroutine that can look
for standard files on many types of OS's without ever having to call a
nonstandard subroutine such as system().

-Javier

Evangelos Bertakis

unread,
Jun 15, 2009, 7:27:03 AM6/15/09
to gg...@googlegroups.com
How about using DATE_AND_TIME() to construct a seed? The output of the
function is bound to be different every time you run the program.

Evangelos

John McFarland

unread,
Jun 15, 2009, 11:59:55 AM6/15/09
to gg95
Thanks, this is also what Andy Vaught suggested, and we are now using
it. See here: http://gcc.gnu.org/onlinedocs/gcc-4.0.2/gfortran/DATE_005fAND_005fTIME.html.
This function returns milliseconds, seconds, minutes, etc., and can
easily be used to generate a seed. Works with both g95 and gfortran.
Reply all
Reply to author
Forward
0 new messages