Skip to content

Commit

Permalink
improved performance in accessing operators
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Apr 27, 2020
1 parent e3d62e3 commit 2f346a9
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 33 deletions.
33 changes: 27 additions & 6 deletions src/binaryop.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import Base: show

# create new unary op from function fun, called s
function binaryop(s::Symbol, fun::Function; xtype::GType = NULL, ztype::GType = NULL, ytype::GType = NULL)
bop = get!(Binaryop, s, BinaryOperator(fun, string(s)))
function binaryop(fun::Function; xtype::GType = NULL, ztype::GType = NULL, ytype::GType = NULL, name::Union{Symbol, Nothing} = nothing)
if name != nothing
if hasproperty(Binaryop, name)
bop = getproperty(Binaryop, name)
else
bop = BinaryOperator(fun, string(name))
@eval(Binaryop, $name = $bop)
@eval(Binaryop, export $name)
end
else
bop = BinaryOperator(fun, "$(string(name))_$(ztype.name)")
end
if xtype != NULL && ztype != NULL && ytype != NULL
if findfirst(op->op.xtype == xtype && op.ztype == ztype && op.ytype == ytype, bop.impl) == nothing
op = GrB_BinaryOp_new(fun, ztype, xtype, ytype)
Expand All @@ -14,14 +24,21 @@ end

function load_builtin_binaryop()

function load(lst; ztype = NULL)
function load(lst; ztype = nothing)
for op in lst
bpn = split(op, "_")
type = str2gtype(string(bpn[end]))

binaryop_name = Symbol(join(bpn[2:end - 1]))
binaryop = get!(Binaryop, binaryop_name, BinaryOperator(string(binaryop_name)))
push!(binaryop.impl, GrB_BinaryOp(op, ztype == NULL ? type : ztype, type, type))
if hasproperty(Binaryop, binaryop_name)
binaryop = getproperty(Binaryop, binaryop_name)
else
binaryop = BinaryOperator(string(binaryop_name))
@eval(Binaryop, $binaryop_name = $binaryop)
@eval(Binaryop, export $binaryop_name)
end

push!(binaryop.impl, GrB_BinaryOp(op, ztype == nothing ? type : ztype, type, type))
end
end

Expand Down Expand Up @@ -91,4 +108,8 @@ function __enter__(bop::BinaryOperator)
return (binaryop=old,)
end

show(io::IO, bop::BinaryOperator) = print(io, "BinaryOperator($(bop.name))")
show(io::IO, bop::BinaryOperator) = print(io, "BinaryOperator($(bop.name))")

baremodule Binaryop
# to fill with binary ops built in and user defined
end
28 changes: 24 additions & 4 deletions src/monoid.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import Base: show

# create new monoid from binary operation and identity value
function monoid(s::Symbol, bin_op::BinaryOperator, identity::T) where T <: valid_types
function monoid(bin_op::BinaryOperator, identity::T; name::Union{Symbol, Nothing} = nothing) where T <: valid_types
domain = j2gtype(T)
monoid = get!(Monoids, s, Monoid(string(s)))
if name != nothing
if hasproperty(Monoids, name)
monoid = getproperty(Monoids, name)
else
monoid = Monoid(string(name))
@eval(Monoids, $name = $monoid)
@eval(Monoids, export $name)
end
else
monoid = Monoids(string(name))
end
index = findfirst(mon -> mon.domain == domain, monoid.impl)
if index == nothing
# create a new monoid
Expand Down Expand Up @@ -31,7 +41,13 @@ function load_builtin_monoid()
type = str2gtype(string(bpn[end-1]))

monoid_name = Symbol(join(bpn[2:end-2], "_"))
monoid = get!(Monoids, monoid_name, Monoid(string(monoid_name)))
if hasproperty(Monoids, monoid_name)
monoid = getproperty(Monoids, monoid_name)
else
monoid = Monoid(string(monoid_name))
@eval(Monoids, $monoid_name = $monoid)
@eval(Monoids, export $monoid_name)
end
push!(monoid.impl, GrB_Monoid(op, type))
end
end
Expand Down Expand Up @@ -77,4 +93,8 @@ function __enter__(mon::Monoid)
return (monoid=old,)
end

show(io::IO, mon::Monoid) = print(io, "Monoid($(mon.name))")
show(io::IO, mon::Monoid) = print(io, "Monoid($(mon.name))")

baremodule Monoids
# to fill with built in and user defined monoids
end
30 changes: 26 additions & 4 deletions src/semiring.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import Base: show

