Skip to content

Commit 61e8bb4

Browse files
add all vector methods
1 parent 75dee19 commit 61e8bb4

File tree

5 files changed

+208
-12
lines changed

5 files changed

+208
-12
lines changed

src/Object_Methods/Print_Objects.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ macro GxB_Matrix_fprint(A, pr)
1212
name = string(A)
1313
return :(GxB_Matrix_fprint($(esc(A)), $name, $pr))
1414
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)
24+
end
25+
26+
macro GxB_Vector_fprint(v, pr)
27+
name = string(v)
28+
return :(GxB_Vector_fprint($(esc(v)), $name, $pr))
29+
end

src/Object_Methods/Vector_Methods.jl

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
function GrB_Vector_new(v::GrB_Vector, type::GrB_Type, n::T) where T <: GrB_Index
2+
v_ptr = pointer_from_objref(v)
3+
4+
return GrB_Info(
5+
ccall(
6+
dlsym(graphblas_lib, "GrB_Vector_new"),
7+
Cint,
8+
(Ptr{Cvoid}, Ptr{Cvoid}, Cintmax_t),
9+
v_ptr, type.p, n
10+
)
11+
)
12+
end
13+
14+
function GrB_Vector_dup(w::GrB_Vector, u::GrB_Vector)
15+
w_ptr = pointer_from_objref(w)
16+
17+
return GrB_Info(
18+
ccall(
19+
dlsym(graphblas_lib, "GrB_Vector_dup"),
20+
Cint,
21+
(Ptr{Cvoid}, Ptr{Cvoid}),
22+
w_ptr, u.p
23+
)
24+
)
25+
end
26+
27+
function GrB_Vector_clear(v::GrB_Vector)
28+
return GrB_Info(
29+
ccall(
30+
dlsym(graphblas_lib, "GrB_Vector_clear"),
31+
Cint,
32+
(Ptr{Cvoid}, ),
33+
v.p
34+
)
35+
)
36+
end
37+
38+
function GrB_Vector_size(v::GrB_Vector)
39+
n = Ref(UInt64(0))
40+
result = GrB_Info(
41+
ccall(
42+
dlsym(graphblas_lib, "GrB_Vector_size"),
43+
Cint,
44+
(Ptr{UInt64}, Ptr{Cvoid}),
45+
n, v.p
46+
)
47+
)
48+
result != GrB_SUCCESS && return result
49+
return _GrB_Index(n[])
50+
end
51+
52+
function GrB_Vector_nvals(v::GrB_Vector)
53+
nvals = Ref(UInt64(0))
54+
result = GrB_Info(
55+
ccall(
56+
dlsym(graphblas_lib, "GrB_Vector_nvals"),
57+
Cint,
58+
(Ptr{UInt64}, Ptr{Cvoid}),
59+
nvals, v.p
60+
)
61+
)
62+
result != GrB_SUCCESS && return result
63+
return _GrB_Index(nvals[])
64+
end
65+
66+
function GrB_Vector_build(w::GrB_Vector, I::Vector{U}, X::Vector{T}, nvals::U, dup::GrB_BinaryOp) where{U <: GrB_Index, T <: valid_types}
67+
I_ptr = pointer(I)
68+
X_ptr = pointer(X)
69+
fn_name = "GrB_Vector_build_" * get_suffix(T)
70+
71+
return GrB_Info(
72+
ccall(
73+
dlsym(graphblas_lib, fn_name),
74+
Cint,
75+
(Ptr{Cvoid}, Ptr{U}, Ptr{T}, Cintmax_t, Ptr{Cvoid}),
76+
w.p, I_ptr, X_ptr, nvals, dup.p
77+
)
78+
)
79+
end
80+
81+
function GrB_Vector_setElement(w::GrB_Vector, x::T, i::U) where {U <: GrB_Index, T <: valid_int_types}
82+
fn_name = "GrB_Vector_setElement_" * get_suffix(T)
83+
return GrB_Info(
84+
ccall(
85+
dlsym(graphblas_lib, fn_name),
86+
Cint,
87+
(Ptr{Cvoid}, Cintmax_t, Cintmax_t),
88+
w.p, x, i
89+
)
90+
)
91+
end
92+
93+
function GrB_Vector_setElement(w::GrB_Vector, x::Float32, i::U) where U <: GrB_Index
94+
fn_name = "GrB_Vector_setElement_FP32"
95+
return GrB_Info(
96+
ccall(
97+
dlsym(graphblas_lib, fn_name),
98+
Cint,
99+
(Ptr{Cvoid}, Cfloat, Cintmax_t),
100+
w.p, x, i
101+
)
102+
)
103+
end
104+
105+
function GrB_Vector_setElement(w::GrB_Vector, x::Float64, i::U) where U <: GrB_Index
106+
fn_name = "GrB_Vector_setElement_FP64"
107+
return GrB_Info(
108+
ccall(
109+
dlsym(graphblas_lib, fn_name),
110+
Cint,
111+
(Ptr{Cvoid}, Cdouble, Cintmax_t),
112+
w.p, x, i
113+
)
114+
)
115+
end
116+
117+
function GrB_Vector_extractElement(v::GrB_Vector, i::U) where U <: GrB_Index
118+
res, v_type = GxB_Vector_type(v)
119+
res != GrB_SUCCESS && return res
120+
suffix, T = get_suffix_and_type(v_type)
121+
fn_name = "GrB_Vector_extractElement_" * suffix
122+
123+
element = Ref(T(0))
124+
result = GrB_Info(
125+
ccall(
126+
dlsym(graphblas_lib, fn_name),
127+
Cint,
128+
(Ptr{Cvoid}, Ptr{Cvoid}, Cintmax_t),
129+
element, v.p, i
130+
)
131+
)
132+
result != GrB_SUCCESS && return result
133+
return element[]
134+
end
135+
136+
function GrB_Vector_extractTuples(v::GrB_Vector)
137+
res, v_type = GxB_Vector_type(v)
138+
res != GrB_SUCCESS && return res
139+
suffix, T = get_suffix_and_type(v_type)
140+
nvals = GrB_Vector_nvals(v)
141+
I = Vector{typeof(nvals)}(undef, nvals)
142+
X = Vector{T}(undef, nvals)
143+
n = Ref(UInt64(nvals))
144+
145+
fn_name = "GrB_Vector_extractTuples_" * suffix
146+
result = GrB_Info(
147+
ccall(
148+
dlsym(graphblas_lib, fn_name),
149+
Cint,
150+
(Ptr{Cvoid}, Ptr{Cvoid}, Ptr{UInt64}, Ptr{Cvoid}),
151+
pointer(I), pointer(X), n, v.p
152+
)
153+
)
154+
result != GrB_SUCCESS && return result
155+
return I, X
156+
end

