Skip to content

Commit 2b2b7a8

Browse files
add monoid and semiring methods, make all GraphBLAS structures subtypes of abstract type GrB_Struct
1 parent 207f12e commit 2b2b7a8

File tree

4 files changed

+145
-48
lines changed

4 files changed

+145
-48
lines changed

src/Object_Methods/Algebra_Methods.jl

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,20 @@ function GrB_BinaryOp_new(
6363
binaryop_fn_C = @cfunction($binaryop_fn, Cvoid, (Ptr{T}, Ref{U}, Ref{V}))
6464

6565
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
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+
)
7172
)
72-
)
7373
end
7474

75+
"""
76+
GrB_UnaryOp_new(op, fn, ztype, xtype, ytype)
77+
78+
Initializes a new GraphBLAS unary operator with a specified user-defined function and its types.
79+
"""
7580
function GrB_UnaryOp_new(
7681
op::GrB_UnaryOp,
7782
fn::Function,
@@ -88,11 +93,76 @@ function GrB_UnaryOp_new(
8893
unaryop_fn_C = @cfunction($unaryop_fn, Cvoid, (Ptr{T}, Ref{U}))
8994

9095
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+
ccall(
97+
dlsym(graphblas_lib, "GrB_UnaryOp_new"),
98+
Cint,
99+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
100+
op_ptr, unaryop_fn_C, ztype.p, xtype.p
101+
)
102+
)
103+
end
104+
105+
"""
106+
GrB_Monoid_new(monoid, binary_op, identity)
107+
108+
Creates a new monoid with specified binary operator and identity value.
109+
"""
110+
function GrB_Monoid_new(monoid::GrB_Monoid, binary_op::GrB_BinaryOp, identity::T) where T <: valid_int_types
111+
monoid_ptr = pointer_from_objref(monoid)
112+
fn_name = "GrB_Monoid_new_" * suffix(T)
113+
114+
return GrB_Info(
115+
ccall(
116+
dlsym(graphblas_lib, fn_name),
117+
Cint,
118+
(Ptr{Cvoid}, Ptr{Cvoid}, Cintmax_t),
119+
monoid_ptr, binary_op.p, identity
120+
)
121+
)
122+
end
123+
124+
function GrB_Monoid_new(monoid::GrB_Monoid, binary_op::GrB_BinaryOp, identity::Float32)
125+
monoid_ptr = pointer_from_objref(monoid)
126+
fn_name = "GrB_Monoid_new_FP32"
127+
128+
return GrB_Info(
129+
ccall(
130+
dlsym(graphblas_lib, fn_name),
131+
Cint,
132+
(Ptr{Cvoid}, Ptr{Cvoid}, Cfloat),
133+
monoid_ptr, binary_op.p, identity
134+
)
135+
)
136+
end
137+
138+
function GrB_Monoid_new(monoid::GrB_Monoid, binary_op::GrB_BinaryOp, identity::Float64)
139+
monoid_ptr = pointer_from_objref(monoid)
140+
fn_name = "GrB_Monoid_new_FP64"
141+
142+
return GrB_Info(
143+
ccall(
144+
dlsym(graphblas_lib, fn_name),
145+
Cint,
146+
(Ptr{Cvoid}, Ptr{Cvoid}, Cdouble),
147+
monoid_ptr, binary_op.p, identity
148+
)
149+
)
150+
end
151+
152+
"""
153+
GrB_Semiring_new(semiring, monoid, binary_op)
154+
155+
Creates a new semiring with specified monoid and binary operator.
156+
"""
157+
function GrB_Semiring_new(semiring::GrB_Semiring, monoid::GrB_Monoid, binary_op::GrB_BinaryOp)
158+
semiring_ptr = pointer_from_objref(semiring)
159+
160+
return GrB_Info(
161+
ccall(
162+
dlsym(graphblas_lib, "GrB_Semiring_new"),
163+
Cint,
164+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
165+
semiring_ptr, monoid.p, binary_op.p
166+
)
96167
)
97-
)
98168
end

