1function test121
2%TEST121 performance tests for GrB_assign
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('test121:---------------- C(I,J)+=A performance\n') ;
8
9[save save_chunk] = nthreads_get ;
10chunk = 4096 ;
11
12rng ('default') ;
13n = 1e6 ;
14k = n/10 ;
15
16%   I.begin = 0 ;
17%   I.inc = 1 ;
18%   I.end = k-1 ;
19    I1 = randperm (k) ;
20    I0 = uint64 (I1) - 1 ;
21
22ncores = feature ('numcores') ;
23
24for dc = [ 0 1e-6 1e-5 1e-4 ]
25
26    C0 = sprandn (n, n, dc) ;
27
28    for da = [ 0 1e-6 1e-5 1e-4 1e-3 ]
29
30        A = sprandn (k, k, da) ;
31
32        % warmup
33        C1 = C0 ;
34        C1 (1:k,1:k) = C1 (1:k,1:k) + A ;
35
36        fprintf ('\n--------------------------------------\n') ;
37        fprintf ('dc = %g, da = %g\n', dc, da) ;
38        tic
39        C1 = C0 ;
40        % C1 (1:k,1:k) = C1 (1:k,1:k) + A ;
41        C1 (I1,I1) = C1 (I1,I1) + A ;
42        tm = toc ;
43
44        for nthreads = [1 2 4 8 16 20 32 40 64]
45            if (nthreads > 2*ncores)
46                break ;
47            end
48            nthreads_set (nthreads, chunk) ;
49            if (nthreads > 1 && t1 < 0.01)
50                continue ;
51            end
52
53            C2 = GB_mex_assign (C0, [ ], 'plus', A, I0, I0) ;
54            C2 = GB_mex_assign (C0, [ ], 'plus', A, I0, I0) ;
55            tg = grbresults ;
56            assert (isequal (C1, C2.matrix)) ;
57            if (nthreads == 1)
58                t1 = tg ;
59            end
60
61            fprintf ('%3d : MATLAB: %10.4f GB: %10.4f', nthreads, tm, tg) ;
62            fprintf ('  speedup %10.4f %10.4f\n', t1/tg, tm / tg) ;
63        end
64    end
65end
66
67nthreads_set (save, save_chunk) ;
68