File: GB_meta16_definitions.h

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 503; asm: 369; python: 125; awk: 10
file content (306 lines) | stat: -rw-r--r-- 12,512 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
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
//------------------------------------------------------------------------------
// GB_meta16_definitions.h: methods that depend on the sparsity of A and B
//------------------------------------------------------------------------------

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

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

// Define macros that depend on the sparsity of A and B for GB_meta16_factory.
// These macros are only used by saxpy3 methods.

//------------------------------------------------------------------------------
// GB_GET_B_j: prepare to iterate over B(:,j)
//------------------------------------------------------------------------------

#undef GB_GET_B_j

#if defined ( GB_META16 )

    #if ( GB_B_IS_HYPER || GB_A_IS_HYPER )

        // A or B are hyper
        #define GB_GET_B_j \
        GB_GET_B_j_FOR_ALL_FORMATS (GB_A_IS_HYPER,GB_B_IS_SPARSE,GB_B_IS_HYPER)

    #else

        #if ( GB_B_IS_SPARSE )

            // B is sparse
            #define GB_GET_B_j                              \
                const int64_t j = kk ;                      \
                int64_t pB = Bp [kk] ;                      \
                const int64_t pB_end = Bp [kk+1] ;          \
                const int64_t bjnz = pB_end - pB ;          \
                GB_GET_T_FOR_SECONDJ

        #else

            // B is bitmap or full
            #define GB_GET_B_j                              \
                const int64_t j = kk ;                      \
                int64_t pB = kk * bvlen ;                   \
                const int64_t pB_end = pB + bvlen ;         \
                const int64_t bjnz = bvlen ;                \
                GB_GET_T_FOR_SECONDJ

        #endif

    #endif

#else

    // define GB_GET_B_j for all sparsity formats
    #define GB_GET_B_j \
        GB_GET_B_j_FOR_ALL_FORMATS (A_is_hyper, B_is_sparse, B_is_hyper)

#endif

//------------------------------------------------------------------------------
// GB_GET_B_kj_INDEX: get the index k of the entry B(k,j)
//------------------------------------------------------------------------------

#undef GB_GET_B_kj_INDEX

#if defined ( GB_META16 )

    #if ( GB_B_IS_HYPER || GB_B_IS_SPARSE )

        // B is hyper or sparse
        #define GB_GET_B_kj_INDEX               \
            const int64_t k = Bi [pB]

    #elif ( GB_B_IS_BITMAP )

        // B is bitmap
        #define GB_GET_B_kj_INDEX               \
            if (!Bb [pB]) continue ;            \
            const int64_t k = pB % bvlen

    #else

        // B is full
        #define GB_GET_B_kj_INDEX               \
            const int64_t k = pB % bvlen

    #endif

#else

    // for any format of B
    #define GB_GET_B_kj_INDEX                   \
        if (!GBB (Bb, pB)) continue ;           \
        const int64_t k = GBI (Bi, pB, bvlen)

#endif

//------------------------------------------------------------------------------
// GB_GET_A_k: prepare to iterate over the vector A(:,k)
//------------------------------------------------------------------------------

#undef GB_GET_A_k

#if defined ( GB_META16 )

    #if ( GB_A_IS_HYPER )

        // A is hyper
        #define GB_GET_A_k GB_GET_A_k_FOR_ALL_FORMATS (true)

    #elif ( GB_A_IS_SPARSE )

        // A is sparse
        #define GB_GET_A_k                              \
            const int64_t pA_start = Ap [k] ;           \
            const int64_t pA_end = Ap [k+1] ;           \
            const int64_t aknz = pA_end - pA_start

    #else

        // A is bitmap or full
        #define GB_GET_A_k                              \
            const int64_t pA_start = k * avlen ;        \
            const int64_t pA_end = pA_start + avlen ;   \
            const int64_t aknz = avlen

    #endif

