Skip to content

Commit ab5ca46

Browse files
add matrix, vector and descriptor interface (#4)
* add matrix interface * add vector interface * make indices from 1 to n * add interface for descriptor methods
1 parent 3cb61fa commit ab5ca46

File tree

6 files changed

+736
-0
lines changed

6 files changed

+736
-0
lines changed

src/Interface/Interface.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Interface
2+
3+
using SuiteSparseGraphBLAS
4+
5+
import Base:
6+
getindex, setindex!, empty!, copy, size
7+
8+
import SuiteSparseGraphBLAS:
9+
GrB_Info, GrB_Index, GrB_Matrix, GrB_Vector, GrB_Descriptor, GrB_Desc_Field, GrB_Desc_Value,
10+
valid_types, get_GrB_Type, default_dup
11+
12+
include("./Object_Methods/Matrix_Methods.jl")
13+
include("./Object_Methods/Vector_Methods.jl")
14+
include("./Object_Methods/Descriptor_Methods.jl")
15+
export findnz, nnz
16+
17+
end # end of module
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
GrB_Descriptor(d::Dict{GrB_Desc_Field, GrB_Desc_Value})
3+
4+
Create a new GraphBLAS descriptor from a dictionary of descriptor field and value pairs.
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> desc = GrB_Descriptor(Dict(GrB_INP0 => GrB_TRAN, GrB_OUTP => GrB_REPLACE))
14+
GrB_Descriptor
15+
```
16+
"""
17+
function GrB_Descriptor(d::Dict{GrB_Desc_Field, GrB_Desc_Value})
18+
desc = GrB_Descriptor()
19+
res = GrB_Descriptor_new(desc)
20+
if res != GrB_SUCCESS
21+
error(res)
22+
end
23+
for (field, value) in d
24+
res = GrB_Descriptor_set(desc, field, value)
25+
if res != GrB_SUCCESS
26+
error(res)
27+
end
28+
end
29+
return desc
30+
end
31+
32+
"""
33+
setindex!(desc, val, field)
34+
35+
Set the value for a field of an existing descriptor
36+
37+
# Examples
38+
```jldoctest
39+
julia> using SuiteSparseGraphBLAS
40+
41+
julia> GrB_init(GrB_NONBLOCKING)
42+
GrB_SUCCESS::GrB_Info = 0
43+
44+
julia> desc = GrB_Descriptor(Dict(GrB_INP0 => GrB_TRAN))
45+
GrB_Descriptor
46+
47+
julia> desc[GrB_MASK] = GrB_SCMP;
48+
```
49+
"""
50+
function setindex!(desc::GrB_Descriptor, val::GrB_Desc_Value, field::GrB_Desc_Field)
51+
res = GrB_Descriptor_set(desc, field, val)
52+
if res != GrB_SUCCESS
53+
error(res)
54+
end
55+
end
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
"""
2+
GrB_Matrix(I, J, X,[ nrows, ncols, nvals, dup])
3+
4+
Create a GraphBLAS matrix of dimensions nrows x ncols such that A[I[k], J[k]] = X[k].
5+
dup is a GraphBLAS binary operator used to combine duplicates, it defaults to `FIRST`.
6+
If nrows and ncols are not specified, they are set to maximum(I) and maximum(J) respectively.
7+
nvals is set to length(I) is not specified.
8+
9+
# Examples
10+
```jldoctest
11+
julia> using SuiteSparseGraphBLAS
12+
13+
julia> A = GrB_Matrix([1, 1, 2, 3], [1, 1, 2, 3], UInt32[1, 10, 1, 1], dup = GrB_PLUS_UINT32)
14+
GrB_Matrix{UInt32}
15+
16+
julia> A[1, 1]
17+
0x0000000b
18+
```
19+
"""
20+
function GrB_Matrix(
21+
I::Vector{U},
22+
J::Vector{U},
23+
X::Vector{T};
24+
nrows::U = maximum(I),
25+
ncols::U = maximum(J),
26+
nvals::U = length(I),
27+
dup::GrB_BinaryOp = default_dup(T)) where {T <: valid_types, U <: GrB_Index}
28+
29+
A = GrB_Matrix{T}()
30+
GrB_T = get_GrB_Type(T)
31+
res = GrB_Matrix_new(A, GrB_T, nrows, ncols)
32+
if res != GrB_SUCCESS
33+
error(res)
34+
end
35+
res = GrB_Matrix_build(A, I.-1, J.-1, X, nvals, dup)
36+
if res != GrB_SUCCESS
37+
error(res)
38+
end
39+
return A
40+
end
41+
42+
"""
43+
GrB_Matrix(T, nrows, ncols)
44+
45+
Create an empty GraphBLAS matrix of type T and dimensions nrows x ncols.
46+
47+
# Examples
48+
```jldoctest
49+
julia> using SuiteSparseGraphBLAS
50+
51+
julia> GrB_init(GrB_NONBLOCKING)
52+
GrB_SUCCESS::GrB_Info = 0
53+
54+
julia> A = GrB_Matrix(Float64, 4, 4)
55+
GrB_Matrix{Float64}
56+
57+
julia> nnz(A)
58+
0
59+
```
60+
"""
61+
function GrB_Matrix(T::DataType, nrows::GrB_Index, ncols::GrB_Index)
62+
A = GrB_Matrix{T}()
63+
GrB_T = get_GrB_Type(T)
64+
res = GrB_Matrix_new(A, GrB_T, nrows, ncols)
65+
if res != GrB_SUCCESS
66+
error(res)
67+
end
68+
return A
69+
end
70+
71+
"""
72+
findnz(A)
73+
74+
Return a tuple (I, J, X) where I and J are the row and column indices of the stored values in GraphBLAS matrix A,
75+
and X is a vector of the values.
76+
77+
# Examples
78+
```jldoctest
79+
julia> using SuiteSparseGraphBLAS
80+
81+
julia> GrB_init(GrB_NONBLOCKING)
82+
GrB_SUCCESS::GrB_Info = 0
83+
84+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
85+
GrB_Matrix{Int64}
86+
87+
julia> findnz(A)
88+
([1, 2, 3], [1, 2, 3], [1, 1, 1])
89+
```
90+
"""
91+
function findnz(A::GrB_Matrix)
92+
res = GrB_Matrix_extractTuples(A)
93+
if typeof(res) == GrB_Info
94+
error(res)
95+
end
96+
I, J, X = res
97+
return I.+1, J.+1, X
98+
end
99+
100+
"""
101+
nnz(A)
102+
103+
Return the number of stored (filled) elements in a GraphBLAS matrix.
104+
105+
# Examples
106+
```jldoctest
107+
julia> using SuiteSparseGraphBLAS
108+
109+
julia> GrB_init(GrB_NONBLOCKING)
110+
GrB_SUCCESS::GrB_Info = 0
111+
112+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
113+
GrB_Matrix{Int64}
114+
115+
julia> nnz(A)
116+
3
117+
```
118+
"""
119+
function nnz(A::GrB_Matrix)
120+
nvals = GrB_Matrix_nvals(A)
121+
if typeof(nvals) == GrB_Info
122+
error(nvals)
123+
end
124+
return nvals
125+
end
126+
127+
"""
128+
size(A,[ dim])
129+
130+
Return number of rows or/and columns in a GraphBLAS matrix.
131+
132+
# Examples
133+
```jldoctest
134+
julia> using SuiteSparseGraphBLAS
135+
136+
julia> GrB_init(GrB_NONBLOCKING)
137+
GrB_SUCCESS::GrB_Info = 0
138+
139+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
140+
GrB_Matrix{Int64}
141+
142+
julia> size(A)
143+
(3, 3)
144+
145+
julia> size(A, 1)
146+
3
147+
148+
julia> size(A, 2)
149+
3
150+
```
151+
"""
152+
function size(A::GrB_Matrix)
153+
nrows = GrB_Matrix_nrows(A)
154+
if typeof(nrows) == GrB_Info
155+
error(nrows)
156+
end
157+
ncols = GrB_Matrix_ncols(A)
158+
if typeof(ncols) == GrB_Info
159+
error(ncols)
160+
end
161+
return (nrows, ncols)
162+
end
163+
164+
function size(A::GrB_Matrix, dim::Int64)
165+
if dim <= 0
166+
return error("dimension out of range")
167+
end
168+
169+
if dim == 1
170+
nrows = GrB_Matrix_nrows(A)
171+
if typeof(nrows) == GrB_Info
172+
error(nrows)
173+
end
174+
return nrows
175+
elseif dim == 2
176+
ncols = GrB_Matrix_ncols(A)
177+
if typeof(ncols) == GrB_Info
178+
error(ncols)
179+
end
180+
return ncols
181+
end
182+
183+
return 1
184+
end
185+
186+
"""
187+
getindex(A, row_index, col_index)
188+
189+
Return A[row_index, col_index] where A is a GraphBLAS matrix.
190+
191+
# Examples
192+
```jldoctest
193+
julia> using SuiteSparseGraphBLAS
194+
195+
julia> GrB_init(GrB_NONBLOCKING)
196+
GrB_SUCCESS::GrB_Info = 0
197+
198+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
199+
GrB_Matrix{Int64}
200+
201+
julia> A[1, 1]
202+
1
203+
```
204+
"""
205+
function getindex(A::GrB_Matrix, row_index::GrB_Index, col_index::GrB_Index)
206+
res = GrB_Matrix_extractElement(A, row_index-1, col_index-1)
207+
if typeof(res) == GrB_Info
208+
error(res)
209+
end
210+
return res
211+
end
212+
213+
"""
214+
setindex!(A, X, I, J)
215+
216+
Set A[I, J] = X where A is a GraphBLAS matrix.
217+
218+
# Examples
219+
```jldoctest
220+
julia> using SuiteSparseGraphBLAS
221+
222+
julia> GrB_init(GrB_NONBLOCKING)
223+
GrB_SUCCESS::GrB_Info = 0
224+
225+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
226+
GrB_Matrix{Int64}
227+
228+
julia> A[1, 1]
229+
1
230+
231+
julia> A[1, 1] = 5
232+
5
233+
234+
julia> A[1, 1]
235+
5
236+
```
237+
"""
238+
function setindex!(A::GrB_Matrix{T}, X::T, I::GrB_Index, J::GrB_Index) where {T <: valid_types}
239+
res = GrB_Matrix_setElement(A, X, I-1, J-1)
240+
if res != GrB_SUCCESS
241+
error(res)
242+
end
243+
end
244+
245+
"""
246+
empty!(A)
247+
248+
Remove all stored entries from GraphBLAS matrix A.
249+
250+
# Examples
251+
```jldoctest
252+
julia> using SuiteSparseGraphBLAS
253+
254+
julia> GrB_init(GrB_NONBLOCKING)
255+
GrB_SUCCESS::GrB_Info = 0
256+
257+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
258+
GrB_Matrix{Int64}
259+
260+
julia> nnz(A)
261+
3
262+
263+
julia> empty!(A)
264+
265+
julia> nnz(A)
266+
0
267+
```
268+
"""
269+
function empty!(A::GrB_Matrix)
270+
res = GrB_Matrix_clear(A)
271+
if res != GrB_SUCCESS
272+
error(res)
273+
end
274+
end
275+
276+
"""
277+
copy(A)
278+
279+
Create a new GraphBLAS matrix with the same domain, dimensions, and contents as GraphBLAS matrix A.
280+
281+
# Examples
282+
```jldoctest
283+
julia> using SuiteSparseGraphBLAS
284+
285+
julia> GrB_init(GrB_NONBLOCKING)
286+
GrB_SUCCESS::GrB_Info = 0
287+
288+
julia> A = GrB_Matrix([1, 2, 3], [1, 2, 3], [1, 1, 1])
289+
GrB_Matrix{Int64}
290+
291+
julia> B = copy(A)
292+
GrB_Matrix{Int64}
293+
294+
julia> findnz(B)
295+
([1, 2, 3], [1, 2, 3], [1, 1, 1])
296+
```
297+
"""
298+
function copy(A::GrB_Matrix{T}) where T <: valid_types
299+
C = GrB_Matrix{T}()
300+
res = GrB_Matrix_dup(C, A)
301+
if res != GrB_SUCCESS
302+
error(res)
303+
end
304+
return C
305+
end

0 commit comments

Comments
 (0)