1function test26(longtests)
2%TEST26 performance test for GxB_select
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\ntest26 ------------------------------performance of GxB_select\n') ;
8
9[save_nthreads save_chunk] = nthreads_get ;
10chunk = 4096 ;
11nthreads = feature ('numcores') ;
12nthreads_set (nthreads, chunk) ;
13
14[~, ~, ~, ~, ~, select_ops] = GB_spec_opsall ;
15
16if (nargin < 1)
17    longtests = 0 ;
18end
19
20if (longtests)
21    % ssget will be used
22    nprobs = 5 ;
23else
24    nprobs = 3 ;
25end
26
27rng ('default') ;
28
29dt = struct ('inp0', 'tran') ;
30
31for probs = 1:nprobs
32
33    switch probs
34        case 1
35            A = sparse (200, 1, 0.1) ;
36        case 2
37            A = sparse (200, 100, 0.1) ;
38        case 3
39            A1 = spones (sprand (10, 10, 0.5)) ;
40            A2 = spones (sprand (10, 10, 0.5)) ;
41            A3 = sparse (10,10) ;
42            A4 = GB_mex_Matrix_eWiseAdd (A3, [], [], 'minus', A1, A2, [ ]) ;
43            A = A4.matrix ;
44            % GB_spok(A) will fail since it has intentional explicit zeros
45        case 4
46            A = sparse (rand (6000)) ;
47        case 5
48            Prob = ssget (2662) ;
49            A = Prob.A ;
50    end
51
52    [m n] = size (A) ;
53    fprintf ('\nProblem: m %d n %d nnz %d\n', m, n, nnz (A)) ;
54    Cin = sparse (m,n) ;
55
56    for k2 = 1:length(select_ops)
57        op = select_ops {k2} ;
58        fprintf ('%s:\n', op) ;
59
60        for k = [-m -floor(m/2) -50 -1 0 1 50 floor(n/2) n]
61
62            fprintf ('k: %10d ', k) ;
63
64            tic
65            C1 = GB_mex_select (Cin, [], [], op, A, k, []) ;
66            t1 = grbresults ; % toc ;
67            fprintf ('GB: %10.6f ', t1) ;
68
69            C3 = 'none' ;
70            C2 = 'none' ;
71
72            switch (op)
73                case 'tril'
74                    tic
75                    C2 = tril (A,k) ;
76                    t2 = toc ;
77                    tic
78                    C3 = GB_mex_tril (A, k) ;
79                    t3 = grbresults ; % toc ;
80                case 'triu'
81                    tic
82                    C2 = triu (A,k) ;
83                    t2 = toc ;
84                    tic
85                    C3 = GB_mex_triu (A, k) ;
86                    t3 = grbresults ; % toc ;
87                case 'diag'
88                    if (size (A,2) > 1)
89                        tic
90                        C2 = spdiags (spdiags (A,k), k, m, n) ;
91                        t2 = toc ;
92                    end
93                    tic
94                    C3 = GB_mex_diag (A, k) ;
95                    t3 = grbresults ; % toc ;
96                case 'offdiag'
97                    if (size (A,2) > 1)
98                        tic
99                        C2 = A - spdiags (spdiags (A,k), k, m, n) ;
100                        t2 = toc ;
101                    end
102                    tic
103                    C3 = GB_mex_offdiag (A, k) ;
104                    t3 = grbresults ; % toc ;
105                case 'nonzero'
106                    tic
107                    C2 = A .* (A ~= 0) ;
108                    t2 = toc ;
109                    assert (isequal (1*C2,1*A)) ;
110                    tic
111                    C3 = GB_mex_nonzero (A) ;
112                    t3 = grbresults ; % toc ;
113                    assert (isequal (1*C3,1*A)) ;
114            end
115
116            if (~ischar (C3))
117                fprintf (' %10.6f ', t3) ;
118                assert (isequal (C3, C1.matrix))
119            end
120
121            if (~ischar (C2))
122                fprintf ('MATLAB: %10.6f ', t2) ;
123                fprintf ('nnz: %10d speedup %5.2f ', nnz (C2), t2/t1) ;
124
125                if (~ischar (C3))
126                    fprintf (' %5.2f ', t2/t3) ;
127                end
128                assert (isequal (1*C1.matrix, 1*C2))
129            end
130            fprintf ('\n') ;
131
132        end
133    end
134end
135
136ok = true ;
137A = sparse (ones (4)) ;
138try
139    C = GB_mex_select (A, [ ], [ ], 'tril', A, A, [ ]) ;
140    ok = false ;
141catch me
142    fprintf ('\nexpected error: %s\n', me.message) ;
143end
144
145nthreads_set (save_nthreads, save_chunk) ;
146fprintf ('test26: all tests passed\n') ;
147