File: test40.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 (161 lines) | stat: -rw-r--r-- 3,755 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
function test40
%TEST40 test GrB_Matrix_extractElement

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

fprintf ('\n ------ quick test of GrB_Matrix_extractElement\n') ;

Prob = ssget (936) ;
A = Prob.A ;
[m n] = size (A) ;

for i = double (1:500:m)
    for j = double (1:500:n)
        x1 = full (A (i,j)) ;
        use_scalar = (rand (1) > 0.5) ;
        x2 = GB_mex_Matrix_extractElement (A, uint64 (i-1), uint64 (j-1), '', use_scalar) ;
        assert (isequal (x1,x2)) ;
    end
end

i2 = double (1:10:m) ;
j2 = double (1:10:n) ;
ni = length (i2) * length (j2) ;
I = zeros (ni, 1) ;
J = zeros (ni, 1) ;
X = zeros (ni, 1) ;

k = 0 ;
for i = i2
    for j = j2
        k = k + 1 ;
        I (k) = i ;
        J (k) = j ;
    end
end

% loop 1
tic
for i = i2
    for j = j2
        x1 = full (A (i,j)) ;
    end
end
t1 = toc ;
fprintf ('built-in, scalar A(i,j): %g sec\n', t1) ;

% loop 2 is about the same speed
tic
for k = 1:ni
    X (k) = full (A (I (k), J (k))) ;
end
t2 = toc ;
fprintf ('built-in, scalar A(i,j): %g sec (2nd loop)\n', t2) ;
t0 = min (t1, t2) ;

I = uint64 (I-1) ;
J = uint64 (J-1) ;
X1 = zeros (ni, 1) ;

tic
for k = 1:ni
    use_scalar = (rand (1) > 0.5) ;
    X1 (k) = GB_mex_Matrix_extractElement (A, I (k), J (k), '', use_scalar) ;
end
t3 = toc ;
fprintf ('GrB     single A(i,j): %g sec speedup: %g\n', t3, t0/ t3) ;

% This is about 15x faster than built-in loop 1 and 2.
% the loop is internal in the mexFunction, so this code
% avoids the interpretive overhead.  It also avoids the
% malloc need to construct the GraphBLAS header for
% the shallow copy of A, and the malloc for the scalar result,
% for each iteration. Instead, those mallocs are done once.
tic
X2 = GB_mex_Matrix_extractElement (A, I, J) ;
t4 = toc ;
fprintf ('GrB     many   A(i,j): %g sec speedup: %g\n', t4, t0/ t4) ;

assert (isequal (X, X2)) ;
assert (isequal (X, X1)) ;

X2 = GB_mex_Matrix_extractElement (A, I, J, '', true) ;
assert (isequal (X, X2)) ;


tic
[I,J,X] = find (A) ;
t7 = toc ;
fprintf ('built-in, [I,J,X] = find(A) %g sec\n', t7) ;

tic
[I2, J2, X2] = GB_mex_extractTuples (A) ;
t8 = toc ;
fprintf ('GrB     [I,J,X] = find(A) %g sec, speedup %g\n', t8, t7/t8) ;
assert (isequal (I, double (I2+1)))
assert (isequal (J, double (J2+1)))
assert (isequal (X, X2))

ni = length (I) ;

% built-in loop 3
tic
for k = 1:ni
    X (k) = full (A (I (k), J (k))) ;
end
t5 = toc ;
fprintf ('built-in, scalar A(i,j): %g sec (all entries)\n', t5) ;

tic
X2 = GB_mex_Matrix_extractElement (A, I2, J2) ;
t6 = toc ;
fprintf ('GrB     many   A(i,j): %g sec (all entries) speedup: %g\n', t6, t5/ t6) ;
assert (isequal (X, X2)) ;

X2 = GB_mex_Matrix_extractElement (A, I2, J2, '', true) ;
assert (isequal (X, X2)) ;

%-------------------------------------------------------------------------------
% vector

V = A (:) ;

mn = length (V) ;
for i = double (1:500:mn)
    x1 = full (V (i)) ;
    x2 = GB_mex_Vector_extractElement (V, uint64 (i-1)) ;
    assert (isequal (x1,x2)) ;
end

tic
[I,J,X] = find (V) ;
t9 = toc ;
fprintf ('built-in, [I,J,X] = find(V) %g sec\n', t9) ;
ni = length (I) ;

tic
[I2, J2, X2] = GB_mex_extractTuples (V) ;
t8 = toc ;
fprintf ('GrB     [I,J,X] = find(V) %g sec, speedup %g\n', t8, t7/t8) ;
assert (isequal (I, double (I2+1)))
assert (isequal (J, double (J2+1)))
assert (isequal (X, X2))

% built-in vector loop
tic
for k = 1:ni
    X (k) = full (V (I (k))) ;
end
t10 = toc ;
fprintf ('built-in, scalar V(i): %g sec (all entries)\n', t10) ;

tic
X2 = GB_mex_Vector_extractElement (V, I2) ;
t6 = toc ;
fprintf ('GrB     many   A(i,j): %g sec (all entries) speedup: %g\n', t6, t5/ t6) ;

assert (isequal (X, X2)) ;

fprintf ('\ntest40: all tests passed\n') ;