#else

    // define GB_GET_A_k for all sparsity formats
    #define GB_GET_A_k GB_GET_A_k_FOR_ALL_FORMATS (A_is_hyper)

#endif

//------------------------------------------------------------------------------
// GB_GET_A_ik_INDEX: get the index i of the entry A(i,k)
//------------------------------------------------------------------------------

#undef GB_GET_A_ik_INDEX

#if defined ( GB_META16 )

    #if ( GB_A_IS_HYPER || GB_A_IS_SPARSE )

        // A is hyper or sparse
        #define GB_GET_A_ik_INDEX               \
            const int64_t i = Ai [pA]

    #elif ( GB_A_IS_BITMAP )

        // A is bitmap
        #define GB_GET_A_ik_INDEX               \
            if (!Ab [pA]) continue ;            \
            const int64_t i = pA % avlen

    #else

        // A is full
        #define GB_GET_A_ik_INDEX               \
            const int64_t i = pA % avlen

    #endif

#else

    // for any format of A
    #define GB_GET_A_ik_INDEX                   \
        if (!GBB (Ab, pA)) continue ;           \
        const int64_t i = GBI (Ai, pA, avlen)

#endif

//------------------------------------------------------------------------------
// GB_COMPUTE_C_j_WHEN_NNZ_B_j_IS_ONE: compute C(:,j) when nnz(B(:,j)) == 1
//------------------------------------------------------------------------------

// C(:,j) = A(:,k)*B(k,j) when there is a single entry in B(:,j)
// The mask must not be present.  A must be sparse or hypersparse.

#undef GB_COMPUTE_C_j_WHEN_NNZ_B_j_IS_ONE

#if GB_IS_ANY_PAIR_SEMIRING

    // ANY_PAIR: result is purely symbolic; no numeric work to do
    #define GB_COMPUTE_C_j_WHEN_NNZ_B_j_IS_ONE                      \
        ASSERT (A_is_sparse || A_is_hyper) ;                        \
        GB_GET_B_kj_INDEX ;         /* get index k of B(k,j) */     \
        GB_GET_A_k ;                /* get A(:,k) */                \
        memcpy (Ci + pC, Ai + pA_start, aknz * sizeof (int64_t)) ;  \
        /* C becomes jumbled if A is jumbled */                     \
        task_C_jumbled = task_C_jumbled || A_jumbled ;

#else

    // typical semiring
    #define GB_COMPUTE_C_j_WHEN_NNZ_B_j_IS_ONE                      \
        ASSERT (A_is_sparse || A_is_hyper) ;                        \
        GB_GET_B_kj_INDEX ;         /* get index k of B(k,j) */     \
        GB_GET_A_k ;                /* get A(:,k) */                \
        GB_GET_B_kj ;               /* bkj = B(k,j) */              \
        /* scan A(:,k) */                                           \
        for (int64_t pA = pA_start ; pA < pA_end ; pA++)            \
        {                                                           \
            GB_GET_A_ik_INDEX ;     /* get index i of A(i,k) */     \
            GB_MULT_A_ik_B_kj ;         /* t = A(i,k)*B(k,j) */     \
            GB_CIJ_WRITE (pC, t) ;      /* Cx [pC] = t */           \
            Ci [pC++] = i ;                                         \
        }                                                           \
        /* C becomes jumbled if A is jumbled */                     \
        task_C_jumbled = task_C_jumbled || A_jumbled ;

#endif

//------------------------------------------------------------------------------
// GB_COMPUTE_DENSE_C_j: compute C(:,j)=A*B(:,j) when C(:,j) is completely dense
//------------------------------------------------------------------------------

// This method is not used for the saxpy3 generic method.

#undef GB_COMPUTE_DENSE_C_j

#if GB_IS_ANY_PAIR_SEMIRING

    // ANY_PAIR: result is purely symbolic; no numeric work to do
    #define GB_COMPUTE_DENSE_C_j                                    \
        for (int64_t i = 0 ; i < cvlen ; i++)                       \
        {                                                           \
            Ci [pC + i] = i ;                                       \
        }

