1function test111 2%TEST111 performance test for eWiseAdd 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('\ntest111 performance tests : eWiseAdd \n') ; 8rng ('default') ; 9 10[save save_chunk] = nthreads_get ; 11chunk = 4096 ; 12 13n = 40e6 ; 14% n = 1e6 ; 15Empty = sparse (n, 1) ; 16 17% for d = [0.001 0.01 0.1:.1:1] 18for d = [0.001 0.01 0.1 0.4 1 2 3] 19 20 if (d == 1) 21 A = sparse (rand (n,1)) ; 22 B = sparse (rand (n,1)) ; 23 mds = 1 ; 24 elseif (d == 2) 25 A = sparse (rand (n,1)) ; 26 B = sprand (n,1,0.1) ; 27 mds = 0.1 ; 28 elseif (d == 3) 29 A = sprand (n,1,0.1) ; 30 B = sparse (rand (n,1)) ; 31 mds = 0.1 ; 32 else 33 A = sprand (n,1,d) ; 34 B = sprand (n,1,d) ; 35 mds = [d/36 d/8 d] ; 36 end 37 38 for md = mds 39 40 if (md == 1) 41 M = sparse (rand (n,1)) ; 42 else 43 M = sprand (n,1,md) ; 44 end 45 46 M = spones (M) ; 47 M0 = logical (M) ; 48 49 fprintf ('\n######################################################\n') ; 50 fprintf ('d = %12.4f md = %12.4f\n', d, md) ; 51 fprintf ('######################################################\n') ; 52 fprintf ('nnz (A) = %d nnz (B) = %d nnz (M) = %d\n', ... 53 nnz (A), nnz (B), nnz (M)) ; 54 55 %---------------------------------------------------------------------- 56 % add 57 %---------------------------------------------------------------------- 58 59 % warmup 60 C1 = A + B ; 61 tic 62 C1 = A + B ; 63 toc 64 tm = toc ; 65 fprintf ('nnz (C) = %d\n', nnz (C1)); 66 fprintf ('\nvia GB_add:\n') ; 67 68 for nthreads = [1 2 4 8 20 40] 69 nthreads_set (nthreads, chunk) ; 70 C4 = GB_mex_AplusB (A, B, 'plus') ; 71 tg = grbresults ; 72 if (nthreads == 1) 73 t1 = tg ; 74 end 75 fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ; 76 fprintf ('speedup %12.4f over MATLAB: %12.4f\n', t1/tg, tm/tg) ; 77 assert (GB_spok (C4) == 1) ; 78 assert (isequal (C1, C4)) ; 79 end 80 81 %---------------------------------------------------------------------- 82 % masked add 83 %---------------------------------------------------------------------- 84 85 fprintf ('\nmasked add:\n') ; 86 % warmup 87 C1 = M.*(A + B) ; 88 tic 89 C1 = M.*(A + B) ; 90 toc 91 tm = toc ; 92 fprintf ('nnz (C) = %d\n', nnz (C1)); 93 fprintf ('\nvia masked GB_add:\n') ; 94 95 for nthreads = [1 2 4 8 20 40] 96 nthreads_set (nthreads, chunk) ; 97 % warmup 98 C4 = GB_mex_Vector_eWiseAdd (Empty, M0, [ ], 'plus', A, B, [ ]) ; 99 % 100 C4 = GB_mex_Vector_eWiseAdd (Empty, M0, [ ], 'plus', A, B, [ ]) ; 101 tg = grbresults ; 102 if (nthreads == 1) 103 t1 = tg ; 104 end 105 fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ; 106 fprintf ('speedup %12.4f over MATLAB: %12.4f\n', t1/tg, tm/tg) ; 107 assert (GB_spok (C4.matrix) == 1) ; 108 assert (isequal (C1, C4.matrix)) ; 109 end 110 111 %----------------------------------------------------------------------- 112 % unmasked add then ewise mult 113 %----------------------------------------------------------------------- 114 115 fprintf ('\nunmasked add then emult:\n') ; 116 % warmup 117 C1 = M.*(A + B) ; 118 tic 119 C1 = M.*(A + B) ; 120 toc 121 tm = toc ; 122 fprintf ('nnz (C) = %d\n', nnz (C1)); 123 fprintf ('\nvia unmasked add then emult:\n') ; 124 125 for nthreads = [1 2 4 8 20 40] 126 nthreads_set (nthreads, chunk) ; 127 % warmup 128 C4 = GB_mex_Vector_eWiseAdd (Empty, [ ], [ ], 'plus', A, B, [ ]) ; 129 C4 = GB_mex_Vector_eWiseMult(Empty, [ ], [ ], 'times',M, C4, [ ]) ; 130 % 131 C4 = GB_mex_Vector_eWiseAdd (Empty, [ ], [ ], 'plus', A, B, [ ]) ; 132 tg1 = grbresults ; 133 C4 = GB_mex_Vector_eWiseMult(Empty, [ ], [ ], 'times',M, C4, [ ]) ; 134 tg2 = grbresults ; 135 tg = tg1 + tg2 ; 136 if (nthreads == 1) 137 t1 = tg ; 138 end 139 fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ; 140 fprintf ('speedup %12.4f over MATLAB: %12.4f\n', t1/tg, tm/tg) ; 141 assert (GB_spok (C4.matrix) == 1) ; 142 assert (isequal (C1, C4.matrix)) ; 143 end 144 145 %----------------------------------------------------------------------- 146 % ewise multiply 147 %----------------------------------------------------------------------- 148 149 % warmup 150 C1 = A .* B ; 151 tic 152 C1 = A .* B ; 153 toc 154 tm = toc ; 155 fprintf ('nnz (C) = %d for A.*B\n', nnz (C1)); 156 fprintf ('\nvia GB_eWiseMult:\n') ; 157 158 for nthreads = [1 2 4 8 20 40] 159 nthreads_set (nthreads, chunk) ; 160 % warmup 161 C4 = GB_mex_Vector_eWiseMult (Empty, [ ], [ ], 'times', A,B, [ ]) ; 162 % 163 C4 = GB_mex_Vector_eWiseMult (Empty, [ ], [ ], 'times', A,B, [ ]) ; 164 tg = grbresults ; 165 if (nthreads == 1) 166 t1 = tg ; 167 end 168 fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ; 169 fprintf ('speedup %12.4f over MATLAB: %12.4f\n', t1/tg, tm/tg) ; 170 assert (GB_spok (C4.matrix) == 1) ; 171 assert (isequal (C1, C4.matrix)) ; 172 end 173 174 %----------------------------------------------------------------------- 175 % masked ewise multiply 176 %----------------------------------------------------------------------- 177 178 fprintf ('\nmasked emult:\n') ; 179 % warmup 180 C1 = M.* (A .* B) ; 181 tic 182 C1 = M.* (A .* B) ; 183 toc 184 tm = toc ; 185 fprintf ('nnz (C) = %d for A.*B\n', nnz (C1)); 186 fprintf ('\nvia GB_eWiseMult:\n') ; 187 188 for nthreads = [1 2 4 8 20 40] 189 nthreads_set (nthreads, chunk) ; 190 % warmup 191 C4 = GB_mex_Vector_eWiseMult (Empty, M0, [ ], 'times', A,B, [ ]) ; 192 % 193 C4 = GB_mex_Vector_eWiseMult (Empty, M0, [ ], 'times', A,B, [ ]) ; 194 tg = grbresults ; 195 if (nthreads == 1) 196 t1 = tg ; 197 end 198 fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ; 199 fprintf ('speedup %12.4f over MATLAB: %12.4f\n', t1/tg, tm/tg) ; 200 assert (GB_spok (C4.matrix) == 1) ; 201 assert (isequal (C1, C4.matrix)) ; 202 end 203 204 end 205end 206 207fprintf ('\ndense matrices:\n') ; 208 209A = full (A) ; 210B = full (B) ; 211 212for trial = 1:4 213 tic 214 C1 = A + B ; 215 toc 216 tm = toc ; 217end 218nthreads_set (save, save_chunk) ; 219 220fprintf ('test111: all tests passed\n') ; 221