1function test45(use_ssget)
2%TEST45 test GrB_*_setElement and GrB_*_*build
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\ntest45\n------------------ testing GrB_setElement and _build\n') ;
8
9if (nargin < 1)
10    use_ssget = true ;
11end
12
13rng ('default') ;
14A = sparse (rand (3,2)) ;
15
16C = GB_mex_setElement (A, uint64(0), uint64(0), 42.1) ;
17
18A = rand (3,2) ;
19A (2,2) = 0 ;
20A = sparse (A)  ;
21
22C = GB_mex_setElement (A, uint64(1), uint64(1), 99) ;
23GB_spok (C.matrix) ;
24
25if (use_ssget)
26    Prob = ssget ('HB/west0067') ;
27    A = Prob.A ;
28else
29    A = sprand (67, 67, 0.1) ;
30end
31[m n] = size (A) ;
32
33ntuples = 1000 ;
34A1 = A ;
35I = 1 + floor (m * rand (ntuples, 1)) ;
36J = 1 + floor (n * rand (ntuples, 1)) ;
37X = 100 * rand (ntuples, 1) ;
38I0 = uint64 (I)-1 ;
39J0 = uint64 (J)-1 ;
40
41for k = 1:ntuples
42    A1 (I (k), J (k)) =  X (k) ;
43end
44
45A2 = A ;
46A3 = GB_mex_setElement (A2, I0, J0, X) ;
47assert (GB_spok (A3.matrix) == 1)
48
49assert (isequal (A3.matrix, A1)) ;
50% nnz (A)
51% ntuples
52% nnz (A1)
53% nnz (A3.matrix)
54% nnz (A) + ntuples
55
56if (use_ssget)
57    Prob = ssget (2662)
58    A = Prob.A ;
59else
60    n = 2999349 ;
61    nz = 14.3e6 ;
62    density = nz / (n^2) ;
63    A = sprandn (n, n, density) ;
64end
65[m n] = size (A) ;
66fprintf ('nnz(A) = %g\n', nnz (A)) ;
67
68for trial = 1:3
69
70    if (trial == 1)
71        fprintf ('\n---------------------- with I,J,X in sorted order\n') ;
72    elseif (trial == 2)
73        fprintf ('\n---------------------- with I,J,X in randomized order\n') ;
74    elseif (trial == 3)
75        fprintf ('\n---------------------- with I,J,X in randomized order') ;
76        fprintf (' and duplicates\n') ;
77    end
78
79    ntuples = 100 ;
80    A1 = A ;
81    I = 1 + floor (m * rand (ntuples, 1)) ;
82    J = 1 + floor (n * rand (ntuples, 1)) ;
83    X = 100 * rand (ntuples, 1) ;
84    I0 = uint64 (I)-1 ;
85    J0 = uint64 (J)-1 ;
86
87    fprintf ('starting MATLAB... please wait\n') ;
88    tic
89    for k = 1:ntuples
90        A1 (I (k), J (k)) =  X (k) ;
91    end
92    t = toc ;
93    fprintf ('MATLAB set element:   %g sec\n', t) ;
94
95    tic
96    A2 = GB_mex_setElement (A, I0, J0, X) ;
97    t2 = toc ;
98    fprintf ('GraphBLAS setElement: %g seconds speedup %g\n', t2, t/t2) ;
99
100    assert (isequal (A1, A2.matrix))
101
102    tic
103    [I,J,X]=find(A) ;
104    t = toc ;
105    fprintf ('MATLAB find:          %g sec\n', t) ;
106
107    if (trial >= 2)
108        p = randperm (length (X)) ;
109        X = X (p) ;
110        I = I (p) ;
111        J = J (p) ;
112    end
113
114    tic
115    G=sparse(I,J,X) ;
116    t3 = toc ;
117    fprintf ('MATLAB sparse:        %g sec\n', t3) ;
118
119    I0 = uint64 (I)-1 ;
120    J0 = uint64 (J)-1 ;
121    S = sparse (m,n) ;
122    tic
123    S = GB_mex_setElement (S, I0, J0, X) ;
124    t5 = toc ;
125    fprintf ('GraphBLAS setElement: %g sec from scratch, nnz %d\n', ...
126        t5, nnz (S.matrix)) ;
127
128    % fprintf ('GB_spok it 1\n') ;
129    assert (GB_spok (S.matrix*1) == 1) ;
130    assert (isequal (G, S.matrix)) ;
131
132    if (trial == 3)
133        X = [X ; X] ;
134        I = [I ; I] ;
135        J = [J ; J] ;
136        I0 = uint64 (I)-1 ;
137        J0 = uint64 (J)-1 ;
138        fprintf ('\nnow with lots of duplicates\n') ;
139    end
140
141    tic
142    G=sparse(I,J,X) ;
143    t3 = toc ;
144    fprintf ('MATLAB sparse:        %g sec\n', t3) ;
145
146    tic
147    T = GB_mex_Matrix_build (I0, J0, X, m, n) ;
148    t4 = toc ;
149    fprintf ('GraphBLAS build:      %g sec from scratch, nnz %d\n', ...
150        t4, nnz (T.matrix)) ;
151
152    % fprintf ('GB_spok it 2\n') ;
153    assert (GB_spok (T.matrix*1) == 1) ;
154    assert (isequal (G, T.matrix)) ;
155
156    fprintf ('\n------------------- now try a vector B = A(:)\n') ;
157
158    B = A (:) ;
159    blen = size (B,1) ;
160    fprintf ('vector B has length %d with %d nonzeros\n', blen, nnz (B)) ;
161
162    tic
163    [I,J,X]=find(B) ;
164    t = toc ;
165    fprintf ('MATLAB find:          %g sec\n', t) ;
166
167    if (trial == 2)
168        p = randperm (length (X)) ;
169        X = X (p) ;
170        I = I (p) ;
171        J = J (p) ;
172    end
173
174    tic
175    G=sparse(I,J,X,blen,1) ;
176    t3 = toc ;
177    fprintf ('MATLAB sparse:        %g sec\n', t3) ;
178
179    I0 = uint64 (I)-1 ;
180    J0 = uint64 (J)-1 ;
181    S = sparse (blen,1) ;
182
183    tic
184    S = GB_mex_setElement (S, I0, J0, X) ;
185    t5 = toc ;
186    fprintf ('GraphBLAS setElement: %g sec from scratch, nnz %d\n', ...
187        t5, nnz (S.matrix)) ;
188
189    % fprintf ('GB_spok it 3\n') ;
190    assert (GB_spok (S.matrix*1) == 1) ;
191    assert (isequal (G, S.matrix)) ;
192
193    if (trial == 3)
194        X = [X ; X] ;
195        I = [I ; I] ;
196        J = [J ; J] ;
197        I0 = uint64 (I)-1 ;
198        J0 = uint64 (J)-1 ;
199        fprintf ('\nnow with lots of duplicates\n') ;
200    end
201
202    tic
203    G=sparse(I,J,X,blen,1) ;
204    t3 = toc ;
205    fprintf ('MATLAB sparse:        %g sec\n', t3) ;
206
207    tic
208    T = GB_mex_Matrix_build (I0, J0, X, blen, 1) ;
209    t4 = toc ;
210    fprintf ('GraphBLAS mtx: build: %g sec from scratch, nnz %d\n', ...
211        t4, nnz (T.matrix)) ;
212
213    % fprintf ('GB_spok it 4\n') ;
214    T_matrix = T.matrix * 1 ;
215    assert (GB_spok (T_matrix) == 1) ;
216    % assert (isequal (G, T.matrix)) ;
217    assert (norm (G -  T_matrix, 1) / norm (G,1) < 1e-12) ;
218
219    tic
220    T = GB_mex_Vector_build (I0, X, blen) ;
221    t4 = toc ;
222    fprintf ('GraphBLAS vec: build: %g sec from scratch, nnz %d\n', ...
223        t4, nnz (T.matrix)) ;
224
225    % fprintf ('GB_spok it 4\n') ;
226    T_matrix = T.matrix * 1 ;
227    assert (GB_spok (T_matrix) == 1) ;
228    assert (norm (G -  T_matrix, 1) / norm (G,1) < 1e-12) ;
229
230end
231
232
233fprintf ('\ntest45: all tests passed\n') ;
234
235