1function gbtest_perf2
2%GBTEST_PERF2 test A'*x performance
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: GPL-3.0-or-later
6
7max_nthreads = GrB.threads ;
8threads = [1 2 4 8 16 20 32 40 64] ;
9desc = struct ('in0', 'transpose') ;
10rng ('default') ;
11
12n = 1e6 ; nz = 20e6 ;
13% n = 1e5 ; nz = 1e6 ;
14d = nz / n^2 ;
15% same as A = sprand (n,n,d), but faster:
16G = GrB.random (n,n,d) ;
17A = double (G) ;
18% warmup to make sure the GrB library is loaded
19y = GrB (rand (2)) * GrB (rand (2)) ;
20
21degree = sum (spones (G)) ;
22nempty = length (find (degree == 0)) ;
23fprintf ('matrix: n: %d nnz: %d  # empty cols: %d\n', n, nnz (A), nempty) ;
24
25ntrials = 1 ;
26
27for test = 1:4
28
29    if (test == 1)
30        X = 'sparse (rand (n,1))' ;
31        x =  sparse (rand (n,1)) ;
32    elseif (test == 2)
33        X = 'rand (n,1)' ;
34        x =  rand (n,1) ;
35    elseif (test == 3)
36        X = 'sprand (n,1,0.5)' ;
37        x =  sprand (n,1,0.5) ;
38    else
39        X = 'sprand (n,1,0.05)' ;
40        x =  sprand (n,1,0.05) ;
41    end
42
43    fprintf ('\n\n========================\n') ;
44    fprintf ('in MATLAB: y = A''*x where x = %s\n', X) ;
45
46    tic
47    for trial = 1:ntrials
48        y = A'*x ;
49    end
50    tmatlab = toc ;
51    fprintf ('MATLAB time: %8.4f sec\n', tmatlab) ;
52    ymatlab = y ;
53
54    fprintf ('\nGrB: y = A''*x where x = %s\n', X) ;
55
56    for nthreads = threads
57        if (nthreads > max_nthreads)
58            break ;
59        end
60        GrB.threads (nthreads) ;
61        tic
62        for trial = 1:ntrials
63            % y = G'*x ;
64            y = GrB.mxm (G, '+.*', x, desc) ;
65        end
66        t = toc ;
67        if (nthreads == 1)
68            t1 = t ;
69        end
70        fprintf (...
71            'threads: %2d GrB time: %8.4f speedup vs MATLAB: %8.2f  vs: GrB(1 thread) %8.2f\n', ...
72            nthreads, t, tmatlab / t, t1 / t) ;
73        assert (norm (y-ymatlab, 1) / norm (ymatlab,1) < 1e-12)
74    end
75
76    fprintf ('\nGrB: y = zeros(n,1) + A''*x where x = %s\n', X) ;
77
78    for nthreads = threads
79        if (nthreads > max_nthreads)
80            break ;
81        end
82        GrB.threads (nthreads) ;
83        tic
84        for trial = 1:ntrials
85            y = zeros (n,1) ;
86            % y = y + G'*x
87            y = GrB.mxm (y, '+', G, '+.*', x, desc) ;
88        end
89        t = toc ;
90        if (nthreads == 1)
91            t1 = t ;
92        end
93        fprintf (...
94            'threads: %2d GrB time: %8.4f speedup vs MATLAB: %8.2f  vs: GrB(1 thread) %8.2f\n', ...
95            nthreads, t, tmatlab / t, t1 / t) ;
96        assert (norm (y-ymatlab, 1) / norm (ymatlab,1) < 1e-12)
97    end
98
99end
100
101GrB.burble (0) ;
102
103