Skip to content

Commit

Permalink
Minor formatting change
Browse files Browse the repository at this point in the history
  • Loading branch information
ffevotte committed Dec 22, 2020
1 parent 3129c64 commit 548379a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 70 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ julia> using GFlops
julia> x = rand(1000);

julia> @count_ops sum($x)
Flop Counter:
Flop Counter: 999 flop
┌─────┬─────────┐
│ │ Float64 │
├─────┼─────────┤
Expand Down Expand Up @@ -64,7 +64,7 @@ mixed_dot (generic function with 1 method)
julia> x = rand(Float32, 1000); y = rand(Float32, 1000);

julia> cnt = @count_ops mixed_dot($x, $y)
Flop Counter:
Flop Counter: 1000 flop
┌─────┬─────────┬─────────┐
│ │ Float32 │ Float64 │
├─────┼─────────┼─────────┤
Expand Down Expand Up @@ -106,19 +106,15 @@ fma_dot (generic function with 1 method)

julia> x = rand(100); y = rand(100);

# 100 FMAs...
# 100 FMAs but 200 flop
julia> cnt = @count_ops fma_dot($x, $y)
Flop Counter:
Flop Counter: 200 flop
┌─────┬─────────┐
│ │ Float64 │
├─────┼─────────┤
│ fma │ 100
└─────┴─────────┘

# ...but 200 FLOPs
julia> GFlops.flop(cnt)
200

julia> @gflops fma_dot($x, $y);
1.58 GFlops, 2.12% peak (2.00e+02 flop, 1.27e-07 s, 0 alloc: 0 bytes)
```
Expand All @@ -133,7 +129,7 @@ calls:
julia> using LinearAlgebra

julia> @count_ops dot($x, $y)
Flop Counter: no flop detected
Flop Counter: 0 flop
```

This is a known issue; we'll try and find a way to circumvent the problem.
4 changes: 1 addition & 3 deletions examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ function fma_dot(x, y)
acc
end
x = rand(100); y = rand(100);
println("# 100 FMAs...")
println("# 100 FMAs but 200 flop")
cnt = @count_ops fma_dot($x, $y)
println("# ...but 200 FLOPs")
GFlops.flop(cnt)
@gflops fma_dot($x, $y);


Expand Down
54 changes: 54 additions & 0 deletions src/Counter.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@eval mutable struct Counter
$((:($(Symbol(op[1], typ[2])) ::Int) for op in ops for typ in typs)...)
Counter() = new($((0 for _ in 1:length(ops)*length(typs))...))
end

# Relatively inefficient, but there should be no need for performance here...

function flop(c::Counter)
total = 0
for (typ, suffix) in typs
for (name, op, cnt) in ops
fieldname = Symbol(name, suffix)
total += cnt * getfield(c, fieldname)
end
end
total
end

import Base: ==, *, show

function Base.show(io::IO, c::Counter)
type_names = [typ for (typ, _) in typs]
type_suffix = [suffix for (_, suffix) in typs]
op_names = [name for (name, _) in ops]

mat = [getfield(c, Symbol(name, suffix)) for
name in op_names,
suffix in type_suffix]

fl = flop(c)
print(io, "Flop Counter: $fl flop")
fl == 0 && return

print(io, "\n")
fc(data, i) = any(data[:,i] .> 0)
fr(data, i) = any(data[i,:] .> 0)
pretty_table(io, mat, type_names,
row_names = op_names,
filters_col = (fc,),
filters_row = (fr,),
newline_at_end = false)
end

function ==(c1::Counter, c2::Counter)
all(getfield(c1, field)==getfield(c2, field) for field in fieldnames(Counter))
end

function *(n::Int, c::Counter)
ret = Counter()
for field in fieldnames(Counter)
setfield!(ret, field, n*getfield(c, field))
end
ret
end
1 change: 1 addition & 0 deletions src/GFlops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using PrettyTables: pretty_table
export @gflops, @count_ops

include("overdub.jl")
include("Counter.jl")
include("count_ops.jl")

end # module
57 changes: 0 additions & 57 deletions src/overdub.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ const typs = (
)


@eval mutable struct Counter
$((:($(Symbol(op[1], typ[2])) ::Int) for op in ops for typ in typs)...)
Counter() = new($((0 for _ in 1:length(ops)*length(typs))...))
end

function gen_count(ops, suffix)
body = Expr(:block)
for (name, op) in ops
Expand Down Expand Up @@ -67,55 +62,3 @@ for (typ, suffix) in typs
$(gen_count(unops, suffix))
end
end


# Relatively inefficient, but there should be no need for performance here...

function flop(c::Counter)
total = 0
for (typ, suffix) in typs
for (name, op, cnt) in ops
fieldname = Symbol(name, suffix)
total += cnt * getfield(c, fieldname)
end
end
total
end

import Base: ==, *, show

function Base.show(io::IO, c::Counter)
type_names = [typ for (typ, _) in typs]
type_suffix = [suffix for (_, suffix) in typs]
op_names = [name for (name, _) in ops]

mat = [getfield(c, Symbol(name, suffix)) for
name in op_names,
suffix in type_suffix]

if flop(c) == 0
print(io, "Flop Counter: no flop detected")
return
end

println(io, "Flop Counter:")
fc(data, i) = any(data[:,i] .> 0)
fr(data, i) = any(data[i,:] .> 0)
pretty_table(io, mat, type_names,
row_names = op_names,
filters_col = (fc,),
filters_row = (fr,),
newline_at_end = false)
end

function ==(c1::Counter, c2::Counter)
all(getfield(c1, field)==getfield(c2, field) for field in fieldnames(Counter))
end

function *(n::Int, c::Counter)
ret = Counter()
for field in fieldnames(Counter)
setfield!(ret, field, n*getfield(c, field))
end
ret
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ end
let
cnt = GFlops.Counter()
str = string(cnt)
@test str == "Flop Counter: no flop detected"
@test str == "Flop Counter: 0 flop"
end
end

Expand Down

0 comments on commit 548379a

Please sign in to comment.