src/Object_Methods/Print_Objects.jl

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,55 @@
1-
function GxB_Matrix_fprint(A::GrB_Matrix, name::String, pr::GxB_Print_Level)
2-
function fn(path, io)
1+
function GxB_fprint(A::GrB_Struct, name::String, pr::GxB_Print_Level)
2+
function f(path, io)
33
FILE = Libc.FILE(io)
4-
ccall(dlsym(graphblas_lib, "GxB_Matrix_fprint"), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint, Ptr{Cvoid}), A.p, pointer(name), pr, FILE)
4+
ccall(dlsym(graphblas_lib, fn_name), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint, Ptr{Cvoid}), A.p, pointer(name), pr, FILE)
55
ccall(:fclose, Cint, (Ptr{Cvoid},), FILE)
66
foreach(println, eachline(path))
77
end
8-
mktemp(fn)
8+
s = ""
9+
for i in string(typeof(A))[5:end]
10+
i == '{' && break
11+
s *= i
12+
end
13+
fn_name::String = "GxB_" * s * "_fprint"
14+
mktemp(f)
915
end
1016

1117
macro GxB_Matrix_fprint(A, pr)
1218
name = string(A)
13-
return :(GxB_Matrix_fprint($(esc(A)), $name, $pr))
14-
end
15-
16-
function GxB_Vector_fprint(v::GrB_Vector, name::String, pr::GxB_Print_Level)
17-
function fn(path, io)
18-
FILE = Libc.FILE(io)
19-
ccall(dlsym(graphblas_lib, "GxB_Vector_fprint"), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint, Ptr{Cvoid}), v.p, pointer(name), pr, FILE)
20-
ccall(:fclose, Cint, (Ptr{Cvoid},), FILE)
21-
foreach(println, eachline(path))
22-
end
23-
mktemp(fn)
19+
return :(GxB_fprint($(esc(A)), $name, $pr))
2420
end
2521

2622
macro GxB_Vector_fprint(v, pr)
2723
name = string(v)
28-
return :(GxB_Vector_fprint($(esc(v)), $name, $pr))
29-
end
30-
31-
function GxB_Descriptor_fprint(desc::GrB_Descriptor, name::String, pr::GxB_Print_Level)
32-
function fn(path, io)
33-
FILE = Libc.FILE(io)
34-
ccall(dlsym(graphblas_lib, "GxB_Descriptor_fprint"), Cint, (Ptr{Cvoid}, Ptr{UInt8}, Cint, Ptr{Cvoid}), desc.p, pointer(name), pr, FILE)
35-
ccall(:fclose, Cint, (Ptr{Cvoid},), FILE)
36-
foreach(println, eachline(path))
37-
end
38-
mktemp(fn)
24+
return :(GxB_fprint($(esc(v)), $name, $pr))
3925
end
4026

4127
macro GxB_Descriptor_fprint(desc, pr)
4228
name = string(desc)
43-
return :(GxB_Descriptor_fprint($(esc(desc)), $name, $pr))
29+
return :(GxB_fprint($(esc(desc)), $name, $pr))
30+
end
31+
32+
macro GxB_UnaryOp_fprint(op, pr)
33+
name = string(op)
34+
return :(GxB_fprint($(esc(op)), $name, $pr))
35+
end
36+
37+
macro GxB_BinaryOp_fprint(op, pr)
38+
name = string(op)
39+
return :(GxB_fprint($(esc(op)), $name, $pr))
40+
end
41+
42+
macro GxB_Monoid_fprint(monoid, pr)
43+
name = string(monoid)
44+
return :(GxB_fprint($(esc(monoid)), $name, $pr))
45+
end
46+
47+
macro GxB_Semiring_fprint(semiring, pr)
48+
name = string(semiring)
49+
return :(GxB_fprint($(esc(semiring)), $name, $pr))
50+
end
51+
52+
macro GxB_fprint(A, pr)
53+
varname = string(A)
54+
return :(GxB_fprint($(esc(A)), $varname, $pr))
4455
end

src/Structures.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,53 @@
11
import Base.show
22
import Base.==
3-
export GrB_Type, GrB_UnaryOp, GrB_BinaryOp, GrB_Vector, GrB_Matrix, GrB_Descriptor
3+
export GrB_Type, GrB_UnaryOp, GrB_BinaryOp, GrB_Monoid, GrB_Semiring,
4+
GrB_Vector, GrB_Matrix, GrB_Descriptor
45

