Skip to content

Commit 44b79de

Browse files
make matrix and vector structures parametric
1 parent 565495b commit 44b79de

File tree

4 files changed

+91
-127
lines changed

4 files changed

+91
-127
lines changed

src/Object_Methods/Matrix_Methods.jl

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@ julia> using SuiteSparseGraphBLAS
1010
julia> GrB_init(GrB_NONBLOCKING)
1111
GrB_SUCCESS::GrB_Info = 0
1212
13-
julia> MAT = GrB_Matrix()
14-
GrB_Matrix
13+
julia> MAT = GrB_Matrix{Int8}()
14+
GrB_Matrix{Int8}
1515
1616
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
1717
GrB_SUCCESS::GrB_Info = 0
1818
```
1919
"""
20-
function GrB_Matrix_new(A::GrB_Matrix, type::GrB_Type, nrows::T, ncols::T) where T <: GrB_Index
20+
function GrB_Matrix_new(A::GrB_Matrix{T}, type::GrB_Type, nrows::U, ncols::U) where {U <: GrB_Index, T <: valid_types}
2121
A_ptr = pointer_from_objref(A)
22-
22+
if jl_type(type) != T
23+
error("Given domain and matrix type do not match")
24+
end
2325
return GrB_Info(
2426
ccall(
2527
dlsym(graphblas_lib, "GrB_Matrix_new"),
@@ -42,8 +44,8 @@ julia> using SuiteSparseGraphBLAS
4244
julia> GrB_init(GrB_NONBLOCKING)
4345
GrB_SUCCESS::GrB_Info = 0
4446
45-
julia> MAT = GrB_Matrix()
46-
GrB_Matrix
47+
julia> MAT = GrB_Matrix{Int8}()
48+
GrB_Matrix{Int8}
4749
4850
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
4951
GrB_SUCCESS::GrB_Info = 0
@@ -73,11 +75,11 @@ row: 3 : 1 entries [4:4]
7375
7476
```
7577
"""
76-
function GrB_Matrix_build(C::GrB_Matrix, I::Vector{U}, J::Vector{U}, X::Vector{T}, nvals::U, dup::GrB_BinaryOp) where{U <: GrB_Index, T <: valid_types}
78+
function GrB_Matrix_build(C::GrB_Matrix{T}, I::Vector{U}, J::Vector{U}, X::Vector{T}, nvals::U, dup::GrB_BinaryOp) where{U <: GrB_Index, T <: valid_types}
7779
I_ptr = pointer(I)
7880
J_ptr = pointer(J)
7981
X_ptr = pointer(X)
80-
fn_name = "GrB_Matrix_build_" * get_suffix(T)
82+
fn_name = "GrB_Matrix_build_" * suffix(T)
8183
return GrB_Info(
8284
ccall(
8385
dlsym(graphblas_lib, fn_name),
@@ -101,8 +103,8 @@ julia> using SuiteSparseGraphBLAS
101103
julia> GrB_init(GrB_NONBLOCKING)
102104
GrB_SUCCESS::GrB_Info = 0
103105
104-
julia> MAT = GrB_Matrix()
105-
GrB_Matrix
106+
julia> MAT = GrB_Matrix{Int8}()
107+
GrB_Matrix{Int8}
106108
107109
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
108110
GrB_SUCCESS::GrB_Info = 0
@@ -138,8 +140,8 @@ julia> using SuiteSparseGraphBLAS
138140
julia> GrB_init(GrB_NONBLOCKING)
139141
GrB_SUCCESS::GrB_Info = 0
140142
141-
julia> MAT = GrB_Matrix()
142-
GrB_Matrix
143+
julia> MAT = GrB_Matrix{Int8}()
144+
GrB_Matrix{Int8}
143145
144146
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
145147
GrB_SUCCESS::GrB_Info = 0
@@ -175,8 +177,8 @@ julia> using SuiteSparseGraphBLAS
175177
julia> GrB_init(GrB_NONBLOCKING)
176178
GrB_SUCCESS::GrB_Info = 0
177179
178-
julia> MAT = GrB_Matrix()
179-
GrB_Matrix
180+
julia> MAT = GrB_Matrix{Int8}()
181+
GrB_Matrix{Int8}
180182
181183
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
182184
GrB_SUCCESS::GrB_Info = 0
@@ -216,8 +218,8 @@ julia> using SuiteSparseGraphBLAS
216218
julia> GrB_init(GrB_NONBLOCKING)
217219
GrB_SUCCESS::GrB_Info = 0
218220
219-
julia> MAT = GrB_Matrix()
220-
GrB_Matrix
221+
julia> MAT = GrB_Matrix{Int8}()
222+
GrB_Matrix{Int8}
221223
222224
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
223225
GrB_SUCCESS::GrB_Info = 0
@@ -227,8 +229,8 @@ julia> I = [1, 2, 2, 2, 3]; J = [1, 2, 1, 3, 3]; X = Int8[2, 3, 4, 5, 6]; n = 5;
227229
julia> GrB_Matrix_build(MAT, I, J, X, n, GrB_FIRST_INT8)
228230
GrB_SUCCESS::GrB_Info = 0
229231
230-
julia> B = GrB_Matrix()
231-
GrB_Matrix
232+
julia> B = GrB_Matrix{Int8}()
233+
GrB_Matrix{Int8}
232234
233235
julia> GrB_Matrix_dup(B, MAT)
234236
GrB_SUCCESS::GrB_Info = 0
@@ -252,7 +254,7 @@ row: 3 : 1 entries [4:4]
252254
253255
```
254256
"""
255-
function GrB_Matrix_dup(C::GrB_Matrix, A::GrB_Matrix)
257+
function GrB_Matrix_dup(C::GrB_Matrix{T}, A::GrB_Matrix{T}) where T <: valid_types
256258
C_ptr = pointer_from_objref(C)
257259

