1function test52
2%TEST52 test AdotB vs AxB
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\n----------------------- AdotB versus AxB\n') ;
8
9rng ('default')
10x = sparse (rand (10,1)) ;
11y = sparse (rand (10,1)) ;
12c = x'*y ;
13'AdotB'
14C = GB_mex_AdotB (x, y)
15'did AdotB'
16c - C
17assert (isequal (c, C))
18
19x = sprandn (100, 1, 0.2) ;
20y = sprandn (100, 1, 0.2) ;
21c = x'*y ;
22C = GB_mex_AdotB (x, y)
23assert (isequal (c, C))
24
25for m = 1:10
26    for n = 1:10
27        for k = [10 100 1000 10000]
28
29            A = sprandn (k, m, 0.2) ;
30            B = sprandn (k, n, 0.2) ;
31            Mask = sprand (m, n, 0.5) ;
32
33            C = A'*B ;
34            C2 = GB_mex_AdotB (A,B) ;
35
36            assert (isequal (C, C2)) ;
37            assert (GB_spok (C2) == 1)
38
39            C = spones (Mask) .* C ;
40            C2 = GB_mex_AdotB (A,B, Mask) ;
41
42            assert (isequal (C, C2)) ;
43            assert (GB_spok (C2) == 1)
44        end
45    end
46end
47
48relwork = [ ] ;
49reltime = [ ] ;
50
51k = 10e6 ;
52fprintf ('\nbuilding random sparse matrices %d by M\n', k) ;
53for m = [1 10 20:10:60 61:65 70:10:100]
54fprintf ('\n') ;
55for n = [1 10 20:10:60 61:65 70:10:100]
56
57    d = 0.001 ;
58    A = sprandn (k, m, d) ;
59    B = sprandn (k, n, d) ;
60    Mask = spones (sprandn (m, n, 0.5)) ;
61    % A (:,m) = sparse (rand (k,1)) ;
62    % B (:,m) = sparse (rand (k,1)) ;
63
64    cwork = m*n ;
65    awork = min (nnz(A) + k + m, nnz(B) + k + n) ;
66
67    relwork = [relwork cwork/awork] ;
68
69    % fprintf ('MATLAB:\n') ;
70    tic
71    C = A'*B ;
72    t1 = toc ;
73
74    % fprintf ('GrB AdotB:\n') ;
75    tic
76    C2 = GB_mex_AdotB (A,B) ;
77    t2 = toc ;
78
79    % fprintf ('GrB A''*B native:\n') ;
80    tic
81    C4 = GB_mex_AxB (A,B, true) ;
82    t4 = toc ;
83    t4 = grbresults ;
84
85    % fprintf ('GrB A''*B native:\n') ;
86    tic
87    C5 = GB_mex_AxB (A',B) ;
88    t5 = toc ;
89
90    reltime = [reltime t2/t5] ;
91
92    fprintf (...
93'm %3d n %3d %10.2e MATLAB: %10.4f AdotB : %10.4f GB,auto:: %10.4f outer %10.4f', ...
94    m, n, cwork/awork, t1, t2, t4, t5) ;
95    % fprintf (' speedup: %10.4f (no Mask)\n', t2/t5) ;
96    fprintf (' rel: %10.4f ', t2/t5) ;
97    fprintf (' speedup: %10.4f\n', t1/t4) ;
98
99    assert (isequal (C, C2)) ;
100    assert (isequal (C, C4)) ;
101    assert (isequal (C, C5)) ;
102
103    %{
104
105    % fprintf ('MATLAB:\n') ;
106    tic
107    C = Mask .* (A'*B) ;
108    t1 = toc ;
109
110    % fprintf ('GrB AdotB:\n') ;
111    tic
112    C2 = GB_mex_AdotB (A,B, Mask) ;
113    t2 = toc ;
114
115    % fprintf ('GrB A''*B native:\n') ;
116    tic
117    C4 = Mask .* GB_mex_AxB (A,B, true) ;
118    t4 = toc ;
119
120    % fprintf ('GrB A''*B native:\n') ;
121    tic
122    C5 = Mask .* GB_mex_AxB (A',B) ;
123    t5 = toc ;
124
125    fprintf (...
126    'm %2d MATLAB: %10.4f AdotB : %10.4f   GB,auto:: %10.4f outer %10.4f', ...
127    m, t1, t2, t4, t5) ;
128    fprintf (' speedup: %10.4f (with Mask)\n', t1/t4) ;
129
130    assert (isequal (C, C2)) ;
131    assert (isequal (C, C4)) ;
132    %}
133
134    loglog (relwork, reltime, 'o') ;
135    drawnow
136
137end
138end
139
140fprintf ('\ntest52: all tests passed\n') ;
141