1function test87
2%TEST87 performance test of GrB_mxm
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7[save save_chunk] = nthreads_get ;
8chunk = 4096 ;
9nthreads = feature ('numcores') ;
10nthreads_set (nthreads, chunk) ;
11
12rng ('default') ;
13
14%-------------------------------------------------------------------------------
15
16fprintf ('\n--------------------------------------------------\n') ;
17
18k = 30e6
19fprintf ('building random sparse matrix, %d by %d\n', k,2) ;
20A = sprandn (k, 2, 0.01) ;
21B = sprandn (k, 2, 0.01) ;
22
23fprintf ('MATLAB:\n') ;
24tic
25C = A'*B ;
26toc
27tm = toc ;
28
29fprintf ('GrB AdotB:\n') ;
30tic
31C2 = GB_mex_AdotB (A,B) ;
32toc
33
34fprintf ('GrB (A'')*B:\n') ;
35tic
36C3 = GB_mex_AxB (A',B) ;
37toc
38
39fprintf ('GrB A''*B native:\n') ;
40% tic
41C4 = GB_mex_AxB (A,B, true) ;
42% toc
43tg = grbresults ;
44
45assert (norm (C-C2,1) / norm (C,1) < 1e-12)
46assert (norm (C-C3,1) / norm (C,1) < 1e-12)
47assert (norm (C-C4,1) / norm (C,1) < 1e-12)
48
49fprintf ('MATLAB: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
50    tm, tg, tm/tg) ;
51
52%-------------------------------------------------------------------------------
53fprintf ('\n--------------------------------------------------\n') ;
54
55k = 30e6
56m = 100
57fprintf ('building random sparse matrix, %d by %d\n', k,m) ;
58A = sprandn (k, 2, 0.01) ;
59B = sprandn (k, m, 0.01) ;
60
61fprintf ('MATLAB:\n') ;
62tic
63C = A'*B ;
64toc
65tm = toc ;
66
67fprintf ('GrB AdotB:\n') ;
68tic
69C2 = GB_mex_AdotB (A,B) ;
70toc
71
72fprintf ('GrB (A'')*B:\n') ;
73tic
74C3 = GB_mex_AxB (A',B) ;
75toc
76tg1 = grbresults ;
77fprintf ('just A*B %g (both A and B non-hypersparse)\n', tg1) ;
78
79% this is slower than GB_mex_AxB (A',B) even though it uses the
80% same method, because the MATLAB A' above is non-hypersparse,
81% but the internal AT=A' is hypersparse.
82
83fprintf ('GrB A''*B native (AT becomes hypersparse):\n') ;
84tic
85C4 = GB_mex_AxB (A,B, true) ;
86toc
87tg = grbresults ;
88
89fprintf ('MATLAB: %10.4f  GB:auto: %10.4f(%s) speedup %10.4f\n', ...
90    tm, tg, tm/tg) ;
91
92assert (norm (C-C2,1) / norm (C,1) < 1e-12)
93assert (norm (C-C3,1) / norm (C,1) < 1e-12)
94assert (norm (C-C4,1) / norm (C,1) < 1e-12)
95
96%-------------------------------------------------------------------------------
97fprintf ('\n--------------------------------------------------\n') ;
98
99fprintf ('matrix A above, transposed:\n') ;
100
101tic
102AT1 = A' ;
103toc
104tm = toc ;
105
106[mm nn] = size (AT1) ;
107S = sparse (mm,nn) ;
108
109tic
110AT2 = GB_mex_transpose (S, [ ], [ ], A)
111toc
112tg = grbresults ;
113
114assert (isequal (AT1, AT2.matrix)) ;
115
116fprintf ('size of AT: %d %d\n', mm, nn) ;
117fprintf ('MATLAB transpose %g GB %g speedup %g\n', tm, tg, tm/tg) ;
118
119fprintf ('GrB (AT)*B:\n') ;
120tic
121C3 = GB_mex_AxB (AT1,B) ;
122toc
123tg1 = grbresults ;
124fprintf ('just A*B %g\n', tg1) ;
125
126%-------------------------------------------------------------------------------
127fprintf ('\n--------------------------------------------------\n') ;
128
129fprintf ('\nA''*x where A is big and x is a dense vector\n') ;
130Prob = ssget (2662) ;
131A = Prob.A ;
132n = size (A, 1) ;
133x = sparse (rand (n,1)) ;
134z = full (x) ;
135
136fprintf ('MATLAB: x full:\n') ;
137tic
138y0 = A'*z ;
139toc
140
141fprintf ('MATLAB: x sparse:\n') ;
142tic
143y1 = A'*x ;
144toc
145tm = toc ;
146
147fprintf ('GrB AdotB:\n') ;
148tic
149y2 = GB_mex_AdotB (A,x) ;
150toc
151
152fprintf ('GrB A''xB auto select:\n') ;
153tic
154y3 = GB_mex_AxB (A,x, true) ;
155toc
156tg = grbresults ;
157fprintf ('GrB time is %g\n', tg) ;
158
159fprintf ('GrB (A'')xB outer:\n') ;
160tic
161y3 = GB_mex_AxB (A',x) ;
162toc
163
164assert (isequal (y1, sparse (y0))) ;
165assert (isequal (y1, y2)) ;
166% assert (isequal (y1, y3)) ;
167assert (norm (y1-y3,1) / norm (y1,1) < eps)
168
169fprintf ('MATLAB: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
170    tm, tg, tm/tg) ;
171
172%-------------------------------------------------------------------------------
173fprintf ('\n--------------------------------------------------\n') ;
174
175fprintf ('\nx''A where A is big and x is a dense vector\n') ;
176
177fprintf ('MATLAB: x full:\n') ;
178tic
179y0 = z'*A ;
180toc
181
182fprintf ('MATLAB: x sparse:\n') ;
183tic
184y1 = x'*A ;
185toc
186tm = toc ;
187
188fprintf ('GrB AdotB:\n') ;
189tic
190y2 = GB_mex_AdotB (x,A) ;
191toc
192
193fprintf ('GrB A''xB auto select:\n') ;
194tic
195y3 = GB_mex_AxB (x, A, true) ;
196toc
197tg = grbresults ;
198
199fprintf ('GrB (A''B outer:\n') ;
200tic
201y3 = GB_mex_AxB (x', A) ;
202toc
203
204assert (isequal (y1, sparse (y0))) ;
205assert (isequal (y1, y2)) ;
206% assert (isequal (y1, y3)) ;
207assert (norm (y1-y2,1) / norm (y2,1) < eps)
208
209fprintf ('MATLAB: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
210    tm, tg, tm/tg) ;
211
212%-------------------------------------------------------------------------------
213fprintf ('\n--------------------------------------------------\n') ;
214fprintf ('\nA*x where A is big and x is a dense vector\n') ;
215
216fprintf ('MATLAB: x full:\n') ;
217tic
218y0 = A*z ;
219toc
220
221fprintf ('MATLAB: x sparse:\n') ;
222tic
223y1 = A*x ;
224toc
225tm = toc ;
226
227fprintf ('GrB AxB:\n') ;
228tic
229y3 = GB_mex_AxB (A, x, false) ;
230toc
231tg = grbresults ;
232
233assert (isequal (y1, sparse (y0))) ;
234% assert (isequal (y1, y3)) ;
235assert (norm (y1-y3,1) / norm (y1,1) < eps)
236
237fprintf ('MATLAB: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
238    tm, tg, tm/tg) ;
239
240%-------------------------------------------------------------------------------
241fprintf ('\n--------------------------------------------------\n') ;
242
243fprintf ('\nA''*x where A is big and x is a very sparse vector\n') ;
244x = sprandn (n,1, 0.0001) ;
245
246fprintf ('MATLAB: x sparse:\n') ;
247tic
248y1 = A'*x ;
249toc
250tm = toc ;
251
252fprintf ('GrB AdotB:\n') ;
253tic
254y2 = GB_mex_AdotB (A,x) ;
255toc
256
257fprintf ('GrB A''xB auto select:\n') ;
258tic
259y3 = GB_mex_AxB (A,x, true) ;
260toc
261tg = grbresults ;
262
263fprintf ('GrB (A'')xB outer:\n') ;
264tic
265y3 = GB_mex_AxB (A',x) ;
266toc
267
268assert (isequal (y1, y2)) ;
269% assert (isequal (y1, y3)) ;
270assert (norm (y1-y3,1) / norm (y1,1) < eps)
271
272fprintf ('MATLAB: %10.4f  GB:auto: %10.4f speedup %10.4f\n', ...
273    tm, tg, tm/tg) ;
274
275fprintf ('\ntest87: all tests passed\n') ;
276
277nthreads_set (save, save_chunk) ;
278