1function test135 2%TEST135 reduce-to-scalar, built-in monoids with terminal values 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('test135: reduce to scalar\n') ; 8 9rng ('default') ; 10 11n = 10e6 ; 12 13[save_nthreads, save_chunk] = nthreads_get ; 14nthreads_max = feature ('numcores') 15 16%------------------------------------------------------------------------------- 17fprintf ('================== int8 min:\n') ; 18 19X = rand (n,1) ; 20X = (256 * X) - 128 ; 21X = int8 (X) ; 22s = int8 (inf) ; 23tic 24c0 = min (X) ; 25tm = toc ; 26fprintf ('MATLAB: %g sec\n', tm) ; 27 28A.matrix = sparse (double (X)) ; 29A.pattern = logical (spones (X)) ; 30A.class = 'int8' ; 31 32nthreads_set (1,1) ; 33c1 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ; 34assert (c1 == c0) ; 35t1 = grbresults ; 36fprintf ('1 thread %g sec\n', t1) ; 37 38nthreads_set (nthreads_max,1) ; 39c2 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ; 40assert (c2 == c0) ; 41t2 = grbresults ; 42fprintf ('%d threads %g sec\n', nthreads_max, t2) ; 43 44%------------------------------------------------------------------------------- 45fprintf ('================== double min:\n') ; 46 47X = sparse (rand (n,1)) ; 48 49A.matrix = X ; 50A.pattern = logical (spones (X)) ; 51A.class = 'double' ; 52 53tic 54c0 = min (X) ; 55tm = toc ; 56fprintf ('MATLAB: %g sec\n', tm) ; 57 58s = double (inf) ; 59 60nthreads_set (1,1) ; 61c1 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ; 62assert (c1 == c0) ; 63t1 = grbresults ; 64fprintf ('1 thread %g sec\n', t1) ; 65 66nthreads_set (nthreads_max,1) ; 67c2 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ; 68assert (c2 == c0) ; 69t2 = grbresults ; 70fprintf ('%d threads %g sec\n', nthreads_max, t2) ; 71 72%------------------------------------------------------------------------------- 73fprintf ('================== double sum:\n') ; 74 75X = rand (n,1) ; 76tic 77c0 = sum (X) ; 78tm = toc ; 79fprintf ('MATLAB: %g sec (full)\n', tm) ; 80 81X = sparse (X) ; 82 83A.matrix = X ; 84A.pattern = logical (spones (X)) ; 85A.class = 'double' ; 86 87tic 88c0 = full (sum (X)) ; 89tm = toc ; 90fprintf ('MATLAB: %g sec (sparse)\n', tm) ; 91 92s = double (inf) ; 93 94nthreads_set (1,1) ; 95c1 = GB_mex_reduce_to_scalar (s, [ ], 'plus', A) ; 96assert (norm (c1 - c0) / norm (c0) < 1e-12) ; 97t1 = grbresults ; 98fprintf ('1 thread %g sec\n', t1) ; 99 100nthreads_set (nthreads_max,1) ; 101c2 = GB_mex_reduce_to_scalar (s, [ ], 'plus', A) ; 102assert (norm (c2 - c0) / norm (c0) < 1e-12) ; 103t2 = grbresults ; 104fprintf ('%d threads %g sec\n', nthreads_max, t2) ; 105 106%------------------------------------------------------------------------------- 107nthreads_set (save_nthreads, save_chunk) ; 108fprintf ('test135: all tests passed\n') ; 109