1function test39(use_ssget) 2%TEST39 performance test for GrB_transpose 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('\ntest39 performance tests : GrB_transpose \n') ; 8 9if (nargin < 1) 10 use_ssget = true ; 11end 12 13[save save_chunk] = nthreads_get ; 14chunk = 4096 ; 15nthreads = feature ('numcores') ; 16nthreads_set (nthreads, chunk) ; 17 18rng ('default') ; 19 20if (use_ssget) 21 try 22 Prob = ssget (939) 23 A = Prob.A ; 24 catch 25 use_ssget = false ; 26 end 27end 28 29if (~use_ssget) 30 fprintf ('not using ssget\n') ; 31 n = 72000 ; 32 nz = 29e6 ; 33 A = sprandn (n, n, nz/n^2) ; 34end 35 36[m n] = size (A) ; 37Cin = sprandn (n, m, 0.000001) ; 38A (1,2) =1 ; 39Empty = sparse (n, m) ; 40 41fprintf ('\n===============================================================n') ; 42fprintf ('\nC = A''\n') ; 43tic 44C1 = A' ; 45toc 46tm = toc ; 47 48fprintf ('GraphBLAS, transpose :\n') ; 49tic 50C = GB_mex_transpose (Empty, [ ], [ ], A) ; 51toc 52tg = grbresults ; 53fprintf ('GraphBLAS time: %g\n', tg) ; 54assert (isequal (C1, C.matrix)) ; 55fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 56 57fprintf ('\n===============================================================n') ; 58fprintf ('\nGraphBLAS: C = (single) A'' compared with C=A'' in MATLAB\n') ; 59clear Empty_struct 60Empty_struct.matrix = sparse (n, m) ; 61Empty_struct.class = 'single' ; 62 63tic 64C1 = A' ; 65toc 66tm = toc ; 67 68fprintf ('GraphBLAS, transpose:\n') ; 69% C = A' 70tic 71C2 = GB_mex_transpose (Empty_struct, [ ], '', A) ; 72toc 73tg = grbresults ; 74fprintf ('GraphBLAS time: %g\n', tg) ; 75fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 76 77[I1, J1, X1] = find (C1) ; 78[I2, J2, X2] = find (C2.matrix) ; 79clear C2 80 81assert (isequal (I1, I2)) ; 82assert (isequal (J1, J2)) ; 83assert (isequal (single(X1), X2)) ; 84 85fprintf ('\n===============================================================n') ; 86fprintf ('\nC = Cin + A''\n') ; 87tic 88C1 = Cin + A' ; 89toc 90tm = toc ; 91 92fprintf ('GraphBLAS, transpose and then accum with GB_add:\n') ; 93% C = Cin + A' 94tic 95C = GB_mex_transpose (Cin, [ ], 'plus', A) ; 96toc 97tg = grbresults ; 98fprintf ('GraphBLAS time: %g\n', tg) ; 99assert (isequal (C1, C.matrix)) ; 100fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 101 102fprintf ('\n===============================================================n') ; 103fprintf ('\nC = A + B\n') ; 104 105B = sprandn (m, n, 0.00001) ; 106fprintf ('nnz (A) = %d nnz (B) = %d\n', nnz (A), nnz (B)) ; 107 108tic 109C1 = A + B ; 110toc 111tm = toc ; 112 113D = struct ('inp0', 'tran') ; 114 115fprintf ('\nusing accum and subassign, then GB_Matrix_wait:\n') ; 116tic 117C2 = GB_mex_transpose (A, [ ], 'plus', B, D) ; 118toc 119tg = grbresults ; 120fprintf ('GraphBLAS time: %g\n', tg) ; 121fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 122assert (isequal (C1, C2.matrix)) ; 123 124fprintf ('\nusing accum and GB_add:\n') ; 125tic 126C2 = GB_mex_transpose (B, [ ], 'plus', A, D) ; 127toc 128tg = grbresults ; 129fprintf ('GraphBLAS time: %g\n', tg) ; 130fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 131assert (isequal (C1, C2.matrix)) ; 132 133fprintf ('\nvia GB_add and then accum:\n') ; 134clear Cin 135Cin = sparse (m,n) ; 136tic 137C3 = GB_mex_Matrix_eWiseAdd (Cin, [ ], '', 'plus', A, B) ; 138toc 139tg = grbresults ; 140fprintf ('GraphBLAS time: %g\n', tg) ; 141fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 142assert (isequal (C1, C3.matrix)) ; 143 144fprintf ('\nvia GB_add:\n') ; 145tic 146C4 = GB_mex_AplusB (A, B, 'plus') ; 147toc 148tg = grbresults ; 149fprintf ('GraphBLAS time: %g\n', tg) ; 150fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 151assert (isequal (C1, C4)) ; 152 153fprintf ('\nvia GB_add:\n') ; 154tic 155C4 = GB_mex_AplusB (B, A, 'plus') ; 156toc 157tg = grbresults ; 158fprintf ('GraphBLAS time: %g\n', tg) ; 159fprintf ('speedup over MATLAB: %g\n\n', tm/tg) ; 160assert (isequal (C1, C4)) ; 161 162fprintf ('\n===============================================================n') ; 163fprintf ('\nC = Cin + A + B\n') ; 164 165Cin = sprandn (m, n, 0.0001) ; 166 167tic 168C1 = Cin + A + B ; 169toc 170tm1 = toc ; 171 172tic 173C1 = (Cin + A) + B ; 174toc 175tm2 = toc ; 176 177tic 178C1 = Cin + (A + B) ; 179toc 180tm3 = toc ; 181 182tic 183C1 = (Cin + B) + A ; 184toc 185tm5 = toc ; 186 187tic 188C3 = GB_mex_Matrix_eWiseAdd (Cin, [ ], 'plus', 'plus', A, B) ; 189toc 190tg = grbresults ; 191fprintf ('GraphBLAS time: %g\n', tg) ; 192fprintf ('speedup over MATLAB: %g\n\n', tm1/tg) ; 193 194assert (isequal (C1, C3.matrix)) ; 195 196fprintf ('\nvia two GB_add: (Cin+A)+B:\n') ; 197tic 198C4 = GB_mex_AplusB (Cin, A, 'plus') ; 199tg1 = grbresults ; 200C4 = GB_mex_AplusB (C4, B, 'plus') ; 201toc 202tg2 = grbresults ; 203fprintf ('GraphBLAS time: %g\n', tg1+tg2) ; 204fprintf ('speedup over MATLAB: %g\n\n', tm2/(tg1+tg2)) ; 205assert (isequal (C1, C4)) ;; 206 207fprintf ('\nvia two GB_add: (Cin+(A+B)):\n') ; 208tic 209C4 = GB_mex_AplusB (A, B, 'plus') ; 210tg1 = grbresults ; 211C4 = GB_mex_AplusB (C4, Cin, 'plus') ; 212tg2 = grbresults ; 213toc 214tg = grbresults ; 215fprintf ('GraphBLAS time: %g\n', tg1+tg2) ; 216fprintf ('speedup over MATLAB: %g\n\n', tm3/(tg1+tg2)) ; 217assert (isequal (C1, C4)) 218 219fprintf ('\nvia two GB_add: (Cin+B)+A)):\n') ; 220tstart = tic ; 221C4 = GB_mex_AplusB (Cin, B, 'plus') ; 222tg1 = grbresults ; 223C4 = GB_mex_AplusB (C4, A, 'plus') ; 224toc (tstart) 225tg2 = grbresults ; 226fprintf ('GraphBLAS time: %g\n', tg1+tg2) ; 227fprintf ('speedup over MATLAB: %g\n\n', tm5/(tg1+tg2)) ; 228assert (isequal (C1, C4)) ;; 229 230nthreads_set (save, save_chunk) ; 231 232fprintf ('\ntest39: all tests passed\n') ; 233 234