[J3] SYSTEM_CLOCK

Steven G. Kargl kargl at uw.edu
Wed Jan 27 16:33:28 UTC 2021


On Wed, Jan 27, 2021 at 09:11:57AM +0000, John Reid via J3 wrote:
> Steven G. Kargl wrote:
> > On Tue, Jan 26, 2021 at 10:06:19PM +0000, John Reid via J3 wrote:
> > > Steve Lionel via J3 wrote:
> > > >    * Add a note warning of unpredictable results if integer kinds are
> > > >      mixed across calls
> > > > 
> > > Do we really have to say anything about doing obviously stupid things like
> > > looking at the difference between COUNT values from different clocks?
> > > 
> > Apparently, yes.  Steve L started to look at SYSTEM_CLOCK due to
> > a post by me in comp.lang.fortran about a gfortran bug report.
> 
> > The user did
> > 
> >     use iso_fortran_env
> > 
> >     integer(int32_t) rate
> >     integer(int64_t) cnt1, cnt2
> > 
> >     call system_clock(count=cnt1, count_rate=rate)
> >     ....
> >     call system_clock(count_cnt2)
> >     print *, (cnt2 - cnt1) / real(rate)
> 
> Well, he was not  looking at the difference between COUNT values from
> different clocks and this code would be picked up at compile time with the
> suggested changes because of different integer kinds in a single call of
> SYSTEM_CLOCK
> 

As pointed out by Steve L, three calls could have been done,
and to up the ante the calls need not be done in the same
scoping unit as the others:

   module times
      use iso_fortran_env
      integer(int32_t) cnt1, rate  ! gfortran's ms time scale
      integer(int64_t) cnt2        ! gfortran's ns time scale
   end module times

   subroutine foo
      use times, only : rate
      ...
      call system_clock(count_rate=rate)
      call bar
      ...
   end subroutine foo

   subroutine bar
      use times, only : cnt1
      ...
      call system_clock(count=cnt1)
      do i = 1, 10
         ... 
         call bah
      end do
   end subroutine bar 

   suboroutine bah
      use times, only : cnt2
      ...
      call system_clock(count=cnt2)
      ...
      call timings
   end subroutine bah 

   subroutine timings
       use times
       print *, (cnt2 - cnt1) / real(rate)
   end subroutine timings 

so having the Fortran processor check that rate, cnt1, and cnt2
are compatible is not possible.  Finally, to counter your next
objection that the above is contrived and silly, I have triaged
enough gfortran bug reports to know programmers have all sorts
of inventive ways of testing a processor.

-- 
Steve


More information about the J3 mailing list