File: test10.m

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (384 lines) | stat: -rw-r--r-- 13,952 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
function test10
%TEST10 test GrB_apply

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

% NOTE: for full test coverage, the JIT cache must be empty before running
% this test, or before running testall.

fprintf ('\ntest10: GrB_apply tests\n') ;

[~, unary_ops, ~, types, ~, ~] = GB_spec_opsall ;
types = types.all ;

rng ('default') ;

m = 8 ;
n = 4 ;
dt = struct ('inp0', 'tran') ;
dr = struct ('outp', 'replace') ;

track_coverage = false ;
if (track_coverage)
    global GraphBLAS_grbcov
    track_coverage = ~isempty (GraphBLAS_grbcov) ;
    clast = sum (GraphBLAS_grbcov > 0) ;
    cfirst = clast ;
end

for k1 = 1:length(types)
    atype = types {k1} ;
    fprintf ('\n%s: ', atype) ;

    Mask = GB_random_mask (m, n, 0.5, true, false) ;
    Cin = GB_spec_random (m, n, 0.3, 100, atype) ;
    Cmask = spones (GB_mex_cast (full (Cin.matrix), Cin.class)) ;

    % most operators :
    A = GB_spec_random (m, n, 0.3, 100, atype) ;
    B = GB_spec_random (n, m, 0.3, 100, atype) ;

    A_matrix = A.matrix ;
    B_matrix = B.matrix ;

    % pow, sqrt, log, log10, log2, gammaln (domain is [0,inf]):
    A_pos_matrix = abs (A.matrix) ;
    B_pos_matrix = abs (B.matrix) ;

    % asin, acos, atanh (domain is [-1,1]):
    A_1_matrix = A_matrix ;
    B_1_matrix = B_matrix ;
    A_1_matrix (abs (A_matrix) > 1) = 1 ;
    B_1_matrix (abs (B_matrix) > 1) = 1 ;

    % acosh, asech (domain is [1, inf]):
    A_1inf_matrix = A_matrix ;
    B_1inf_matrix = B_matrix ;
    A_1inf_matrix (A_matrix < 1 & A_matrix ~= 0) = 1 ;
    B_1inf_matrix (B_matrix < 1 & B_matrix ~= 0) = 1 ;

    % log1p (domain is [-1, inf]):
    A_n1inf_matrix = A_matrix ;
    B_n1inf_matrix = B_matrix ;
    A_n1inf_matrix (A_matrix < -1) = 1 ;
    B_n1inf_matrix (B_matrix < -1) = 1 ;

    % tanh: domain is [-inf,inf], but rounding to
    % integers fails when x is outside this range:
    A_5_matrix = A_matrix ;
    B_5_matrix = B_matrix ;
    A_5_matrix (abs (A_matrix) > 5) = 5 ;
    B_5_matrix (abs (B_matrix) > 5) = 5 ;

    % gamma: domain is [-inf,inf], but not defined if negative
    % integers, and rounding to integers fails when x is outside this range:
    A_pos5_matrix = A_matrix ;
    B_pos5_matrix = B_matrix ;
    A_pos5_matrix (A_matrix <= 0.1 & A_matrix ~= 0) = 0.1 ;
    B_pos5_matrix (B_matrix <= 0.1 & B_matrix ~= 0) = 0.1 ;
    A_pos5_matrix (A_matrix > 5) = 5 ;
    B_pos5_matrix (B_matrix > 5) = 5 ;

    % do longer tests with a few types
    longer_tests = isequal (atype, 'double') || isequal (atype, 'int64') ;
    if (longer_tests)
        hrange = 0 ; % SKIP [0 1] ;
        crange = 0 ; % SKIP [0 1] ;
    else
        hrange = 0 ;
        crange = 1 ;
    end

    % skip some ops, depending on the atype
    unops = unary_ops.all ;
    if (isequal (atype, 'int64'))
        % just do some of the ops
        unops = [unary_ops.alltypes unary_ops.real unary_ops.int ...
            unary_ops.positional]  ;
    elseif (test_contains (atype, 'int'))
        unops = [unary_ops.alltypes unary_ops.real unary_ops.int ] ;
    else
        % do all of the ops
        unops = unary_ops.all ;
    end
    unops = setdiff (unops, {'one'}) ;
    if (~test_contains (atype, 'logical'))
        unops = setdiff (unops, {'identity'}) ;
    end
    if (isequal (atype, 'double'))
        unops = setdiff (unops, {'ainv'}) ;
    end
    if (test_contains (atype, 'complex'))
        unops = setdiff (unops, {'not', 'lgamma', 'tgamma', 'erf', 'erfc', ...
            'frexpx', 'frexpe', 'cbrt'}) ;
    end
    if (~(isequal (atype, 'logical') || isequal (atype, 'single complex')))
        unops = setdiff (unops, {'conj'}) ;
    end
    if (isequal (atype, 'double') || isequal (atype, 'single'))
        unops = setdiff (unops, {'real', 'imag', 'carg'}) ;
    end
    if (test_contains (atype, 'double') || ...
        test_contains (atype, 'single') || ...
        isequal (atype, 'logical'))
        unops = setdiff (unops, {'bnot'}) ;
    end

    % just do some of the optypes, depending on the matrix type
    switch (atype)
        case 'logical'
            optypes = types ;      % do all optypes
        case 'int8'
            optypes = { 'logical', 'int8', 'int16' } ;
        case 'int16'
            optypes = { 'int16' } ;
        case 'int32'
            optypes = { 'int32' } ;
        case 'int64'
            optypes = { 'int32', 'int64' } ;
        case 'uint8'
            optypes = { 'uint8' } ;
        case 'uint16'
            optypes = { 'uint16' } ;
        case 'uint32'
            optypes = { 'uint32' } ;
        case 'uint64'
            optypes = { 'uint64'  } ;
        case 'single'
            optypes = { 'single' } ;
        case 'double'
            optypes = { 'double' } ;
        case 'single complex'
            optypes = { 'single complex' } ;
        case 'double complex'
            optypes = { 'double complex' } ;
    end

    for k2 = 1:length(unops)
        op.opname = unops {k2} ;
        fprintf ('\n%-12s ', op.opname) ;
        GB_mex_finalize ;

        for k3 = 1:length(optypes)
            op.optype = optypes {k3} ;

            if (ispc && test_contains (op.opname, 'asin') && ...
                test_contains (op.optype, 'complex'))
                % casin and casinf are broken on Windows
                fprintf ('#') ;
                continue ;
            end

            if (ismac && test_contains (op.opname, 'log') ...
                && test_contains (op.optype, 'complex') ...
                && (test_contains (atype, 'int') || isequal (atype, 'logical')))
                % complex log2 on the mac has a slight O(eps) roundoff that
                % results in a different result when typecasted to integer
                fprintf ('#') ;
                continue ;
            end

            try
                [opname optype ztype xtype ytype] = GB_spec_operator (op) ;
            catch
                continue 
            end

            A.matrix = A_matrix ;
            B.matrix = B_matrix ;

            switch (opname)
                % domain is ok, but limit it to avoid integer typecast
                % failures from O(eps) errors, or overflow to inf
                case { 'tanh', 'exp', 'sin', 'cos', 'tan', ...
                    'sinh', 'cosh', 'asin', 'acos', 'acosh', 'asinh', ...
                    'atanh', 'exp2', 'expm1', 'carg', 'atan' }
                    A.matrix = A_5_matrix ;
                    B.matrix = B_5_matrix ;
                case { 'tgamma' }
                    A.matrix = A_pos5_matrix ;
                    B.matrix = B_pos5_matrix ;
                otherwise
                    % no change
            end

            if (~test_contains (optype, 'complex'))

                % real operators, avoiding complex results:
                switch (opname)
                    case { 'pow', 'sqrt', 'log', 'log10', 'log2', ...
                        'gammaln', 'lgamma' }
                        A.matrix = A_pos_matrix ;
                        B.matrix = B_pos_matrix ;
                    case { 'asin', 'acos', 'atanh' }
                        A.matrix = A_1_matrix ;
                        B.matrix = B_1_matrix ;
                    case { 'acosh', 'asech' }
                        A.matrix = A_1inf_matrix ;
                        B.matrix = B_1inf_matrix ;
                    case 'log1p'
                        A.matrix = A_n1inf_matrix ;
                        B.matrix = B_n1inf_matrix ;
                    case { 'tanh', 'exp' }
                        % domain is ok, but limit it to avoid integer typecast
                        % failures from O(eps) errors
                        A.matrix = A_5_matrix ;
                        B.matrix = B_5_matrix ;
                    case { 'minv' }
