-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbenchmarks2.jl
276 lines (259 loc) · 9.22 KB
/
benchmarks2.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using Pkg
Pkg.activate(".")
Pkg.instantiate()
using SuiteSparseMatrixCollection
using SuiteSparseGraphBLAS
using SparseArrays
using LinearAlgebra
using StorageOrders
macro gbbench(ex)
return quote
gbset(:burble, true)
$(esc(ex))
gbset(:burble, false)
local taccum = 0
for i ∈ 1:3
local t0 = time_ns()
$(esc(ex))
local t1 = time_ns()
taccum += t1 - t0
end
taccum / 1e9
end
end
macro bench(ex)
return quote
local taccum = 0
for i ∈ 1:3
local t0 = time_ns()
$(esc(ex))
local t1 = time_ns()
taccum += t1 - t0
end
taccum / 1e9
end
end
# Comment or uncomment this line to disable or enable MKLSparse respectively.
# This will only work for SpMM and SpMV and only operates on CSC.
#using MKLSparse
const threadlist = [1, 2, 16]
const ssmc = ssmc_db()
function mxm(A::SparseMatrixCSC, B)
printstyled(stdout, "\nC = A::SparseMatrixCSC($(size(A))) * B::$(typeof(B))($(size(B)))\n")
result = @bench A * B
println(stdout, result, "s")
GC.gc()
flush(stdout)
return result
end
function mxm(A::SuiteSparseGraphBLAS.GBArray, B::SuiteSparseGraphBLAS.GBArray; accumdenseoutput=false)
Ao = storageorder(A) == ColMajor() ? "C" : "R"
Bo = storageorder(B) == ColMajor() ? "C" : "R"
if !accumdenseoutput
printstyled(stdout, "\nC::GBArray = A::GBArray($Ao, $(size(A))) * B::GBArray($Bo, $(size(B)))\n")
flush(stdout)
result = @gbbench *(A, B)
else
printstyled(stdout, "\nC::GBArray += A::GBArray($Ao, $(size(A))) * B::GBArray($Bo, $(size(B)))\n")
C = GBMatrix(zeros(eltype(A), size(A, 1), size(B, 2)))
flush(stdout)
result = @gbbench mul!(C, A, B; accum=+)
end
println(stdout, result, "s")
GC.gc()
flush(stdout)
return result
end
function tpose(A::SuiteSparseGraphBLAS.GBArray)
Ao = storageorder(A) == ColMajor() ? "C" : "R"
C = similar(A)
gbset(C, :format, storageorder(A) === ColMajor() ? SuiteSparseGraphBLAS.BYCOL : SuiteSparseGraphBLAS.BYROW)
Co = storageorder(A) == ColMajor() ? "C" : "R"
printstyled(stdout, "\nC::GBArray($(Co)) = transpose(A::GBArray($Ao, $(size(A))))\n")
result = @gbbench SuiteSparseGraphBLAS.gbtranspose!(C, A)
println(stdout, result, "s")
GC.gc()
flush(stdout)
return result
end
function tpose(A::SparseMatrixCSC)
printstyled(stdout, "\nC = transpose(A::SparseMatrixCSC($(size(A))))\n")
result = @bench copy(transpose(A))
println(stdout, result, "s")
GC.gc()
flush(stdout)
return result
end
function runthreadedt(A; accumdenseoutput=false)
v = []
for t ∈ threadlist
printstyled(stdout, "\nRunning GraphBLAS with $t threads\n"; bold=true)
gbset(:nthreads, t)
push!(v, tpose(A))
end
return v
end
function idx(C, A, I, J)
if C isa SuiteSparseGraphBLAS.AbstractGBArray
Ao = storageorder(A) == ColMajor() ? "C" : "R"
Co = storageorder(A) == ColMajor() ? "C" : "R"
printstyled(stdout, "\nC::GBArray($(Co))[I, J] = A::GBArray($Ao, $(size(A)))\n")
result = @gbbench begin
C[I, J] = A
wait(C)
end
println(stdout, result, "s")
GC.gc()
else
printstyled(stdout, "\nC[I, J] = A::SparseMatrixCSC($(size(A)))\n")
result = @bench C[I, J] = A
println(stdou, result, "s")
GC.gc()
end
flush(stdout)
return result
end
function runthreadedidx(C, A, I, J)
v = []
for t ∈ threadlist
printstyled(stdout, "\nRunning GraphBLAS with $t threads\n"; bold=true)
gbset(:nthreads, t)
push!(v, idx(C, A, I, J))
end
return v
end
function singlebench(pathornum)
x = tryparse(Int64, pathornum)
if x !== nothing
ssmc[x, :real] == true || throw(ArgumentError("SSMC ID must be for a matrix with real values"))
path = joinpath(fetch_ssmc(ssmc[x, :group], ssmc[x, :name]), "$(ssmc[x, :name]).mtx")
elseif isfile(pathornum)
path = pathornum
else
throw(ErrorException("Argument is not a path or SuiteSparseMatrixCollection ID number"))
end
name = basename(path)
A = SuiteSparseGraphBLAS.mmread(path)
if eltype(A) == Bool
A = Int64.(A)
end
GC.gc()
printstyled(stdout, "\n#################################################################################\n"; bold=true, color=:green)
printstyled(stdout, "Benchmarking $name:\n"; bold=true, color=:green)
printstyled(stdout, "#################################################################################\n"; bold=true, color=:green)
printstyled(stdout, "\nSparse * Vec\n"; bold=true)
println(stdout, "################################")
flush(stdout)
B = rand(eltype(A), size(A, 2))
B = GBVector(B)
#
gbresultsR = runthreaded(A, B; accumdenseoutput=true)
gbset(A, :format, SuiteSparseGraphBLAS.BYCOL)
diag(A)
gbresultsC = runthreaded(A, B; accumdenseoutput=true)
SAresults = mxm(SparseMatrixCSC(A), Vector(B))
printstyled(stdout, "\nRESULTS, Sparse * DenseVec: \n"; bold=true, color=:green)
println(stdout, "################################")
println(stdout, "A by row (1, 2, 16 thread): $gbresultsR")
println(stdout, "A by col (1, 2, 16 thread): $gbresultsC")
println(stdout, "SparseArrays: $SAresults")
flush(stdout)
#
printstyled(stdout, "\nSparse * (n x 2)\n"; bold=true)
println(stdout, "################################")
flush(stdout)
B = GBMatrix(rand(eltype(A), size(A, 2), 2))
gbset(A, :format, SuiteSparseGraphBLAS.BYROW)
diag(A)
gbresultsR = runthreaded(A, B; accumdenseoutput=true)
gbset(A, :format, SuiteSparseGraphBLAS.BYCOL)
diag(A)
gbresultsC = runthreaded(A, B; accumdenseoutput=true)
SAresults = mxm(SparseMatrixCSC(A), Matrix(B))
printstyled(stdout, "\nRESULTS, Sparse * n x 2 Dense: \n"; bold=true, color=:green)
println(stdout, "################################")
println(stdout, "A by row (1, 2, 16 thread): $gbresultsR")
println(stdout, "A by col (1, 2, 16 thread): $gbresultsC")
println(stdout, "SparseArrays: $SAresults")
flush(stdout)
#
printstyled(stdout, "\nSparse * (n x 32)\n"; bold=true)
println(stdout, "################################")
flush(stdout)
B = GBMatrix(rand(eltype(A), size(A, 2), 32))
gbset(A, :format, SuiteSparseGraphBLAS.BYROW)
diag(A)
gbresultsR = runthreaded(A, B; accumdenseoutput=true)
gbset(A, :format, SuiteSparseGraphBLAS.BYCOL)
diag(A)
gbresultsC = runthreaded(A, B; accumdenseoutput=true)
SAresults = mxm(SparseMatrixCSC(A), Matrix(B))
printstyled(stdout, "\nRESULTS, Sparse * n x 32 Dense: \n"; bold=true, color=:green)
println(stdout, "################################")
println(stdout, "A by row (1, 2, 16 thread): $gbresultsR")
println(stdout, "A by col (1, 2, 16 thread): $gbresultsC")
println(stdout, "SparseArrays: $SAresults")
flush(stdout)
#
printstyled(stdout, "\nSparse * Sparse'"; bold=true)
println(stdout, "################################")
flush(stdout)
gbset(A, :format, SuiteSparseGraphBLAS.BYROW)
diag(A)
gbresultsR = runthreaded(A, transpose(A))
gbset(A, :format, SuiteSparseGraphBLAS.BYCOL)
diag(A)
gbresultsC = runthreaded(A, transpose(A))
A2 = SparseMatrixCSC(A)
SAresults = mxm(A2, transpose(A2))
println(stdout, )
printstyled(stdout, "\nRESULTS, Sparse * n x 32 Dense: \n"; bold=true, color=:green)
println(stdout, "################################")
println(stdout, "A by row (1, 2, 16 thread): $gbresultsR")
println(stdout, "A by col (1, 2, 16 thread): $gbresultsC")
println(stdout, "SparseArrays: $SAresults")
flush(stdout)
printstyled(stdout, "\nC = copy(transpose(A))"; bold=true)
println(stdout, "################################")
flush(stdout)
gbset(A, :format, SuiteSparseGraphBLAS.BYROW)
diag(A)
gbresultsR = runthreadedt(A)
gbset(A, :format, SuiteSparseGraphBLAS.BYCOL)
diag(A)
gbresultsC = runthreadedt(A)
A2 = SparseMatrixCSC(A)
SAresults = tpose(A2)
println(stdout, )
printstyled(stdout, "\nRESULTS, C = copy(transpose(A)): \n"; bold=true, color=:green)
println(stdout, "################################")
println(stdout, "A by row (1, 2, 16 thread): $gbresultsR")
println(stdout, "A by col (1, 2, 16 thread): $gbresultsC")
println(stdout, "SparseArrays: $SAresults")
flush(stdout)
return nothing
end
function runthreaded(A, B; accumdenseoutput=false)
v = []
for t ∈ threadlist
printstyled(stdout, "\nRunning GraphBLAS with $t threads\n"; bold=true)
gbset(:nthreads, t)
push!(v, mxm(A, B; accumdenseoutput))
end
return v
end
if length(ARGS) != 0
if isfile(ARGS[1])
if splitext(ARGS[1])[2] == ".mtx"
singlebench(ARGS[1])
else
lines = readlines(ARGS[1])
filter!((x) -> !occursin("#", x), lines)
singlebench.(lines)
end
elseif tryparse(Int64, ARGS[1]) !== nothing
singlebench(ARGS[1])
else
throw(ArgumentError("The first argument must a file with a list of SuiteSparse ID numbers or paths to MatrixMarket files"))
end
end