File: GB_bitmap_AxB_saxpy_A_bitmap_B_bitmap_template.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (289 lines) | stat: -rw-r--r-- 11,204 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
//------------------------------------------------------------------------------
// GB_bitmap_AxB_saxpy_A_bitmap_B_bitmap: C<#M>+=A*B, C bitmap, M any format
//------------------------------------------------------------------------------

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

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

// C is bitmap, A and B are bitmap/full.  M has any format.

{

    int64_t tid ;
    #pragma omp parallel for num_threads(nthreads) schedule(dynamic,1) \
        reduction(+:cnvals)
    for (tid = 0 ; tid < ntasks ; tid++)
    {
        
        //----------------------------------------------------------------------
        // get the task to compute C (I,J)
        //----------------------------------------------------------------------

        int64_t I_tid = tid / nI_tasks ;
        int64_t J_tid = tid % nI_tasks ;

        // I = istart:iend-1
        int64_t istart = I_tid * GB_TILE_SIZE ;
        int64_t iend   = GB_IMIN (avlen, istart + GB_TILE_SIZE) ;

        // J = jstart:jend-1
        int64_t jstart = J_tid * GB_TILE_SIZE ;
        int64_t jend   = GB_IMIN (bvdim, jstart + GB_TILE_SIZE) ;

        int64_t task_cnvals = 0 ;

        //----------------------------------------------------------------------
        // check if any entry in the M(I,J) mask permits any change to C(I,J)
        //----------------------------------------------------------------------

        #if GB_MASK_IS_SPARSE_OR_HYPER || GB_MASK_IS_BITMAP_OR_FULL

            bool any_update_allowed = false ;

            for (int64_t j = jstart ; j < jend && !any_update_allowed ; j++)
            {
                for (int64_t i = istart ; i < iend && !any_update_allowed ; i++)
                { 

                    //----------------------------------------------------------
                    // get pointer to C(i,j) and M(i,j)
                    //----------------------------------------------------------

                    int64_t pC = j * avlen + i ;

                    //----------------------------------------------------------
                    // check M(i,j)
                    //----------------------------------------------------------

                    #if GB_MASK_IS_SPARSE_OR_HYPER

                        // M is sparse or hypersparse
                        int8_t cb = Cb [pC] ;
                        bool mij = (cb & 2) ;

                    #elif GB_MASK_IS_BITMAP_OR_FULL

                        // M is bitmap or full
                        GB_GET_M_ij (pC) ;

                    #endif

                    if (Mask_comp) mij = !mij ;
                    if (!mij) continue ;
                    any_update_allowed = true ;
                }
            }

            if (!any_update_allowed)
            { 
                // C(I,J) cannot be modified at all; skip it
                continue ;
            }

        #endif

        //----------------------------------------------------------------------
        // declare local storage for this task
        //----------------------------------------------------------------------

//      GB_ATYPE Ax_cache [GB_TILE_SIZE * GB_KTILE_SIZE] ;
//      int8_t Ab_cache [GB_TILE_SIZE * GB_KTILE_SIZE] ;
        bool Ab_any_in_row [GB_TILE_SIZE] ;

        //----------------------------------------------------------------------
        // C<#M>(I,J) += A(I,:) * B(:,J)
        //----------------------------------------------------------------------

        for (int64_t kstart = 0 ; kstart < avdim ; kstart += GB_KTILE_SIZE)
        {
            // K = kstart:kend-1
            int64_t kend = GB_IMIN (avdim, kstart + GB_KTILE_SIZE) ;

            //------------------------------------------------------------------
            // TODO: load A(I,K) into local storage
            //------------------------------------------------------------------

            // For built-in semirings, load A(I,K) into local storage of size
            // GB_TILE_SIZE * GB_KTILE_SIZE and transpose it.  Load in the
            // bitmap Ab if not NULL, and Ax if not NULL.

#if 0
            for (int64_t k = kstart ; k < kend ; k++)
            {
                for (int64_t i = istart ; i < iend ; i++)
                {
                    int64_t pA = i + k * avlen ;
                    int8_t ab = GBB (Ab, pA) ;
                    i_local = i - istart ;
                    k_local = k - kstart ;
                    Ab_cache [i_local * GB_KTILE_SIZE ...
                }
            }
#endif

            //------------------------------------------------------------------
            // Check for entries in each row of A(I,K)
            //------------------------------------------------------------------

            if (A_is_bitmap)
            {
                for (int i = 0 ; i < GB_TILE_SIZE ; i++)
                { 
                    Ab_any_in_row [i] = false ;
                }
                for (int64_t k = kstart ; k < kend ; k++)
                {
                    for (int64_t i = istart ; i < iend ; i++)
                    { 
                        int64_t pA = i + k * avlen ;    // get pointer to A(i,k)
                        int8_t  ab = Ab [pA] ;
                        // Ab_cache [(i-istart) * GB_KTILE_SIZE + (k-kstart)]
                        //      = ab ;
                        Ab_any_in_row [i-istart] |= ab ;
                    }
                }
            }

            //------------------------------------------------------------------
            // C<#M>(I,J) += A(I,K) * B(K,J)
            //------------------------------------------------------------------

            for (int64_t j = jstart ; j < jend ; j++)
            {

                //--------------------------------------------------------------
                // B is bitmap or full: check if any B(K,j) entry exists
                //--------------------------------------------------------------

                if (B_is_bitmap)
                {
                    int b = 0 ;
                    for (int64_t k = kstart ; k < kend ; k++)
                    { 
                        int64_t pB = k + j * bvlen ;    // pointer to B(k,j)
                        b += Bb [pB] ;
                    }
                    if (b == 0)
                    { 
                        // no entry exists in B(K,j)
                        continue ;
                    }
                }

                //--------------------------------------------------------------
                // C<#M>(I,j) += A(I,K) * B(K,j)
                //--------------------------------------------------------------

                GB_GET_T_FOR_SECONDJ ;

                for (int64_t i = istart ; i < iend ; i++)
                {

                    //----------------------------------------------------------
                    // skip if A(i,K) has no entries
                    //----------------------------------------------------------

                    if (A_is_bitmap && !Ab_any_in_row [i - istart])
                    { 
                        continue ;
                    }

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

                    int64_t pC = i + j * avlen ;

                    //----------------------------------------------------------
                    // check M(i,j)
                    //----------------------------------------------------------

                    #if GB_MASK_IS_SPARSE_OR_HYPER

                        // M is sparse or hypersparse
                        int8_t cb = Cb [pC] ;
                        bool mij = (cb & 2) ;
                        if (Mask_comp) mij = !mij ;
                        if (!mij) continue ;
                        cb = (cb & 1) ;

                    #elif GB_MASK_IS_BITMAP_OR_FULL

                        // M is bitmap or full
                        GB_GET_M_ij (pC) ;
                        if (Mask_comp) mij = !mij ;
                        if (!mij) continue ;
                        int8_t cb = Cb [pC] ;

                    #else

                        // no mask
                        int8_t cb = Cb [pC] ;

                    #endif

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

                    if (cb == 0)
                    {

                        //------------------------------------------------------
                        // C(i,j) does not yet exist
                        //------------------------------------------------------

                        for (int64_t k = kstart ; k < kend ; k++)
                        {
                            int64_t pA = i + k * avlen ;    // pointer to A(i,k)
                            int64_t pB = k + j * bvlen ;    // pointer to B(k,j)
                            if (!GBB (Ab, pA)) continue ;
                            if (!GBB (Bb, pB)) continue ;
                            GB_GET_B_kj ;                   // get B(k,j)
                            GB_MULT_A_ik_B_kj ;             // t = A(i,k)*B(k,j)
                            if (cb == 0)
                            { 
                                // C(i,j) = A(i,k) * B(k,j)
                                GB_CIJ_WRITE (pC, t) ;
                                Cb [pC] = keep ;
                                cb = keep ;
                                task_cnvals++ ;
                            }
                            else
                            { 
                                // C(i,j) += A(i,k) * B(k,j)
                                GB_CIJ_UPDATE (pC, t) ;
                            }
                        }

                    }
                    else
                    {

                        //------------------------------------------------------
                        // C(i,j) already exists
                        //------------------------------------------------------

                        #if !GB_IS_ANY_PAIR_SEMIRING
                        for (int64_t k = kstart ; k < kend ; k++)
                        { 
                            int64_t pA = i + k * avlen ;    // pointer to A(i,k)
                            int64_t pB = k + j * bvlen ;    // pointer to B(k,j)
                            if (!GBB (Ab, pA)) continue ;
                            if (!GBB (Bb, pB)) continue ;
                            GB_GET_B_kj ;                   // get B(k,j)
                            GB_MULT_A_ik_B_kj ;             // t = A(i,k)*B(k,j)
                            // C(i,j) += A(i,k) * B(k,j)
                            GB_CIJ_UPDATE (pC, t) ;
                        }
                        #endif
                    }
                }
            }
        }
        cnvals += task_cnvals ;
    }
}