1function test19(fulltest)
2%TEST19 test GxB_subassign and GrB_*_setElement with many pending operations
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7if (nargin < 1)
8    fulltest = 0 ;
9end
10if (fulltest)
11    nt = 3000 ;
12else
13    % check if malloc debugging is enabled
14    debug_status = stat ;
15    if (debug_status)
16        % exhaustive malloc debugging
17        nt = 50 ; % was 500, which takes too long
18    else
19        nt = 500 ;
20    end
21end
22
23fprintf ('\nGxB_subassign and setElement test, many pending computations\n') ;
24
25for problem = 0:2
26
27    clear Work Work2
28    switch (problem)
29        case 0
30            Corig = sparse (5,5) ;
31            d = 1 ;
32            ntrials = 100 ;
33        case 1
34            Corig = sprandn (10,20,0.1) ;
35            d = 0.3 ;
36            ntrials = nt ;
37        case 2
38            Corig = sparse (rand (10, 20)) ;
39            d = 0.3 ;
40            ntrials = nt ;
41    end
42
43    rng ('default') ;
44    [m n] = size (Corig) ;
45    fprintf ('problem %d: C is %d-by-%d, # assign/setElement to do: %d\n', ...
46        problem, m, n, ntrials) ;
47
48    if (problem > 0)
49        fprintf ('... please wait\n') ;
50    end
51
52    for k = 1:ntrials
53
54        c = randi (10,1) ;
55        if (c == 10)
56            d = struct ('outp', 'replace') ;
57        elseif (c == 9)
58            d = struct ('outp', 'replace', 'mask', 'complement') ;
59        elseif (c == 8)
60            d = struct ('mask', 'complement') ;
61        else
62            d = [ ] ;
63        end
64
65%       c = randi (10,1) ;
66%       if (c == 10)
67%           d = struct ('outp', 'replace') ;
68%       else
69%           d = [ ] ;
70%       end
71
72        c = randi (3,1) ;
73        switch (c)
74            case 1
75                accum = '' ;
76            case 2
77                accum = 'plus' ;
78            case 3
79                accum = 'second' ;
80        end
81
82        c = randi (10,1) ;
83        if (c < 8)
84            ni = randi (3,1) ;
85            nj = randi (3,1) ;
86            J = randperm (n, nj) ;
87            I = randperm (m, ni) ;
88            A = sprand (ni, nj, 0.3) ;
89            scalar = 0 ;
90        elseif (c == 8)
91            % scalar expansion
92            ni = 2 ;
93            nj = 2 ;
94            J = randperm (n, nj) ;
95            I = randperm (m, ni) ;
96            A = sparse (rand (1)) ;
97            scalar = 1 ;
98        elseif (c == 9)
99            ni = 1 ;
100            nj = 1 ;
101            I = randperm (m,1) ;
102            J = randperm (n,1) ;
103            A = sparse (rand (1)) ;
104            scalar = 0 ;
105        else
106            ni = 2 ;
107            nj = 2 ;
108            I = randperm (m,2) ;
109            J = randperm (n,2) ;
110            A = sparse (rand (2)) ;
111            scalar = 0 ;
112        end
113
114        c = randi (2,1) ;
115        switch (c)
116            case 1
117                Mask = [ ] ;
118            case 2
119                Mask = (sprand (ni, nj, 0.3) ~= 0) ;
120        end
121
122        Work (k).A = A ;
123        Work (k).I = I ;
124        Work (k).J = J ;
125        Work (k).Mask = Mask ;
126        Work (k).accum = accum ;
127        Work (k).desc = d ;
128        Work (k).scalar = scalar ;
129
130        Work2 (k).A = A ;
131        Work2 (k).I = uint64 (I-1) ;
132        Work2 (k).J = uint64 (J-1) ;
133        Work2 (k).Mask = Mask ;
134        Work2 (k).accum = accum ;
135        Work2 (k).desc = d ;
136
137    end
138
139    C3 = Corig ;
140    for k = 1:ntrials
141        J = Work (k).J ;
142        I = Work (k).I ;
143        A = Work (k).A ;
144        M = Work (k).Mask ;
145        accum = Work (k).accum ;
146        d = Work (k).desc ;
147        scalar = Work (k).scalar ;
148        C3 = GB_spec_subassign (C3, M, accum, A, I, J, d, scalar) ;
149    end
150
151    % default sparsity
152    C2 = GB_mex_subassign (Corig, Work2) ;
153    GB_spec_compare (C2, C3) ;
154
155    % with sparsity control
156    for C_sparsity_control = [1 2 4 8]
157        for M_sparsity_control = [2 12]
158            C2 = GB_mex_subassign (Corig, Work2, ...
159                [C_sparsity_control M_sparsity_control]) ;
160            GB_spec_compare (C2, C3) ;
161        end
162    end
163
164    % with no accum
165
166    C3 = Corig ;
167    for k = 1:ntrials
168        J = Work (k).J ;
169        I = Work (k).I ;
170        A = Work (k).A ;
171        M = Work (k).Mask ;
172        d = Work (k).desc ;
173        scalar = Work (k).scalar ;
174        C3 = GB_spec_subassign (C3, M, [ ], A, I, J, d, scalar) ;
175        Work2 (k).accum = [ ] ;
176    end
177
178    % default sparsity
179    C2 = GB_mex_subassign (Corig, Work2) ;
180    GB_spec_compare (C2, C3) ;
181
182    % with sparsity control
183    for C_sparsity_control = [1 2 4 8]
184        for M_sparsity_control = [2 12]
185            C2 = GB_mex_subassign (Corig, Work2, ...
186                [C_sparsity_control M_sparsity_control]) ;
187            GB_spec_compare (C2, C3) ;
188        end
189    end
190
191end
192
193fprintf ('\ntest19: all tests passed\n') ;
194
195