Skip to content

Commit a9e600f

Browse files
authored
Clean up output and fix optimization problem
1 parent 7e5dc8d commit a9e600f

File tree

4 files changed

+50
-52
lines changed

4 files changed

+50
-52
lines changed

timers/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ SHELL=/bin/sh
44
.SUFFIXES: .o .F90 .f90
55

66
FC=ifx
7-
FCFLAGS=-O2 -march=native -D__linux__
8-
FXFLAGS=-O2 -march=native -D__linux__
7+
FCFLAGS=-O2 -march=native
8+
FXFLAGS=-O2 -march=native
99

1010
# For Linux OS add -D__linux__ to FCFLAGS and FXFLAGS
1111

timers/README.txt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@
1616
Each function is used to time a summation over 1.E8 iterations. Two
1717
get the Linux specific timers you must add -D__linux__ to your compiler
1818
options. test_timers.F90 also tests the Fortran intrinsic CPU_TIME which
19-
is called directly.
19+
is called directly.
2020

21-
The test program defaults to compiling with -O0 optimization to give some
22-
realisitic time values. Compiling with higher optimization can lead to
23-
some of the results returning 0 to 15 decimals places. This is thought to be
24-
due to SYSTEM_CLOCK and DATE_AND_TIME having a finite interval between when
25-
the results returned by these routines are updated. For -02 optimization, the
26-
time taken in the summation loops is probably less than the update interval
27-
for these routines resulting in the same value for the starting and ending
28-
times used in the test program
21+
You can change the number of iterations by adding -D__IMAX__=x and
22+
-D__JMAX=y where x and y are integer values to the compile options
23+
for test_timers.F90
2924

3025
These programs were compiled and tested on a Linux Mint 21.3 system running on an AMD Ryzen 5 5600x processor with the following compilers
3126

@@ -46,4 +41,3 @@
4641

4742
flang 4.2
4843

49-

timers/test_timers.F90

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,28 @@ Program test_timers
33
USE ISO_FORTRAN_ENV, WP=>REAL64
44
USE timers
55

6-
#define __JMAX__ 10000
7-
#define __IMAX__ 10000
6+
#ifndef __JMAX__
7+
#define __JMAX__ 10000
8+
#endif
9+
#ifndef __IMAX__
10+
#define __IMAX__ 10000
11+
#endif
812

913
Implicit NONE
1014

15+
Integer :: niters
1116
Real(WP) :: time_s, time_e, sum, delta
1217

18+
Character(22) :: buf
19+
1320
Integer :: i, j
1421

22+
niters = (__IMAX__)*(__JMAX__)
1523
Print *,''
1624
Print *,' *** Test of Fortran and C timer routines ***'
17-
Print *,''
18-
Print *,' C gettimeofday'
19-
Print *,''
25+
Print *,''
26+
Write (*, '(" Summation loops elapsed times for ",i0," iterations")') niters
27+
Print *,''
2028
sum = 0.0_WP
2129
time_e = 0.0_WP
2230
time_s = 0.0_WP
@@ -28,11 +36,9 @@ Program test_timers
2836
End Do
2937
Call timer_gtd(time_e)
3038
delta = time_e - time_s
31-
Write(*,'( " delta time C ctd = ", g0.15)') delta
39+
Write(*,'( " C gettimeofday = ", 1PE22.15)') delta
40+
Write(buf,'(1PE22.15)') sum
3241

33-
Print *,''
34-
Print *,' C clock_gettime with MONOTONIC clock'
35-
Print *,''
3642
sum = 0.0_WP
3743
time_e = 0.0_WP
3844
time_s = 0.0_WP
@@ -44,11 +50,9 @@ Program test_timers
4450
End Do
4551
Call timer_cgt(time_e, CLOCK_MONOTONIC)
4652
delta = time_e - time_s
47-
Write(*,'( " delta time C cgt monotonic = ", g0.15)') delta
53+
Write(*,'( " C clock_gettime (MONOTONIC) = ", 1PE22.15)') delta
54+
Write(buf,'(1PE22.15)') sum
4855

49-
Print *,''
50-
Print *,' C clock_gettime with REALTIME clock'
51-
Print *,''
5256
sum = 0.0_WP
5357
time_e = 0.0_WP
5458
time_s = 0.0_WP
@@ -60,11 +64,10 @@ Program test_timers
6064
End Do
6165
Call timer_cgt(time_e, CLOCK_REALTIME)
6266
delta = time_e - time_s
63-
Write(*,'( " delta time C cgt realtime = ", g0.15)') delta
67+
Write(*,'( " C clock_gettime (REALTIME) = ", 1PE22.15)') delta
68+
Write(buf,'(1PE22.15)') sum
69+
6470
#ifdef __linux__
65-
Print *,''
66-
Print *,' C clock_gettime with CLOCK_THREAD_CPUTIME_ID clock (Linux only)'
67-
Print *,''
6871
sum = 0.0_WP
6972
time_e = 0.0_WP
7073
time_s = 0.0_WP
@@ -76,12 +79,10 @@ Program test_timers
7679
End Do
7780
Call timer_cgt(time_e, CLOCK_THREAD_CPUTIME_ID)
7881
delta = time_e - time_s
79-
Write(*,'( " delta time C cgt thread cputime = ", g0.15)') delta
82+
Write(*,'( " C clock_gettime (THREAD_CPUTIME) = ", 1PE22.15," Linux only")') delta
83+
Write(buf,'(1PE22.15)') sum
8084
#endif
8185

