1function test42
2%TEST42 test GrB_Matrix_build
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\n----------------------- performance tests for GrB_Matrix_build\n') ;
8
9[save save_chunk] = nthreads_get ;
10chunk = 4096 ;
11nthreads_max = feature ('numcores') ;
12% fprintf ('GraphBLAS: one thread\n') ;
13% nthreads_set (1, chunk) ;
14
15Prob = ssget ('HB/west0067')
16A = Prob.A ;
17[m n] = size (A) ;
18
19[i j x] = find (Prob.A) ;
20i = uint64 (i-1) ;
21j = uint64 (j-1) ;
22T = sparse (double (i+1), double (j+1), x) ;
23assert (isequal (A,T)) ;
24
25S = GB_mex_Matrix_build (i,j,x) ;
26S = S.matrix ;
27assert (isequal (A,S)) ;
28assert (GB_spok (S) == 1) ;
29
30nz = nnz (A) ;
31p = randperm (nz) ;
32i = i(p) ;
33j = j(p) ;
34x = x(p) ;
35
36S = GB_mex_Matrix_build (i,j,x) ;
37S = S.matrix ;
38assert (isequal (A,(S')'))
39assert (isequal (A,S)) ;
40assert (GB_spok (S) == 1) ;
41
42% duplicates
43rng ('default') ;
44i2 = floor (rand (100,1) * n) + 1 ;
45j2 = floor (rand (100,1) * n) + 1 ;
46x2 = rand (100,1) ;
47i = [i ; uint64(i2-1)] ;
48j = [j ; uint64(j2-1)] ;
49x = [x ; x2] ;
50T = sparse (double (i+1), double (j+1), x) ;
51
52S = GB_mex_Matrix_build (i,j,x) ;
53S = S.matrix ;
54assert (isequal (spones (S), spones (T)))
55assert (norm (S-T,1) == 0) ;
56assert (GB_spok (T) == 1) ;
57
58%-------------------------------------------------------------------------------
59fprintf ('----------------------- matrix from collection, no sorting:\n') ;
60Prob = ssget (939)
61A = Prob.A ;
62[m n] = size (A) ;
63[i j x] = find (Prob.A) ;
64i = uint64 (i-1) ;
65j = uint64 (j-1) ;
66i1 = double (i+1) ;
67j1 = double (j+1) ;
68fprintf ('MATLAB:\n') ;
69tic
70T = sparse (i1, j1, x) ;
71toc
72assert (isequal (A,T))
73
74fprintf ('GrB:\n') ;
75for nth = [1 2 4 8 16 20 40]
76    if (nth > 2*nthreads_max)
77        break ;
78    end
79    nthreads_set (nth) ;
80    tic
81    S = GB_mex_Matrix_build (i,j,x) ;
82    S = S.matrix ;
83    t = toc ;
84    fprintf ('GrB with %d threads: %g\n', nth, t) ;
85    assert (isequal (A,S))
86    assert (GB_spok (S) == 1) ;
87end
88
89try
90    fprintf ('Csparse:\n') ;
91    tic
92    W = cs_sparse (i1,j1,x) ;
93    toc
94    ok = isequal (A,W) && (GB_spok (W) == 1) ;
95catch
96    % CSparse not available
97    ok = true ;
98end
99assert (ok) ;
100
101fprintf ('sparse2:\n') ;
102try
103    tic
104    Y = sparse2 (i1,j1,x) ;
105    toc
106    ok = (isequal (A,Y)) && assert (GB_spok (Y) == 1) ;
107catch
108    % CHOLMOD not available
109    ok = true ;
110end
111assert (ok) ;
112
113%-------------------------------------------------------------------------------
114fprintf ('\n----------------------- same matrix, but unsorted:\n') ;
115
116rng ('default') ;
117nz = length (x)
118p = randperm (nz) ;
119i1 = i1 (p) ;
120j1 = j1 (p) ;
121x  = x  (p) ;
122i  = i  (p) ;
123j  = j  (p) ;
124
125fprintf ('MATLAB:\n') ;
126tic
127T = sparse (i1, j1, x) ;
128toc
129
130fprintf ('GrB:\n') ;
131for nth = [1 2 4 8 16 20 40]
132    if (nth > 2*nthreads_max)
133        break ;
134    end
135    nthreads_set (nth) ;
136    tic
137    S = GB_mex_Matrix_build (i,j,x) ;
138    S = S.matrix ;
139    t = toc ;
140    fprintf ('GrB with %d threads: %g\n', nth, t) ;
141    assert (isequal (T,S))
142    assert (GB_spok (S) == 1) ;
143end
144
145%-------------------------------------------------------------------------------
146fprintf ('\n----------------------- random matrix, with duplicates:\n') ;
147i2 = floor (rand (1000000,1) * n) + 1 ;
148j2 = floor (rand (1000000,1) * n) + 1 ;
149x2 = rand (1000000,1) ;
150i = [i ; uint64(i2-1)] ;
151j = [j ; uint64(j2-1)] ;
152x = [x ; x2] ;
153i1 = double (i+1) ;
154j1 = double (j+1) ;
155
156fprintf ('MATLAB:\n') ;
157tic
158T = sparse (i1, j1, x) ;
159toc
160
161fprintf ('GrB:\n') ;
162for nth = [1 2 4 8 16 20 40]
163    if (nth > 2*nthreads_max)
164        break ;
165    end
166    nthreads_set (nth) ;
167    tic
168    S = GB_mex_Matrix_build (i,j,x) ;
169    S = S.matrix ;
170    % norm (T-S,1)
171    t = toc ;
172    fprintf ('GrB with %d threads: %g\n', nth, t) ;
173    assert (isequal (T,S))
174    assert (GB_spok (S) == 1) ;
175end
176
177try
178    fprintf ('Csparse:\n') ;
179    tic
180    W = cs_sparse (i1,j1,x) ;
181    toc
182    ok = isequal (T,W) && (GB_spok (W) == 1) ;
183catch
184    % CSparse not available
185    ok = true ;
186end
187assert (ok) ;
188
189fprintf ('sparse2:\n') ;
190try
191    tic
192    Y = sparse2 (i1,j1,x) ;
193    toc
194    % norm (T-Y,1)
195    ok = (isequal (T,Y)) && assert (GB_spok (Y) == 1) ;
196catch
197    % CHOLMOD not available
198    ok = true ;
199end
200assert (ok) ;
201
202fprintf ('\n----------------------- same random matrix, but presorted:\n') ;
203[ignore,p] = sortrows ([j i x]) ;
204i = i (p) ;
205j = j (p) ;
206x = x (p) ;
207i1 = i1 (p) ;
208j1 = j1 (p) ;
209
210fprintf ('MATLAB:\n') ;
211tic
212T = sparse (i1, j1, x) ;
213toc
214
215fprintf ('GrB:\n') ;
216for nth = [1 2 4 8 16 20 40]
217    if (nth > 2*nthreads_max)
218        break ;
219    end
220    nthreads_set (nth) ;
221    tic
222    S = GB_mex_Matrix_build (i,j,x) ;
223    S = S.matrix ;
224    % norm (T-S,1)
225    t = toc ;
226    fprintf ('GrB with %d threads: %g\n', nth, t) ;
227    assert (isequal (T,S))
228    assert (GB_spok (S) == 1) ;
229end
230
231try
232    fprintf ('CSparse:\n') ;
233    tic
234    W = cs_sparse (i1,j1,x) ;
235    toc
236    ok = isequal (T,W) && (GB_spok (W) == 1) ;
237catch
238    % CSparse not available
239    ok = true ;
240end
241assert (ok) ;
242
243fprintf ('sparse2:\n') ;
244try
245    tic
246    Y = sparse2 (i1,j1,x) ;
247    toc
248    % norm (T-Y,1)
249    ok = (isequal (T,Y)) && assert (GB_spok (Y) == 1) ;
250catch
251    % CHOLMOD not available
252    ok = true ;
253end
254assert (ok) ;
255
256fprintf ('\ntest42: all tests passed\n') ;
257
258nthreads_set (save, save_chunk) ;
259