File: test111.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 (222 lines) | stat: -rw-r--r-- 6,947 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
function test111
%TEST111 performance test for eWiseAdd

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

fprintf ('\ntest111 performance tests : eWiseAdd \n') ;
rng ('default') ;

[save save_chunk] = nthreads_get ;
chunk = 4096 ;

n = 40e6 ;
% n = 1e6 ;
Empty = sparse (n, 1) ;

% for d = [0.001 0.01 0.1:.1:1]
for d = [0.001 0.01 0.1 0.4 1 2 3]

    if (d == 1)
        A = sparse (rand (n,1)) ;
        B = sparse (rand (n,1)) ;
        mds = 1 ;
    elseif (d == 2)
        A = sparse (rand (n,1)) ;
        B = sprand (n,1,0.1) ;
        mds = 0.1 ;
    elseif (d == 3)
        A = sprand (n,1,0.1) ;
        B = sparse (rand (n,1)) ;
        mds = 0.1 ;
    else
        A = sprand (n,1,d) ;
        B = sprand (n,1,d) ;
        mds = [d/36 d/8 d] ;
    end

    for md = mds

        if (md == 1)
            M = sparse (rand (n,1)) ;
        else
            M = sprand (n,1,md) ;
        end

        M = spones (M) ;
        M0 = logical (M) ;

        fprintf ('\n######################################################\n') ;
        fprintf ('d = %12.4f md = %12.4f\n', d, md) ;
        fprintf ('######################################################\n') ;
        fprintf ('nnz (A) = %d nnz (B) = %d nnz (M) = %d\n', ...
            nnz (A), nnz (B), nnz (M)) ;

        %----------------------------------------------------------------------
        % add
        %----------------------------------------------------------------------

        % warmup
        C1 = A + B ;
        tic
        C1 = A + B ;
        toc
        tm = toc ;
        fprintf ('nnz (C) = %d\n', nnz (C1));
        fprintf ('\nvia GB_add:\n') ;

        for nthreads = [1 2 4 8 20 40]
            nthreads_set (nthreads, chunk) ;
            tic ;
            C4 = GB_mex_AplusB (A, B, 'plus') ;
            tg = toc ;
            if (nthreads == 1)
                t1 = tg ;
            end
            fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ;
            fprintf ('speedup %12.4f over built-in: %12.4f\n', t1/tg, tm/tg) ;
            assert (GB_spok (C4) == 1) ;
            assert (isequal (C1, C4)) ;
        end

        %----------------------------------------------------------------------
        % masked add
        %----------------------------------------------------------------------

        fprintf ('\nmasked add:\n') ;
        % warmup
        C1 = M.*(A + B) ;
        tic
        C1 = M.*(A + B) ;
        toc
        tm = toc ;
        fprintf ('nnz (C) = %d\n', nnz (C1));
        fprintf ('\nvia masked GB_add:\n') ;

        for nthreads = [1 2 4 8 20 40]
            nthreads_set (nthreads, chunk) ;
            % warmup
            C4 = GB_mex_Vector_eWiseAdd (Empty, M0, [ ], 'plus', A, B, [ ]) ;
            tic
            C4 = GB_mex_Vector_eWiseAdd (Empty, M0, [ ], 'plus', A, B, [ ]) ;
            tg = toc ;
            if (nthreads == 1)
                t1 = tg ;
            end
            fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ;
            fprintf ('speedup %12.4f over built-in: %12.4f\n', t1/tg, tm/tg) ;
            assert (GB_spok (C4.matrix) == 1) ;
            assert (isequal (C1, C4.matrix)) ;
        end

        %-----------------------------------------------------------------------
        % unmasked add then ewise mult
        %-----------------------------------------------------------------------

        fprintf ('\nunmasked add then emult:\n') ;
        % warmup
        C1 = M.*(A + B) ;
        tic
        C1 = M.*(A + B) ;
        toc
        tm = toc ;
        fprintf ('nnz (C) = %d\n', nnz (C1));
        fprintf ('\nvia unmasked add then emult:\n') ;

        for nthreads = [1 2 4 8 20 40]
            nthreads_set (nthreads, chunk) ;
            % warmup
            C4 = GB_mex_Vector_eWiseAdd (Empty, [ ], [ ], 'plus', A, B, [ ]) ;
            C4 = GB_mex_Vector_eWiseMult(Empty, [ ], [ ], 'times',M, C4, [ ]) ;
            tic
            C4 = GB_mex_Vector_eWiseAdd (Empty, [ ], [ ], 'plus', A, B, [ ]) ;
            tg1 = toc ;
            tic
            C4 = GB_mex_Vector_eWiseMult(Empty, [ ], [ ], 'times',M, C4, [ ]) ;
            tg2 = toc ;
            tg = tg1 + tg2 ;
            if (nthreads == 1)
                t1 = tg ;
            end
            fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ;
            fprintf ('speedup %12.4f over built-in: %12.4f\n', t1/tg, tm/tg) ;
            assert (GB_spok (C4.matrix) == 1) ;
            assert (isequal (C1, C4.matrix)) ;
        end

        %-----------------------------------------------------------------------
        % ewise multiply
        %-----------------------------------------------------------------------

        % warmup
        C1 = A .* B ;
        tic
        C1 = A .* B ;
        toc
        tm = toc ;
        fprintf ('nnz (C) = %d for A.*B\n', nnz (C1));
        fprintf ('\nvia GB_eWiseMult:\n') ;

        for nthreads = [1 2 4 8 20 40]
            nthreads_set (nthreads, chunk) ;
            % warmup
            C4 = GB_mex_Vector_eWiseMult (Empty, [ ], [ ], 'times', A,B, [ ]) ;
            tic
            C4 = GB_mex_Vector_eWiseMult (Empty, [ ], [ ], 'times', A,B, [ ]) ;
            tg = toc ;
            if (nthreads == 1)
                t1 = tg ;
            end
            fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ;
            fprintf ('speedup %12.4f over built-in: %12.4f\n', t1/tg, tm/tg) ;
            assert (GB_spok (C4.matrix) == 1) ;
            assert (isequal (C1, C4.matrix)) ;
        end

        %-----------------------------------------------------------------------
        % masked ewise multiply
        %-----------------------------------------------------------------------

        fprintf ('\nmasked emult:\n') ;
        % warmup
        C1 = M.* (A .* B) ;
        tic
        C1 = M.* (A .* B) ;
        toc
        tm = toc ;
        fprintf ('nnz (C) = %d for A.*B\n', nnz (C1));
        fprintf ('\nvia GB_eWiseMult:\n') ;

        for nthreads = [1 2 4 8 20 40]
            nthreads_set (nthreads, chunk) ;
            % warmup
            C4 = GB_mex_Vector_eWiseMult (Empty, M0, [ ], 'times', A,B, [ ]) ;
            tic
            C4 = GB_mex_Vector_eWiseMult (Empty, M0, [ ], 'times', A,B, [ ]) ;
            tg = toc ;
            if (nthreads == 1)
                t1 = tg ;
            end
            fprintf ('nthreads %2d GraphBLAS time: %12.4f ', nthreads, tg) ;
            fprintf ('speedup %12.4f over built-in: %12.4f\n', t1/tg, tm/tg) ;
            assert (GB_spok (C4.matrix) == 1) ;
            assert (isequal (C1, C4.matrix)) ;
        end

    end
end

fprintf ('\ndense matrices:\n') ;

A = full (A) ;
B = full (B) ;

for trial = 1:4
    tic
    C1 = A + B ;
    toc
    tm = toc ;
end
nthreads_set (save, save_chunk) ;

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