5-
mutable struct GrB_Type{T}
6+
abstract type GrB_Struct end
7+
8+
mutable struct GrB_Type{T} <: GrB_Struct
69
p::Ptr{Cvoid}
710
end
811
GrB_Type{T}() where T = GrB_Type{T}(Ptr{Cvoid}(0))
912
Base.show(io::IO, ::GrB_Type{T}) where T = print("GrB_Type{" * string(T) * "}")
1013

11-
mutable struct GrB_UnaryOp
14+
mutable struct GrB_UnaryOp <: GrB_Struct
1215
p::Ptr{Cvoid}
1316
end
1417
GrB_UnaryOp() = GrB_UnaryOp(Ptr{Cvoid}(0))
1518
Base.show(io::IO, ::GrB_UnaryOp) = print("GrB_UnaryOp")
1619

17-
mutable struct GrB_BinaryOp
20+
mutable struct GrB_BinaryOp <: GrB_Struct
1821
p::Ptr{Cvoid}
1922
end
2023
GrB_BinaryOp() = GrB_BinaryOp(Ptr{Cvoid}(0))
2124
Base.show(io::IO, ::GrB_BinaryOp) = print("GrB_BinaryOp")
2225

23-
mutable struct GrB_Vector{T}
26+
mutable struct GrB_Monoid <: GrB_Struct
27+
p::Ptr{Cvoid}
28+
end
29+
GrB_Monoid() = GrB_Monoid(Ptr{Cvoid}(0))
30+
Base.show(io::IO, ::GrB_Monoid) = print("GrB_Monoid")
31+
32+
mutable struct GrB_Semiring <: GrB_Struct
33+
p::Ptr{Cvoid}
34+
end
35+
GrB_Semiring() = GrB_Semiring(Ptr{Cvoid}(0))
36+
Base.show(io::IO, ::GrB_Semiring) = print("GrB_Semiring")
37+
38+
mutable struct GrB_Vector{T} <: GrB_Struct
2439
p::Ptr{Cvoid}
2540
end
2641
GrB_Vector{T}() where T = GrB_Vector{T}(Ptr{Cvoid}(0))
2742
Base.show(io::IO, ::GrB_Vector{T}) where T = print("GrB_Vector{" * string(T) * "}")
2843

29-
mutable struct GrB_Matrix{T}
44+
mutable struct GrB_Matrix{T} <: GrB_Struct
3045
p::Ptr{Cvoid}
3146
end
3247
GrB_Matrix{T}() where T = GrB_Matrix{T}(Ptr{Cvoid}(0))
3348
Base.show(io::IO, ::GrB_Matrix{T}) where T = print("GrB_Matrix{" * string(T) * "}")
3449

35-
mutable struct GrB_Descriptor
50+
mutable struct GrB_Descriptor <: GrB_Struct
3651
p::Ptr{Cvoid}
3752
end
3853
GrB_Descriptor() = GrB_Descriptor(Ptr{Cvoid}(0))

src/SuiteSparseGraphBLAS.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,11 @@ GrB_Vector_nvals, GrB_Vector_setElement, GrB_Vector_extractElement, GrB_Vector_e
9494
GrB_Descriptor_new, GrB_Descriptor_set,
9595

9696
# Algebra Methods
97-
GrB_UnaryOp_new, GrB_BinaryOp_new,
97+
GrB_UnaryOp_new, GrB_BinaryOp_new, GrB_Monoid_new, GrB_Semiring_new,
9898

9999
# Print functions
100-
@GxB_Matrix_fprint, @GxB_Vector_fprint, @GxB_Descriptor_fprint
100+
@GxB_UnaryOp_fprint, @GxB_BinaryOp_fprint, @GxB_Monoid_fprint, @GxB_Semiring_fprint,
101+
@GxB_Matrix_fprint, @GxB_Vector_fprint, @GxB_Descriptor_fprint, @GxB_fprint
101102

102103
# Export global variables
103104

0 commit comments

Comments
 (0)