258260
return GrB_Info(
@@ -277,8 +279,8 @@ julia> using SuiteSparseGraphBLAS
277279
julia> GrB_init(GrB_NONBLOCKING)
278280
GrB_SUCCESS::GrB_Info = 0
279281
280-
julia> MAT = GrB_Matrix()
281-
GrB_Matrix
282+
julia> MAT = GrB_Matrix{Int8}()
283+
GrB_Matrix{Int8}
282284
283285
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
284286
GrB_SUCCESS::GrB_Info = 0
@@ -321,8 +323,8 @@ julia> using SuiteSparseGraphBLAS
321323
julia> GrB_init(GrB_NONBLOCKING)
322324
GrB_SUCCESS::GrB_Info = 0
323325
324-
julia> MAT = GrB_Matrix()
325-
GrB_Matrix
326+
julia> MAT = GrB_Matrix{Int8}()
327+
GrB_Matrix{Int8}
326328
327329
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
328330
GrB_SUCCESS::GrB_Info = 0
@@ -343,8 +345,8 @@ julia> GrB_Matrix_extractElement(MAT, 1, 1)
343345
7
344346
```
345347
"""
346-
function GrB_Matrix_setElement(C::GrB_Matrix, X::T, I::U, J::U) where {U <: GrB_Index, T <: valid_int_types}
347-
fn_name = "GrB_Matrix_setElement_" * get_suffix(T)
348+
function GrB_Matrix_setElement(C::GrB_Matrix{T}, X::T, I::U, J::U) where {U <: GrB_Index, T <: valid_int_types}
349+
fn_name = "GrB_Matrix_setElement_" * suffix(T)
348350
return GrB_Info(
349351
ccall(
350352
dlsym(graphblas_lib, fn_name),
@@ -355,7 +357,7 @@ function GrB_Matrix_setElement(C::GrB_Matrix, X::T, I::U, J::U) where {U <: GrB_
355357
)
356358
end
357359

358-
function GrB_Matrix_setElement(C::GrB_Matrix, X::Float32, I::U, J::U) where U <: GrB_Index
360+
function GrB_Matrix_setElement(C::GrB_Matrix{Float32}, X::Float32, I::U, J::U) where U <: GrB_Index
359361
fn_name = "GrB_Matrix_setElement_FP32"
360362
return GrB_Info(
361363
ccall(
@@ -367,7 +369,7 @@ function GrB_Matrix_setElement(C::GrB_Matrix, X::Float32, I::U, J::U) where U <:
367369
)
368370
end
369371

370-
function GrB_Matrix_setElement(C::GrB_Matrix, X::Float64, I::U, J::U) where U <: GrB_Index
372+
function GrB_Matrix_setElement(C::GrB_Matrix{Float64}, X::Float64, I::U, J::U) where U <: GrB_Index
371373
fn_name = "GrB_Matrix_setElement_FP64"
372374
return GrB_Info(
373375
ccall(
@@ -392,8 +394,8 @@ julia> using SuiteSparseGraphBLAS
392394
julia> GrB_init(GrB_NONBLOCKING)
393395
GrB_SUCCESS::GrB_Info = 0
394396
395-
julia> MAT = GrB_Matrix()
396-
GrB_Matrix
397+
julia> MAT = GrB_Matrix{Int8}()
398+
GrB_Matrix{Int8}
397399
398400
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
399401
GrB_SUCCESS::GrB_Info = 0
@@ -407,11 +409,8 @@ julia> GrB_Matrix_extractElement(MAT, 1, 1)
407409
2
408410
```
409411
"""
410-
function GrB_Matrix_extractElement(A::GrB_Matrix, row_index::U, col_index::U) where U <: GrB_Index
411-
res, A_type = GxB_Matrix_type(A)
412-
res != GrB_SUCCESS && return res
413-
suffix, T = get_suffix_and_type(A_type)
414-
fn_name = "GrB_Matrix_extractElement_" * suffix
412+
function GrB_Matrix_extractElement(A::GrB_Matrix{T}, row_index::U, col_index::U) where {U <: GrB_Index, T <: valid_types}
413+
fn_name = "GrB_Matrix_extractElement_" * suffix(T)
415414

416415
element = Ref(T(0))
417416
result = GrB_Info(
@@ -438,8 +437,8 @@ julia> using SuiteSparseGraphBLAS
438437
julia> GrB_init(GrB_NONBLOCKING)
439438
GrB_SUCCESS::GrB_Info = 0
440439
441-
julia> MAT = GrB_Matrix()
442-
GrB_Matrix
440+
julia> MAT = GrB_Matrix{Int8}()
441+
GrB_Matrix{Int8}
443442
444443
julia> GrB_Matrix_new(MAT, GrB_INT8, 4, 4)
445444
GrB_SUCCESS::GrB_Info = 0
@@ -453,18 +452,15 @@ julia> GrB_Matrix_extractTuples(MAT)
453452
([1, 2, 2, 2, 3], [1, 1, 2, 3, 3], Int8[2, 4, 3, 5, 6])
454453
```
455454
"""
456-
function GrB_Matrix_extractTuples(A::GrB_Matrix)
457-
res, A_type = GxB_Matrix_type(A)
458-
res != GrB_SUCCESS && return res
459-
suffix, T = get_suffix_and_type(A_type)
455+
function GrB_Matrix_extractTuples(A::GrB_Matrix{T}) where T <: valid_types
460456
nvals = GrB_Matrix_nvals(A)
461457
U = typeof(nvals)
462458
row_indices = Vector{U}(undef, nvals)
463459
col_indices = Vector{U}(undef, nvals)
464460
vals = Vector{T}(undef, nvals)
465461
n = Ref(UInt64(nvals))
466462

467-
fn_name = "GrB_Matrix_extractTuples_" * suffix
463+
fn_name = "GrB_Matrix_extractTuples_" * suffix(T)
468464
result = GrB_Info(
469465
ccall(
470466
dlsym(graphblas_lib, fn_name),

0 commit comments

Comments
 (0)