File: test66.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 (125 lines) | stat: -rw-r--r-- 3,039 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
function test66
%TEST66 test GrB_reduce

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

fprintf ('\ntest66: ---- quick test for GrB_reduce_to_scalar and vector\n') ;

rng ('default') ;
A = sparse (rand (4,3)) ;
x = full (sum (sum (A))) + 3.1416
c = GB_mex_reduce_to_scalar (3.1416, 'plus', 'plus', A) ;
assert (isequal (x,c))

tic
x = full (sum (sum (A))) ;
toc
tic
y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
toc
assert (norm(x-y) < nnz (A) * eps * norm(x))

tic
x = full (sum (A (:))) ;
toc
tic
y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
toc
assert (norm(x-y) < nnz (A) * eps * norm(x))
% assert (isequal (x,y))


% reduce to vector
y = sparse (4,1) ;
y = GB_mex_reduce_to_vector (y, [ ], '', 'plus', A) ;
y0 = sum (A')' ;
err = norm (y.matrix - y0) / norm (y0) ;
assert (err < 1e-14) ;
% assert (isequal (y.matrix, sum (A')'))

clear d
d.inp0 = 'tran' ;
y = sparse (3,1) ;
y = GB_mex_reduce_to_vector (y, [ ], '', 'plus', A, d) ;
y0 = sum (A)' ;
err = norm (y.matrix - y0) / norm (y0) ;
assert (err < 1e-14) ;
% assert (isequal (y.matrix, sum (A)'))

A = sprand (3e6, 3e6, 2e-6) ;
n = size (A,1) ;
yin = sparse (rand (n,1)) ;
fprintf ('\nbig matrix with %g million entries\n', nnz (A)/ 1e6) ;

% sum across the rows
fprintf ('row sum with accum:\n') ;
tic
y2 = yin + (sum (A,2)) ;
t1 = toc ;
tic
y = GB_mex_reduce_to_vector (yin, [ ], 'plus', 'plus', A) ;
t2 = toc ;
fprintf ('builtin: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
y1 = 1*y.matrix ;
err = norm (y1-y2,1) / norm (y2,1) ;
assert (err < 1e-14)
% assert (isequal (y.matrix, y2))

% sum across the rows (no accum)
fprintf ('row sum no accum:\n') ;
tic
y2 = (sum (A,2)) ;
t1 = toc ;
tic
y = GB_mex_reduce_to_vector (yin, [ ], [ ], 'plus', A) ;
t2 = toc ;
fprintf ('builtin: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
y1 = 1*y.matrix ;
err = norm (y1-y2,1) / norm (y2,1) ;
assert (err < 1e-14)
% assert (isequal (y.matrix, y2))

% sum down the columns
fprintf ('col sum with accum:\n') ;
yinrow = yin' ;
tic
y2 = yinrow + (sum (A,1)) ;
t1 = toc ;
tic
y = GB_mex_reduce_to_vector (yin, [ ], 'plus', 'plus', A, d) ;
t2 = toc ;
fprintf ('builtin: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
y1 = 1*y.matrix ;
err = norm (y1-y2',1) / norm (y2,1) ;
assert (err < 1e-14)
% assert (isequal (y.matrix, y2'))

% sum down the columns
fprintf ('col sum no accum:\n') ;
yempty = sparse (n,1) ;
tic
y2 = (sum (A,1)) ;
t1 = toc ;
tic
y = GB_mex_reduce_to_vector (yempty, [ ], [ ], 'plus', A, d) ;
t2 = toc ;
fprintf ('builtin: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
y1 = 1*y.matrix ;
err = norm (y1-y2',1) / norm (y2,1) ;
assert (err < 1e-14)
% assert (isequal (y.matrix, y2'))

% reduce to scalar
fprintf ('to scalar:\n') ;
tic
x = full (sum (sum (A))) ;
t1 = toc ;
tic
y = GB_mex_reduce_to_scalar (0, '', 'plus', A) ;
t2 = toc ;
fprintf ('builtin: %g GraphBLAS %g speedup %g\n', t1, t2, t1/t2) ;
assert (norm(x-y) < nnz (A) * eps * norm(x))

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