File: test06.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 (324 lines) | stat: -rw-r--r-- 8,546 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
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
function test06 (A,B,fulltests,method_list)
%TEST06 test GrB_mxm on all semirings
%
% Usage: test06(A)
%        test06(A,B)
%        test06(A,B,fulltests)
%
% with no input, a small 10-by-10 matrix is used.  If A is a scalar, it is a
% matrix id number from the SuiteSparse collection otherwise A is the sparse
% matrix to use in the test

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

fprintf ('test06: GrB_mxm on all semirings\n') ;

[binops, ~, add_ops, types, ~, ~] = GB_spec_opsall ;
% mult_ops = binops.positional ;
mult_ops = binops.all ;
types = types.all ;

if (nargin < 3)
    fprintf ('\n-------------- GrB_mxm on all semirings\n') ;
    fulltests = 1 ;
end

if (nargin < 2)
    B = [ ] ;
end

if (nargin < 4)
    method_list = 0:3 ;
end

rng ('default') ;

if (isscalar (A))
    Prob = ssget (A)
    A = Prob.A ;
    clear Prob
    A (1,2) = 1 ;
    if (isempty (B))
        B = 2*A ;

%       n = size (A,2) ;
%       p = randperm (n) ;
%       B = sparse (1:n, p, ones (n,1)) ;
%       p = randperm (n) ;
%       B = B + sparse (1:n, p, ones (n,1)) ;
%       p = randperm (n) ;
%       B = B + sparse (1:n, p, ones (n,1)) ;
%       e = ones (n,1) ;
%       % B = spdiags([e -2*e e], -1:1, n, n) ;
%       % B = spdiags([-2*e e], 0:1, n, n) ;
%       % B = speye(n);
%       T = A ; A = B ; B = T ;

    end
end

assert (issparse (A)) ;

[m n] = size (A) ;
Cin = sparse (m, n) ;
assert (m == n) ;

rng ('default') ;
[i, j, ~] = find (A) ;
nz = nnz (A) ;
p = randperm (nz, floor (nz/2)) ;

Mask = sparse (i (p), j (p), ones (length (p),1), m, n) + ...
       spones (sprandn (m, n, 1/n)) ;


tic
C = A*B ;
tm1 = toc ;

% Mask = spones (A) ;
% Mask = sparse (i (p), j (p), ones (length (p),1), m, n) ;

tic
C = A'*B ;
tm2 = toc ;

tic
C = A*B' ;
tm3 = toc ;

tic
C = A'*B' ;
tm5 = toc ;

if (n > 500)
    fprintf ('built-in time: %g %g %g %g\n', tm1, tm2, tm3, tm5) ;
    fprintf ('with mask:\n') ;
end

tic
C = (A*B) .* Mask ;
tmm1 = toc ;

