File: GB_add_sparse_M_sparse.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 (349 lines) | stat: -rw-r--r-- 12,932 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//------------------------------------------------------------------------------
// GB_add_sparse_M_sparse: C(:,j)<M>=A(:,j)+B(:,j), C and M sparse/hyper
//------------------------------------------------------------------------------

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

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

// C and M are both sparse or hyper.

{

    //--------------------------------------------------------------------------
    // setup for C(:,j)<M> = A(:,j) + B(:,j), where C and M are sparse/hyper
    //--------------------------------------------------------------------------

    // get M(:,j) where M is sparse or hypersparse
    int64_t pM = -1 ;
    int64_t pM_end = -1 ;

    if (fine_task)
    { 
        // A fine task operates on Mi,Mx [pM...pM_end-1],
        // which is a subset of the vector M(:,j)
        pM     = TaskList [taskid].pM ;
        pM_end = TaskList [taskid].pM_end ;
    }
    else
    {
        int64_t kM = -1 ;
        if (Ch_is_Mh)
        { 
            // Ch is the same as Mh (a deep copy)
            ASSERT (Ch != NULL) ;
            ASSERT (M_is_hyper) ;
            #ifdef GB_DEBUG
            GB_Mh_DECLARE (Mh, const) ; GB_Mh_PTR (Mh, M) ;
            ASSERT (GB_IGET (Ch, k) == GB_IGET (Mh, k)) ;
            #endif
            kM = k ;
        }
        else
        { 
            kM = (C_to_M == NULL) ? j : C_to_M [k] ;
        }
        if (kM >= 0)
        { 
            pM     = GB_IGET (Mp, kM  ) ;
            pM_end = GB_IGET (Mp, kM+1) ;
        }
    }

    // The "easy mask" condition requires M to be sparse/hyper and structural.
    // A and B cannot be bitmap, and one of these 3 conditions must hold:
    // (1) all entries are present in A(:,j) and M == B
    // (2) all entries are present in B(:,j) and M == A
    // (3) both A and B are aliased to M
    // This test is done on a vector-by-vector basis.  See GB_add_sparsity.c
    // for a global test.

    if (Mask_struct &&          // M must be structural
        !A_is_bitmap &&         // A must not be bitmap
        !B_is_bitmap &&         // B must not be bitmap
        ((adense && M_is_B) ||  // one of 3 conditions holds
         (bdense && M_is_A) ||
         (M_is_A && M_is_B)))
    {

        //----------------------------------------------------------------------
        // special case: M is present and very easy to use
        //----------------------------------------------------------------------

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

        // A and B are sparse, hypersparse or full, not bitmap.

        int64_t mjnz = pM_end - pM ;        // nnz (M (:,j))

        #if ( GB_ADD_PHASE == 1 )

        // M is structural, and sparse or hypersparse, so every entry in the
        // mask is guaranteed to appear in A+B.  The symbolic count is thus
        // trivial.

        cjnz = mjnz ;

        #else

        // copy the pattern into C (:,j)
        int64_t pC_start = pC ;
        int64_t pM_start = pM ;
        #if defined ( GB_DEBUG ) || !defined ( GB_ISO_ADD )
        int64_t pA_offset = pA_start - iA_first ;
        int64_t pB_offset = pB_start - iB_first ;
        #endif

        if (adense && M_is_B)
        { 

            //------------------------------------------------------------------
            // Method11: A dense, M == B
            //------------------------------------------------------------------

            GB_PRAGMA_SIMD_VECTORIZE
            for (int64_t p = 0 ; p < mjnz ; p++)
            {
                int64_t pM = p + pM_start ;
                int64_t pC = p + pC_start ;
                int64_t i = GB_IGET (Mi, pM) ;
                GB_ISET (Ci, pC, i) ;           // Ci [pC] = i
                ASSERT (GB_MCAST (Mx, pM, msize)) ;
                ASSERT (GBi_A (Ai, pA_offset + i, vlen) == i) ;
                ASSERT (GBi_B (Bi, pM, vlen) == i) ;
                #ifndef GB_ISO_ADD
                GB_LOAD_A (aij, Ax, pA_offset + i, A_iso) ;
                GB_LOAD_B (bij, Bx, pM, B_iso) ;
                GB_EWISEOP (Cx, pC, aij, bij, i, j) ;
                #endif
            }

        }
        else if (bdense && M_is_A)
        { 

            //------------------------------------------------------------------
            // Method12: B dense, M == A
            //------------------------------------------------------------------

            GB_PRAGMA_SIMD_VECTORIZE
            for (int64_t p = 0 ; p < mjnz ; p++)
            {
                int64_t pM = p + pM_start ;
                int64_t pC = p + pC_start ;
                int64_t i = GB_IGET (Mi, pM) ;
                GB_ISET (Ci, pC, i) ;           // Ci [pC] = i
                ASSERT (GB_MCAST (Mx, pM, msize)) ;
                ASSERT (GBi_A (Ai, pM, vlen) == i) ;
                ASSERT (GBi_B (Bi, pB_offset + i, vlen) == i) ;
                #ifndef GB_ISO_ADD
                GB_LOAD_A (aij, Ax, pM, A_iso) ;
                GB_LOAD_B (bij, Bx, pB_offset + i, B_iso) ;
                GB_EWISEOP (Cx, pC, aij, bij, i, j) ;
                #endif
            }

        }
        else // (M == A) && (M == B)
        { 

            //------------------------------------------------------------------
            // Method13: M == A == B: all three matrices the same
            //------------------------------------------------------------------

            GB_PRAGMA_SIMD_VECTORIZE
            for (int64_t p = 0 ; p < mjnz ; p++)
            {
                int64_t pM = p + pM_start ;
                int64_t pC = p + pC_start ;
                int64_t i = GB_IGET (Mi, pM) ;
                GB_ISET (Ci, pC, i) ;           // Ci [pC] = i
                #ifndef GB_ISO_ADD
                #if GB_OP_IS_SECOND
                GB_LOAD_B (t, Bx, pM, B_iso) ;
                #else
                GB_LOAD_A (t, Ax, pM, A_iso) ;
                #endif
                GB_EWISEOP (Cx, pC, t, t, i, j) ;
                #endif
            }

        }
        #endif

    }
    else
    {

        //----------------------------------------------------------------------
        // Method14: C and M are sparse or hypersparse
        //----------------------------------------------------------------------

        //      ------------------------------------------
        //      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    (+)

        // (*) This method is efficient except when either A or B are sparse,
        // and when M is sparse but with many entries.  When M is sparse and
        // either A or B are sparse, the method is designed to be very
        // efficient when M is very sparse compared with A and/or B.  It
        // traverses all entries in the sparse M, and (for sparse A or B) does
        // a binary search for entries in A or B.  In that case, if M has many
        // entries, the mask M should be ignored, and C=A+B should be computed
        // without any mask.  The test for when to use M here should ignore A
        // or B if they are bitmap or full.

        // If A or B are aliased to M, but the rest of "easy mask" condition is
        // not triggered, then GB_add_sparsity will decide to apply the mask
        // later, not in this phase.  As a result, if M is present, it is not
        // aliased to A or B.

        // (+) TODO: if C and M are sparse/hyper, and A and B are both
        // bitmap/full, then use GB_emult_04_template instead, but with (Ab [p]
        // || Bb [p]) instead of (Ab [p] && Bb [p]).

        // A and B can have any sparsity pattern (hypersparse, sparse, bitmap,
        // or full).

        for ( ; pM < pM_end ; pM++)
        {

            //------------------------------------------------------------------
            // get M(i,j) for A(i,j) + B (i,j)
            //------------------------------------------------------------------

            int64_t i = GB_IGET (Mi, pM) ;
            bool mij = GB_MCAST (Mx, pM, msize) ;
            if (!mij) continue ;

            //------------------------------------------------------------------
            // get A(i,j)
            //------------------------------------------------------------------

            bool afound ;
            if (adense)
            { 
                // A is dense, bitmap, or full; use quick lookup
                pA = pA_start + (i - iA_first) ;
                afound = GBb_A (Ab, pA) ;
            }
            else
            { 
                // A is sparse; use binary search.  This is slow unless
                // M is very sparse compared with A.
                int64_t apright = pA_end - 1 ;
                afound = GB_binary_search (i, Ai, GB_Ai_IS_32, &pA, &apright) ;
            }

            ASSERT (GB_IMPLIES (afound, GBi_A (Ai, pA, vlen) == i)) ;

            //------------------------------------------------------------------
            // get B(i,j)
            //------------------------------------------------------------------

            bool bfound ;
            if (bdense)
            { 
                // B is dense; use quick lookup
                pB = pB_start + (i - iB_first) ;
                bfound = GBb_B (Bb, pB) ;
            }
            else
            { 
                // B is sparse; use binary search.  This is slow unless
                // M is very sparse compared with B.
                int64_t bpright = pB_end - 1 ;
                bfound = GB_binary_search (i, Bi, GB_Bi_IS_32, &pB, &bpright) ;
            }

            ASSERT (GB_IMPLIES (bfound, GBi_B (Bi, pB, vlen) == i)) ;

            //------------------------------------------------------------------
            // C(i,j) = A(i,j) + B(i,j)
            //------------------------------------------------------------------

            if (afound && bfound)
            { 
                // C (i,j) = A (i,j) + B (i,j)
                #if ( GB_ADD_PHASE == 1 )
                cjnz++ ;
                #else
                GB_ISET (Ci, pC, i) ;       // Ci [pC] = i ;
                #ifndef GB_ISO_ADD
                GB_LOAD_A (aij, Ax, pA, A_iso) ;
                GB_LOAD_B (bij, Bx, pB, B_iso) ;
                GB_EWISEOP (Cx, pC, aij, bij, i, j) ;
                #endif
                pC++ ;
                #endif
            }
            else if (afound)
            { 
                #if ( GB_ADD_PHASE == 1 )
                cjnz++ ;
                #else
                GB_ISET (Ci, pC, i) ;       // Ci [pC] = i ;
                #ifndef GB_ISO_ADD
                #if GB_IS_EWISEUNION
                { 
                    // C (i,j) = A(i,j) + beta
                    GB_LOAD_A (aij, Ax, pA, A_iso) ;
                    GB_EWISEOP (Cx, pC, aij, beta_scalar, i, j) ;
                }
                #else
                { 
                    // C (i,j) = A (i,j)
                    GB_COPY_A_to_C (Cx, pC, Ax, pA, A_iso) ;
                }
                #endif
                #endif
                pC++ ;
                #endif
            }
            else if (bfound)
            { 
                #if ( GB_ADD_PHASE == 1 )
                cjnz++ ;
                #else
                GB_ISET (Ci, pC, i) ;       // Ci [pC] = i ;
                #ifndef GB_ISO_ADD
                #if GB_IS_EWISEUNION
                { 
                    // C (i,j) = alpha + B(i,j)
                    GB_LOAD_B (bij, Bx, pB, B_iso) ;
                    GB_EWISEOP (Cx, pC, alpha_scalar, bij, i, j) ;
                }
                #else
                { 
                    // C (i,j) = B (i,j)
                    GB_COPY_B_to_C (Cx, pC, Bx, pB, B_iso) ;
                }
                #endif
                #endif
                pC++ ;
                #endif
            }
        }

        #if ( GB_ADD_PHASE == 2 )
        ASSERT (pC == pC_end) ;
        #endif
    }
}