#else

    // typical semiring
    #define GB_COMPUTE_DENSE_C_j                                    \
        for (int64_t i = 0 ; i < cvlen ; i++)                       \
        {                                                           \
            Ci [pC + i] = i ;                                       \
            GB_CIJ_WRITE (pC + i, GB_IDENTITY) ; /* C(i,j)=0 */     \
        }                                                           \
        for ( ; pB < pB_end ; pB++)     /* scan B(:,j) */           \
        {                                                           \
            GB_GET_B_kj_INDEX ;         /* get index k of B(k,j) */ \
            GB_GET_A_k ;                /* get A(:,k) */            \
            if (aknz == 0) continue ;   /* skip if A(:,k) empty */  \
            GB_GET_B_kj ;               /* bkj = B(k,j) */          \
            /* scan A(:,k) */                                       \
            for (int64_t pA = pA_start ; pA < pA_end ; pA++)        \
            {                                                       \
                GB_GET_A_ik_INDEX ;     /* get index i of A(i,k) */ \
                GB_MULT_A_ik_B_kj ;     /* t = A(i,k)*B(k,j) */     \
                GB_CIJ_UPDATE (pC + i, t) ; /* Cx [pC+i]+=t */      \
            }                                                       \
        }

#endif

//------------------------------------------------------------------------------
// GB_SCAN_M_j_OR_A_k: compute C(:,j) using linear scan or binary search
//------------------------------------------------------------------------------

// C(:,j)<M(:,j)>=A(:,k)*B(k,j) using one of two methods
#undef  GB_SCAN_M_j_OR_A_k

#define GB_SCAN_M_j_OR_A_k(A_ok_for_binary_search)                          \
{                                                                           \
    if (A_ok_for_binary_search && aknz > 256 && mjnz_much < aknz &&         \
        mjnz < mvlen && aknz < avlen)                                       \
    {                                                                       \
        /* M and A are both sparse, and nnz(M(:,j)) is much less than */    \
        /* nnz(A(:,k)); scan M(:,j), and do binary search for A(i,k).*/     \
        /* This requires that A is not jumbled. */                          \
        int64_t pA = pA_start ;                                             \
        for (int64_t pM = pM_start ; pM < pM_end ; pM++)                    \
        {                                                                   \
            GB_GET_M_ij (pM) ;      /* get M(i,j) */                        \
            if (!mij) continue ;    /* skip if M(i,j)=0 */                  \
            int64_t i = Mi [pM] ;                                           \
            bool found ;            /* search for A(i,k) */                 \
            if (M_jumbled) pA = pA_start ;                                  \
            int64_t apright = pA_end - 1 ;                                  \
            GB_BINARY_SEARCH (i, Ai, pA, apright, found) ;                  \
            if (found)                                                      \
            {                                                               \
                /* C(i,j)<M(i,j)> += A(i,k) * B(k,j) for this method. */    \
                /* M(i,j) is always 1, as given in the hash table */        \
                GB_IKJ ;                                                    \
            }                                                               \
        }                                                                   \
    }                                                                       \
    else                                                                    \
    {                                                                       \
        /* A(:,j) is sparse enough relative to M(:,j) */                    \
        /* M and/or A can dense, and either can be jumbled. */              \
        /* scan A(:,k), and lookup M(i,j) (in the hash table) */            \
        for (int64_t pA = pA_start ; pA < pA_end ; pA++)                    \
        {                                                                   \
            GB_GET_A_ik_INDEX ;     /* get index i of A(i,j) */             \
            /* do C(i,j)<M(i,j)> += A(i,k) * B(k,j) for this method */      \
            /* M(i,j) may be 0 or 1, as given in the hash table */          \
            GB_IKJ ;                                                        \
        }                                                                   \
    }                                                                       \
}