tic
C = (A'*B) .* Mask ;
tmm2 = toc ;

tic
C = (A*B') .* Mask ;
tmm3 = toc ;

tic
C = (A'*B') .* Mask ;
tmm5 = toc ;

if (n > 500)
    fprintf ('built-in time: %g %g %g %g\n', tmm1, tmm2, tmm3, tmm5) ;
end

dnn = struct ;
dtn = struct ( 'inp0', 'tran' ) ;
dnt = struct ( 'inp1', 'tran' ) ;
dtt = struct ( 'inp0', 'tran', 'inp1', 'tran' ) ;

n_semirings = 0 ;

if (fulltests)
    k1_list = 1:length (mult_ops) ;
    k2_list = 1:length (add_ops) ;
    k3_list = 1:length (types) ;
else
    % just use plus-times-double semiring
    k1_list = 4 ;
    k2_list = 3 ;
    k3_list = 11 ;
end

n = size (A,1) ;

for k1 = k1_list % 1:length(mult_ops)
    mulop = mult_ops {k1} ;
    if (n <= 500)
        fprintf ('\n%s', mulop) ;
    end

    for k2 = k2_list % 1:length(add_ops)
        addop = add_ops {k2} ;

        for k3 = k3_list % 1:length (types)
            semiring_type = types {k3} ;
            if (n <= 500)
               fprintf ('.') ;
            end

            semiring.multiply = mulop ;
            semiring.add = addop ;
            semiring.class = semiring_type ;

            % create the semiring.  some are not valid because the or,and,xor,eq
            % monoids can only be used when z is boolean for z=mult(x,y).
            try
                [mult_op add_op id] = GB_spec_semiring (semiring) ;
                [mult_opname mult_optype ztype xtype ytype] = ...
                    GB_spec_operator (mult_op) ;
                [ add_opname  add_optype] = GB_spec_operator (add_op) ;
                identity = GB_spec_identity (semiring.add, add_optype) ;
            catch me
                if (~isempty (strfind (me.message, 'gotcha')))
                    semiring
                end
                continue
            end

            n_semirings = n_semirings + 1 ;

            for method = method_list % 0:3

                if (n > 500)
                    fprintf ('%3d ', n_semirings) ;
                    fprintf ('[%6s %6s %8s] : ', mulop, addop, semiring_type) ;
                end

                if (method == 1)
                    algo = 'hash' ;
                    if (n > 500)
                        fprintf ('hash ') ;
                    end
                elseif (method == 2)
                    algo = 'gustavson' ;
                    if (n > 500)
                        fprintf ('g/s  ') ;
                    end
                elseif (method == 3)
                    algo = 'dot' ;
                    if (n > 500)
                        fprintf ('dot  ') ;
                    end
                else
                    algo = 'default' ;
                    if (n > 500)
                        fprintf ('auto ') ;
                    end
                end
                if (isequal (algo, 'dot'))
                    ok = (n < 1000) ;
                else
                    ok = true ;
                end

                dnn.axb = algo ;
                dnt.axb = algo ;
                dtn.axb = algo ;
                dtt.axb = algo ;

                t1 = nan ;
                t2 = nan ;
                t3 = nan ;
                t4 = nan ;

                % C = A*B, no mask
                % tic
                if (ok)
                tic
                C1 = GB_mex_mxm  (Cin, [ ], [ ], semiring, A, B, dnn) ;
                t1 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, [ ], [ ], semiring, A, B, dnn);
                GB_spec_compare (C1, C2, id) ;
                end
                end

                % C = A'*B, no mask
                if (ok)
                tic
                C1 = GB_mex_mxm  (Cin, [ ], [ ], semiring, A, B, dtn);
                t2 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, [ ], [ ], semiring, A, B, dtn);
                GB_spec_compare (C1, C2, id) ;
                end
                end

                % C = A*B', no mask
                if (ok)
                tic
                C1 = GB_mex_mxm  (Cin, [ ], [ ], semiring, A, B, dnt);
                t3 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, [ ], [ ], semiring, A, B, dnt);
                GB_spec_compare (C1, C2, id) ;
                end
                end

                % C = A'*B', no mask
                if (ok)
                tic
                C1 = GB_mex_mxm  (Cin, [ ], [ ], semiring, A, B, dtt);
                t4 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, [ ], [ ], semiring, A, B, dtt);
                GB_spec_compare (C1, C2, id) ;
                end
                end

                if (n > 500)
                    fprintf (...
                    'speedups %10.4f(%s) %10.4f(%s) %10.4f(%s) %10.4f(%s) ', ...
                    tm1/t1, tm2/t2, tm3/t3, tm5/t4 ) ;
                end

                % C = A*B, with mask
                tic
                C1 = GB_mex_mxm  (Cin, Mask, [ ], semiring, A, B, dnn);
                t1 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, Mask, [ ], semiring, A, B, dnn);
                GB_spec_compare (C1, C2, id) ;
                end

                % C = A'*B, with mask
                tic
                C1 = GB_mex_mxm  (Cin, Mask, [ ], semiring, A, B, dtn);
                t2 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, Mask, [ ], semiring, A, B, dtn);
                GB_spec_compare (C1, C2, id) ;
                end

                % C = A*B', with mask
                tic
                C1 = GB_mex_mxm  (Cin, Mask, [ ], semiring, A, B, dnt);
                t3 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, Mask, [ ], semiring, A, B, dnt);
                GB_spec_compare (C1, C2, id) ;
                end

                % C = A'*B', with mask
                tic
                C1 = GB_mex_mxm  (Cin, Mask, [ ], semiring, A, B, dtt);
                t4 = toc ;
                if (n < 200)
                C2 = GB_spec_mxm (Cin, Mask, [ ], semiring, A, B, dtt);
                GB_spec_compare (C1, C2, id) ;
                end

                if (n > 500)
                    fprintf (...
                    'speedups %10.4f(%s) %10.4f(%s) %10.4f(%s) %10.4f(%s) ', ...
                    tmm1/t1, tmm2/t2, tmm3/t3, tmm5/t4) ;
                    fprintf ('\n') ;
                end

            end
        end
    end
end

% n_semirings

if (fulltests)
    fprintf ('\ntest06: all tests passed\n') ;
end