src/Structures.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Base.show
22
import Base.==
3-
export GrB_Type, GrB_UnaryOp, GrB_BinaryOp, GrB_Matrix
3+
export GrB_Type, GrB_UnaryOp, GrB_BinaryOp, GrB_Vector, GrB_Matrix
44

55
mutable struct GrB_Type
66
p::Ptr{Cvoid}
@@ -23,6 +23,12 @@ end
2323
GrB_BinaryOp() = GrB_BinaryOp(Ptr{Cvoid}(0))
2424
Base.show(io::IO, ::GrB_BinaryOp) = print("GrB_BinaryOp")
2525

26+
mutable struct GrB_Vector
27+
p::Ptr{Cvoid}
28+
end
29+
GrB_Vector() = GrB_Vector(Ptr{Cvoid}(0))
30+
Base.show(io::IO, ::GrB_Vector) = print("GrB_Vector")
31+
2632
mutable struct GrB_Matrix
2733
p::Ptr{Cvoid}
2834
end

src/SuiteSparseGraphBLAS.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ include("Enums.jl")
7171
include("Context_Methods.jl")
7272
include("Utils.jl")
7373
include("Object_Methods/Matrix_Methods.jl")
74+
include("Object_Methods/Vector_Methods.jl")
7475
include("Object_Methods/Print_Objects.jl")
7576

7677
export
@@ -82,8 +83,12 @@ GrB_Matrix_new, GrB_Matrix_build, GrB_Matrix_dup, GrB_Matrix_clear,
8283
GrB_Matrix_nrows, GrB_Matrix_ncols, GrB_Matrix_nvals, GrB_Matrix_setElement,
8384
GrB_Matrix_extractElement, GrB_Matrix_extractTuples,
8485

85-
# Print function
86-
@GxB_Matrix_fprint
86+
# Vector Methods
87+
GrB_Vector_new, GrB_Vector_build, GrB_Vector_dup, GrB_Vector_clear, GrB_Vector_size,
88+
GrB_Vector_nvals, GrB_Vector_setElement, GrB_Vector_extractElement, GrB_Vector_extractTuples,
89+
90+
# Print functions
91+
@GxB_Matrix_fprint, @GxB_Vector_fprint
8792

8893
# Export global variables
8994

src/Utils.jl

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
function get_suffix(T::DataType)
22
if T == Bool
3-
return "BOOL"
3+
return "BOOL"
44
elseif T == Int8
5-
return "INT8"
5+
return "INT8"
66
elseif T == UInt8
7-
return "UINT8"
7+
return "UINT8"
88
elseif T == Int16
9-
return "INT16"
9+
return "INT16"
1010
elseif T == UInt16
11-
return "UINT16"
11+
return "UINT16"
1212
elseif T == Int32
13-
return "INT32"
13+
return "INT32"
1414
elseif T == UInt32
15-
return "UINT32"
15+
return "UINT32"
1616
elseif T == Int64
17-
return "INT64"
17+
return "INT64"
1818
elseif T == UInt64
19-
return "UINT64"
19+
return "UINT64"
2020
elseif T == Float32
2121
return "FP32"
2222
end
@@ -62,6 +62,20 @@ function GxB_Matrix_type(A::GrB_Matrix)
6262
return result, type
6363
end
6464

65+
function GxB_Vector_type(v::GrB_Vector)
66+
type = GrB_Type()
67+
type_ptr = pointer_from_objref(type)
68+
result = GrB_Info(
69+
ccall(
70+
dlsym(graphblas_lib, "GxB_Vector_type"),
71+
Cint,
72+
(Ptr{Cvoid}, Ptr{Cvoid}),
73+
type_ptr, v.p
74+
)
75+
)
76+
return result, type
77+
end
78+
6579
function _GrB_Index(x::T) where T <: GrB_Index
6680
x > typemax(Int64) && return x
6781
return Int64(x)

0 commit comments

Comments
 (0)