Skip to content

Commit 94ba108

Browse files
author
Wimmerer
committed
Comments
1 parent 8a2b0b7 commit 94ba108

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

src/SuiteSparseGraphBLAS.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ include("options.jl")
9494
include("misc.jl")
9595
export libgb
9696
export UnaryOps, BinaryOps, Monoids, Semirings, SelectOps, Descriptors #Submodules
97+
export UnaryOp, BinaryOp, Monoid, Semiring #UDFs
9798
export Descriptor #Types
9899
export xtype, ytype, ztype
99100
export GBScalar, GBVector, GBMatrix #arrays

src/operators/binaryops.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@ function _createbinaryops()
6868
end
6969

7070
function BinaryOp(name)
71-
if isGxB(name) || isGrB(name)
71+
if isGxB(name) || isGrB(name) #If it's a built-in drop the prefix
7272
simplifiedname = name[5:end]
7373
else
7474
simplifiedname = name
7575
end
7676
containername = Symbol(simplifiedname, "_T")
7777
exportedname = Symbol(simplifiedname)
78-
if isGxB(name) || isGrB(name)
78+
if isGxB(name) || isGrB(name) #Built-in is immutable, no finalizer
7979
structquote = quote
8080
struct $containername <: AbstractBinaryOp
8181
pointers::Dict{DataType, libgb.GrB_BinaryOp}
8282
name::String
8383
$containername() = new(Dict{DataType, libgb.GrB_BinaryOp}(), $name)
8484
end
8585
end
86-
else
86+
else #UDF is mutable for finalizer
8787
structquote = quote
8888
mutable struct $containername <: AbstractBinaryOp
8989
pointers::Dict{DataType, libgb.GrB_BinaryOp}
@@ -101,16 +101,17 @@ function BinaryOp(name)
101101
end
102102
end
103103
end
104-
@eval(Types, $structquote)
104+
@eval(Types, $structquote) #eval container *type* into Types submodule
105105
constquote = quote
106106
const $exportedname = Types.$containername()
107107
export $exportedname
108108
end
109-
@eval(BinaryOps, $constquote)
109+
@eval(BinaryOps, $constquote) #eval actual op into BinaryOps submodule
110110
return getproperty(BinaryOps, exportedname)
111111
end
112112

