File: test20.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 (393 lines) | stat: -rw-r--r-- 10,962 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
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
385
386
387
388
389
390
391
392
393
function test20(fulltest)
%TEST20 test GrB_mxm, mxv, and vxm

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

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

tic

if (nargin < 1)
    fulltest = 0 ;
end

if (fulltest == 2)
    fprintf ('test20: lengthy tests of GrB_mxm, mxv, and vxm\n') ;
    n_semirings_max = inf ;
else
    fprintf ('test20: quick test of GrB_mxm, mxv, and vxm\n') ;
    n_semirings_max = 1 ;
end

% types to test:
kk = 1 ;
% ops to test
aa = 1 ;

if (fulltest > 0)
    k1_list = 1:length(mult_ops) ;
    k2_list = 1:length(add_ops) ;
    k3_list = 1:length(types) ;
else
    k1_list = [ 9 ] ;   % times
    k2_list = [ 3 ] ;   % plus
    k3_list = [ 11 ] ;  % double
end

kk = min (kk, length (types)) ;

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

n_semirings = 0 ;
ntrials = 0 ;

for k1 = k1_list % 1:length(mult_ops)
    mulop = mult_ops {k1} ;
    % if (~GB_spec_is_positional (mulop))
    %     continue ;
    % end

    for k2 = k2_list % 1:length(add_ops)

    addop = add_ops {k2} ;
    fprintf ('\nsemiring %s:%s ', addop, mulop) ;

    for k3 = k3_list % 1:length (types)

    rng ('default') ;
    type = types {k3} ;

    semiring.multiply = mulop ;
    semiring.add = addop ;
    semiring.class = type ;
    if (n_semirings_max == 1)
        semiring
    end

    % create the semiring.  some are not valid because the or,and,xor
    % 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] = GB_spec_operator (mult_op);
        [ add_opname  add_optype] = GB_spec_operator (add_op) ;
        identity = GB_spec_identity (semiring.add, add_optype) ;
    catch
        continue
    end

    if (n_semirings+1 > n_semirings_max)
        fprintf ('\ntest20: all quick tests passed\n') ;
        return ;
    end

    % fprintf ('\n%4d ', n_semirings) ;
    % fprintf ('%12.2f mxm semiring %s:%s:%s ', toc,addop,mulop,type) ;
    % fprintf (' id: %g ', double (identity)) ;
    n_semirings = n_semirings + 1 ;

    % for k4 = [0 5 11 15] % 0:length(mult_ops)
    for k4 = [ 0 randperm(length(mult_ops), aa)]

    if (k4 == 0)
        accum_op = '' ;
        ntypes = 1 ;
    else
        accum_op = mult_ops {k4} ;
        % ntypes = [1 2 8 ] ; % length (types) ;
        % ntypes = 1:length (types) ;
        ntypes = randperm (length (types),kk) ;
    end

    for k5 = ntypes

    clear accum
    if (~isempty (accum_op))
        accum_type = types {k5} ;
        accum.opname = accum_op ;
        accum.optype = accum_type ;
    else
        accum = '' ;
        accum_type = '' ;
    end

    if (GB_spec_is_positional (accum))
        continue ;
    end

    try
        GB_spec_operator (accum) ;
    catch
        continue
    end

    for Mask_complement = [false true]

    if (Mask_complement)
        dnn.mask = 'complement' ;
        dtn.mask = 'complement' ;
        dnt.mask = 'complement' ;
        dtt.mask = 'complement' ;
    else
        dnn.mask = 'default' ;
        dtn.mask = 'default' ;
        dnt.mask = 'default' ;
        dtt.mask = 'default' ;
    end

    for C_replace = [false true]

    if (C_replace)
        dnn.outp = 'replace' ;
        dtn.outp = 'replace' ;
        dnt.outp = 'replace' ;
        dtt.outp = 'replace' ;
    else
        dnn.outp = 'default' ;
        dtn.outp = 'default' ;
        dnt.outp = 'default' ;
        dtt.outp = 'default' ;
    end

    % pick a random class, and int32
    atypes = randperm(length(types),kk) ;
    %  1:length (types)
    atypes = unique ([atypes 6]) ;

    % try all matrix types, to test casting
    for k6 = atypes

    aclas = types {k6} ;

    if (isequal (aclas, 'int32') && ...
        mod (n_semirings, 100) == 1)
        % single or double would lead to
        % different roundoff errors
        hyper_range = 0:1 ;
        csc_range   = 0:1 ;
    else
        hyper_range = 0 ;
        csc_range   = 1 ;
    end

    % try some matrices
    for m = 8 % [1 5 10 ]

    for n = 5 % [ 1 5 10 ]

    for s = 4 % [ 1 5 10 ]
    for density = [0.1 0.2 0.3 0.5]

    % try all combinations of hyper/
    % non-hyper and CSR/CSC
    for A_is_hyper = hyper_range
    for A_is_csc   = csc_range
    for B_is_hyper = hyper_range
    for B_is_csc   = csc_range
    for C_is_hyper = hyper_range
    for C_is_csc   = csc_range
    for M_is_hyper = hyper_range
    for M_is_csc   = csc_range

    if (mod (ntrials, 23) == 0)
        fprintf ('.') ;
    end

    %-----------------------------------
    % A*B
    %-----------------------------------

    A = GB_spec_random (m,s,density,100,aclas, A_is_csc, A_is_hyper) ;
    B = GB_spec_random (s,n,density,100,aclas, B_is_csc, B_is_hyper) ;
    C = GB_spec_random (m,n,density,100,aclas, C_is_csc, C_is_hyper) ;

    % C = A*B, no mask
    Mask = [ ] ;
    C0 = GB_spec_mxm (C, [ ], accum, semiring, A, B, dnn);
    C1 = GB_mex_mxm  (C, [ ], accum, semiring, A, B, dnn);
    GB_spec_compare (C0, C1, identity) ;

    % w = A*u, no mask
    w = GB_spec_random (m,1,density,100,type) ;
    u = GB_spec_random (s,1,density,100,type) ;

    w0 = GB_spec_mxv (w, [ ], accum, semiring, A, u, dnn);
    w1 = GB_mex_mxv  (w, [ ], accum, semiring, A, u, dnn);
    GB_spec_compare (w0, w1, identity) ;

    % w' = u'*A' no mask
    w0 = GB_spec_vxm (w, [ ], accum, semiring, u, A, dnt);
    w1 = GB_mex_vxm  (w, [ ], accum, semiring, u, A, dnt);
    GB_spec_compare (w0, w1, identity) ;

    % C = A*B with mask
    % Mask = sprandn (m,n,0.2) ~= 0 ;
    Mask = GB_random_mask(m,n,0.2, M_is_csc, M_is_hyper) ;
    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dnn);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dnn);
    GB_spec_compare (C0, C1, identity) ;

    % C = A*B with mask (with explicit zero entries)
    Mask1 = sprandn (m,n,0.2) ~= 0 ;
    Mask2 = Mask1 .* spones (sprandn (m,n,0.5)) ;
    S = sparse (m,n) ;
    Mask3 = GB_mex_Matrix_eWiseAdd (S,[ ],[ ],'minus',Mask1,Mask2) ;
    clear Mask
    Mask.matrix = Mask3.matrix ;
    Mask.is_csc = M_is_csc ;
    Mask.is_hyper = M_is_hyper ;
    clear Mask1 Mask2 Mask3
    % the Mask matrix will not pass GB_spok(Mask) test since
    % it will have explicit zeros

    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dnn);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dnn);
    GB_spec_compare (C0, C1, identity) ;

    % w = A*u with mask
    % mask = sprandn (m,1,0.2) ~= 0 ;
    mask = GB_random_mask (m,1,0.2) ;
    w0 = GB_spec_mxv (w, mask, accum, semiring, A, u, dnn);
    w1 = GB_mex_mxv  (w, mask, accum, semiring, A, u, dnn);
    GB_spec_compare (w0, w1, identity) ;

    % w' = u'*A' with mask
    w0 = GB_spec_vxm (w, mask, accum, semiring, u, A, dnt) ;
    w1 = GB_mex_vxm  (w, mask, accum, semiring, u, A, dnt) ;
    GB_spec_compare (w0, w1, identity) ;

    %-----------------------------------
    % A'*B
    %-----------------------------------

    A = GB_spec_random (s,m,density,100,aclas, A_is_csc, A_is_hyper) ;
    B = GB_spec_random (s,n,density,100,aclas, B_is_csc, B_is_hyper) ;
    C = GB_spec_random (m,n,density,100,aclas, C_is_csc, C_is_hyper) ;

    % C = A'*B, no Mask
    C0 = GB_spec_mxm (C, [ ], accum, semiring, A, B, dtn);
    C1 = GB_mex_mxm  (C, [ ], accum, semiring, A, B, dtn);
    GB_spec_compare (C0, C1, identity) ;

    % w = A'*u, no mask

    w0 = GB_spec_mxv (w, [ ], accum, semiring, A, u, dtn);
    w1 = GB_mex_mxv  (w, [ ], accum, semiring, A, u, dtn);
    GB_spec_compare (w0, w1, identity) ;

    % w' = u'*A, no mask
    w0 = GB_spec_vxm (w, [ ], accum, semiring, u, A, dnn);
    w1 = GB_mex_vxm  (w, [ ], accum, semiring, u, A, dnn);
    GB_spec_compare (w0, w1, identity) ;

    % C = A'*B with mask
    % Mask = sprandn (m,n,0.2) ~= 0 ;
    Mask = GB_random_mask (m,n,0.2, M_is_csc, M_is_hyper) ;
    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dtn);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dtn);
    GB_spec_compare (C0, C1, identity) ;

    % C = A'*B with mask
    Mask1 = sprandn (m,n,0.2) ~= 0 ;
    Mask2 = Mask1 .* spones (sprandn (m,n,0.5)) ;
    S = sparse (m,n) ;
    Mask3 = GB_mex_Matrix_eWiseAdd (S,[ ],[ ],'minus',Mask1,Mask2) ;
    clear Mask
    Mask.matrix = Mask3.matrix ;
    Mask.is_csc = M_is_csc ;
    Mask.is_hyper = M_is_hyper ;
    clear Mask1 Mask2 Mask3
    % the Mask matrix will not pass GB_spok(Mask) test since
    % it will have explicit zeros
    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dtn);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dtn);
    GB_spec_compare (C0, C1, identity) ;

    % w = A'*u, with mask
    % mask = sprandn (m,1,0.2) ~= 0 ;
    mask = GB_random_mask (m,1,0.2) ;
    w0 = GB_spec_mxv (w, mask, accum, semiring, A, u, dtn);
    w1 = GB_mex_mxv  (w, mask, accum, semiring, A, u, dtn);
    GB_spec_compare (w0, w1, identity) ;

    % w' = u'*A, with mask
    w0 = GB_spec_vxm (w, mask, accum, semiring, u, A, dnn);
    w1 = GB_mex_vxm  (w, mask, accum, semiring, u, A, dnn);
    GB_spec_compare (w0, w1, identity) ;

    %-----------------------------------
    % A*B'
    %-----------------------------------

    % no mask

    A = GB_spec_random (m,s,density,100,aclas, A_is_csc, A_is_hyper) ;
    B = GB_spec_random (n,s,density,100,aclas, B_is_csc, B_is_hyper) ;
    C = GB_spec_random (m,n,density,100,aclas, C_is_csc, C_is_hyper) ;

    C0 = GB_spec_mxm (C, [ ], accum, semiring, A, B, dnt);
    C1 = GB_mex_mxm  (C, [ ], accum, semiring, A, B, dnt);
    GB_spec_compare (C0, C1, identity) ;

    % with mask
    % Mask = sprandn (m,n,0.2) ~= 0 ;
    Mask = GB_random_mask (m,n,0.2, M_is_csc, M_is_hyper) ;
    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dnt);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dnt);
    GB_spec_compare (C0, C1, identity) ;

    %-----------------------------------
    % A'*B'
    %-----------------------------------

    % no Mask

    A = GB_spec_random (s,m,density,100,aclas, A_is_csc, A_is_hyper) ;
    B = GB_spec_random (n,s,density,100,aclas, B_is_csc, B_is_hyper) ;
    C = GB_spec_random (m,n,density,100,aclas, C_is_csc, C_is_hyper) ;

    C0 = GB_spec_mxm (C, [ ], accum, semiring, A, B, dtt);
    C1 = GB_mex_mxm  (C, [ ], accum, semiring, A, B, dtt);
    GB_spec_compare (C0, C1, identity) ;


    % A'*B', with mask
    % Mask = sprandn (m,n,0.2) ~= 0 ;
    Mask = GB_random_mask (m,n,0.2, M_is_csc, M_is_hyper) ;

    C0 = GB_spec_mxm (C, Mask, accum, semiring, A, B, dtt);
    C1 = GB_mex_mxm  (C, Mask, accum, semiring, A, B, dtt);
    GB_spec_compare (C0, C1, identity) ;

    ntrials = ntrials + 1 ;


end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end

fprintf ('semirings tested: %d\n', n_semirings) ;
fprintf ('\ntest20: all tests passed\n') ;