1function test186
2%TEST186 test saxpy for all sparsity formats
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('test186 --------------- C<!M>A*B for all sparsity formats\n') ;
8
9rng ('default') ;
10
11load west0479 ;
12A.matrix = west0479 ;
13A.class = 'double' ;
14A.pattern = logical (spones (A.matrix)) ;
15m = size (A.matrix, 1) ;
16
17semiring.add = 'plus' ;
18semiring.multiply = 'times' ;
19semiring.class = 'double' ;
20
21any_pair.add = 'any' ;
22any_pair.multiply = 'pair' ;
23any_pair.class = 'double' ;
24
25C0 = sparse (m, 1) ;
26maxerr = 0 ;
27
28M = sparse (rand (m, 1) > 0.5) ;
29desc.mask = 'complement' ;
30
31B = GB_spec_random (m, 1, 0.5, 1, 'double') ;
32B2 = B ;
33B2.class = 'single' ;
34
35% using fine atomic tasks when A is sparse and B is bitmap
36for A_sparsity = [1 2 4 8]
37    for B_sparsity = [1 2 4 8]
38        A.sparsity = A_sparsity ;
39        B.sparsity = B_sparsity ;
40        B2.sparsity = B_sparsity ;
41
42        % C2<!M> = A*B using the conventional semiring
43        C3 = double (~M) .* (A.matrix * B.matrix) ;
44        C2 = GB_mex_mxm  (C0, M, [ ], semiring, A, B, desc) ;
45        err = norm (C3 - C2.matrix, 1) / norm (C3, 1) ;
46        maxerr = max (maxerr, err) ;
47        assert (err < 1e-12) ;
48
49        % C2<!M> = A*single(B) to force typecasting
50        C1 = GB_mex_mxm  (C0, M, [ ], semiring, A, B2, desc) ;
51        err = norm (C3 - C1.matrix, 1) / norm (C3, 1) ;
52        maxerr = max (maxerr, err) ;
53        assert (err < 1e-6) ;
54
55        % C2<!M> = A*B using the any-pair semiring
56        C3 = spones (C3) ;
57        C2 = GB_mex_mxm  (C0, M, [ ], any_pair, A, B, desc) ;
58        err = norm (C3 - C2.matrix, 1) / norm (C3, 1) ;
59        maxerr = max (maxerr, err) ;
60        assert (err < 1e-12) ;
61    end
62end
63
64B3 = GB_spec_random (m, 3, 0.5, 1, 'double') ;
65M3 = sparse (rand (m, 3) > 0.5) ;
66C03 = sparse (m, 3)  ;
67
68% using fine non-atomic tasks when A is sparse and B is bitmap
69A.matrix = sprand (m, m, 0.8) ;
70A.pattern = logical (spones (A.matrix)) ;
71for A_sparsity = [1 2 4 8]
72    for B_sparsity = [1 2 4 8]
73        A.sparsity = A_sparsity ;
74        B.sparsity = B_sparsity ;
75        B3.sparsity = B_sparsity ;
76        fprintf ('.') ;
77
78        % C2<!M> = A*B using the conventional semiring
79        C3 = double (~M) .* (A.matrix * B.matrix) ;
80        C2 = GB_mex_mxm  (C0, M, [ ], semiring, A, B, desc) ;
81        err = norm (C3 - C2.matrix, 1) / norm (C3, 1) ;
82        maxerr = max (maxerr, err) ;
83        assert (err < 1e-12) ;
84
85        % C2<!M> = A*B using the any-pair semiring
86        C3 = spones (C3) ;
87        C2 = GB_mex_mxm  (C0, M, [ ], any_pair, A, B, desc) ;
88        err = norm (C3 - C2.matrix, 1) / norm (C3, 1) ;
89        maxerr = max (maxerr, err) ;
90        assert (err < 1e-12) ;
91
92        % C2<!M3> = A*B3 using the conventional semiring
93        C3 = double (~M3) .* (A.matrix * B3.matrix) ;
94        C2 = GB_mex_mxm  (C03, M3, [ ], semiring, A, B3, desc) ;
95        err = norm (C3 - C2.matrix, 1) / norm (C3, 1) ;
96        maxerr = max (maxerr, err) ;
97        assert (err < 1e-12) ;
98    end
99end
100
101fprintf ('\n') ;
102fprintf ('maxerr: %g\n', maxerr) ;
103fprintf ('test186: all tests passed\n') ;
104
105