Skip to content

Commit efdd0bf

Browse files
author
Wimmerer
committed
asArray and misc changes
1 parent 77da949 commit efdd0bf

File tree

17 files changed

+385
-104
lines changed

17 files changed

+385
-104
lines changed

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ authors = ["Will Kimmerer <[email protected]", "Abhinav Mehndiratta <abhina
44
version = "0.4.0"
55

66
[deps]
7-
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
87
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
98
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
109
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
@@ -15,9 +14,9 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1514
SSGraphBLAS_jll = "7ed9a814-9cab-54e9-8e9e-d9e95b4d61b1"
1615
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1716
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
17+
SuiteSparse = "4607b0f0-06f3-5cda-b6b1-a6196a1729e9"
1818

1919
[compat]
20-
CEnum = "0.4"
2120
MacroTools = "0.5"
2221
SSGraphBLAS_jll = "5.1"
2322
julia = "1.6"

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
# SuiteSparseGraphBLAS.jl
44
A fast, general sparse linear algebra and graph computation package, based on SuiteSparse:GraphBLAS.
55

6-
## v0.5
7-
v0.5 is planned to release in August, after the entire GraphBLAS interface is available and some memory pressure issues have been sorted out. Check back then for more benchmarks, a more Julian interface, automatic differentiation support with ChainRules.jl and better integration with the wider ecosystem!
8-
9-
If you're fine with sharp edges then give v0.4 a try, and let me know of any issues you find.
10-
The docs should provide enough information to run virtually the entire set of GraphBLAS functions.
11-
126
### Installation
137
```julia
148
using Pkg

benchmarks/Manifest.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ version = "0.4.2"
107107
deps = ["Random"]
108108
uuid = "9fa8497b-333b-5362-9e8d-4d0656e87820"
109109

110+
[[IntelOpenMP_jll]]
111+
deps = ["Artifacts", "JLLWrappers", "Libdl", "Pkg"]
112+
git-tree-sha1 = "d979e54b71da82f3a65b62553da4fc3d18c9004c"
113+
uuid = "1d5cc7b8-4909-519e-a0f8-d0f5ad9712d0"
114+
version = "2018.0.3+2"
115+
110116
[[InteractiveUtils]]
111117
deps = ["Markdown"]
112118
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
@@ -150,6 +156,10 @@ git-tree-sha1 = "8076680b162ada2a031f707ac7b4953e30667a37"
150156
uuid = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
151157
version = "0.21.2"
152158

159+
[[LazyArtifacts]]
160+
deps = ["Artifacts", "Pkg"]
161+
uuid = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
162+
153163
[[LibCURL]]
154164
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
155165
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
@@ -182,6 +192,18 @@ version = "0.3.4"
182192
[[Logging]]
183193
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
184194

195+
[[MKLSparse]]
196+
deps = ["LinearAlgebra", "MKL_jll", "SparseArrays"]
197+
git-tree-sha1 = "c5fd60128db7d4e0ad2358d1017d293c81a49335"
198+
uuid = "0c723cd3-b8cd-5d40-b370-ba682dde9aae"
199+
version = "1.1.0"
200+
201+
[[MKL_jll]]
202+
deps = ["Artifacts", "IntelOpenMP_jll", "JLLWrappers", "LazyArtifacts", "Libdl", "Pkg"]
203+
git-tree-sha1 = "5455aef09b40e5020e1520f551fa3135040d4ed0"
204+
uuid = "856f044c-d86e-5d09-b602-aeab76dc8ba7"
205+
version = "2021.1.1+2"
206+
185207
[[MacroTools]]
186208
deps = ["Markdown", "Random"]
187209
git-tree-sha1 = "3d3e902b31198a27340d0bf00d6ac452866021cf"

benchmarks/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[deps]
22
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
3+
MKLSparse = "0c723cd3-b8cd-5d40-b370-ba682dde9aae"
34
MatrixMarket = "4d4711f2-db25-561a-b6b3-d35e7d4047d3"
45
SuiteSparseGraphBLAS = "c2e53296-7b14-11e9-1210-bddfa8111e1d"
56
SuiteSparseMatrixCollection = "ac199af8-68bc-55b8-82c4-7abd6f96ed98"

benchmarks/benchmarks.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,17 @@ using BenchmarkTools
2626
using SparseArrays
2727
using LinearAlgebra
2828

29+
2930
#OPTIONS SET 1:
3031
# Maximum number of samples taken for each benchmark
3132
BenchmarkTools.DEFAULT_PARAMETERS.samples = 10
3233
# Total amount of time allowed for each benchmark, minimum of 1 sample taken.
3334
BenchmarkTools.DEFAULT_PARAMETERS.seconds = 60
3435

36+
# Comment or uncomment this line to disable or enable MKLSparse respectively.
37+
# This will only work for SpMM and SpMV and only operates on CSC.
38+
using MKLSparse
39+
3540
# Change this to change the size of the dense RHS of csrtimesfull and csctimesfull
3641
const sizefullrhs = [1,2,4]
3742

@@ -288,6 +293,8 @@ if length(ARGS) != 0
288293
if splitext(ARGS[1])[2] == ".mtx"
289294
singlebench(ARGS[1])
290295
else
296+
lines = readlines(ARGS[1])
297+
filter!((x) -> !occursin("#", x), lines)
291298
singlebench.(readlines(ARGS[1]))
292299
end
293300
elseif tryparse(Int64, ARGS[1]) !== nothing

benchmarks/do10

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,4 @@
33
# FIXME: the paths would need to point somewhere else.
44
# better yet, the sparse matrix IDs should be used so
55
# they can be directly downloaded from sparse.tamu.edu.
6-
julia benchmarks.jl /home/faculty/d/davis/Freescale2.mtx
7-
julia benchmarks.jl /home/faculty/d/davis/indochina-2004.mtx
8-
julia benchmarks.jl /home/faculty/d/davis/nd24k.mtx
9-
julia benchmarks.jl /home/faculty/d/davis/nd6k.mtx
10-
julia benchmarks.jl /home/faculty/d/davis/roadNet-CA.mtx
11-
julia benchmarks.jl /raid/GAP/GAP-twitter/GAP-twitter.mtx
12-
julia benchmarks.jl /raid/GAP/GAP-road/GAP-road.mtx
13-
julia benchmarks.jl /raid/GAP/GAP-web/GAP-web.mtx
14-
julia benchmarks.jl /raid/GAP/GAP-urand/GAP-urand.mtx
15-
julia benchmarks.jl /raid/GAP/GAP-kron/GAP-kron.mtx
6+
julia benchmarks.jl mtx.txt

benchmarks/mtx.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# You may provide paths or SuiteSparse ID numbers here.
2+
# A hashtag denotes a commented line.
3+
/home/faculty/d/davis/Freescale2.mtx
4+
/home/faculty/d/davis/indochina-2004.mtx
5+
/home/faculty/d/davis/nd24k.mtx
6+
/home/faculty/d/davis/nd6k.mtx
7+
/home/faculty/d/davis/roadNet-CA.mtx
8+
/raid/GAP/GAP-twitter/GAP-twitter.mtx
9+
/raid/GAP/GAP-road/GAP-road.mtx
10+
/raid/GAP/GAP-web/GAP-web.mtx
11+
/raid/GAP/GAP-urand/GAP-urand.mtx
12+
/raid/GAP/GAP-kron/GAP-kron.mtx

src/SuiteSparseGraphBLAS.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ using SparseArrays: nonzeroinds
1919
using MacroTools
2020
using LinearAlgebra
2121
using Random: randsubseq, default_rng, AbstractRNG, GLOBAL_RNG
22-
using CEnum
2322
using SpecialFunctions: lgamma, gamma, erf, erfc
2423
using Base.Broadcast
2524
include("abstracts.jl")
@@ -76,6 +75,8 @@ include("operations/resize.jl")
7675
include("print.jl")
7776
include("import.jl")
7877
include("export.jl")
78+
include("pack.jl")
79+
include("unpack.jl")
7980
include("options.jl")
8081
#EXPERIMENTAL
8182
include("operations/argminmax.jl")
@@ -89,6 +90,7 @@ include("chainrules/selectrules.jl")
8990
include("chainrules/constructorrules.jl")
9091
#include("random.jl")
9192
include("misc.jl")
93+
include("asjulia.jl")
9294
export libgb
9395
export UnaryOps, BinaryOps, Monoids, Semirings #Submodules
9496
export UnaryOp, BinaryOp, Monoid, Semiring #UDFs
@@ -130,6 +132,7 @@ function __init__()
130132
# In the future this should hopefully allow us to do no-copy passing of arrays between Julia and SS:GrB.
131133
# In the meantime it helps Julia respond to memory pressure from SS:GrB and finalize things in a timely fashion.
132134
libgb.GxB_init(libgb.GrB_NONBLOCKING, cglobal(:jl_malloc), cglobal(:jl_calloc), cglobal(:jl_realloc), cglobal(:jl_free), true)
135+
gbset(:nthreads, Sys.CPU_THREADS ÷ 2)
133136
# Eagerly load selectops constants.
134137
_loadselectops()
135138
# Set printing done by SuiteSparse:GraphBLAS to base-1 rather than base-0.

src/asjulia.jl

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
function asArray(f::Function, A::GBVecOrMat{T}; dropzeros=false, freeunpacked=false) where {T}
2+
if gbget(A, SPARSITY_STATUS) != GBDENSE
3+
X = similar(A)
4+
if X isa GBVector
5+
X[:] = zero(T)
6+
else
7+
X[:,:] = zero(T)
8+
end
9+
#I don't like this, it defeats the purpose of this method, which is to make no copies.
10+
# But somehow maintaining the input A in its original form is key to the to_vec implementation
11+
# for ChainRules. Temporarily it's fine, it's no worse than it originally was.
12+
# TODO: fix this issue with the ChainRules code.
13+
A = eadd(X, A)
14+
end
15+
array = _unpackdensematrix!(A)
16+
result = try
17+
f(array, A)
18+
finally
19+
if freeunpacked
20+
ccall(:jl_free, Cvoid, (Ptr{T},), pointer(array))
21+
else
22+
_packdensematrix!(A, array)
23+
if dropzeros
24+
select!(nonzeros, A)
25+
end
26+
end
27+
end
28+
return result
29+
end
30+
31+
function asSparseMatrixCSC(f::Function, A::GBMatrix{T}; freeunpacked=false) where {T}
32+
colptr, rowidx, values = _unpackcscmatrix!(A)
33+
array = SparseMatrixCSC{T, libgb.GrB_Index}(size(A, 1), size(A, 2), colptr, rowidx, values)
34+
result = try
35+
f(array, A)
36+
finally
37+
if freeunpacked
38+
ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(colptr))
39+
ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(rowidx))
40+
ccall(:jl_free, Cvoid, (Ptr{T},), pointer(values))
41+
else
42+
_packcscmatrix!(A, colptr, rowidx, values)
43+
end
44+
end
45+
return result
46+
end
47+
48+
function asSparseVector(f::Function, A::GBVector{T}; freeunpacked=false) where {T}
49+
colptr, rowidx, values = _unpackcscmatrix!(A)
50+
vector = SparseVector{T, libgb.GrB_Index}(size(A, 1), rowidx, values)
51+
result = try
52+
f(vector, A)
53+
finally
54+
if freeunpacked
55+
ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(colptr))
56+
ccall(:jl_free, Cvoid, (Ptr{libgb.GrB_Index},), pointer(rowidx))
57+
ccall(:jl_free, Cvoid, (Ptr{T},), pointer(values))
58+
else
59+
_packcscmatrix!(A, colptr, rowidx, values)
60+
end
61+
end
62+
return result
63+
end
64+
65+
66+
function Base.Matrix(A::GBMatrix)
67+
return asArray(A) do arr, _
68+
return copy(arr)
69+
end
70+
end
71+
72+
function Matrix!(A::GBMatrix)
73+
return asArray(A; freeunpacked=true) do arr, _
74+
return copy(arr)
75+
end
76+
end
77+
78+
function Base.Vector(v::GBVector)
79+
return asArray(v) do vec, _
80+
return copy(vec)
81+
end
82+
end
83+
84+
function Vector!(v::GBVector)
85+
return asArray(v; freeunpacked=true) do vec, _
86+
return copy(vec)
87+
end
88+
end
89+
90+
function SparseArrays.SparseMatrixCSC(A::GBMatrix)
91+
return asArray(A) do arr, _
92+
return copy(arr)
93+
end
94+
end
95+
96+
function SparseArrays.SparseVector(v::GBVector)
97+
return asArray(v) do arr, _
98+
return copy(arr)
99+
end
100+
end

src/chainrules/chainruleutils.jl

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,3 @@ end
2525

2626
# LinearAlgebra.norm doesn't like the nothings.
2727
LinearAlgebra.norm(A::GBArray, p::Real=2) = norm(nonzeros(A), p)
28-
29-
# Broadcast b into the rows of A. WARNING: THIS DOES NOT MATCH JULIA.
30-
function broadcast_emul!(C, A, b, op; mask = nothing, accum = nothing, desc = nothing)
31-
B = diagm(b)
32-
mul!(C, A, B, (any, op); mask, accum, desc)
33-
return C
34-
end
35-
36-
function broadcast_emul(A, b, op; mask = nothing, accum = nothing, desc = nothing)
37-
B = diagm(b)
38-
mul(A, B, (any, op); mask, accum, desc)
39-
end
40-
41-
function find_k(A, B::GBArray, op, minmax; mask = nothing, accum = nothing, desc = nothing)
42-
K = []
43-
for col axes(B, 2)
44-
intermediate = broadcast_emul(A, B[:, col], op; mask, accum, desc)
45-
push!(K, argminmax(intermediate, minmax, 2))
46-
end
47-
return hcat(K...)
48-
end

0 commit comments

Comments
 (0)