1function test117
2%TEST117 performance tests for GrB_assign
3
4% test C(:,:)<M> += A
5
6% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
7% SPDX-License-Identifier: Apache-2.0
8
9fprintf ('test117 ----------------------------------- C(:,:)<M> += A\n') ;
10
11[save save_chunk] = nthreads_get ;
12chunk = 4096 ;
13
14rng ('default') ;
15n = 4000 ;
16
17I.begin = 0 ;
18I.inc = 1 ;
19I.end = n-1 ;
20
21ncores = feature ('numcores') ;
22
23for dc = [1e-5 1e-4 1e-3 1e-2 1e-1 0.5]
24    C0 = sprand (n, n, dc) ;
25
26for da = [1e-5 1e-4 1e-3 1e-2 1e-1 0.5]
27    A  = sprand (n, n, da) ;
28
29for dm = [1e-5 1e-4 1e-3 1e-2 1e-1 0.5]
30
31    M = spones (sprand (n, n, dm)) ;
32
33    for subset = 0:1
34
35        fprintf ('\n--------------------------------------\n') ;
36        fprintf ('dc: %g, da: %g, dm: %g ', dc, da, dm) ;
37        if (subset)
38            fprintf ('M is a subset of C\n') ;
39            M = spones (M.*C0) ;
40        end
41        fprintf ('\n') ;
42
43        fprintf ('nnz(C): %g million, nnz(M): %g million, ', ...
44            nnz (C0) / 1e6, nnz (M) / 1e6) ;
45        fprintf ('nnz(A): %g million\n',  nnz (A) / 1e6) ;
46
47        % warmup
48        C1 = C0 + M.*A ;
49
50        tic
51        C1 = C0 + M.*A ;
52        tm = toc ;
53
54        for nthreads = [1 2 4 8 16 20 32 40 64]
55            if (nthreads > 2*ncores)
56                break ;
57            end
58            nthreads_set (nthreads, chunk) ;
59
60            if (nthreads > 1 & t1 < 0.003)
61                continue
62            end
63
64            % warmup:
65            C2 = GB_mex_assign (C0, M, 'plus', A, I, I) ;
66            C2 = GB_mex_assign (C0, M, 'plus', A, I, I) ;
67            tg = grbresults ;
68            assert (isequal (C1, C2.matrix)) ;
69            if (nthreads == 1)
70                t1 = tg ;
71            end
72
73            % ewise
74            C2 = GB_mex_Matrix_eWiseMult (C0, [ ], 'plus', 'times', M, A) ;
75            C2 = GB_mex_Matrix_eWiseMult (C0, [ ], 'plus', 'times', M, A) ;
76            tg3 = grbresults ;
77
78            assert (isequal (C1, C2.matrix)) ;
79
80            fprintf (...
81                '%3d : %8.4f GB: %8.4f %8.4f rel %8.2f %8.2f\n', ...
82                nthreads, tm, tg, tg3, tm / tg, tm/tg3) ;
83        end
84
85    end
86end
87end
88end
89
90nthreads_set (save, save_chunk) ;
91