Skip to content

Commit 207f12e

Browse files
add methods to create custom unary and binary operators
1 parent 593b931 commit 207f12e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/Object_Methods/Algebra_Methods.jl

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
GrB_BinaryOp_new(op, fn, ztype, xtype, ytype)
3+
4+
Initializes a new GraphBLAS binary operator with a specified user-defined function and its types.
5+
6+
# Examples
7+
```jldoctest
8+
julia> using SuiteSparseGraphBLAS
9+
10+
julia> GrB_init(GrB_NONBLOCKING)
11+
GrB_SUCCESS::GrB_Info = 0
12+
13+
julia> V = GrB_Vector{Float64}()
14+
GrB_Vector{Float64}
15+
16+
julia> GrB_Vector_new(V, GrB_FP64, 4)
17+
GrB_SUCCESS::GrB_Info = 0
18+
19+
julia> I = [0, 0, 3, 3]; X = [2.1, 3.2, 4.5, 5.0]; n = 4; # two values at position 0 and 3
20+
21+
julia> dup = GrB_BinaryOp() # dup is a binary operator which is applied when duplicate values for the same location are present in the vector
22+
GrB_BinaryOp
23+
24+
julia> function ADD(b, c)
25+
return b+c
26+
end
27+
ADD (generic function with 1 method)
28+
29+
julia> GrB_BinaryOp_new(dup, ADD, GrB_FP64, GrB_FP64, GrB_FP64)
30+
GrB_SUCCESS::GrB_Info = 0
31+
32+
julia> GrB_Vector_build(V, I, X, n, dup)
33+
GrB_SUCCESS::GrB_Info = 0
34+
35+
julia> @GxB_Vector_fprint(V, GxB_SHORT) # the value stored at position 0 and 3 will be the sum of the duplicate values
36+
37+
GraphBLAS vector: V
38+
nrows: 4 ncols: 1 max # entries: 2
39+
format: standard CSC vlen: 4 nvec_nonempty: 1 nvec: 1 plen: 1 vdim: 1
40+
hyper_ratio 0.0625
41+
GraphBLAS type: double size: 8
42+
number of entries: 2
43+
column: 0 : 2 entries [0:1]
44+
row 0: double 5.3
45+
row 3: double 9.5
46+
47+
```
48+
"""
49+
function GrB_BinaryOp_new(
50+
op::GrB_BinaryOp,
51+
fn::Function,
52+
ztype::GrB_Type{T},
53+
xtype::GrB_Type{U},
54+
ytype::GrB_Type{V}) where{T <: valid_types, U <: valid_types, V <: valid_types}
55+
56+
op_ptr = pointer_from_objref(op)
57+
58+
function binaryop_fn(z, x, y)
59+
unsafe_store!(z, fn(x, y))
60+
return nothing
61+
end
62+
63+
binaryop_fn_C = @cfunction($binaryop_fn, Cvoid, (Ptr{T}, Ref{U}, Ref{V}))
64+
65+
return GrB_Info(
66+
ccall(
67+
dlsym(graphblas_lib, "GrB_BinaryOp_new"),
68+
Cint,
69+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
70+
op_ptr, binaryop_fn_C, ztype.p, xtype.p, ytype.p
71+
)
72+
)
73+
end
74+
75+
function GrB_UnaryOp_new(
76+
op::GrB_UnaryOp,
77+
fn::Function,
78+
ztype::GrB_Type{T},
79+
xtype::GrB_Type{U}) where{T <: valid_types, U <: valid_types}
80+
81+
op_ptr = pointer_from_objref(op)
82+
83+
function unaryop_fn(z, x)
84+
unsafe_store!(z, fn(x))
85+
return nothing
86+
end
87+
88+
unaryop_fn_C = @cfunction($unaryop_fn, Cvoid, (Ptr{T}, Ref{U}))
89+
90+
return GrB_Info(
91+
ccall(
92+
dlsym(graphblas_lib, "GrB_UnaryOp_new"),
93+
Cint,
94+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
95+
op_ptr, unaryop_fn_C, ztype.p, xtype.p
96+
)
97+
)
98+
end

src/SuiteSparseGraphBLAS.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ include("Enums.jl")
7373
include("Context_Methods.jl")
7474
include("Object_Methods/Matrix_Methods.jl")
7575
include("Object_Methods/Vector_Methods.jl")
76+
include("Object_Methods/Algebra_Methods.jl")
7677
include("Object_Methods/Descriptor_Methods.jl")
7778
include("Object_Methods/Print_Objects.jl")
7879

@@ -92,6 +93,9 @@ GrB_Vector_nvals, GrB_Vector_setElement, GrB_Vector_extractElement, GrB_Vector_e
9293
# Descriptor Methods
9394
GrB_Descriptor_new, GrB_Descriptor_set,
9495

96+
# Algebra Methods
97+
GrB_UnaryOp_new, GrB_BinaryOp_new,
98+
9599
# Print functions
96100
@GxB_Matrix_fprint, @GxB_Vector_fprint, @GxB_Descriptor_fprint
97101

0 commit comments

Comments
 (0)