%                       A.matrix = A_nonzero_matrix ;
%                       B.matrix = B_nonzero_matrix ;
                    otherwise
                        % no change
                end

            else

                % operator is complex
                switch (opname)
                    % domain is ok, but limit it to avoid integer typecast
                    % failures from O(eps) errors
                    case { 'log2' }
                        A.matrix = A_5_matrix ;
                        B.matrix = B_5_matrix ;
                    otherwise
                        % no change
                end

            end

            tol = 0 ;
            if (test_contains (optype, 'single') || ...
                test_contains (atype, 'single'))
                tol = 1e-5 ;
            elseif (test_contains (optype, 'double') || ...
                test_contains (atype, 'double'))
                tol = 1e-12 ;
            end

            for A_sparsity = 0 % SKIP [hrange 2]

                if (A_sparsity == 0)
                    A_is_hyper = 0 ;
                    A_is_bitmap = 0 ;
                    A_sparsity_control = 2 ;    % sparse
                elseif (A_sparsity == 1)
                    A_is_hyper = 1 ;
                    A_is_bitmap = 0 ;
                    A_sparsity_control = 1 ;    % hypersparse
                else
                    A_is_hyper = 0 ;
                    A_is_bitmap = 1 ;
                    A_sparsity_control = 4 ;    % bitmap
                end

                for A_is_csc   = crange
                for C_is_hyper = hrange
                for C_is_csc   = crange
                for M_is_hyper = hrange
                for M_is_csc   = crange

                    A.is_csc    = A_is_csc ; A.is_hyper    = A_is_hyper ;
                    Cin.is_csc  = C_is_csc ; Cin.is_hyper  = C_is_hyper ;
                    B.is_csc    = A_is_csc ; B.is_hyper    = A_is_hyper ;
                    Mask.is_csc = M_is_csc ; Mask.is_hyper = M_is_hyper ;

                    A.sparsity = A_sparsity_control ;
                    B.sparsity = A_sparsity_control ;

                    % no mask
                    C1 = GB_spec_apply (Cin, [], [], op, A, []) ;
                    C2 = GB_mex_apply  (Cin, [], [], op, A, []) ;
                    test10_compare (op, C1, C2, tol) ;

                    % with mask
                    C1 = GB_spec_apply (Cin, Mask, [], op, A, []) ;
                    C2 = GB_mex_apply  (Cin, Mask, [], op, A, []) ;
                    test10_compare (op, C1, C2, tol) ;

                    % with C == mask, and outp = replace
                    C1 = GB_spec_apply (Cin, Cmask, [], op, A, dr) ;
                    C2 = GB_mex_apply_maskalias (Cin, [], op, A, dr) ;
                    test10_compare (op, C1, C2, tol) ;

                    % no mask, transpose
                    C1 = GB_spec_apply (Cin, [], [], op, B, dt) ;
                    C2 = GB_mex_apply  (Cin, [], [], op, B, dt) ;
                    test10_compare (op, C1, C2, tol) ;

                    % with mask, transpose
                    C1 = GB_spec_apply (Cin, Mask, [], op, B, dt) ;
                    C2 = GB_mex_apply  (Cin, Mask, [], op, B, dt) ;
                    test10_compare (op, C1, C2, tol) ;

                    skip_arc = false ;
                    switch (opname)
                        % the results from these operators must be checked
                        % before summing their results with the accum operator,
                        % so skip the rest of the tests.
                        case {'acos', 'asin', 'atan' 'acosh', 'asinh', 'atanh'}
                            skip_arc = true ;
                    end

                    if (~skip_arc)

                        % no mask, with accum
                        C1 = GB_spec_apply (Cin, [], 'plus', op, A, []) ;
                        C2 = GB_mex_apply  (Cin, [], 'plus', op, A, []) ;
                        test10_compare (op, C1, C2, tol) ;

                        % with mask and accum
                        C1 = GB_spec_apply (Cin, Mask, 'plus', op, A, []) ;
                        C2 = GB_mex_apply  (Cin, Mask, 'plus', op, A, []) ;
                        test10_compare (op, C1, C2, tol) ;

                        % with C == mask and accum, and outp = replace
                        C1 = GB_spec_apply (Cin, Cmask, 'plus', op, A, dr) ;
                        C2 = GB_mex_apply_maskalias (Cin, 'plus', op, A, dr) ;
                        test10_compare (op, C1, C2, tol) ;

                        % no mask, with accum, transpose
                        C1 = GB_spec_apply (Cin, [], 'plus', op, B, dt) ;
                        C2 = GB_mex_apply  (Cin, [], 'plus', op, B, dt) ;
                        test10_compare (op, C1, C2, tol) ;

                        % with mask and accum, transpose
                        C1 = GB_spec_apply (Cin, Mask, 'plus', op, B, dt) ;
                        C2 = GB_mex_apply  (Cin, Mask, 'plus', op, B, dt) ;
                        test10_compare (op, C1, C2, tol) ;

                    end

                    if (track_coverage)
                        c = sum (GraphBLAS_grbcov > 0) ;
                        d = c - clast ;
                        if (d > 0)
                            fprintf ('\n[%d,%d,%d,%d,%d,%d]', ...
                            A_sparsity , ...
                            A_is_csc   , ...
                            C_is_hyper , ...
                            C_is_csc   , ...
                            M_is_hyper , ...
                            M_is_csc   ) ;
                            fprintf (' (%d, %d, %s).\n', d, c - cfirst, optype) ;
                        else
                            fprintf ('.') ;
                        end
                        clast = c ;
                    else
                        fprintf ('.') ;
                    end

                end
                end
                end
                end
                end
            end
        end
    end
    fprintf ('\n') ;
end

fprintf ('\ntest10: all tests passed\n') ;