1function test119
2%TEST119 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 ('test119:-------------------  C(I,J) += scalar:\n') ;
8
9[save save_chunk] = nthreads_get ;
10chunk = 4096 ;
11
12rng ('default') ;
13n = 4000 ; ;
14
15k = 3000 ;
16
17%   I.begin = 0 ;
18%   I.inc = 1 ;
19%   I.end = k-1 ;
20    I1 = randperm (k) ;
21    I0 = uint64 (I1) - 1 ;
22
23ncores = feature ('numcores') ;
24
25for dc = [2 0 1e-6 1e-5 1e-4 1e-3 1e-2 0.1 1]
26
27    if (dc == 2)
28        C0 = sparse (rand (n)) ;
29    else
30        C0 = sprand (n, n, dc) ;
31    end
32
33    % warmup
34    C1 = C0 ;
35    % C1 (1:k,1:k) = C1 (1:k,1:k) + pi ;
36    C1 (I1,I1) = C1 (I1,I1) + pi ;
37
38    fprintf ('\n--------------------------------------\n') ;
39    fprintf ('dc = %g  nnz(C) %8.4f  million\n', dc, nnz(C0)/1e6) ;
40    tic
41    C1 = C0 ;
42    % C1 (1:k,1:k) = C1 (1:k,1:k) + pi ;
43    C1 (I1,I1) = C1 (I1,I1) + pi ;
44    tm = toc ;
45
46    scalar = sparse (pi) ;
47
48    for nthreads = [1 2 4 8 16 20 32 40 64]
49        if (nthreads > 2*ncores)
50            break ;
51        end
52        if (nthreads > 1 && t1 < 0.01)
53            break ;
54        end
55
56        nthreads_set (nthreads, chunk) ;
57
58        C2 = GB_mex_assign (C0, [ ], 'plus', scalar, I0, I0) ;
59        C2 = GB_mex_assign (C0, [ ], 'plus', scalar, I0, I0) ;
60        tg = grbresults ;
61        assert (isequal (C1, C2.matrix)) ;
62        if (nthreads == 1)
63            t1 = tg ;
64        end
65
66        fprintf ('%3d : MATLAB: %10.4f GB: %8.4f ', nthreads, tm, tg) ;
67        fprintf (' speedup %10.4f %10.4f\n', tm / tg, t1/tg) ;
68
69    end
70end
71
72nthreads_set (save, save_chunk) ;
73