File: GB_cumsum_template.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (281 lines) | stat: -rw-r--r-- 9,305 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
//------------------------------------------------------------------------------
// GB_cumsum_template: cumlative sum of an array (uint32_t or uint64_t)
//------------------------------------------------------------------------------

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

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

// Compute the cumulative sum of an array count[0:n], of size n+1,
// and of type uint32_t or uint64_t:

//      k = sum (count [0:n-1] != 0) ;
//      count = cumsum ([0 count[0:n-1]]) ;

// That is, count [j] on input is overwritten with sum (count [0..j-1]).
// On input, count [n] is not accessed and is implicitly zero on input.
// On output, count [n] is the total sum.

// If the type is uint32_t, integer overflow is checked.  If it occurs,
// the count array is not modified and the method returns false.

// Testing how GraphBLAS handles integer overflow would require a very large
// test problem (a matrix with over 4 billion entries).  To keep the test suite
// modest in size, an artificial integer overflow can be triggered, but only
// when GraphBLAS is compiled with test coverage, inside MATLAB
// (GraphBLAS/Tcov).

{
    #ifndef GB_NO_KRESULT
    if (kresult != NULL)
    {

        //----------------------------------------------------------------------
        // cumsum, and compute k, for uint32_t or uint64_t cases only
        //----------------------------------------------------------------------

        if (nthreads <= 2)
        {

            //------------------------------------------------------------------
            // cumsum with one thread, also compute k
            //------------------------------------------------------------------

            uint64_t s = 0 ;

            #if GB_CHECK_OVERFLOW
            { 
                for (int64_t i = 0 ; i < n ; i++)
                {
                    s += count [i] ;
                    if (s > UINT32_MAX)
                    { 
                        return (false) ;
                    }
                }
                #ifdef GBCOVER
                // pretend to fail, for test coverage only
                if (GB_Global_hack_get (5)) return (false) ;
                #endif
                s = 0 ;
            }
            #endif

            uint64_t k = 0 ;
            for (int64_t i = 0 ; i < n ; i++)
            { 
                uint64_t c = count [i] ;
                if (c != 0) k++ ;
                count [i] = s ;
                s += c ;
            }
            count [n] = s ;
            (*kresult) = k ;

        }
        else
        {

            //------------------------------------------------------------------
            // cumsum with multiple threads, also compute k
            //------------------------------------------------------------------

            // allocate workspace
            GB_WERK_DECLARE (ws, uint64_t) ;
            GB_WERK_DECLARE (wk, uint64_t) ;
            GB_WERK_PUSH (ws, nthreads, uint64_t) ;
            GB_WERK_PUSH (wk, nthreads, uint64_t) ;
            if (ws == NULL || wk == NULL)
            { 
                // out of memory; use a single thread instead
                GB_WERK_POP (wk, uint64_t) ;
                GB_WERK_POP (ws, uint64_t) ;
                return (GB_cumsum (count, count_is_32, n, kresult, 1, NULL)) ;
            }

            int tid ;
            #pragma omp parallel for num_threads(nthreads) schedule(static)
            for (tid = 0 ; tid < nthreads ; tid++)
            {
                // each task sums up its own part
                int64_t istart, iend ;
                GB_PARTITION (istart, iend, n, tid, nthreads) ;
                uint64_t k = 0 ;
                uint64_t s = 0 ;
                for (int64_t i = istart ; i < iend ; i++)
                { 
                    uint64_t c = count [i] ;
                    if (c != 0) k++ ;
                    s += c ;
                }
                ws [tid] = s ;
                wk [tid] = k ;
            }

            #if GB_CHECK_OVERFLOW
            { 
                // for uint32_t case only
                uint64_t total = 0 ;
                for (tid = 0 ; tid < nthreads ; tid++)
                { 
                    total += ws [tid] ;
                }
                if (total > UINT32_MAX)
                { 
                    GB_WERK_POP (wk, uint64_t) ;
                    GB_WERK_POP (ws, uint64_t) ;
                    return (false) ;
                }
            }
            #ifdef GBCOVER
            if (GB_Global_hack_get (5))
            {
                // pretend to fail, for test coverage only
                GB_WERK_POP (wk, uint64_t) ;
                GB_WERK_POP (ws, uint64_t) ;
                return (false) ;
            }
            #endif
            #endif

            #pragma omp parallel for num_threads(nthreads) schedule(static)
            for (tid = 0 ; tid < nthreads ; tid++)
            {
                // each task computes the cumsum of its own part
                int64_t istart, iend ;
                GB_PARTITION (istart, iend, n, tid, nthreads) ;
                uint64_t s = 0 ;
                for (int i = 0 ; i < tid ; i++)
                { 
                    s += ws [i] ;
                }
                for (int64_t i = istart ; i < iend ; i++)
                { 
                    uint64_t c = count [i] ;
                    count [i] = s ;
                    s += c ;
                }
                if (iend == n)
                { 
                    count [n] = s ;
                }
            }

            uint64_t k = 0 ;
            for (int tid = 0 ; tid < nthreads ; tid++)
            { 
                k += wk [tid] ;
            }
            (*kresult) = (int64_t) k ;

            // free workspace
            GB_WERK_POP (wk, uint64_t) ;
            GB_WERK_POP (ws, uint64_t) ;
        }
    }
    else
    #endif
    {

        //----------------------------------------------------------------------
        // cumsum without k, for all types (uint32_t, uint64_t, and float)
        //----------------------------------------------------------------------

        if (nthreads <= 2)
        {

            //------------------------------------------------------------------
            // cumsum with one thread
            //------------------------------------------------------------------

            return (GB_CUMSUM1_TYPE (count, n)) ;

        }
        else
        {

            //------------------------------------------------------------------
            // cumsum with multiple threads
            //------------------------------------------------------------------

            // allocate workspace
            GB_WERK_DECLARE (ws, GB_WS_TYPE) ;
            GB_WERK_PUSH (ws, nthreads, GB_WS_TYPE) ;
            if (ws == NULL)
            { 
                // out of memory; use a single thread instead
                return (GB_CUMSUM1_TYPE (count, n)) ;
            }

            int tid ;
            #pragma omp parallel for num_threads(nthreads) schedule(static)
            for (tid = 0 ; tid < nthreads ; tid++)
            {
                // each task sums up its own part
                int64_t istart, iend ;
                GB_PARTITION (istart, iend, n, tid, nthreads) ;
                GB_WS_TYPE s = 0 ;
                for (int64_t i = istart ; i < iend ; i++)
                { 
                    s += count [i] ;
                }
                ws [tid] = s ;
            }

            #if GB_CHECK_OVERFLOW
            { 
                // for uint32_t case only
                uint64_t total = 0 ;
                for (tid = 0 ; tid < nthreads ; tid++)
                { 
                    total += ws [tid] ;
                }
                if (total > UINT32_MAX)
                { 
                    GB_WERK_POP (ws, GB_WS_TYPE) ;
                    return (false) ;
                }
            }
            #ifdef GBCOVER
            if (GB_Global_hack_get (5))
            {
                // pretend to fail, for test coverage only
                GB_WERK_POP (ws, GB_WS_TYPE) ;
                return (false) ;
            }
            #endif
            #endif

            #pragma omp parallel for num_threads(nthreads) schedule(static)
            for (tid = 0 ; tid < nthreads ; tid++)
            {
                // each tasks computes the cumsum of its own part
                int64_t istart, iend ;
                GB_PARTITION (istart, iend, n, tid, nthreads) ;
                GB_WS_TYPE s = 0 ;
                for (int i = 0 ; i < tid ; i++)
                { 
                    s += ws [i] ;
                }
                for (int64_t i = istart ; i < iend ; i++)
                { 
                    GB_WS_TYPE c = count [i] ;
                    count [i] = s ;
                    s += c ;
                }
                if (iend == n)
                { 
                    count [n] = s ;
                }
            }

            // free workspace
            GB_WERK_POP (ws, GB_WS_TYPE) ;
        }
    }
}

#undef GB_CHECK_OVERFLOW
#undef GB_CUMSUM1_TYPE