File: test107.m

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (248 lines) | stat: -rw-r--r-- 5,562 bytes parent folder | download | duplicates (3)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
function test107
%TEST107 user-defined terminal monoid

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

fprintf ('test107: reduce with built-in and  user-defined terminal monoids\n') ;

rng ('default') ;

save = nthreads_get ;
if (nargin < 1)
    fulltest = false ;
end
nthreads_max = GB_mex_omp_max_threads ;
if (fulltest)
    nthreads_list = [1 2 4 8 16 20 40 64 160] ;
else
    nthreads_list = [1 nthreads_max 2*nthreads_max] ;
end

% create a matrix with entries [0..2]
A = 2 * sparse (rand (4)) ;
s = full (max (max (A))) ;

c = GB_mex_reduce_terminal (A, 2) ;
assert (c == s) ;

% now add the terminal value somewhere
A (1,2) = 2 ;
s = full (max (max (A))) ;
c = GB_mex_reduce_terminal (A, 2) ;
assert (c == s) ;
clear A

ntrials = 10 ;

%-------------------------------------------------------------------------------
% big matrix ...
fprintf ('\nbig matrix, no early exit\n') ;
if (fulltest)
    n = 6000 ;
else
    n = 1000 ;
end
A = sparse (rand (n)) ;

tic
for trial = 1:ntrials
    s = full (max (max (A))) ;
end
tm = toc ;
fprintf ('builtin max: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > 2*nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        c1 = GB_mex_reduce_to_scalar (0, [ ], 'max', A) ;
    end
    tg = toc ;
    assert (s == c1) ;
    if (nthreads == 1)
        t1 = tg ;
    end

    fprintf ('nthreads %3d built-in      %g  speedup %g\n', nthreads, tg, t1/tg) ;

    tic
    for trial = 1:ntrials
        c2 = GB_mex_reduce_terminal (A, 1) ;    % user-defined
    end
    t2 = toc ;
    fprintf ('nthreads %3d %g\n', nthreads, t2) ;
    assert (s == c2) ;

    tic
    for trial = 1:ntrials
        c3 = GB_mex_reduce_terminal (A, 2) ;    % user-defined
    end
    t3 = toc ;
    fprintf ('nthreads %3d %g\n', nthreads, t3) ;
    assert (s == c3) ;

end

%-------------------------------------------------------------------------------
fprintf ('\nbig matrix, with early exit\n') ;

A (n,1) = 1 ;

tic
for trial = 1:ntrials
    s = full (max (max (A))) ;
end
tm = toc ;
fprintf ('builtin max: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        c1 = GB_mex_reduce_to_scalar (0, [ ], 'max', A) ;
    end
    t1 = toc ;
    fprintf ('nthreads %3d built-in      %g\n', nthreads, t1) ;
    tic
    for trial = 1:ntrials
        c2 = GB_mex_reduce_terminal (A, 1) ;    % user-defined
    end
    t2 = toc ;
    fprintf ('nthreads %3d %g\n', nthreads, t2) ;
    assert (s == c1) ;
    assert (s == c2) ;
end

%-------------------------------------------------------------------------------
fprintf ('\nbig matrix, with inf \n') ;

A (n,1) = inf ;

tic
for trial = 1:ntrials
    s = full (max (max (A))) ;
end
tm = toc ;
fprintf ('builtin max: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        c1 = GB_mex_reduce_to_scalar (0, [ ], 'max', A) ;
    end
    t1 = toc ;
    fprintf ('nthreads %3d built-in      %g\n', nthreads, t1) ;
    tic
    for trial = 1:ntrials
        c2 = GB_mex_reduce_terminal (A, inf) ;
    end
    t2 = toc ;
    fprintf ('nthreads %3d %g\n', nthreads, t2) ;
    assert (s == c1) ;
    assert (s == c2) ;
end

%-------------------------------------------------------------------------------
fprintf ('\nbig matrix, with 2 \n') ;

A (n,1) = 2 ;

tic
for trial = 1:ntrials
    s = full (max (max (A))) ;
end
tm = toc ;
fprintf ('builtin max: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        c1 = GB_mex_reduce_to_scalar (0, [ ], 'max', A) ;
    end
    t1 = toc ;
    fprintf ('nthreads %3d built-in      %g\n', nthreads, t1) ;
    tic
    for trial = 1:ntrials
        c2 = GB_mex_reduce_terminal (A, 2) ;
    end
    t2 = toc ;
    fprintf ('nthreads %3d %g\n', nthreads, t2) ;
    assert (s == c1) ;
    assert (s == c2) ;
end

%-------------------------------------------------------------------------------
fprintf ('\nbig matrix, with nan\n') ;

A (n,1) = nan ;

tic
for trial = 1:ntrials
    s = full (max (max (A))) ;
end
tm = toc ;
fprintf ('builtin max: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        c1 = GB_mex_reduce_to_scalar (0, [ ], 'max', A) ;
    end
    t1 = toc ;
    fprintf ('nthreads %3d built-in      %g\n', nthreads, t1) ;
    assert (s == c1) ;
end

assert (s == c1) ;

%-------------------------------------------------------------------------------
fprintf ('\nsum\n') ;

A (n,1) = 1 ;

tic
for trial = 1:ntrials
    ss = full (sum (sum (A))) ;
end
tm = toc ;
fprintf ('builtin sum: %g\n', tm) ;
for nthreads = nthreads_list
    fprintf ('\n') ;
    if (nthreads > nthreads_max)
        break ;
    end
    nthreads_set (nthreads) ;
    tic
    for trial = 1:ntrials
        cc = GB_mex_reduce_to_scalar (0, [ ], 'plus', A) ;
    end
    t1 = toc ;
    fprintf ('nthreads %3d built-in      %g\n', nthreads, t1) ;
    assert (s == c1) ;
end

err = abs (ss - cc) / ss 
assert (err < 1e-12) ;

nthreads_set (save) ;
fprintf ('test107: all tests passed\n') ;