1function [flops mwork] = flopcount (M,Mask_complement,A,B) ;
2%FLOPCOUNT cumulative sum of flop counts for A*B, C<M>=A*B, C<!M>=A*B
3%
4% flops = flopcount (M,Mask_complementA,B) ;
5%
6% flops(j) is the flops to compute A*B(1:j-1), and flops(n+1) is the total
7% flopcount, if B is m-by-n.
8%
9% Each 'flop' counted is actually a multiply-add.  M can be [ ]. The
10% flopcount m-file returns the same thing as GB_AxB_saxpy3_flopcount.  Also
11% included in flops(j) is the work needed to access the mask M(:,j).
12
13% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
14% SPDX-License-Identifier: Apache-2.0
15
16n = size (B,2) ;
17flops = zeros (1,n) ;
18mwork = 0 ;
19
20if (isempty (M))
21
22    % C = A*B
23
24    for j = 1:n
25        brows = find (B (:,j)) ;
26        brows = brows (:)' ;
27        for k = brows
28            flops (j) = flops (j) + nnz (A (:,k)) ;
29        end
30    end
31
32else
33
34    % C<M>=A*B and C<!M>=A*B
35
36    mask_is_M = (~Mask_complement) ;    % true for C<M>=A*B
37    mask_is_dense = (nnz (M) == prod (size (M))) ;
38
39    for j = 1:n
40        brows = find (B (:,j)) ;
41        if (isempty (brows))
42            continue ;
43        end
44
45        if (~mask_is_dense)
46            mrows = find (M (:,j)) ;
47            mjnz = length (mrows) ;
48            if (mask_is_M & mjnz == 0)
49                continue ;
50            end
51            flops (j) = flops (j) + mjnz ;
52            mwork = mwork + mjnz ;
53        end
54
55        brows = brows (:)' ;
56        for k = brows
57            flops (j) = flops (j) + nnz (A (:,k)) ;
58        end
59    end
60
61end
62
63flops = cumsum ([0 flops]) ;
64