Skip to content

Commit ef5a72f

Browse files
committed
early changes to arrays docs, reduce extract/getindex/setindex/assign verbosity in code
1 parent 421e267 commit ef5a72f

File tree

10 files changed

+58
-132
lines changed

10 files changed

+58
-132
lines changed

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
33
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
4-
SuiteSparseGraphBLAS = "c2e53296-7b14-11e9-1210-bddfa8111e1d"
4+
SuiteSparseGraphBLAS = "c2e53296-7b14-11e9-1210-bddfa8111e1d"

docs/src/arrays.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
11
# Array Types
22

3-
There are two primary datastructures in in `SuiteSparseGraphBLAS.jl`: the `GBVector` and `GBMatrix`.
3+
There are two primary array types in SuiteSparseGraphBLAS.jl: [`GBVector`](@ref) and [`GBMatrix`](@ref), as well as a few specialized versions of those array types. The full type hierarchy is:
44

5-
Both types currently implement most of the `AbstractArray` interface and part of the `SparseArrays`
6-
interface.
5+
```
6+
AbstractGBArray{T, N, F} <: AbstractSparseArray{T, N}
7+
├ N = 2 ─ AbstractGBMatrix{T, F}
8+
│ ├─ GBMatrix{T, F}
9+
│ └─ OrientedGBMatrix{T, F, O}
10+
└ N = 1 ─ AbstractGBVector{T, F}
11+
└─ GBVector{T, F}
12+
```
13+
14+
The `T` parameter is the element type of the array, `N` is the dimensionality, `F` is the type of the fill value (often `Nothing` or `T`). The [`OrientedGBMatrix`](@ref) restricts the orientation to the parameter `O` which is either `ByRow()` or `ByCol()`.
715

8-
## Matrix Construction
16+
All of these types attempt to implement most of the `AbstractArray` interface, and the relevant parts of the `SparseArrays` interface.
17+
18+
## GBMatrix
19+
20+
There are several methods to construct GBArrays. Shown here are empty construction, conversion from a dense matrix and a sparse matrix, and coordinate form with uniform or *iso* coefficients.
921
```@setup mat
1022
using SuiteSparseGraphBLAS
1123
using SparseArrays
1224
```
1325
```@repl mat
1426
x = GBMatrix{Bool}(20_000_000, 50_000)
1527
x = GBMatrix([[1,2] [3,4]])
16-
x = GBMatrix(sprand(100, 100, 0.5))
17-
x = GBMatrix(rand(1:50_000, 5000), rand(1:500_000, 5000), 1; ncols = 500_000, nrows = 500_000)
28+
x = GBMatrix(sprand(100, 100, 0.5); fill = 0.0)
29+
x = GBMatrix(
30+
rand(1:50_000, 5000), rand(1:500_000, 5000), 1;
31+
ncols = 500_000, nrows = 500_000
32+
)
1833
```
1934

2035
```@docs
2136
GBMatrix
2237
SuiteSparseGraphBLAS.GBMatrix(::Matrix)
23-
SuiteSparseGraphBLAS.GBMatrix(::SparseMatrixCSC)
2438
```
25-
Conversion back to matrices, sparse or dense, is also supported.
26-
## Vector Construction
39+
40+
## GBVector
2741
```@repl mat
2842
v = GBVector{ComplexF32}(100)
29-
v = GBMatrix(rand(ComplexF64, 3))
43+
v = GBMatrix(rand(ComplexF64, 3); fill = nothing)
3044
v = GBVector(sprand(Bool, 100_000_000, 0.001))
3145
```
3246

3347
```@docs
3448
GBVector
3549
SuiteSparseGraphBLAS.GBVector(::Vector)
36-
SuiteSparseGraphBLAS.GBVector(::AbstractVector{<:Integer}, ::AbstractVector)
3750
```
3851

3952
# Indexing

