1function test90 2%TEST90 test AxB with user-defined semirings: plus_rdiv and plus_rdiv2 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('\n -------------- A*B plus_rdiv (user-defined semiring)\n') ; 8 9 % 1001: Gustavson 10 % 1003: dot 11 % 1004: hash 12 % 1005: saxpy 13 14rng ('default') ; 15 16for N = [10 100] % 1000] 17 18 % create the problem 19 A = sprand (4*N, 5*N, 0.01) ; 20 B = sprand (5*N, 3*N, 0.01) ; 21 22 [i j x] = find (A) ; 23 [m n] = size (A) ; 24 X = sparse (i, j, 1./x, m, n) ; 25 clear i j x 26 27 fprintf ('\nmatlab: ') ; 28 tic 29 C1 = X*B ; 30 toc 31 32 fprintf ('GrB num: ') ; 33 tic 34 C2 = GB_mex_rdiv (A, B) ; 35 toc 36 t = grbresults ; fprintf ('GB time %g\n', t) ; 37 assert (norm (C1-C2,1) / norm (C1,1) < 1e-10) ; 38 39 for method = [1001 1003 1004 1005] 40 fprintf ('method: %d\n', method) ; 41 cprint = (N <= 10) ; 42 tic 43 C2 = GB_mex_rdiv (A, B, method, cprint) ; 44 toc 45 t = grbresults ; fprintf ('GB time %g\n', t) ; 46 assert (norm (C1-C2,1) / norm (C1,1) < 1e-10) ; 47 end 48 49 % try rdiv2, which typecasts B to single precision first 50 fprintf ('GrB num: ') ; 51 tic 52 C3 = GB_mex_rdiv2 (A, B) ; 53 toc 54 assert (norm (C1-C3,1) / norm (C1,1) < 1e-5) ; 55 56 [i j x] = find (B) ; 57 [m n] = size (B) ; 58 Y = sparse (i, j, 1./x, m, n) ; 59 60 fprintf ('\nmatlab: ') ; 61 tic 62 C4 = A*Y ; 63 toc 64 65 % try rdiv2, with flipxy, which inverts A instead of B 66 fprintf ('GrB num: ') ; 67 tic 68 C5 = GB_mex_rdiv2 (A, B, false, false, 0, 1) ; 69 toc 70 assert (norm (C4-C5,1) / norm (C4,1) < 1e-5) ; 71 72 %-------------------------------------------------------------------------- 73 fprintf ('\nextensive tests:\n') ; 74 75 A = sprand (n, n, 0.01) ; 76 B = sprand (n, n, 0.01) ; 77 [i j x] = find (A) ; 78 X = sparse (i, j, 1./x, n, n) ; 79 [i j x] = find (B) ; 80 Y = sparse (i, j, 1./x, n, n) ; 81 82 flipxy = 0 ; 83 at = 0 ; 84 bt = 0 ; 85 fprintf ('\nmatlab:\n') ; 86 tic 87 C0 = X*B ; 88 toc 89 for method = [1001 1003 1004 1005] 90 fprintf ('method %d\n', method) ; 91 tic 92 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 93 % toc 94 t = grbresults ; fprintf ('GB time %g\n', t) ; 95 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 96 end 97 98 flipxy = 0 ; 99 at = 1 ; 100 bt = 0 ; 101 fprintf ('\nmatlab:\n') ; 102 tic 103 C0 = X'*B ; 104 toc 105 for method = [1001 1003 1004 1005] 106 fprintf ('method %d\n', method) ; 107 tic 108 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 109 % toc 110 t = grbresults ; fprintf ('GB time %g\n', t) ; 111 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 112 end 113 114 flipxy = 0 ; 115 at = 0 ; 116 bt = 1 ; 117 fprintf ('\nmatlab:\n') ; 118 tic 119 C0 = X*B' ; 120 toc 121 for method = [1001 1003 1004 1005] 122 fprintf ('method %d\n', method) ; 123 tic 124 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 125 % toc 126 t = grbresults ; fprintf ('GB time %g\n', t) ; 127 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 128 end 129 130 flipxy = 0 ; 131 at = 1 ; 132 bt = 1 ; 133 fprintf ('\nmatlab:\n') ; 134 tic 135 C0 = X'*B' ; 136 toc 137 for method = [1001 1003 1004 1005] 138 fprintf ('method %d\n', method) ; 139 tic 140 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 141 % toc 142 t = grbresults ; fprintf ('GB time %g\n', t) ; 143 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 144 end 145 146 flipxy = 1 ; 147 at = 0 ; 148 bt = 0 ; 149 fprintf ('\nmatlab:\n') ; 150 tic 151 C0 = A*Y ; 152 toc 153 for method = [1001 1003 1004 1005] 154 fprintf ('method %d\n', method) ; 155 tic 156 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 157 % toc 158 t = grbresults ; fprintf ('GB time %g\n', t) ; 159 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 160 end 161 162 flipxy = 1 ; 163 at = 1 ; 164 bt = 0 ; 165 fprintf ('\nmatlab:\n') ; 166 tic 167 C0 = A'*Y ; 168 toc 169 for method = [1001 1003 1004 1005] 170 fprintf ('method %d\n', method) ; 171 tic 172 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 173 % toc 174 t = grbresults ; fprintf ('GB time %g\n', t) ; 175 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 176 end 177 178 flipxy = 1 ; 179 at = 0 ; 180 bt = 1 ; 181 fprintf ('\nmatlab:\n') ; 182 tic 183 C0 = A*Y' ; 184 toc 185 for method = [1001 1003 1004 1005] 186 fprintf ('method %d\n', method) ; 187 tic 188 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 189 % toc 190 t = grbresults ; fprintf ('GB time %g\n', t) ; 191 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 192 end 193 194 flipxy = 1 ; 195 at = 1 ; 196 bt = 1 ; 197 fprintf ('\nmatlab:\n') ; 198 tic 199 C0 = A'*Y' ; 200 toc 201 for method = [1001 1003 1004 1005] 202 fprintf ('method %d\n', method) ; 203 tic 204 C5 = GB_mex_rdiv2 (A, B, at, bt, method, flipxy) ; 205 % toc 206 t = grbresults ; fprintf ('GB time %g\n', t) ; 207 assert (norm (C0-C5,1) / norm (C5,1) < 1e-5) ; 208 end 209 210end 211fprintf ('\ntest90: all tests passed\n') ; 212 213