This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
Re: Random seed initialization
- From: Tobias Burnus <burnus at net-b dot de>
- To: Angelo Graziosi <angelo dot graziosi at alice dot it>
- Cc: Janus Weil <janus at gcc dot gnu dot org>, fortran <fortran at gcc dot gnu dot org>, dave dot korn dot cygwin at gmail dot com
- Date: Thu, 11 Apr 2013 11:58:36 +0200
- Subject: Re: Random seed initialization
- References: <51603400 dot 3030705 at alice dot it> <CAKwh3qgG=pPbKyE6YT8H9++E_74uKmFC0UKG+Q3cW8bxYHVHRw at mail dot gmail dot com> <51603FEF dot 9020300 at alice dot it> <516042C9 dot 9010002 at alice dot it> <CAKwh3qgKztRYUGh-OjsRatsEqK=TmAF2+62S3jQfd8DkhUYi-Q at mail dot gmail dot com> <51604788 dot 3020603 at alice dot it> <CAKwh3qg0x2=b+QpKvzGSU36sKc_C6BcUs13mT0=-jxG-HnaU4g at mail dot gmail dot com> <51667E13 dot 7040300 at alice dot it>
Angelo Graziosi wrote:
A new GCC 4.7.2 has been released
(http://cygwin.com/ml/cygwin/2013-04/msg00170.html),
but the problems described in this thread still remain.
Can you try the attached program? Run it a couple of time and check the
output. On my system, I can simply use:
$ gcc test.c && ./a.out
gettimeofday: err=0, secs=1365674085, usecs=67639
clock_gettime: err=0, secs=1365674085, usecs=67683
time: err=0, secs=1365674085, usecs=0
clock_gettime: err=0, secs=9343, nsecs=0, tck=1000000000
However, depending on the system, you might need to add extra libraries
(e.g. "-lrt" with older glibc) or you have to disable some of the tests
if they are not supported.
I think the test covers all possibilities from libgfortran but I might
have missed one. (One known exception is trying whether the weak binding
for librt's function works.)
Tobias
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#define __USE_POSIX199309 1
#include <time.h>
/* #ifdef HAVE_GETTIMEOFDAY */
static int
gf_gettime_1 (time_t * secs, long * usecs)
{
struct timeval tv;
int err;
err = gettimeofday (&tv, NULL);
*secs = tv.tv_sec;
*usecs = tv.tv_usec;
return err;
}
/* #elif defined(HAVE_CLOCK_GETTIME) */
static int
gf_gettime_2 (time_t * secs, long * usecs)
{
struct timespec ts;
int err = clock_gettime (CLOCK_REALTIME, &ts);
*secs = ts.tv_sec;
*usecs = ts.tv_nsec / 1000;
return err;
}
/* #else */
static int
gf_gettime_3 (time_t * secs, long * usecs)
{
time_t t = time (NULL);
*secs = t;
*usecs = 0;
if (t == ((time_t)-1))
return -1;
return 0;
}
/* #ifdef HAVE_CLOCK_GETTIME */
static int
gf_gettime_mono (time_t * secs, long * nanosecs, long * tck)
{
int err;
struct timespec ts;
*tck = 1000000000;
err = clock_gettime (CLOCK_MONOTONIC, &ts);
*secs = ts.tv_sec;
*nanosecs = ts.tv_nsec;
return err;
}
int
main ()
{
int err;
time_t secs;
long usecs;
long nsecs, tck;
err = gf_gettime_1 (&secs, &usecs);
printf ("gettimeofday: err=%d, secs=%ld, usecs=%ld\n",
err, (long) secs, usecs);
err = gf_gettime_2 (&secs, &usecs);
printf ("clock_gettime: err=%d, secs=%ld, usecs=%ld\n",
err, (long) secs, usecs);
err = gf_gettime_3 (&secs, &usecs);
printf ("time: err=%d, secs=%ld, usecs=%ld\n",
err, (long) secs, usecs);
err = gf_gettime_mono (&secs, &nsecs, &tck);
printf ("clock_gettime: err=%d, secs=%ld, nsecs=%ld, tck=%ld\n",
err, (long) secs, usecs, tck);
return 0;
}