File: test14.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (250 lines) | stat: -rw-r--r-- 7,833 bytes parent folder | download | duplicates (3)
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
function test14
%TEST14 test GrB_reduce

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

fprintf ('\ntest14: reduce to column and scalar\n') ;

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

rng ('default') ;

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

for k1 = 1:length(types)
    atype = types {k1} ;
    fprintf ('.') ;
    A = GB_spec_random (m, n, 0.3, 100, atype) ;
    B = GB_spec_random (n, m, 0.3, 100, atype) ;
    w = GB_spec_random (m, 1, 0.3, 100, atype) ;
    cin = GB_mex_cast (0, atype) ;

    clear S_input
    S_input.matrix = cin ;
    S_input.pattern = true ;
    S_input.class = atype ;

    clear E_input
    E_input.matrix = sparse (0) ;
    E_input.pattern = false ;
    E_input.class = atype ;

    mask = GB_random_mask (m, 1, 0.5, true, false) ;

    if (isequal (atype, 'logical'))
        ops = {'or', 'and', 'xor', 'eq', 'any'} ;
    else
        ops = {'min', 'max', 'plus', 'times', 'any'} ;
    end

    if (isequal (atype, 'double'))
        hrange = [0 1] ;
        crange = [0 1] ;
    else
        hrange = 0 ;
        crange = 1 ;
    end

    is_float = test_contains (atype, 'single') || test_contains (atype, 'double') ;

    for A_is_hyper = 0:1
    for A_is_csc   = 0:1

    A.is_csc = A_is_csc ; A.is_hyper = A_is_hyper ;
    B.is_csc = A_is_csc ; B.is_hyper = A_is_hyper ;

    for k2 = 1:length(add_ops)
        op = add_ops {k2} ;

        if (isequal (op, 'any'))
            tol = [ ] ;
        elseif (test_contains (atype, 'single'))
            tol = 1e-5 ;
        elseif (test_contains (atype, 'double'))
            tol = 1e-12 ;
        else
            tol = 0 ;
        end

        try
            GB_spec_operator (op, atype) ;
            identity = GB_spec_identity (op, atype) ;
        catch
            continue
        end

        % no mask
        w1 = GB_spec_reduce_to_vector (w, [], [], op, A, []) ;
        w2 = GB_mex_reduce_to_vector  (w, [], [], op, A, []) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % no mask, with accum
        w1 = GB_spec_reduce_to_vector (w, [], 'plus', op, A, []) ;
        w2 = GB_mex_reduce_to_vector  (w, [], 'plus', op, A, []) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % with mask
        w1 = GB_spec_reduce_to_vector (w, mask, [], op, A, []) ;
        w2 = GB_mex_reduce_to_vector  (w, mask, [], op, A, []) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % with mask and accum
        w1 = GB_spec_reduce_to_vector (w, mask, 'plus', op, A, []) ;
        w2 = GB_mex_reduce_to_vector  (w, mask, 'plus', op, A, []) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % no mask, transpose
        w1 = GB_spec_reduce_to_vector (w, [], [], op, B, dt) ;
        w2 = GB_mex_reduce_to_vector  (w, [], [], op, B, dt) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % no mask, with accum, transpose
        w1 = GB_spec_reduce_to_vector (w, [], 'plus', op, B, dt) ;
        w2 = GB_mex_reduce_to_vector  (w, [], 'plus', op, B, dt) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % with mask, transpose
        w1 = GB_spec_reduce_to_vector (w, mask, [], op, B, dt) ;
        w2 = GB_mex_reduce_to_vector  (w, mask, [], op, B, dt) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % with mask and accum, transpose
        w1 = GB_spec_reduce_to_vector (w, mask, 'plus', op, B, dt) ;
        w2 = GB_mex_reduce_to_vector  (w, mask, 'plus', op, B, dt) ;
        GB_spec_compare (w1, w2, identity, tol) ;

        % GB_spec_reduce_to_scalar always operates column-wise, but GrB_reduce
        % operates in whatever order it is given: by column if CSC or by row if
        % CSR.  The result can vary slightly because of different round off
        % errors.  A_flip causes GB_spec_reduce_to_scalar to operate in the
        % same order as GrB_reduce.

        A_flip = A ;
        if (~A.is_csc && is_float)
            A_flip.matrix = A_flip.matrix.' ;
            A_flip.pattern = A_flip.pattern' ;
            A_flip.is_csc = true ;
        end

        % Parallel reduction leads to different roundoff.  So even with A_flip,
        % c1 and c2 can only be compared to within round-off error.

        % to scalar
        c2 = GB_mex_reduce_to_scalar  (cin, [ ], op, A) ;
        if (isequal (op, 'any'))
            X = GB_mex_cast (full (A.matrix (A.pattern)), A.class) ;
            assert (any (X == c2)) ;
        else
            c1 = GB_spec_reduce_to_scalar (cin, [ ], op, A_flip) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % to GrB_Scalar
        S = GB_mex_reduce_to_GrB_Scalar (S_input, [ ], op, A) ;
        c2 = S.matrix ;
        if (isequal (op, 'any'))
            X = GB_mex_cast (full (A.matrix (A.pattern)), A.class) ;
            assert (any (X == c2)) ;
        else
            c1 = GB_spec_reduce_to_scalar (cin, [ ], op, A_flip) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % to GrB_Scalar
        S = GB_mex_reduce_to_GrB_Scalar (E_input, [ ], op, A) ;
        c2 = S.matrix ;
        if (isequal (op, 'any'))
            X = GB_mex_cast (full (A.matrix (A.pattern)), A.class) ;
            assert (any (X == c2)) ;
        else
            c1 = GB_spec_reduce_to_scalar (cin, [ ], op, A_flip) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % vector to GrB_Scalar
        S = GB_mex_reduce_to_GrB_Scalar (S_input, [ ], op, w) ;
        c2 = S.matrix ;
        if (isequal (op, 'any'))
            X = GB_mex_cast (full (w.matrix (w.pattern)), w.class) ;
            assert (any (X == c2)) ;
        else
            c1 = GB_spec_reduce_to_scalar (cin, [ ], op, w) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % to scalar, with accum
        c2 = GB_mex_reduce_to_scalar (cin, 'plus', op, A) ;
        if (~isequal (op, 'any'))
            c1 = GB_spec_reduce_to_scalar (cin, 'plus', op, A_flip) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % to GrB_Scalar, with accum
        S = GB_mex_reduce_to_GrB_Scalar (S_input, 'plus', op, A) ;
        c2 = S.matrix ;
        if (~isequal (op, 'any'))
            c1 = GB_spec_reduce_to_scalar (cin, 'plus', op, A_flip) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

        % vector to GrB_Scalar, with accum
        S = GB_mex_reduce_to_GrB_Scalar (S_input, 'plus', op, w) ;
        c2 = S.matrix ;
        if (~isequal (op, 'any'))
            c1 = GB_spec_reduce_to_scalar (cin, 'plus', op, w) ;
            if (is_float)
                assert (abs (c1-c2) < tol *  (abs(c1) + 1))
            else
                assert (isequal (c1, c2)) ;
            end
        end

    end
    end
    end
end

clear A
A.matrix = sparse (4,5) ;
A.pattern = false (4,5) ;
A.class = 'double' ;

clear S_input
S_input.matrix = 1 ;
S_input.pattern = true ;
S_input.class = 'double' ;

% empty matrix to GrB_Scalar
S = GB_mex_reduce_to_GrB_Scalar (S_input, [ ], 'plus', A) ;
assert (nnz (S.matrix) == 0) ;

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