File: GB_spec_reduce_to_vector.m

package info (click to toggle)
suitesparse 1%3A5.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 152,716 kB
  • sloc: ansic: 774,385; cpp: 24,213; makefile: 6,310; fortran: 1,927; java: 1,826; csh: 1,686; ruby: 725; sh: 535; perl: 225; python: 209; sed: 164; awk: 60
file content (93 lines) | stat: -rw-r--r-- 2,836 bytes parent folder | download
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
function w = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, descriptor)
%GB_SPEC_REDUCE_TO_VECTOR a MATLAB mimic of GrB_reduce (to vector)
%
% Usage:
% w = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, desc)
%
% Reduces a matrix to a vector

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights Reserved.
% http://suitesparse.com   See GraphBLAS/Doc/License.txt for license.

%-------------------------------------------------------------------------------
% get inputs
%-------------------------------------------------------------------------------

if (nargout > 1 || nargin ~= 6)
    error ...
    ('usage: c = GB_spec_reduce_to_vector (w, mask, accum, reduce, A, desc)') ;
end

% get the class of A
if (isstruct (A))
    aclass = A.class ;
else
    aclass = GB_spec_type (A) ;
end

% get the reduce operator. default type is the type of A
if (isempty (reduce))
    reduce = 'plus'
end
[reduce_op reduce_class] = GB_spec_operator (reduce, aclass) ;

% get the identity
identity = GB_spec_identity (reduce_op, reduce_class) ;
if (isempty (identity))
    identity = 0 ;
end

% get the input matrix
A = GB_spec_matrix (A, identity) ;

% get the input vector
w = GB_spec_matrix (w, identity) ;

% get the mask
[C_replace Mask_comp Atrans Btrans Mask_struct] = ...
    GB_spec_descriptor (descriptor) ;
mask = GB_spec_getmask (mask, Mask_struct) ;

%-------------------------------------------------------------------------------
% do the work via a clean MATLAB interpretation of the entire GraphBLAS spec
%-------------------------------------------------------------------------------

% apply the descriptor to A
if (Atrans)
    A.matrix = A.matrix.' ;
    A.pattern = A.pattern' ;
end

tclass = reduce_class ;
[m n] = size (A.matrix) ;
T.matrix = GB_spec_zeros ([m 1], tclass) ;
T.pattern = zeros (m, 1, 'logical') ;
T.matrix (:,:) = identity ;
T.class = tclass ;

% cast A to the type of the reduce operator, but only entries in the pattern

% Note that GraphBLAS does not need the identity value to do this step.
% Nor is the identity value specified in the GraphBLAS spec.
% It is needed here since T is a dense column, not sparse, and the
% implicit values need to be inserted properly.

for i = 1:m
    t = identity ;
    for j = 1:n
        if (A.pattern (i,j))
            aij = GB_mex_cast (A.matrix (i,j), reduce_class) ;
            t = GB_spec_op (reduce, t, aij) ;
            T.pattern (i) = true ;
        end
    end
    T.matrix (i) = t ;
end

%-------------------------------------------------------------------------------
% apply the mask and accum
%-------------------------------------------------------------------------------

% w<mask> = accum (C,T): apply the accum, then mask, and return the result
w = GB_spec_accum_mask (w, mask, accum, T, C_replace, Mask_comp, identity) ;