File: test135.m

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (143 lines) | stat: -rw-r--r-- 3,554 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
function test135
%TEST135 reduce-to-scalar, built-in monoids with terminal values

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

fprintf ('test135: reduce to scalar\n') ;

rng ('default') ;

n = 10e6 ;

[save_nthreads, save_chunk] = nthreads_get ;
nthreads_max = feature_numcores

%-------------------------------------------------------------------------------

fprintf ('================== int32 min:\n') ;

    X = rand (n,1) ;
    X = (double (int32 (inf)) * X) - double (int32 (inf) / 2) ;
    X = int32 (X) ;
    X (n/2) = int32 (-inf) ;
    s = int32 (inf) ;
    tic
    c0 = min (X) ;
    tm = toc ;
    fprintf ('built-in: %g sec\n', tm) ;
    A.matrix = sparse (double (X)) ;
    A.pattern = logical (spones (X)) ;
    A.class = 'int32' ;
    nthreads_set (1,1) ;
    tic
    c1 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
    t1 = toc ;
    assert (c1 == c0) ;
    fprintf ('1 thread  %g sec\n', t1) ;
    for nth = 2:64
        nthreads_set (nth,1) ;
        tic
        c2 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
        t2 = toc ;
        assert (c2 == c0) ;
        % fprintf ('%d threads %g sec\n', nth, t2) ;
    end

%-------------------------------------------------------------------------------

fprintf ('================== int8 min:\n') ;

    X = rand (n,1) ;
    X = (256 * X) - 128 ;
    X = int8 (X) ;
    s = int8 (inf) ;
    tic
    c0 = min (X) ;
    tm = toc ;
    fprintf ('built-in: %g sec\n', tm) ;
    A.matrix = sparse (double (X)) ;
    A.pattern = logical (spones (X)) ;
    A.class = 'int8' ;
    nthreads_set (1,1) ;
    tic
    c1 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
    t1 = toc ;
    assert (c1 == c0) ;
    fprintf ('1 thread  %g sec\n', t1) ;
    nthreads_set (nthreads_max,1) ;
    tic
    c2 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
    t2 = toc ;
    assert (c2 == c0) ;
    fprintf ('%d threads %g sec\n', nthreads_max, t2) ;

%-------------------------------------------------------------------------------
fprintf ('================== double min:\n') ;

X = sparse (rand (n,1)) ;

A.matrix = X ;
A.pattern = logical (spones (X)) ;
A.class = 'double' ;

tic
c0 = min (X) ;
tm = toc ;
fprintf ('built-in: %g sec\n', tm) ;

s = double (inf) ;

nthreads_set (1,1) ;
tic
c1 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
t1 = toc ;
assert (c1 == c0) ;
fprintf ('1 thread  %g sec\n', t1) ;

nthreads_set (nthreads_max,1) ;
tic
c2 = GB_mex_reduce_to_scalar (s, [ ], 'min', A) ;
t2 = toc ;
assert (c2 == c0) ;
fprintf ('%d threads %g sec\n', nthreads_max, t2) ;

%-------------------------------------------------------------------------------
fprintf ('================== double sum:\n') ;

X = rand (n,1) ;
tic
c0 = sum (X) ;
tm = toc ;
fprintf ('built-in: %g sec (full)\n', tm) ;

X = sparse (X) ;

A.matrix = X ;
A.pattern = logical (spones (X)) ;
A.class = 'double' ;

tic
c0 = full (sum (X)) ;
tm = toc ;
fprintf ('built-in: %g sec (sparse)\n', tm) ;

s = double (inf) ;

nthreads_set (1,1) ;
tic
c1 = GB_mex_reduce_to_scalar (s, [ ], 'plus', A) ;
t1 = toc ;
assert (norm (c1 - c0) / norm (c0) < 1e-12) ;
fprintf ('1 thread  %g sec\n', t1) ;

nthreads_set (nthreads_max,1) ;
tic
c2 = GB_mex_reduce_to_scalar (s, [ ], 'plus', A) ;
t2 = toc ;
assert (norm (c2 - c0) / norm (c0) < 1e-12) ;
fprintf ('%d threads %g sec\n', nthreads_max, t2) ;

%-------------------------------------------------------------------------------
nthreads_set (save_nthreads, save_chunk) ;
fprintf ('test135: all tests passed\n') ;