113113
#This is adapted from the fork by cvdlab.
114+
#Add a new GrB_BinaryOp to an AbstractBinaryOp
114115
function _addbinaryop(
115116
op::AbstractBinaryOp,
116117
fn::Function,
@@ -130,14 +131,21 @@ function _addbinaryop(
130131
return nothing
131132
end
132133

134+
#BinaryOp constructors
135+
######################
136+
133137
function BinaryOp(name::String, fn::Function, ztype, xtype, ytype)
134138
op = BinaryOp(name)
135139
_addbinaryop(op, fn, toGBType(ztype), toGBType(xtype), toGBType(ytype))
136140
return op
137141
end
142+
143+
#xtype == ytype == ztype
138144
function BinaryOp(name::String, fn::Function, type::DataType)
139145
return BinaryOp(name, fn, type, type, type)
140146
end
147+
148+
#Vectors of _type, add one function for each triple.
141149
function BinaryOp(
142150
name::String,
143151
fn::Function,
@@ -153,9 +161,13 @@ function BinaryOp(
153161
end
154162
return op
155163
end
164+
165+
#Vector of type, xtype == ytype == ztype
156166
function BinaryOp(name::String, fn::Function, type::Vector{DataType})
157167
return BinaryOp(name, fn, type, type, type)
158168
end
169+
170+
#Use the built-in primitives.
159171
function BinaryOp(name::String, fn::Function)
160172
return BinaryOp(name, fn, valid_vec)
161173
end

src/operators/monoids.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ function _createmonoids()
3636
Monoid(name)
3737
end
3838
end
39+
3940
function Monoid(name)
4041
containername, exportedname = _monoidnames(name)
41-
if isGxB(name) || isGrB(name)
42+
if isGxB(name) || isGrB(name) #Built-ins are immutable
4243
structquote = quote
4344
struct $containername <: AbstractMonoid
4445
pointers::Dict{DataType, libgb.GrB_Monoid}
4546
name::String
4647
$containername() = new(Dict{DataType, libgb.GrB_Monoid}(), $name)
4748
end
4849
end
49-
else
50+
else #UDFs are mutable for finalizing
5051
structquote = quote
5152
mutable struct $containername <: AbstractMonoid
5253
pointers::Dict{DataType, libgb.GrB_Monoid}
@@ -74,6 +75,7 @@ function Monoid(name)
7475
end
7576

7677
#This is adapted from the fork by cvdlab
78+
#NOTE: Terminals do not work with built in BinaryOps.
7779
function _addmonoid(op::AbstractMonoid, binop::BinaryUnion, id::T, terminal = nothing) where {T}
7880
if terminal === nothing
7981
terminal = C_NULL
@@ -96,8 +98,12 @@ function _addmonoid(op::AbstractMonoid, binop::BinaryUnion, id::T, terminal = no
9698
op.pointers[T] = monref[]
9799
return nothing
98100
end
101+
102+
#Monoid Constructors
103+
####################
104+
99105
function Monoid(name::String, binop::BinaryUnion, id::T, terminal = nothing) where {T}
100-
if binop isa AbstractBinaryOp
106+
if binop isa AbstractBinaryOp #If this is an AbstractBinaryOp we need to narrow down
101107
binop = binop[T]
102108
end
103109
m = Monoid(name)

src/operators/semirings.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,22 @@ function Semiring(name)
274274
return getproperty(Semirings, exportedname)
275275
end
276276

277+
#Add typed ⊕ and ⊗ to semiring
277278
function _addsemiring(rig::AbstractSemiring, add::libgb.GrB_Monoid, mul::libgb.GrB_BinaryOp)
278279
rigref = Ref{libgb.GrB_Semiring}()
279280
libgb.GrB_Semiring_new(rigref, add, mul)
280281
rig.pointers[xtype(add)] = rigref[]
281282
return nothing
282283
end
283284

285+
#New semiring with typed ⊕ and ⊗
284286
function Semiring(name::String, add::libgb.GrB_Monoid, mul::libgb.GrB_BinaryOp)
285287
rig = Semiring(name)
286288
_addsemiring(rig, add, mul)
287289
return rig
288290
end
289291

292+
#New semiring with all supported types in the intersection of validtypes(⊕) and validtypes(⊗)
290293
function Semiring(name::String, add::AbstractMonoid, mul::AbstractBinaryOp)
291294
tadd = validtypes(add)
292295
tmul = validtypes(mul)

src/operators/unaryops.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@ end
55

66
const UnaryUnion = Union{AbstractUnaryOp, libgb.GrB_UnaryOp}
77

8-
function _unarynames(name)
9-
simple = splitconstant(name)[2]
10-
containername = Symbol(simple, "_UNARY_T")
11-
exportedname = Symbol(simple)
12-
return containername, exportedname
13-
end
14-
158
#TODO: Rewrite
169
function _createunaryops()
1710
builtins = [
@@ -71,13 +64,14 @@ function _createunaryops()
7164
end
7265

7366
function UnaryOp(name)
74-
if isGxB(name) || isGrB(name)
67+
if isGxB(name) || isGrB(name) #If it's a GrB/GxB op we don't want the prefix
7568
simplifiedname = name[5:end]
7669
else
7770
simplifiedname = name
7871
end
7972
tname = Symbol(simplifiedname * "_T")
8073
simplifiedname = Symbol(simplifiedname)
74+
#If it's a built-in we probably want immutable struct. Need to check original name.
8175
if isGxB(name) || isGrB(name)
8276
structquote = quote
8377
struct $tname <: AbstractUnaryOp
@@ -86,7 +80,7 @@ function UnaryOp(name)
8680
$tname() = new(Dict{DataType, libgb.GrB_UnaryOp}(), $name)
8781
end
8882
end
89-
else
83+
else #If it's a UDF we need a mutable for finalizing purposes.
9084
structquote = quote
9185
mutable struct $tname <: AbstractUnaryOp
9286
pointers::Dict{DataType, libgb.GrB_UnaryOp}
@@ -104,7 +98,7 @@ function UnaryOp(name)
10498
end
10599
end
106100
end
107-
@eval(Types, $structquote)
101+
@eval(Types, $structquote) #Eval the struct into the Types submodule to avoid clutter.
108102
constquote = quote
109103
const $simplifiedname = Types.$tname()
110104
export $simplifiedname
@@ -114,8 +108,8 @@ function UnaryOp(name)
114108
end
115109

116110
#This is adapted from the fork by cvdlab.
111+
#Add a new GrB_UnaryOp to an AbstractUnaryOp.
117112
function _addunaryop(op::AbstractUnaryOp, fn::Function, ztype::GBType{T}, xtype::GBType{U}) where {T, U}
118-
119113
function unaryopfn(z, x)
120114
unsafe_store!(z, fn(x))
121115
return nothing
@@ -127,14 +121,18 @@ function _addunaryop(op::AbstractUnaryOp, fn::Function, ztype::GBType{T}, xtype:
127121
return nothing
128122
end
129123

124+
#UnaryOp constructors
125+
#####################
130126
function UnaryOp(name::String, fn::Function, ztype, xtype)
131127
op = UnaryOp(name)
132128
_addunaryop(op, fn, toGBType(ztype), toGBType(xtype))
133129
return op
134130
end
131+
#Same xtype, ztype.
135132
function UnaryOp(name::String, fn::Function, type)
136133
return UnaryOp(name, fn, type, type)
137134
end
135+
#Vector of xtypes and ztypes, add a GrB_UnaryOp for each.
138136
function UnaryOp(name::String, fn::Function, ztype::Vector{DataType}, xtype::Vector{DataType})
139137
op = UnaryOp(name)
140138
length(ztype) == length(xtype) || error("Lengths of ztype and xtype must match.")
@@ -143,9 +141,11 @@ function UnaryOp(name::String, fn::Function, ztype::Vector{DataType}, xtype::Vec
143141
end
144142
return op
145143
end
144+
#Vector but same ztype xtype.
146145
function UnaryOp(name::String, fn::Function, type::Vector{DataType})
147146
return UnaryOp(name, fn, type, type)
148147
end
148+
#Construct it using the built in primitives.
149149
function UnaryOp(name::String, fn::Function)
150150
return UnaryOp(name, fn, valid_vec)
151151
end

0 commit comments

Comments
 (0)