Skip to content

Commit a4fd14d

Browse files
Adding optional barriers to tic!
1 parent 2a06ba4 commit a4fd14d

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

src/PTimers.jl

+31-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11

2-
struct Timing
3-
ns::UInt
2+
struct Timing{T<:Number}
3+
ns::T
44
end
55

6-
function Base.:-(a::Timing,b::Timing)
6+
function Base.:-(a::Timing{T},b::Timing{T}) where {T}
77
ns = a.ns-b.ns
88
Timing(ns)
99
end
1010

1111
@inline Timing() = Timing(time_ns())
12+
Timing(::AbstractPData)=Timing()
13+
function Timing(::MPIData)
14+
Timing(MPI.Wtime())
15+
end
1216

13-
mutable struct PTimer{A,B<:Dict}
17+
mutable struct PTimer{A,B<:Dict,C}
1418
parts::A
1519
timings::B
16-
current::Timing
20+
current::Timing{C}
1721
verbose::Bool
1822
end
1923

24+
function _to_secs(::PTimer,t::Timing)
25+
Float64(t.ns)/1.0e9
26+
end
27+
28+
function _to_secs(::PTimer{<:MPIData},t::Timing)
29+
t.ns
30+
end
31+
2032
function PTimer(parts::AbstractPData{<:Integer};verbose::Bool=false)
21-
current = Timing()
33+
current = Timing(parts)
2234
timing = map_parts(p->current,parts)
2335
T = typeof(timing)
2436
timings = Dict{String,T}()
@@ -35,7 +47,7 @@ function Base.getproperty(t::PTimer, sym::Symbol)
3547
for (name,timing) in t.timings
3648
timing_main = gather(timing)
3749
map_main(timing_main,data) do part_to_timing,data
38-
ns = map(i->Float64(i.ns)/1.0e9,part_to_timing)
50+
ns = map(i->_to_secs(t,i),part_to_timing)
3951
d = (min=minimum(ns),max=maximum(ns),avg=sum(ns)/length(ns))
4052
data[name] = d
4153
end
@@ -50,21 +62,28 @@ function Base.propertynames(x::PTimer, private=false)
5062
(fieldnames(typeof(x))...,:data)
5163
end
5264

53-
function tic!(t::PTimer)
54-
t.current = Timing()
65+
function tic!(t::PTimer;barrier=false)
66+
t.current = Timing(t.parts)
67+
end
68+
69+
function tic!(t::PTimer{<:MPIData};barrier=false)
70+
if barrier
71+
MPI.Barrier(t.parts.comm)
72+
end
73+
t.current = Timing(t.parts)
5574
end
5675

5776
function toc!(t::PTimer,name::String)
58-
current = Timing()
77+
current = Timing(t.parts)
5978
dt = current-t.current
6079
timing = map_parts(p->dt,t.parts)
6180
t.timings[name] = timing
6281
if t.verbose == true
6382
map_main(timing) do i
64-
println("[$(lstrip(_nice_time(Float64(i.ns)/1.0e9))) s in MAIN] $name")
83+
println("[$(lstrip(_nice_time(_to_secs(t,i)))) s in MAIN] $name")
6584
end
6685
end
67-
t.current = Timing()
86+
t.current = Timing(t.parts)
6887
end
6988

7089
function Base.show(io::IO,k::MIME"text/plain",t::PTimer)
@@ -127,4 +146,3 @@ function _print_footer(io,longest_name,w,linechars)
127146
header_w = longest_name+3*w
128147
println(io,rule^header_w)
129148
end
130-

src/PartitionedArrays.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ include("SparseUtils.jl")
110110

111111
include("IndexSets.jl")
112112

113-
include("PTimers.jl")
114-
115113
include("SequentialBackend.jl")
116114

117115
include("MPIBackend.jl")
118116

117+
include("PTimers.jl")
118+
119119
end

test/test_p_timers.jl

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ using PartitionedArrays
44
function test_p_timers(parts)
55

66
t = PTimer(parts)
7-
8-
tic!(t)
7+
8+
tic!(t;barrier=true)
99
toc!(t,"Phase 1")
1010
toc!(t,"Phase 3")
1111
sleep(0.2)
1212
toc!(t,"Phase 143")
13-
tic!(t)
13+
tic!(t;barrier=true)
1414
sleep(0.4)
1515
toc!(t,"Matrix Assembly")
1616

@@ -21,21 +21,20 @@ function test_p_timers(parts)
2121
println(io,data)
2222
end
2323
end
24-
24+
2525
display(t)
2626

2727
t = PTimer(parts,verbose=true)
28-
29-
tic!(t)
28+
29+
tic!(t;barrier=true)
3030
toc!(t,"Phase 1")
3131
toc!(t,"Phase 3")
3232
sleep(0.2)
3333
toc!(t,"Phase 143")
34-
tic!(t)
34+
tic!(t;barrier=true)
3535
sleep(0.4)
3636
toc!(t,"Matrix Assembly")
3737

3838
display(t)
3939

4040
end
41-

0 commit comments

Comments
 (0)