1function test43
2%TEST43 test subref
3
4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
5% SPDX-License-Identifier: Apache-2.0
6
7fprintf ('\n------------------------------ testing GB_mex_Matrix_subref\n') ;
8
9rng ('default')
10
11A = sparse (rand (3,4)) ;
12I = uint64 (0:2) ;
13J = uint64 (0:3) ;
14
15C0 = A (I+1,J+1) ;
16C = GB_mex_Matrix_subref (A, I, J) ;
17assert (isequal (C, C0))
18
19C0 = A (:,:) ;
20C = GB_mex_Matrix_subref (A, [ ], [ ]) ;
21assert (isequal (C, C0))
22
23C0 = A (1,:) ;
24C = GB_mex_Matrix_subref (A, uint64(0), [ ]) ;
25assert (isequal (C, C0))
26
27C0 = A (:,1) ;
28C = GB_mex_Matrix_subref (A, [ ], uint64(0)) ;
29assert (isequal (C, C0))
30
31I = uint64 ([0 1]) ;
32J = uint64 ([0 1]) ;
33A = sparse (rand (4)) ;
34C0 = full (A (I+1,J+1)) ;
35full (A) ;
36C = GB_mex_Matrix_subref (A, I, J) ;
37full (C) ;
38assert (isequal (C, C0))
39
40I = uint64 ([2 1]) ;
41J = uint64 ([3 1]) ;
42C0 = full (A (I+1,J+1)) ;
43C = GB_mex_Matrix_subref (A, I, J) ;
44C = full (C) ;
45assert (isequal (C, C0)) ;
46
47fprintf ('-------------------------- problem:\n') ;
48% Prob = ssget ('HB/west0067') ; A = Prob.A ;
49  Prob = ssget (939) ; A = Prob.A ;
50% A = sparse (rand (8000)) ;
51A(1,2) =44 ;
52
53fprintf ('-------------------------- case 5, ni large, qsort, no dupl:\n') ;
54p = amd (A) ;
55fprintf ('MATLAB:\n') ;
56tic
57A1 = A (p,p) ;
58t0 = toc ;
59p0 = uint64 (p-1) ;
60fprintf ('GB:\n') ;
61tic
62A2 = GB_mex_Matrix_subref (A, p0, p0) ;
63t1 = toc ;
64
65fprintf ('CSparse permute:\n') ;
66try
67    tic
68    A3 = cs_permute (A, p, p) ;
69    t2 = toc ;
70    ok = isequal (A1, A3) ;
71catch
72    % CSparse not available
73    t2 = 0 ;
74    ok = true ;
75end
76assert (ok) ;
77
78assert (isequal (A1, A2)) ;
79fprintf ('MATLAB %g GrB %g CSparse %g\n', t0, t1, t2) ;
80
81n = size (A,1) ;
82
83fprintf ('-------------------------- case 5, ni large, qsort, no dupl:\n') ;
84I = uint64 (randperm (floor (n/2))) ;
85J = uint64 (randperm (floor (n/2))) ;
86I1 = I + 1 ;
87J1 = J + 1 ;
88fprintf ('MATLAB:\n') ;
89    tic
90    C0 = A (I1,J1) ;
91    toc
92fprintf ('GB:\n') ;
93    tic
94    C1 = GB_mex_Matrix_subref (A, I, J) ;
95    toc
96    assert (isequal (C0, C1)) ;
97
98fprintf ('-------------------------- contig:\n') ;
99I = sort (I) ;
100J = sort (J) ;
101I1 = I + 1 ;
102J1 = J + 1 ;
103fprintf ('length (I), %d min %d max %d\n', length (I), min (I), max (I)) ;
104fprintf ('MATLAB:\n') ;
105    tic
106    C0 = A (I1,J1) ;
107    toc
108fprintf ('GB:\n') ;
109    tic
110    C1 = GB_mex_Matrix_subref (A, I, J) ;
111    toc
112    assert (isequal (C0, C1)) ;
113
114fprintf ('-------------------------- case 5, ni large, qsort, with dupl:\n') ;
115I = uint64 (floor (n * rand (n,1))) ;
116J = uint64 (floor (n * rand (n,1))) ;
117I1 = I + 1 ;
118J1 = J + 1 ;
119fprintf ('MATLAB:\n') ;
120    tic
121    C0 = A (I1,J1) ;
122    toc
123fprintf ('GB:\n') ;
124    tic
125    C1 = GB_mex_Matrix_subref (A, I, J) ;
126    toc
127    assert (isequal (C0, C1)) ;
128
129    fprintf ('double transpose time in MATLAB:\n') ;
130    tic
131    C0 = C0'' ;
132    toc
133
134fprintf ('-------------------------- first rows:\n') ;
135i = uint64 (floor(n/2)) ;
136I = [ ] ;
137J = uint64 (0:n-1) ;
138J1 = J + 1 ;
139for k = i:(i+50)
140    if (k >= size (A,1))
141        break ;
142    end
143    I = [I k] ;
144    I1 = I+1 ;
145    fprintf (' %3d rows: ', length (I)) ;
146    tic
147    C0 = A (I1,:) ;
148    t0 = toc ;
149    fprintf ('C0: %9d %9d  ', nnz (C0), nzmax (C0)) ;
150    tic
151    C1 = GB_mex_Matrix_subref (A, I, [ ]) ;
152    t1 = toc ;
153    assert (isequal (C0, C1)) ;
154    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
155end
156
157fprintf ('-------------------------- last row:\n') ;
158i = uint64 (n-1) ;
159I = i ;
160I1 = I+1 ;
161tic
162C0 = A (I1,:) ;
163t0 = toc ;
164fprintf ('C0: %9d %9d  ', nnz (C0), nzmax (C0)) ;
165tic
166C1 = GB_mex_Matrix_subref (A, I, [ ]) ;
167t1 = toc ;
168assert (isequal (C0, C1)) ;
169fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
170
171fprintf ('-------------------------- contig, sorted:\n') ;
172I = uint64 (0:floor(n/2)) ;
173J = uint64 (0:floor(n/2)) ;
174I1 = I + 1 ;
175J1 = J + 1 ;
176fprintf ('MATLAB:\n') ;
177    tic
178    C0 = A (I1,J1) ;
179    toc
180fprintf ('GB:\n') ;
181    tic
182    C1 = GB_mex_Matrix_subref (A, I, J) ;
183    toc
184    assert (isequal (C0, C1)) ;
185
186fprintf ('-------------------------- contig lower half, sorted:\n') ;
187I = uint64 (floor(n/2):n-1) ;
188J = uint64 (floor(n/2):n-1) ;
189I1 = I + 1 ;
190J1 = J + 1 ;
191fprintf ('length (I), %d min %d max %d\n', length (I), min (I), max (I)) ;
192fprintf ('MATLAB:\n') ;
193    tic
194    C0 = A (I1,J1) ;
195    toc
196fprintf ('GB:\n') ;
197    tic
198    C1 = GB_mex_Matrix_subref (A, I, J) ;
199    toc
200    assert (isequal (C0, C1)) ;
201
202fprintf ('-------------------------- one row:\n') ;
203for i = 0:1000:n-1
204    I = uint64 (i) ;
205    J = uint64 (0:n-1) ;
206    I1 = I+1 ;
207    J1 = J+1 ;
208    tic
209    C0 = A (I1,J1) ;
210    t0 = toc ;
211    tic
212    C1 = GB_mex_Matrix_subref (A, I, J) ;
213    t1 = toc ;
214    assert (isequal (C0, C1)) ;
215    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
216end
217
218
219
220fprintf ('-------------------------- two rows:\n') ;
221for i = 0:1000:n-2
222    I = uint64([i i+1]) ;
223    J = uint64 (0:n-1) ;
224    I1 = I+1 ;
225    J1 = J+1 ;
226    tic
227    C0 = A (I1,J1) ;
228    t0 = toc ;
229    tic
230    C1 = GB_mex_Matrix_subref (A, I, J) ;
231    t1 = toc ;
232    assert (isequal (C0, C1)) ;
233    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
234end
235
236
237fprintf ('-------------------------- three contig rows:\n') ;
238for i = 0:1000:n-3
239    I = uint64([i i+1 i+2]) ;
240    J = uint64 (0:n-1) ;
241    I1 = I+1 ;
242    J1 = J+1 ;
243    tic
244    C0 = A (I1,J1) ;
245    t0 = toc ;
246    tic
247    C1 = GB_mex_Matrix_subref (A, I, J) ;
248    t1 = toc ;
249    assert (isequal (C0, C1)) ;
250    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
251end
252
253fprintf ('-------------------------- four contig rows:\n') ;
254for i = 0:1000:n-4
255    I = uint64([i i+1 i+2 i+3]) ;
256    J = uint64 (0:n-1) ;
257    I1 = I+1 ;
258    J1 = J+1 ;
259    tic
260    C0 = A (I1,J1) ;
261    t0 = toc ;
262    tic
263    C1 = GB_mex_Matrix_subref (A, I, J) ;
264    t1 = toc ;
265    assert (isequal (C0, C1)) ;
266    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
267end
268
269
270fprintf ('-------------------------- one column :\n') ;
271for j = 0:1000:n-4
272    % I = uint64(0:n-1) ;
273    J = uint64 (j) ;
274    % I1 = I+1 ;
275    J1 = J+1 ;
276    tic
277    C0 = A (:,J1) ;
278    t0 = toc ;
279    tic
280    C1 = GB_mex_Matrix_subref (A, [ ], J) ;
281    t1 = toc ;
282    assert (isequal (C0, C1)) ;
283    fprintf ('MATLAB: %0.6f  speedup %6.2f\n', t0, t0/t1) ;
284end
285
286
287fprintf ('-------------------------- A (:,2:n):\n') ;
288I = [ ] ;
289J = uint64 (1:n-1) ;
290I1 = I + 1 ;
291J1 = J + 1 ;
292fprintf ('MATLAB:\n') ;
293    tic
294    C0 = A (:,J1) ;
295    toc
296fprintf ('GB:\n') ;
297    tic
298    C1 = GB_mex_Matrix_subref (A, [ ], J) ;
299    toc
300    assert (isequal (C0, C1)) ;
301
302fprintf ('-------------------------- case 6: ni large, no qsort, dupl :\n') ;
303I = uint64 (floor (n/2 : 0.5 : n-1)) ;
304J = uint64 (1:n-1) ;
305I1 = I + 1 ;
306J1 = J + 1 ;
307fprintf ('MATLAB:\n') ;
308    tic
309    C0 = A (I1,J1) ;
310    toc
311fprintf ('GB:\n') ;
312    tic
313    C1 = GB_mex_Matrix_subref (A, I, J) ;
314    toc
315    assert (isequal (C0, C1)) ;
316
317fprintf ('-------------------------- case 7: ni large, no qsort, no dupl :\n') ;
318I = uint64 ([ floor(n/2) floor((2+n/2):n-1) ]) ;
319J = uint64 (1:n-1) ;
320I1 = I + 1 ;
321J1 = J + 1 ;
322fprintf ('MATLAB:\n') ;
323    tic
324    C0 = A (I1,J1) ;
325    toc
326fprintf ('GB:\n') ;
327    tic
328    C1 = GB_mex_Matrix_subref (A, I, J) ;
329    toc
330    assert (isequal (C0, C1)) ;
331
332fprintf ('-------------------------- case 3: contig :\n') ;
333I = uint64 ([ floor(n/2:n-1) ]) ;
334J = uint64 (1:n-1) ;
335I1 = I + 1 ;
336J1 = J + 1 ;
337fprintf ('MATLAB:\n') ;
338    tic
339    C0 = A (I1,J1) ;
340    toc
341fprintf ('GB:\n') ;
342    tic
343    C1 = GB_mex_Matrix_subref (A, I, J) ;
344    toc
345    assert (isequal (C0, C1)) ;
346
347fprintf ('\ntest43: all tests passed\n') ;
348
349