File: gbtest_perf2.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 (102 lines) | stat: -rw-r--r-- 2,649 bytes parent folder | download | duplicates (2)
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
function gbtest_perf2
%GBTEST_PERF2 test A'*x performance

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

max_nthreads = GrB.threads ;
threads = [1 2 4 8 16 20 32 40 64] ;
desc = struct ('in0', 'transpose') ;
rng ('default') ;

n = 1e6 ; nz = 20e6 ;
% n = 1e5 ; nz = 1e6 ;
d = nz / n^2 ;
% same as A = sprand (n,n,d), but faster:
G = GrB.random (n,n,d) ;
A = double (G) ;
% warmup to make sure the GrB library is loaded
y = GrB (rand (2)) * GrB (rand (2)) ;

degree = sum (spones (G)) ;
nempty = length (find (degree == 0)) ;
fprintf ('matrix: n: %d nnz: %d  # empty cols: %d\n', n, nnz (A), nempty) ;

ntrials = 1 ;

for test = 1:4

    if (test == 1)
        X = 'sparse (rand (n,1))' ;
        x =  sparse (rand (n,1)) ;
    elseif (test == 2)
        X = 'rand (n,1)' ;
        x =  rand (n,1) ;
    elseif (test == 3)
        X = 'sprand (n,1,0.5)' ;
        x =  sprand (n,1,0.5) ;
    else
        X = 'sprand (n,1,0.05)' ;
        x =  sprand (n,1,0.05) ;
    end

    fprintf ('\n\n========================\n') ;
    fprintf ('built-in: y = A''*x where x = %s\n', X) ;

    tic
    for trial = 1:ntrials
        y = A'*x ;
    end
    tbuiltin = toc ;
    fprintf ('built-in time: %8.4f sec\n', tbuiltin) ;
    ybuiltin = y ;

    fprintf ('\nGrB: y = A''*x where x = %s\n', X) ;

    for nthreads = threads
        if (nthreads > max_nthreads)
            break ;
        end
        GrB.threads (nthreads) ;
        tic
        for trial = 1:ntrials
            % y = G'*x ;
            y = GrB.mxm (G, '+.*', x, desc) ;
        end
        t = toc ;
        if (nthreads == 1)
            t1 = t ;
        end
        fprintf (...
            'threads: %2d GrB time: %8.4f speedup vs built-in: %8.2f  vs: GrB(1 thread) %8.2f\n', ...
            nthreads, t, tbuiltin / t, t1 / t) ;
        assert (norm (y-ybuiltin, 1) / norm (ybuiltin,1) < 1e-12)
    end

    fprintf ('\nGrB: y = zeros(n,1) + A''*x where x = %s\n', X) ;

    for nthreads = threads
        if (nthreads > max_nthreads)
            break ;
        end
        GrB.threads (nthreads) ;
        tic
        for trial = 1:ntrials
            y = zeros (n,1) ;
            % y = y + G'*x
            y = GrB.mxm (y, '+', G, '+.*', x, desc) ;
        end
        t = toc ;
        if (nthreads == 1)
            t1 = t ;
        end
        fprintf (...
            'threads: %2d GrB time: %8.4f speedup vs built-in: %8.2f  vs: GrB(1 thread) %8.2f\n', ...
            nthreads, t, tbuiltin / t, t1 / t) ;
        assert (norm (y-ybuiltin, 1) / norm (ybuiltin,1) < 1e-12)
    end

end

GrB.burble (0) ;