1function test66
2%TEST66 test GrB_reduce
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\ntest66: ---- quick test for GrB_reduce_to_scalar and vector\n') ;
8
9rng ('default') ;
10A = sparse (rand (4,3)) ;
11x = full (sum (sum (A))) + 3.1416
12c = GB_mex_reduce_to_scalar (3.1416, 'plus', 'plus', A) ;
13assert (isequal (x,c))
14
15tic
16x = full (sum (sum (A))) ;
17toc
18tic
19y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
20toc
21assert (norm(x-y) < nnz (A) * eps * norm(x))
22
23tic
24x = full (sum (A (:))) ;
25toc
26tic
27y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
28toc
29assert (norm(x-y) < nnz (A) * eps * norm(x))
30% assert (isequal (x,y))
31
32
33% reduce to vector
34y = sparse (4,1) ;
35y = GB_mex_reduce_to_vector (y, [ ], '', 'plus', A) ;
36y0 = sum (A')' ;
37err = norm (y.matrix - y0) / norm (y0) ;
38assert (err < 1e-14) ;
39% assert (isequal (y.matrix, sum (A')'))
40
41clear d
42d.inp0 = 'tran' ;
43y = sparse (3,1) ;
44y = GB_mex_reduce_to_vector (y, [ ], '', 'plus', A, d) ;
45y0 = sum (A)' ;
46err = norm (y.matrix - y0) / norm (y0) ;
47assert (err < 1e-14) ;
48% assert (isequal (y.matrix, sum (A)'))
49
50A = sprand (3e6, 3e6, 2e-6) ;
51n = size (A,1) ;
52yin = sparse (rand (n,1)) ;
53fprintf ('\nbig matrix with %g million entries\n', nnz (A)/ 1e6) ;
54
55% sum across the rows
56fprintf ('row sum with accum:\n') ;
57tic
58y2 = yin + (sum (A,2)) ;
59t1 = toc ;
60tic
61y = GB_mex_reduce_to_vector (yin, [ ], 'plus', 'plus', A) ;
62% t2 = toc ;
63t2 = grbresults ;
64fprintf ('MATLAB: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
65y1 = 1*y.matrix ;
66err = norm (y1-y2,1) / norm (y2,1) ;
67assert (err < 1e-14)
68% assert (isequal (y.matrix, y2))
69
70% sum across the rows (no accum)
71fprintf ('row sum no accum:\n') ;
72tic
73y2 = (sum (A,2)) ;
74t1 = toc ;
75tic
76y = GB_mex_reduce_to_vector (yin, [ ], [ ], 'plus', A) ;
77% t2 = toc ;
78t2 = grbresults ;
79fprintf ('MATLAB: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
80y1 = 1*y.matrix ;
81err = norm (y1-y2,1) / norm (y2,1) ;
82assert (err < 1e-14)
83% assert (isequal (y.matrix, y2))
84
85% sum down the columns
86fprintf ('col sum with accum:\n') ;
87yinrow = yin' ;
88tic
89y2 = yinrow + (sum (A,1)) ;
90t1 = toc ;
91tic
92y = GB_mex_reduce_to_vector (yin, [ ], 'plus', 'plus', A, d) ;
93% t2 = toc ;
94t2 = grbresults ;
95fprintf ('MATLAB: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
96y1 = 1*y.matrix ;
97err = norm (y1-y2',1) / norm (y2,1) ;
98assert (err < 1e-14)
99% assert (isequal (y.matrix, y2'))
100
101% sum down the columns
102fprintf ('col sum no accum:\n') ;
103yempty = sparse (n,1) ;
104tic
105y2 = (sum (A,1)) ;
106t1 = toc ;
107tic
108y = GB_mex_reduce_to_vector (yempty, [ ], [ ], 'plus', A, d) ;
109% t2 = toc ;
110t2 = grbresults ;
111fprintf ('MATLAB: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
112y1 = 1*y.matrix ;
113err = norm (y1-y2',1) / norm (y2,1) ;
114assert (err < 1e-14)
115% assert (isequal (y.matrix, y2'))
116
117% reduce to scalar
118fprintf ('to scalar:\n') ;
119tic
120x = full (sum (sum (A))) ;
121t1 = toc ;
122tic
123y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
124%t2 = toc ;
125t2 = grbresults ;
126fprintf ('MATLAB: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
127assert (norm(x-y) < nnz (A) * eps * norm(x))
128
129fprintf ('\ntest66: all tests passed\n') ;
130
131