1function gbdemo2 (bnz)
2%GBDEMO2 Extreme performance differences: GraphBLAS vs MATLAB.
3%
4% Usage:
5%
6%       gbdemo2             % uses a default bnz = 6000
7%       gbdemo2 (20000)     % uses bnz = 20000
8%
9% Many of the GraphBLAS operations used in gbdemo are perhaps 3x to
10% 50x faster than the corresponding MATLAB operations, depending on how
11% many cores your computer has.  Here's an example where GraphBLAS is
12% asymptotically far faster than MATLAB R2019a: a simple assignment
13% for a large matrix C:
14%
15%       C(I,J) = A
16%
17% The matrix C is constructed via C = kron (B,B) where nnz (B) is
18% roughly the bnz provided on input (with a default of bnz = 6000),
19% so that C will have about bnz^2 entries, or 36 million by default.
20% I and J are chosen randomly, and A is 5000-by-5000.
21%
22% When the problem becomes large, MATLAB will take a very long time.
23% If you have enough memory, and want to see higher speedups in
24% GraphBLAS, increase bnz (and be prepared to wait even longer).
25% With the default bnz = 6000, this test takes about 4GB of RAM.
26%
27% On my Dell XPS 4-core laptop (Intel(R) Core(TM) i7-8565U, 16GB
28% RAM), using MATLAB R2019a, when C becomes 9 million by 9 million,
29% the computation C(I,J)=A for MATLAB matrices C, I, J, and A takes
30% several minutes, whereas GraphBLAS takes less than a second, or
31% about 500x faster than MATLAB.  On a desktop with an Intel(R)
32% Xeon(R) CPU E5-2698 v4 @ 2.20GHz with 20 hardware cores, the
33% speedup over MATLAB is even more dramatic (up to 2,660x has been
34% observed).
35%
36% See also GrB.assign, subsasgn.
37
38% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
39% SPDX-License-Identifier: GPL-3.0-or-later
40
41% reset to the default number of threads
42maxNumCompThreads ('automatic') ;
43GrB.clear ;
44
45nthreads = GrB.threads ;
46help gbdemo2
47fprintf ('\n# of threads used in GraphBLAS: %d\n\n', nthreads) ;
48
49if (nargin < 1)
50    bnz = 6000 ;
51end
52
53k = 5000 ;
54anz = 50000 ;
55A = sprandn (k, k, anz / k^2) ;
56
57for n = 1000:1000:6000
58
59    % reset the random number generator for repeatable results
60    rng ('default') ;
61
62    tic
63    B = sprandn (n, n, bnz / n^2) ;
64    C = kron (B, B) ;
65    cn = size (C,1) ;
66    I = randperm (cn, k) ;
67    J = randperm (cn, k) ;
68    G = GrB (C) ;
69    t_setup = toc ;
70
71    fprintf ('\nC(I,J)=A where C is %g million -by- %g million\n', ...
72        cn /1e6, cn /1e6) ;
73    fprintf ('with %g million entries:\n\n', nnz (C) / 1e6) ;
74    fprintf ('    A is %d-by-%d with %d entries\n', k, k, nnz (A)) ;
75    fprintf ('    setup time:     %g sec\n', t_setup) ;
76
77    % do the assignment in GraphBLAS
78    tic
79    G (I,J) = A ;
80    gb_time = toc ;
81
82    fprintf ('    GraphBLAS time: %g sec\n', gb_time) ;
83    fprintf ('    Starting MATLAB ... please wait ... \n') ;
84
85    % do the same assignment in pure MATLAB
86    tic
87    C (I,J) = A ;
88    matlab_time = toc ;
89
90    fprintf ('    MATLAB time:    %g sec\n', matlab_time) ;
91    fprintf ('    Speedup of GraphBLAS over MATLAB: %g\n', ...
92        matlab_time / gb_time) ;
93
94    % check the result
95    tic
96    assert (isequal (C, double (G))) ;
97    t_check = toc ;
98    fprintf ('    check time:     %g sec\n', t_check) ;
99    fprintf ('    all tests passed\n') ;
100
101end
102
103