82-
Print *,''
83-
Print *,' DATE_AND_TIME'
84-
Print *,''
8586
sum = 0.0_WP
8687
time_e = 0.0_WP
8788
time_s = 0.0_WP
@@ -93,11 +94,9 @@ Program test_timers
9394
End Do
9495
Call timer_dt(time_e)
9596
delta = time_e - time_s
96-
Write(*,'( " delta time DT = ", g0.15)') delta
97+
Write(*,'( " DATE_AND_TIME = ", 1PE22.15)') delta
98+
Write(buf,'(1PE22.15)') sum
9799

98-
Print *,''
99-
Print *,' CPU_TIME'
100-
Print *,''
101100
sum = 0.0_WP
102101
time_e = 0.0_WP
103102
time_s = 0.0_WP
@@ -109,11 +108,9 @@ Program test_timers
109108
End Do
110109
Call CPU_TIME(time_e)
111110
delta = time_e - time_s
112-
Write(*,'( " delta time CPU = ", g0.15)') delta
111+
Write(*,'( " CPU_TIME = ", 1PE22.15)') delta
112+
Write(buf,'(1PE22.15)') sum
113113

114-
Print *,''
115-
Print *,' SYSTEM_CLOCK'
116-
Print *,''
117114
sum = 0.0_WP
118115
time_e = 0.0_WP
119116
time_s = 0.0_WP
@@ -125,11 +122,11 @@ Program test_timers
125122
End Do
126123
Call timer_sc(time_e)
127124
delta = time_e - time_s
128-
Write(*,'( " delta time SC = ", g0.15)') delta
129-
Print *,''
125+
Write(*,'( " SYSTEM_CLOCK = ", 1PE22.15)') delta
126+
Write(buf,'(1PE22.15)') sum
130127

131128
Print *,''
132-
Print *,' DATE_AND_TIME increment'
129+
Print *,'DATE_AND_TIME and SYSTEM_CLOCK increments'
133130
Print *,''
134131
time_e = 0.0_WP
135132
time_s = 0.0_WP
@@ -139,10 +136,7 @@ Program test_timers
139136
If (time_e > time_s) EXIT
140137
End Do
141138
delta = time_e - time_s
142-
Write(*,'( " delta time DT increment = ", g0.15)') delta
143-
Print *,''
144-
Print *,' SYSTEM CLOCK increment'
145-
Print *,''
139+
Write(*,'( " DATE_AND_TIME increment = ", 1PE22.15)') delta
146140
time_e = 0.0_WP
147141
time_s = 0.0_WP
148142
Call timer_sc(time_s)
@@ -151,8 +145,8 @@ Program test_timers
151145
If (time_e > time_s) EXIT
152146
End Do
153147
delta = time_e - time_s
154-
Write(*,'( " delta time SC increment = ", g0.15)') delta
155-
148+
Write(*,'( " SYSTEM_CLOCK increment = ", 1PE22.15)') delta
149+
Print *,''
156150
Stop
157151

158152
End Program test_timers

timers/timers.F90

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ Module timers
44
USE ISO_C_BINDING, ONLY: C_INT, C_LONG, C_PTR, C_NULL_PTR, C_DOUBLE
55

66
Implicit NONE
7-
7+
PRIVATE
88
! Define some clock ids for C clock_gettime function
99

1010
Integer(C_INT), Parameter :: CLOCK_REALTIME = 0_C_INT
1111
Integer(C_INT), Parameter :: CLOCK_MONOTONIC = 1_C_INT
12-
#ifdef __linux__
1312
! These are Linux only
13+
#ifdef __linux__
1414
Integer(C_INT), Parameter :: CLOCK_PROCESS_CPUTIME_ID = 2_C_INT
1515
Integer(C_INT), Parameter :: CLOCK_THREAD_CPUTIME_ID = 3_C_INT
1616
Integer(C_INT), Parameter :: CLOCK_TAI = 11_C_INT
@@ -54,6 +54,16 @@ Function clock_gettime_c(clockid, tp) BIND(C,NAME="clock_gettime")
5454
End Function clock_gettime_c
5555
End Interface
5656

57+
Public :: timer_dt, timer_gtd, timer_cgt, timer_sc
58+
Public :: CLOCK_REALTIME
59+
Public :: CLOCK_MONOTONIC
60+
! These are Linux only
61+
#ifdef __linux__
62+
Public :: CLOCK_PROCESS_CPUTIME_ID
63+
Public :: CLOCK_THREAD_CPUTIME_ID
64+
Public :: CLOCK_TAI
65+
#endif
66+
5767
Contains
5868

5969
Subroutine timer_dt(time)

0 commit comments

Comments
 (0)