1function test40 2%TEST40 test GrB_Matrix_extractElement 3 4% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved. 5% SPDX-License-Identifier: Apache-2.0 6 7fprintf ('\n ------ quick test of GrB_Matrix_extractElement\n') ; 8 9Prob = ssget (936) ; 10A = Prob.A ; 11[m n] = size (A) ; 12 13for i = double (1:500:m) 14 for j = double (1:500:n) 15 x1 = full (A (i,j)) ; 16 x2 = GB_mex_Matrix_extractElement (A, uint64 (i-1), uint64 (j-1)) ; 17 assert (isequal (x1,x2)) ; 18 end 19end 20 21i2 = double (1:10:m) ; 22j2 = double (1:10:n) ; 23ni = length (i2) * length (j2) ; 24I = zeros (ni, 1) ; 25J = zeros (ni, 1) ; 26X = zeros (ni, 1) ; 27 28k = 0 ; 29for i = i2 30 for j = j2 31 k = k + 1 ; 32 I (k) = i ; 33 J (k) = j ; 34 end 35end 36 37% MATLAB loop 1 38tic 39for i = i2 40 for j = j2 41 x1 = full (A (i,j)) ; 42 end 43end 44t1 = toc ; 45fprintf ('MATLAB, scalar A(i,j): %g sec\n', t1) ; 46 47% MATLAB loop 2 is about the same speed 48tic 49for k = 1:ni 50 X (k) = full (A (I (k), J (k))) ; 51end 52t2 = toc ; 53fprintf ('MATLAB, scalar A(i,j): %g sec (2nd loop)\n', t2) ; 54t0 = min (t1, t2) ; 55 56I = uint64 (I-1) ; 57J = uint64 (J-1) ; 58X1 = zeros (ni, 1) ; 59 60% this is about 3x slower than MATLAB loop 1 and 2 61tic 62for k = 1:ni 63 X1 (k) = GB_mex_Matrix_extractElement (A, I (k), J (k)) ; 64end 65t3 = toc ; 66fprintf ('GrB single A(i,j): %g sec speedup: %g\n', t3, t0/ t3) ; 67 68% This is about 15x faster than MATLAB loop 1 and 2. 69% the loop is internal in the mexFunction, so this code 70% avoids the interpretive overhead. It also avoids the 71% malloc need to construct the GraphBLAS header for 72% the shallow copy of A, and the malloc for the scalar result, 73% for each iteration. Instead, those mallocs are done once. 74tic 75X2 = GB_mex_Matrix_extractElement (A, I, J) ; 76t4 = toc ; 77fprintf ('GrB many A(i,j): %g sec speedup: %g\n', t4, t0/ t4) ; 78 79assert (isequal (X, X2)) ; 80assert (isequal (X, X1)) ; 81 82 83tic 84[I,J,X] = find (A) ; 85t7 = toc ; 86fprintf ('MATLAB, [I,J,X] = find(A) %g sec\n', t7) ; 87 88tic 89[I2, J2, X2] = GB_mex_extractTuples (A) ; 90t8 = toc ; 91fprintf ('GrB [I,J,X] = find(A) %g sec, speedup %g\n', t8, t7/t8) ; 92assert (isequal (I, double (I2+1))) 93assert (isequal (J, double (J2+1))) 94assert (isequal (X, X2)) 95 96ni = length (I) ; 97 98% MATLAB loop 3 99tic 100for k = 1:ni 101 X (k) = full (A (I (k), J (k))) ; 102end 103t5 = toc ; 104fprintf ('MATLAB, scalar A(i,j): %g sec (all entries)\n', t5) ; 105 106tic 107X2 = GB_mex_Matrix_extractElement (A, I2, J2) ; 108t6 = toc ; 109fprintf ('GrB many A(i,j): %g sec (all entries) speedup: %g\n', t6, t5/ t6) ; 110 111assert (isequal (X, X2)) ; 112 113%------------------------------------------------------------------------------- 114% vector 115 116V = A (:) ; 117 118mn = length (V) ; 119for i = double (1:500:mn) 120 x1 = full (V (i)) ; 121 x2 = GB_mex_Vector_extractElement (V, uint64 (i-1)) ; 122 assert (isequal (x1,x2)) ; 123end 124 125tic 126[I,J,X] = find (V) ; 127t9 = toc ; 128fprintf ('MATLAB, [I,J,X] = find(V) %g sec\n', t9) ; 129ni = length (I) ; 130 131tic 132[I2, J2, X2] = GB_mex_extractTuples (V) ; 133t8 = toc ; 134fprintf ('GrB [I,J,X] = find(V) %g sec, speedup %g\n', t8, t7/t8) ; 135assert (isequal (I, double (I2+1))) 136assert (isequal (J, double (J2+1))) 137assert (isequal (X, X2)) 138 139% MATLAB vector loop 140tic 141for k = 1:ni 142 X (k) = full (V (I (k))) ; 143end 144t10 = toc ; 145fprintf ('MATLAB, scalar V(i): %g sec (all entries)\n', t10) ; 146 147tic 148X2 = GB_mex_Vector_extractElement (V, I2) ; 149t6 = toc ; 150fprintf ('GrB many A(i,j): %g sec (all entries) speedup: %g\n', t6, t5/ t6) ; 151 152assert (isequal (X, X2)) ; 153 154fprintf ('\ntest40: all tests passed\n') ; 155 156