function semiring(s::Symbol, add::Monoid, mult::BinaryOperator)
sem = get!(Semirings, s, Semiring(add, mult, string(s)))
function semiring(add::Monoid, mult::BinaryOperator; name::Union{Symbol, Nothing} = nothing)
if name != nothing
if hasproperty(Semirings, name)
sem = getproperty(Semirings, name)
else
sem = Semiring(add, mult, string(name))
@eval(Semirings, $name = $sem)
@eval(Semirings, export $name)
end
else
sem = Semiring(add, mult, string(name))
end
return sem
end

Expand Down Expand Up @@ -33,7 +43,15 @@ function load_builtin_semiring()
type = str2gtype(string(bpn[end]))

semiring_name = Symbol(join(bpn[2:end-1], "_"))
semiring = get!(Semirings, semiring_name, Semiring(string(semiring_name)))

if hasproperty(Semirings, semiring_name)
semiring = getproperty(Semirings, semiring_name)
else
semiring = Semiring(string(semiring_name))
@eval(Semirings, $semiring_name = $semiring)
@eval(Semirings, export $semiring_name)
end

push!(semiring.impl, GrB_Semiring(op, type, type, ztype == NULL ? type : ztype))
end
end
Expand Down Expand Up @@ -89,4 +107,8 @@ function __enter__(sem::Semiring)
return (semiring=old,)
end

show(io::IO, sem::Semiring) = print(io, "Semiring($(sem.name))")
show(io::IO, sem::Semiring) = print(io, "Semiring($(sem.name))")

baremodule Semirings
# to fill with built in and user defined semirings
end
15 changes: 0 additions & 15 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,11 @@ mutable struct GBMatrix{T <: valid_types}
GBMatrix{T}() where T = new(C_NULL, j2gtype(T))
end

function Base.getproperty(d::Dict{Symbol,T}, s::Symbol) where T <: Union{UnaryOperator,BinaryOperator,Monoid,Semiring,SelectOperator}
try
return getfield(d, s)
catch
return d[s]
end
end

_gb_pointer(op::GrB_UnaryOp) = op.p
_gb_pointer(op::GrB_BinaryOp) = op.p
_gb_pointer(op::GrB_Monoid) = op.p
_gb_pointer(op::GrB_Semiring) = op.p
_gb_pointer(op::SelectOperator) = op.p

# global variables
const Unaryop = Dict{Symbol,UnaryOperator}()
const Binaryop = Dict{Symbol,BinaryOperator}()
const Monoids = Dict{Symbol,Monoid}()
const Semirings = Dict{Symbol,Semiring}()
const Selectop = Dict{Symbol, SelectOperator}()

# default methods operators
g_operators = nothing
28 changes: 24 additions & 4 deletions src/unaryop.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import Base: show

# create new unary op from function fun, called s
function unaryop(s::Symbol, fun::Function; xtype::GType = NULL, ztype::GType = NULL)
uop = get!(Unaryop, s, UnaryOperator(fun, string(s)))
function unaryop(fun::Function; xtype::GType = NULL, ztype::GType = NULL, name::Union{Symbol, Nothing} = nothing)
if name != nothing
if hasproperty(Unaryop, name)
uop = getproperty(Unaryop, name)
else
uop = UnaryOperator(fun, string(name))
@eval(Unaryop, $name = $uop)
@eval(Unaryop, export $name)
end
else
uop = UnaryOperator(fun, "$(string(name))_$(ztype.name)")
end
if xtype != NULL && ztype != NULL
if findfirst(op->op.xtype == xtype && op.ztype == ztype, uop.impl) == nothing
op = GrB_UnaryOp_new(fun, ztype, xtype)
Expand All @@ -26,7 +36,13 @@ function load_builtin_unaryop()
type = str2gtype(string(opn[end]))

unaryop_name = Symbol(join(opn[2:end - 1]))
unaryop = get!(Unaryop, unaryop_name, UnaryOperator(string(unaryop_name)))
if hasproperty(Unaryop, unaryop_name)
unaryop = getproperty(Unaryop, unaryop_name)
else
unaryop = UnaryOperator(string(unaryop_name))
@eval(Unaryop, $unaryop_name = $unaryop)
@eval(Unaryop, export $unaryop_name)
end
push!(unaryop.impl, GrB_UnaryOp(op, type, type))
end

Expand Down Expand Up @@ -79,4 +95,8 @@ function __enter__(uop::UnaryOperator)
return (unaryop=old,)
end

show(io::IO, uop::UnaryOperator) = print(io, "UnaryOperator($(uop.name))")
show(io::IO, uop::UnaryOperator) = print(io, "UnaryOperator($(uop.name))")

baremodule Unaryop
# to fill with binary ops built in and user defined
end

0 comments on commit 2f346a9

Please sign in to comment.