File: GB_add_sparsity.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 254,920 kB
  • sloc: ansic: 1,134,743; cpp: 46,133; makefile: 4,875; fortran: 2,087; java: 1,826; sh: 996; ruby: 725; python: 495; asm: 371; sed: 166; awk: 44
file content (262 lines) | stat: -rw-r--r-- 11,225 bytes parent folder | download | duplicates (2)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//------------------------------------------------------------------------------
// GB_add_sparsity: determine the sparsity structure for C<M or !M>=A+B
//------------------------------------------------------------------------------

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

//------------------------------------------------------------------------------

// Determines the sparsity structure for C, for computing C=A+B, C<M>=A+B,
// or C<!M>=A+B, based on the sparsity structures of M, A, and B, and whether
// or not M is complemented.  It also decides if the mask M should be applied
// by GB_add, or if C=A+B should be computed without the mask, and the mask
// applied later.

// If C should be hypersparse or sparse, on output, this function simply
// returns GxB_SPARSE.  The final determination is made by GB_add_phase0.

#include "add/GB_add.h"
#include "mask/GB_mask_very_sparse.h"

int GB_add_sparsity         // return the sparsity structure for C
(
    // output:
    bool *apply_mask,       // if true then mask will be applied by GB_add
    // input:
    const GrB_Matrix M,     // optional mask for C, unused if NULL
    const bool Mask_struct, // if true, use only the structure of M
    const bool Mask_comp,   // if true, use !M
    const GrB_Matrix A,     // input A matrix
    const GrB_Matrix B      // input B matrix
)
{

    //--------------------------------------------------------------------------
    // determine the sparsity of C
    //--------------------------------------------------------------------------

    // Unless deciding otherwise, use the mask if it appears
    (*apply_mask) = (M != NULL) ;

    int C_sparsity ;

    // In the table below, sparse/hypersparse are listed as "sparse".  If C is
    // listed as sparse: it is hypersparse if M is hypersparse (and not
    // complemented), or if both A and B are hypersparse, and sparse otherwise.
    // This is determined by GB_add_phase0.  If M is complemented and all 4
    // matrices are sparse, then C=A+B is always computed.  So C is hypersparse
    // if both A and B are hypersparse, in this case.

    bool M_is_sparse_or_hyper = GB_IS_SPARSE (M) || GB_IS_HYPERSPARSE (M) ;
    bool A_is_sparse_or_hyper = GB_IS_SPARSE (A) || GB_IS_HYPERSPARSE (A) ;
    bool B_is_sparse_or_hyper = GB_IS_SPARSE (B) || GB_IS_HYPERSPARSE (B) ;
    bool A_is_full = GB_IS_FULL (A) ;
    bool B_is_full = GB_IS_FULL (B) ;

    if (M == NULL)
    {

        //      ------------------------------------------
        //      C       =           A       +       B
        //      ------------------------------------------
        //      sparse  .           sparse          sparse
        //      bitmap  .           sparse          bitmap
        //      full    .           sparse          full  
        //      bitmap  .           bitmap          sparse
        //      bitmap  .           bitmap          bitmap
        //      full    .           bitmap          full  
        //      full    .           full            sparse
        //      full    .           full            bitmap
        //      full    .           full            full  

        if (A_is_sparse_or_hyper && B_is_sparse_or_hyper)
        { 
            C_sparsity = GxB_SPARSE ;
        }
        else if (A_is_full || B_is_full)
        { 
            C_sparsity = GxB_FULL ;
        }
        else
        { 
            C_sparsity = GxB_BITMAP ;
        }

    }
    else if (!Mask_comp)
    {

        if (M_is_sparse_or_hyper)
        { 

            //      ------------------------------------------
            //      C      <M> =        A       +       B
            //      ------------------------------------------
            //      sparse  sparse      sparse          sparse
            //      sparse  sparse      sparse          bitmap
            //      sparse  sparse      sparse          full  
            //      sparse  sparse      bitmap          sparse
            //      sparse  sparse      bitmap          bitmap
            //      sparse  sparse      bitmap          full  
            //      sparse  sparse      full            sparse
            //      sparse  sparse      full            bitmap
            //      sparse  sparse      full            full  

            // if A and B are both bitmap or full, then always use the mask.

            // if M and A and/or B are all sparse, use the mask only if
            // M is very easy to use, or if:
            // 8*nnz(M) <= ( (A sparse or hyper) ? nnz(A) : 0 ) +
            //             ( (B sparse or hyper) ? nnz(B) : 0 )

            if (A_is_sparse_or_hyper || B_is_sparse_or_hyper)
            { 
                // see ewise/template/GB_add_sparse_M_sparse.c for a
                // vector-by-vector test of the "easy mask" condition.  This
                // test is global for all vectors of the matrices:
                bool M_is_A = GB_all_aliased (M, A) ;
                bool M_is_B = GB_all_aliased (M, B) ;
                bool all_easy_mask = Mask_struct &&
                    ((A_is_full && M_is_B) ||
                     (B_is_full && M_is_A) ||
                     (M_is_A && M_is_B)) ;

                // GB_add_sparse_template handles this case, but exploiting the
                // mask can be asympotically slow, when C and M are sparse, and
                // A and/or B are sparse, and M has many entries.  So in that
                // case, apply the mask later.
                (*apply_mask) = all_easy_mask ||
                    GB_MASK_VERY_SPARSE (8, M,
                        A_is_sparse_or_hyper ? A : NULL,
                        B_is_sparse_or_hyper ? B : NULL) ;
            }

            if (!(*apply_mask))
            {
                // redo the analysis as if there is no mask
                if (A_is_sparse_or_hyper && B_is_sparse_or_hyper)
                { 
                    C_sparsity = GxB_SPARSE ;
                }
                else if (A_is_full || B_is_full)
                { 
                    C_sparsity = GxB_FULL ;
                }
                else
                { 
                    C_sparsity = GxB_BITMAP ;
                }
            }
            else
            {
                // the mask is applied and C is sparse
                C_sparsity = GxB_SPARSE ;
            }

        }
        else
        {

            //      ------------------------------------------
            //      C      <M> =        A       +       B
            //      ------------------------------------------
            //      sparse  bitmap      sparse          sparse
            //      bitmap  bitmap      sparse          bitmap
            //      bitmap  bitmap      sparse          full  
            //      bitmap  bitmap      bitmap          sparse
            //      bitmap  bitmap      bitmap          bitmap
            //      bitmap  bitmap      bitmap          full  
            //      bitmap  bitmap      full            sparse
            //      bitmap  bitmap      full            bitmap
            //      bitmap  bitmap      full            full  

            //      ------------------------------------------
            //      C      <M> =        A       +       B
            //      ------------------------------------------
            //      sparse  full        sparse          sparse
            //      bitmap  full        sparse          bitmap
            //      bitmap  full        sparse          full  
            //      bitmap  full        bitmap          sparse
            //      bitmap  full        bitmap          bitmap
            //      bitmap  full        bitmap          full  
            //      bitmap  full        full            sparse
            //      bitmap  full        full            bitmap
            //      bitmap  full        full            full  

            // The mask is very efficient to use in the case, when C is sparse.

            if (A_is_sparse_or_hyper && B_is_sparse_or_hyper)
            { 
                C_sparsity = GxB_SPARSE ;
            }
            else
            { 
                C_sparsity = GxB_BITMAP ;
            }
        }

    }
    else // Mask_comp
    {

        //      ------------------------------------------
        //      C     <!M> =        A       +       B
        //      ------------------------------------------
        //      sparse  sparse      sparse          sparse      (mask later)
        //      bitmap  sparse      sparse          bitmap
        //      bitmap  sparse      sparse          full  
        //      bitmap  sparse      bitmap          sparse
        //      bitmap  sparse      bitmap          bitmap
        //      bitmap  sparse      bitmap          full  
        //      bitmap  sparse      full            sparse
        //      bitmap  sparse      full            bitmap
        //      bitmap  sparse      full            full  

        //      ------------------------------------------
        //      C     <!M> =        A       +       B
        //      ------------------------------------------
        //      sparse  bitmap      sparse          sparse
        //      bitmap  bitmap      sparse          bitmap
        //      bitmap  bitmap      sparse          full  
        //      bitmap  bitmap      bitmap          sparse
        //      bitmap  bitmap      bitmap          bitmap
        //      bitmap  bitmap      bitmap          full  
        //      bitmap  bitmap      full            sparse
        //      bitmap  bitmap      full            bitmap
        //      bitmap  bitmap      full            full  

        //      ------------------------------------------
        //      C     <!M> =        A       +       B
        //      ------------------------------------------
        //      sparse  full        sparse          sparse
        //      bitmap  full        sparse          bitmap
        //      bitmap  full        sparse          full  
        //      bitmap  full        bitmap          sparse
        //      bitmap  full        bitmap          bitmap
        //      bitmap  full        bitmap          full  
        //      bitmap  full        full            sparse
        //      bitmap  full        full            bitmap
        //      bitmap  full        full            full  

        if (A_is_sparse_or_hyper && B_is_sparse_or_hyper)
        { 
            // !M must be applied later if all 4 matrices are sparse or
            // hypersparse, since the GB_add_sparse_template method does not
            // handle this case.  See the "(mask later)" above.  The method can
            // construct a sparse/hyper C with !M as bitmap or full. 
            (*apply_mask) = !M_is_sparse_or_hyper ;
            C_sparsity = GxB_SPARSE ;
        }
        else
        { 
            // !M can be applied now, or later.  TODO: If M is sparse and
            // either A or B are sparse/hyper, then there might be cases where
            // !M should be applied later, for better performance.
            C_sparsity = GxB_BITMAP ;
        }
    }

    return (C_sparsity) ;
}