src/abstractgbarray.jl

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ function subassign!(C::AbstractGBArray, x, I, J;
241241
_subassign(C, x, I, ni, J, nj, mask, getaccum(accum, eltype(C)), desc)
242242
increment!(I)
243243
increment!(J)
244+
return C
244245
end
245246

246247
function subassign!(C::AbstractGBArray, x::AbstractArray, I, J;
@@ -294,39 +295,22 @@ end
294295
function assign!(C::AbstractGBArray, x, I, J;
295296
mask = nothing, accum = nothing, desc = nothing
296297
)
298+
I, ni = idx(I)
299+
J, nj = idx(J)
297300
I = decrement!(I)
298301
J = decrement!(J)
299302
desc = _handledescriptor(desc)
300303
_assign(gbpointer(C), x, I, ni, J, nj, mask, getaccum(accum, eltype(C)), desc)
301304
increment!(I)
302305
increment!(J)
303-
end
304-
305-
# setindex! uses subassign rather than assign.
306-
function Base.setindex!(
307-
C::AbstractGBMatrix, A, ::Colon, J;
308-
mask = nothing, accum = nothing, desc = nothing
309-
)
310-
subassign!(C, A, ALL, J; mask, accum, desc)
311-
end
312-
function Base.setindex!(
313-
C::AbstractGBMatrix, A, I, ::Colon;
314-
mask = nothing, accum = nothing, desc = nothing
315-
)
316-
subassign!(C, A, I, ALL; mask, accum, desc)
317-
end
318-
function Base.setindex!(
319-
C::AbstractGBMatrix, A, ::Colon, ::Colon;
320-
mask = nothing, accum = nothing, desc = nothing
321-
)
322-
subassign!(C, A, ALL, ALL; mask, accum, desc)
306+
return C
323307
end
324308

325309
function Base.setindex!(
326310
C::AbstractGBMatrix,
327311
A,
328-
I::Union{Vector, UnitRange, StepRange, Number},
329-
J::Union{Vector, UnitRange, StepRange, Number};
312+
I::Union{Vector, UnitRange, StepRange, Number, Colon},
313+
J::Union{Vector, UnitRange, StepRange, Number, Colon};
330314
mask = nothing,
331315
accum = nothing,
332316
desc = nothing
@@ -504,13 +488,6 @@ function assign!(w::AbstractGBVector{T, F}, u, I; mask = nothing, accum = nothin
504488
return assign!(GBMatrix{T, F}(w.p, w.fill), u, I, UInt64[1]; mask, accum, desc)
505489
end
506490

507-
function Base.setindex!(
508-
u::AbstractGBVector, x, ::Colon;
509-
mask = nothing, accum = nothing, desc = nothing
510-
)
511-
subassign!(u, x, ALL; mask, accum, desc)
512-
return nothing
513-
end
514491
# silly overload to help a bit with broadcasting.
515492
function Base.setindex!(
516493
u::AbstractGBVector, x, I::Union{Vector, UnitRange, StepRange, Colon}, ::Colon;
@@ -519,7 +496,7 @@ function Base.setindex!(
519496
Base.setindex!(u, x, I; mask, accum, desc)
520497
end
521498
function Base.setindex!(
522-
u::AbstractGBVector, x, I::Union{Vector, UnitRange, StepRange};
499+
u::AbstractGBVector, x, I::Union{Vector, UnitRange, StepRange, Colon};
523500
mask = nothing, accum = nothing, desc = nothing
524501
)
525502
subassign!(u, x, I; mask, accum, desc)
@@ -531,20 +508,7 @@ function Base.show(io::IO, ::MIME"text/plain", A::AbstractGBArray) #fallback pri
531508
end
532509

533510
function Base.getindex(
534-
A::AbstractGBMatrix, i, ::Colon;
535-
mask = nothing, accum = nothing, desc = nothing
536-
)
537-
return extract(A, i, ALL; mask, accum, desc)
538-
end
539-
function Base.getindex(
540-
A::AbstractGBMatrix, ::Colon, ::Colon;
541-
mask = nothing, accum = nothing, desc = nothing
542-
)
543-
return extract(A, ALL, ALL; mask, accum, desc)
544-
end
545-
546-
function Base.getindex(
547-
A::AbstractGBMatrix, i::Union{Vector, UnitRange, StepRange, Number}, j::Union{Vector, UnitRange, StepRange, Number};
511+
A::AbstractGBMatrix, i::Union{Vector, UnitRange, StepRange, Number, Colon}, j::Union{Vector, UnitRange, StepRange, Number, Colon};
548512
mask = nothing, accum = nothing, desc = nothing
549513
)
550514
return extract(A, i, j; mask, accum, desc)

src/import.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ end
6363
6464
Create a GBMatrix from a SparseArrays.SparseMatrixCSC `S`.
6565
66-
Note, that unlike other methods of construction, the resulting matrix will be held by column.
66+
Note, that unlike most other methods of construction, the resulting matrix will be held by column.
6767
Use `gbset(A, :format, :byrow)` to switch to row orientation.
6868
"""
6969
function GBMatrix(S::SparseMatrixCSC{T}; fill::F = nothing) where {T, F}
@@ -187,6 +187,8 @@ end
187187
GBMatrix(M::Matrix)
188188
189189
Create a GBMatrix from a Julia dense matrix.
190+
Note, that unlike other methods of construction, the resulting matrix will be held by column.
191+
Use `gbset(A, :format, :byrow)` to switch to row orientation.
190192
"""
191193
function GBMatrix(M::Union{AbstractVector{T}, AbstractMatrix{T}}; fill::F = nothing) where {T, F}
192194
if M isa AbstractVector && !(M isa Vector)

src/indexutils.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ scalar index like [`extractElement`].
77
"""
88
function idx(I)
99
# TODO. Do better here, and minimize manual idx management in rest of library.
10+
if I isa Colon
11+
I = ALL
12+
end
1013
if I == ALL
1114
return I, 0 #ni doesn't matter if I=ALL
1215
elseif I isa UnitRange

src/matrix.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GBMatrix{T}(size::Tuple{Base.OneTo, Base.OneTo}; fill = nothing) where {T} =
1919
GBMatrix{T}(size[1].stop, size[2].stop; fill)
2020

2121
"""
22-
GBMatrix(I, J, X; combine = +, nrows = maximum(I), ncols = maximum(J))
22+
GBMatrix(I, J, X; combine = +, nrows = maximum(I), ncols = maximum(J); fill = nothing)
2323
2424
Create an nrows x ncols GBMatrix M such that M[I[k], J[k]] = X[k]. The combine function defaults
2525
to `|` for booleans and `+` for nonbooleans.
@@ -38,7 +38,7 @@ end
3838

3939
#iso constructors
4040
"""
41-
GBMatrix(I, J, x; nrows = maximum(I), ncols = maximum(J))
41+
GBMatrix(I, J, x; nrows = maximum(I), ncols = maximum(J); fill = nothing)
4242
4343
Create an nrows x ncols GBMatrix M such that M[I[k], J[k]] = x.
4444
The resulting matrix is "iso-valued" such that it only stores `x` once rather than once for

src/operations/extract.jl

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Determine the size of the output for an operation like extract or range-based indexing.
55
"""
66
function _outlength(A, I, J)
7+
I isa Colon && (I = ALL)
8+
J isa Colon && (J = ALL)
79
if I == ALL
810
Ilen = size(A, 1)
911
else
@@ -18,6 +20,7 @@ function _outlength(A, I, J)
1820
end
1921

2022
function _outlength(u, I)
23+
I isa Colon && (I = ALL)
2124
if I == ALL
2225
wlen = size(u)
2326
else
@@ -70,28 +73,6 @@ function extract!(
7073
J isa Vector && increment!(J)
7174
return C
7275
end
73-
74-
function extract!(
75-
C::AbstractGBMatrix, A::GBMatOrTranspose, ::Colon, J;
76-
mask = nothing, accum = nothing, desc = nothing
77-
)
78-
return extract!(C, A, ALL, J; mask, accum, desc)
79-
end
80-
81-
function extract!(
82-
C::AbstractGBMatrix, A::GBMatOrTranspose, I, ::Colon;
83-
mask = nothing, accum = nothing, desc = nothing
84-
)
85-
return extract!(C, A, I, ALL; mask, accum, desc)
86-
end
87-
88-
function extract!(
89-
C::AbstractGBMatrix, A::GBMatOrTranspose, ::Colon, ::Colon;
90-
mask = nothing, accum = nothing, desc = nothing
91-
)
92-
return extract!(C, A, ALL, ALL; mask, accum, desc)
93-
end
94-
9576
"""
9677
extract(A::GBMatOrTranspose, I, J; kwargs...)::GBMatrix
9778
extract(A::GBVector, I; kwargs...)::GBVector
@@ -126,34 +107,6 @@ function extract(
126107
return extract!(C, A, I, J; mask, accum, desc)
127108
end
128109

129-
function extract(
130-
A::GBMatOrTranspose, ::Colon, J;
131-
mask = nothing, accum = nothing, desc = nothing
132-
)
133-
return extract(A, ALL, J; mask, accum, desc)
134-
end
135-
136-
function extract(
137-
A::GBMatOrTranspose, I, ::Colon;
138-
mask = nothing, accum = nothing, desc = nothing
139-
)
140-
return extract(A, I, ALL; mask, accum, desc)
141-
end
142-
143-
function extract(
144-
A::GBMatOrTranspose, ::Colon, ::Colon;
145-
mask = nothing, accum = nothing, desc = nothing
146-
)
147-
return extract(A, ALL, ALL; mask, accum, desc)
148-
end
149-
150-
function Base.getindex(
151-
A::GBMatOrTranspose, ::Colon, j;
152-
mask = nothing, accum = nothing, desc = nothing
153-
)
154-
return extract(A, ALL, j; mask, accum, desc)
155-
end
156-
157110
function extract!(
158111
w::AbstractGBVector, u::AbstractGBVector, I;
159112
mask = nothing, accum = nothing, desc = nothing
@@ -167,22 +120,11 @@ function extract!(
167120
return w
168121
end
169122

170-
function extract!(
171-
w::GBVector, u::GBVector, ::Colon;
172-
mask = nothing, accum = nothing, desc = nothing
173-
)
174-
return extract!(w, u, ALL; mask, accum, desc)
175-
end
176-
177123
function extract(
178124
u::GBVector, I;
179125
mask = nothing, accum = nothing, desc = nothing
180126
)
181127
wlen = _outlength(u, I)
182128
w = similar(u, wlen)
183129
return extract!(w, u, I; mask, accum, desc)
184-
end
185-
186-
function extract(u::GBVector, ::Colon; mask = nothing, accum = nothing, desc = nothing)
187-
extract(u, ALL; mask, accum, desc)
188-
end
130+
end

src/print.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function gxbstring(x, name = "", level::LibGraphBLAS.GxB_Print_Level = LibGraphB
3838
seekstart(f)
3939
x = read(f, String)
4040
close(cf)
41-
x
41+
lstrip(x)
4242
end
4343
return str
4444
end

src/types.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ mutable struct GBScalar{T}
249249
end
250250

251251
"""
252-
GBVector{T} <: AbstractSparseArray{T, UInt64, 1}
252+
GBVector{T, F} <: AbstractSparseArray{T, UInt64, 1}
253253
254-
One-dimensional GraphBLAS array with elements of type T. Internal representation is
255-
specified as opaque, but may be either a dense array, bitmap array, or
254+
One-dimensional GraphBLAS array with elements of type T. `F` is the type of the fill-value,
255+
which is typically `Nothing` or `T`.
256+
Internal representation is specified as opaque, but may be either a dense vector, bitmap vector, or
256257
compressed sparse vector.
257258
258259
See also: [`GBMatrix`](@ref).
@@ -263,11 +264,12 @@ mutable struct GBVector{T, F} <: AbstractGBVector{T, F}
263264
end
264265

265266
"""
266-
GBMatrix{T} <: AbstractSparseArray{T, UInt64, 2}
267+
GBMatrix{T, F} <: AbstractSparseArray{T, UInt64, 2}
267268
268-
Two-dimensional GraphBLAS array with elements of type T. Internal representation is
269-
specified as opaque, but in this implementation is stored as one of the following in either
270-
row or column orientation:
269+
Two-dimensional GraphBLAS array with elements of type `T`. `F` is the type of the fill-value,
270+
which is typically `Nothing` or `T`.
271+
Internal representation is specified as opaque, but in this implementation is stored as one of
272+
the following in either row or column orientation:
271273
272274
1. Dense
273275
2. Bitmap

src/vector.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GBVector{T}(nrows::Base.OneTo; fill = nothing) where {T} =
1919
GBVector{T}(nrows::Tuple{Base.OneTo,}; fill = nothing) where {T} = GBVector{T}(first(nrows); fill)
2020

2121
"""
22-
GBVector(I::AbstractVector, X::AbstractVector{T})
22+
GBVector(I::AbstractVector, X::AbstractVector{T}; fill = nothing)
2323
2424
Create a GBVector from a vector of indices `I` and a vector of values `X`.
2525
"""
@@ -33,7 +33,7 @@ end
3333

3434
#iso valued constructors.
3535
"""
36-
GBVector(I, x; nrows = maximum(I))
36+
GBVector(I, x; nrows = maximum(I) fill = nothing)
3737
3838
Create an `n` length GBVector `v` such that `M[I[k]] = x`.
3939
The resulting vector is "iso-valued" such that it only stores `x` once rather than once for
@@ -47,7 +47,7 @@ function GBVector(I::AbstractVector{U}, x::T;
4747
end
4848

4949
"""
50-
GBVector(n, x)
50+
GBVector(n, x; fill = nothing)
5151
5252
Create an `n` length dense GBVector `v` such that M[I[k]] = x.
5353
The resulting vector is "iso-valued" such that it only stores `x` once rather than once for

0 commit comments

Comments
 (0)