File: test154.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 (179 lines) | stat: -rw-r--r-- 4,835 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
function test154
%TEST154 test GrB_apply with scalar binding

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

[binops, ~, ~, types, ~, ~] = GB_spec_opsall ;
ops = binops.all ;
types = types.all ;

fprintf ('\n--- testing apply with binaryop and scalar binding\n') ;
rng ('default') ;
% the right approach for apply_bind1st and apply_bind2nd
desc0.inp0 = 'tran' ;
desc1.inp1 = 'tran' ;
% shotgun approach for eWiseMult
desc.inp0 = 'tran' ;
desc.inp1 = 'tran' ;

n_operators = 0 ;
for k2 = 1:length(ops)
    mulop = ops {k2} ;
    fprintf ('\n%-10s ', mulop) ;

    for k1 = 1:length (types)
    type = types {k1} ;

    % create the op
    clear op
    op.opname = mulop ;
    op.optype = type ;

    try
        [oname ot ztype xtype ytype] = GB_spec_operator (op) ;
    catch
        continue ;
    end
    n_operators = n_operators + 1  ;

    switch (mulop)
        case { 'pow' }
            xlimits = [0, 5] ;
            ylimits = [0, 5] ;
        case { 'ldexp' }
            xlimits = [-5, 5] ;
            ylimits = [-5, 5] ;
        otherwise
            xlimits = [ ] ;
            ylimits = [ ] ;
    end

    if (test_contains (type, 'single'))
        tol = 1e-5 ;
    elseif (test_contains (type, 'double'))
        tol = 1e-12 ;
    else
        tol = 0 ;
    end

    fprintf ('.') ;

    for m = [1 4] % [ 1 10 ]% 100]
    for n = [1 4] % [1 10 ]% 100]
    for hi = [1 5] % [-1:2:5 ]
    for lo = [-1 0] % [-3:2:5 ]
    Amat = (hi*sprand (m,n,0.8)-lo) .* sprand (m,n,0.5) ;
    Bmat = (hi*sprand (m,n,0.8)-lo) .* sprand (m,n,0.5) ;
    xmat = (hi*sparse (rand(1))-lo) .* sparse (rand(1)) ;
    ymat = (hi*sparse (rand(1))-lo) .* sparse (rand(1)) ;
    Cmat = sparse (m, n) ;

    if (~isempty (xlimits))
        Amat = max (Amat, xlimits (1)) ;
        Amat = min (Amat, xlimits (2)) ;
        xmat = max (xmat, xlimits (1)) ;
        xmat = min (xmat, xlimits (2)) ;
    end
    if (xmat == 0)
        xmat = sparse (0.5) ;
    end

    if (~isempty (ylimits))
        Bmat = max (Bmat, ylimits (1)) ;
        Bmat = min (Bmat, ylimits (2)) ;
        ymat = max (ymat, ylimits (1)) ;
        ymat = min (ymat, ylimits (2)) ;
    end
    if (ymat == 0)
        ymat = sparse (0.5) ;
    end

    C.matrix = Cmat ;
    C.class = ztype ;

    CT.matrix = Cmat' ;
    CT.class = ztype ;

    A.matrix = Amat ;
    A.class = xtype ;

    B.matrix = Bmat ;
    B.class = ytype ;

    x.matrix = xmat ;
    x.class = xtype ;

    y.matrix = ymat ;
    y.class = ytype ;

    X.matrix = xmat .* spones (Bmat) ;
    X.class = xtype ;

    Y.matrix = ymat .* spones (Amat) ; 
    Y.class = ytype ;

    op_ewise_bind1st = op ;
    op_ewise_bind2nd = op ;
    if (isequal (op.opname, 'any'))
        op_ewise_bind1st.opname = 'first' ;
        op_ewise_bind2nd.opname = 'second' ;
    end

    C1 = GB_mex_apply1 (C, [ ], [ ], op, 0, x, B) ;
    C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise_bind1st, X, B, [ ]) ;
    GB_spec_compare (C1, C2, 0, tol) ;
    C1 = GB_mex_apply1 (C, [ ], [ ], op, 1, x, B) ;
    GB_spec_compare (C1, C2, 0, tol) ;

    C1 = GB_mex_apply1 (CT, [ ], [ ], op, 0, x, B, desc1) ;
    C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise_bind1st, X, B, desc) ;
    GB_spec_compare (C1, C2, 0, tol) ;
    C1 = GB_mex_apply1 (CT, [ ], [ ], op, 1, x, B, desc1) ;
    GB_spec_compare (C1, C2, 0, tol) ;

    for csc = 0:1

        A.is_csc = csc ;
        C.is_csc = csc ;
        CT.is_csc = csc ;

        C1 = GB_mex_apply2 (C, [ ], [ ], op, 0, A, y) ;
        C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise_bind2nd, A, Y, [ ]) ;
        GB_spec_compare (C1, C2, 0, tol) ;
        C1 = GB_mex_apply2 (C, [ ], [ ], op, 1, A, y) ;
        GB_spec_compare (C1, C2, 0, tol) ;

        C1 = GB_mex_apply2 (CT, [ ], [ ], op, 0, A, y, desc0) ;
        C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise_bind2nd, A, Y, desc) ;
        GB_spec_compare (C1, C2, 0, tol) ;
        C1 = GB_mex_apply2 (CT, [ ], [ ], op, 1, A, y, desc0) ;
        GB_spec_compare (C1, C2, 0, tol) ;
    end

    y.class = 'double' ;
    Y.class = 'double' ;

    C1 = GB_mex_apply2 (C, [ ], [ ], op, 0, A, y) ;
    C2 = GB_spec_Matrix_eWiseMult (C, [ ], [ ], op_ewise_bind2nd, A, Y, [ ]) ;
    GB_spec_compare (C1, C2, 0, tol) ;

    C1 = GB_mex_apply2 (C, [ ], [ ], op, 1, A, y) ;
    GB_spec_compare (C1, C2, 0, tol) ;

    C1 = GB_mex_apply2 (CT, [ ], [ ], op, 0, A, y, desc0) ;
    C2 = GB_spec_Matrix_eWiseMult (CT, [ ], [ ], op_ewise_bind2nd, A, Y, desc) ;
    GB_spec_compare (C1, C2, 0, tol) ;
    C1 = GB_mex_apply2 (CT, [ ], [ ], op, 1, A, y, desc0) ;
    GB_spec_compare (C1, C2, 0, tol) ;

end
end
end
end
end
end

fprintf ('\nNumber of built-in GraphBLAS operators: %d\n',  n_operators) ;
fprintf ('\ntest154: all tests passed\n') ;