File: test87.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (267 lines) | stat: -rw-r--r-- 5,771 bytes parent folder | download | duplicates (3)
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
function test87
%TEST87 performance test of GrB_mxm

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

[save save_chunk] = nthreads_get ;
chunk = 4096 ;
nthreads = feature_numcores ;
nthreads_set (nthreads, chunk) ;

rng ('default') ;

%-------------------------------------------------------------------------------

fprintf ('\n--------------------------------------------------\n') ;

k = 30e6
fprintf ('building random sparse matrix, %d by %d\n', k,2) ;
A = sprandn (k, 2, 0.01) ;
B = sprandn (k, 2, 0.01) ;

fprintf ('builtin:\n') ;
tic
C = A'*B ;
toc
tm = toc ;

fprintf ('GrB AdotB:\n') ;
tic
C2 = GB_mex_AdotB (A,B) ;
toc

fprintf ('GrB (A'')*B:\n') ;
tic
C3 = GB_mex_AxB (A',B) ;
toc

fprintf ('GrB A''*B native:\n') ;
C4 = GB_mex_AxB (A,B, true) ;
tg = toc ;

assert (norm (C-C2,1) / norm (C,1) < 1e-12)
assert (norm (C-C3,1) / norm (C,1) < 1e-12)
assert (norm (C-C4,1) / norm (C,1) < 1e-12)

fprintf ('builtin: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;

k = 30e6
m = 100
fprintf ('building random sparse matrix, %d by %d\n', k,m) ;
A = sprandn (k, 2, 0.01) ;
B = sprandn (k, m, 0.01) ;

fprintf ('builtin:\n') ;
tic
C = A'*B ;
toc
tm = toc ;

fprintf ('GrB AdotB:\n') ;
tic
C2 = GB_mex_AdotB (A,B) ;
toc

fprintf ('GrB (A'')*B:\n') ;
tic
C3 = GB_mex_AxB (A',B) ;
tg1 = toc ;
fprintf ('just A*B %g (both A and B non-hypersparse)\n', tg1) ;

% this is slower than GB_mex_AxB (A',B) even though it uses the
% same method, because the builtin A' above is non-hypersparse,
% but the internal AT=A' is hypersparse.

fprintf ('GrB A''*B native (AT becomes hypersparse):\n') ;
tic
C4 = GB_mex_AxB (A,B, true) ;
tg = toc ;

fprintf ('builtin: %10.4f  GB:auto: %10.4f(%s) speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

assert (norm (C-C2,1) / norm (C,1) < 1e-12)
assert (norm (C-C3,1) / norm (C,1) < 1e-12)
assert (norm (C-C4,1) / norm (C,1) < 1e-12)

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;

fprintf ('matrix A above, transposed:\n') ;

tic
AT1 = A' ;
toc
tm = toc ;

[mm nn] = size (AT1) ;
S = sparse (mm,nn) ;

tic
AT2 = GB_mex_transpose (S, [ ], [ ], A)
tg = toc ;

assert (isequal (AT1, AT2.matrix)) ;

fprintf ('size of AT: %d %d\n', mm, nn) ;
fprintf ('builtin transpose %g GB %g speedup %g\n', tm, tg, tm/tg) ;

fprintf ('GrB (AT)*B:\n') ;
tic
C3 = GB_mex_AxB (AT1,B) ;
tg1 = toc ;
fprintf ('just A*B %g\n', tg1) ;

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;

fprintf ('\nA''*x where A is big and x is a dense vector\n') ;
Prob = ssget (2662) ;
A = Prob.A ;
n = size (A, 1) ;
x = sparse (rand (n,1)) ;
z = full (x) ;

fprintf ('builtin: x full:\n') ;
tic
y0 = A'*z ;
toc

fprintf ('builtin: x sparse:\n') ;
tic
y1 = A'*x ;
toc
tm = toc ;

fprintf ('GrB AdotB:\n') ;
tic
y2 = GB_mex_AdotB (A,x) ;
toc

fprintf ('GrB A''xB auto select:\n') ;
tic
y3 = GB_mex_AxB (A,x, true) ;
tg = toc ;
fprintf ('GrB time is %g\n', tg) ;

fprintf ('GrB (A'')xB outer:\n') ;
tic
y3 = GB_mex_AxB (A',x) ;
toc

assert (isequal (y1, sparse (y0))) ;
assert (isequal (y1, y2)) ;
% assert (isequal (y1, y3)) ;
assert (norm (y1-y3,1) / norm (y1,1) < eps)

fprintf ('builtin: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;

fprintf ('\nx''A where A is big and x is a dense vector\n') ;

fprintf ('builtin: x full:\n') ;
tic
y0 = z'*A ;
toc

fprintf ('builtin: x sparse:\n') ;
tic
y1 = x'*A ;
toc
tm = toc ;

fprintf ('GrB AdotB:\n') ;
tic
y2 = GB_mex_AdotB (x,A) ;
toc

fprintf ('GrB A''xB auto select:\n') ;
tic
y3 = GB_mex_AxB (x, A, true) ;
tg = toc ;

fprintf ('GrB (A''B outer:\n') ;
tic
y3 = GB_mex_AxB (x', A) ;
toc

assert (isequal (y1, sparse (y0))) ;
assert (isequal (y1, y2)) ;
% assert (isequal (y1, y3)) ;
assert (norm (y1-y2,1) / norm (y2,1) < eps)

fprintf ('builtin: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;
fprintf ('\nA*x where A is big and x is a dense vector\n') ;

fprintf ('builtin: x full:\n') ;
tic
y0 = A*z ;
toc

fprintf ('builtin: x sparse:\n') ;
tic
y1 = A*x ;
toc
tm = toc ;

fprintf ('GrB AxB:\n') ;
tic
y3 = GB_mex_AxB (A, x, false) ;
tg = toc ;

assert (isequal (y1, sparse (y0))) ;
% assert (isequal (y1, y3)) ;
assert (norm (y1-y3,1) / norm (y1,1) < eps)

fprintf ('builtin: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

%-------------------------------------------------------------------------------
fprintf ('\n--------------------------------------------------\n') ;

fprintf ('\nA''*x where A is big and x is a very sparse vector\n') ;
x = sprandn (n,1, 0.0001) ;

fprintf ('builtin: x sparse:\n') ;
tic
y1 = A'*x ;
toc
tm = toc ;

fprintf ('GrB AdotB:\n') ;
tic
y2 = GB_mex_AdotB (A,x) ;
toc

fprintf ('GrB A''xB auto select:\n') ;
tic
y3 = GB_mex_AxB (A,x, true) ;
tg = toc ;

fprintf ('GrB (A'')xB outer:\n') ;
tic
y3 = GB_mex_AxB (A',x) ;
toc

assert (isequal (y1, y2)) ;
% assert (isequal (y1, y3)) ;
assert (norm (y1-y3,1) / norm (y1,1) < eps)

fprintf ('builtin: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
    tm, tg, tm/tg) ;

fprintf ('\ntest87: all tests passed\n') ;

nthreads_set (save, save_chunk) ;