1function test120 2%TEST120 performance tests for GrB_assign 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('test120:------------------- C(I,J)<!M> += scalar:\n') ; 8 9[save save_chunk] = nthreads_get ; 10chunk = 4096 ; 11 12rng ('default') ; 13n = 4000 ; ; 14 15k = 3000 ; 16 17% I0.begin = 0 ; 18% I0.inc = 1 ; 19% I0.end = k-1 ; 20 I1 = randperm (k) ; 21 I0 = uint64 (I1) - 1 ; 22 23d.mask = 'complement' ; 24 25ncores = feature ('numcores') ; 26 27for dc = [2 0 1e-6 1e-5 1e-4 1e-3 1e-2 0.1 1] 28 29 if (dc == 2) 30 C0 = sparse (rand (n)) ; 31 else 32 C0 = sprand (n, n, dc) ; 33 end 34 35 for dm = [2 0 1e-6 1e-5 1e-4 1e-3 1e-2 0.1 1] 36 37 if (dm == 2) 38 M = sparse (ones (k)) ; 39 else 40 M = spones (sprand (k, k, dm)) ; 41 end 42 43 Mbool = logical (M) ; 44 45 fprintf ('\n--------------------------------------\n') ; 46 fprintf ('dc = %g nnz(C) %8.4f million\n', dc, nnz(C0)/1e6) ; 47 fprintf ('dm = %g nnz(M) %8.4f million\n', dm, nnz(M)/1e6) ; 48 49 tm = inf ; 50 if (n < 500) 51 % MATLAB is exceedingly slow for this case 52 tic 53 C1 = C0 ; 54 % Csub = C1 (1:k, 1:k) ; 55 Csub = C1 (I1, I1) ; 56 Csub (~Mbool) = Csub (~Mbool) + pi ; 57 % C1 (1:k, 1:k) = Csub ; 58 C1 (I1, I1) = Csub ; 59 tm = toc ; 60 end 61 62 scalar = sparse (pi) ; 63 64 for nthreads = [1 2 4 8 16 20 32 40 64] 65 if (nthreads > 2*ncores) 66 break ; 67 end 68 if (nthreads > 1 && t1 < 0.01) 69 break ; 70 end 71 72 nthreads_set (nthreads, chunk) ; 73 74 C2 = GB_mex_subassign (C0, M, 'plus', scalar, I0, I0, d) ; 75 C2 = GB_mex_subassign (C0, M, 'plus', scalar, I0, I0, d) ; 76 tg = grbresults ; 77 if (n < 500) 78 assert (isequal (C1, C2.matrix)) ; 79 end 80 if (nthreads == 1) 81 t1 = tg ; 82 end 83 84 fprintf ('%3d : MATLAB: %10.4f GB: %8.4f ', nthreads, tm, tg) ; 85 fprintf (' speedup %10.4f %10.4f\n', tm / tg, t1/tg) ; 86 87 end 88 end 89end 90 91nthreads_set (save, save_chunk) ; 92