File: GB_mx_mxv_iterator_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 (406 lines) | stat: -rw-r--r-- 17,302 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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//------------------------------------------------------------------------------
// GB_mx_mxv_iterator: Y = A*X using an iterator
//------------------------------------------------------------------------------

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

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

// Template for #include'ing into GB_mex_mxv_iterator.c

//------------------------------------------------------------------------------
// MULTADD: Y (i) += A(i,j) * X(j)
//------------------------------------------------------------------------------

#define MULTADD                                                         \
{                                                                       \
    if (type == GrB_BOOL)                                               \
    {                                                                   \
        bool aij = GxB_Iterator_get_BOOL (iterator) ;                   \
        ((bool *) Y->x) [i] |= aij && ((bool *) X->x) [j] ;             \
    }                                                                   \
    else if (type == GrB_INT8)                                          \
    {                                                                   \
        int8_t aij = GxB_Iterator_get_INT8 (iterator) ;                 \
        ((int8_t *) Y->x) [i] += aij * ((int8_t *) X->x) [j] ;          \
    }                                                                   \
    else if (type == GrB_INT16)                                         \
    {                                                                   \
        int16_t aij = GxB_Iterator_get_INT16 (iterator) ;               \
        ((int16_t *) Y->x) [i] += aij * ((int16_t *) X->x) [j] ;        \
    }                                                                   \
    else if (type == GrB_INT32)                                         \
    {                                                                   \
        int32_t aij = GxB_Iterator_get_INT32 (iterator) ;               \
        ((int32_t *) Y->x) [i] += aij * ((int32_t *) X->x) [j] ;        \
    }                                                                   \
    else if (type == GrB_INT64)                                         \
    {                                                                   \
        int64_t aij = GxB_Iterator_get_INT64 (iterator) ;               \
        ((int64_t *) Y->x) [i] += aij * ((int64_t *) X->x) [j] ;        \
    }                                                                   \
    else if (type == GrB_UINT8)                                         \
    {                                                                   \
        uint8_t aij = GxB_Iterator_get_UINT8 (iterator) ;               \
        ((uint8_t *) Y->x) [i] += aij * ((uint8_t *) X->x) [j] ;        \
    }                                                                   \
    else if (type == GrB_UINT16)                                        \
    {                                                                   \
        uint16_t aij = GxB_Iterator_get_UINT16 (iterator) ;             \
        ((uint16_t *) Y->x) [i] += aij * ((uint16_t *) X->x) [j] ;      \
    }                                                                   \
    else if (type == GrB_UINT32)                                        \
    {                                                                   \
        uint32_t aij = GxB_Iterator_get_UINT32 (iterator) ;             \
        ((uint32_t *) Y->x) [i] += aij * ((uint32_t *) X->x) [j] ;      \
    }                                                                   \
    else if (type == GrB_UINT64)                                        \
    {                                                                   \
        uint64_t aij = GxB_Iterator_get_UINT64 (iterator) ;             \
        ((uint64_t *) Y->x) [i] += aij * ((uint64_t *) X->x) [j] ;      \
    }                                                                   \
    else if (type == GrB_FP32)                                          \
    {                                                                   \
        float aij = GxB_Iterator_get_FP32 (iterator) ;                  \
        ((float *) Y->x) [i] += aij * ((float *) X->x) [j] ;            \
    }                                                                   \
    else if (type == GrB_FP64)                                          \
    {                                                                   \
        double aij = GxB_Iterator_get_FP64 (iterator) ;                 \
        ((double *) Y->x) [i] += aij * ((double *) X->x) [j] ;          \
    }                                                                   \
    else if (type == GxB_FC32)                                          \
    {                                                                   \
        GxB_FC32_t aij = GxB_Iterator_get_FC32 (iterator) ;             \
        ((GxB_FC32_t *) Y->x) [i] += aij * ((GxB_FC32_t *) X->x) [j] ;  \
    }                                                                   \
    else if (type == GxB_FC64)                                          \
    {                                                                   \
        GxB_FC64_t aij = GxB_Iterator_get_FC64 (iterator) ;             \
        ((GxB_FC64_t *) Y->x) [i] += aij * ((GxB_FC64_t *) X->x) [j] ;  \
    }                                                                   \
    else if (type == Complex)                                           \
    {                                                                   \
        GxB_FC64_t aij ;                                                \
        GxB_Iterator_get_UDT (iterator, &aij) ;                         \
        ((GxB_FC64_t *) Y->x) [i] += aij * ((GxB_FC64_t *) X->x) [j] ;  \
    }                                                                   \
    else                                                                \
    {                                                                   \
        mexErrMsgTxt ("type unknown") ;                                 \
    }                                                                   \
}

{
    if (kind == 0)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a row iterator
        //----------------------------------------------------------------------

        // GxB_print (A, 3) ;

        // attach it to the matrix A
        OK (GxB_rowIterator_attach (iterator, A, NULL)) ;
        // get the kount
        GrB_Index kount2 = 0 ;
        GrB_Index kount1 = GxB_rowIterator_kount (iterator) ;
        // seek to A(0,:)
        info = GxB_rowIterator_seekRow (iterator, 0) ;
        OK (info) ;
        while (info != GxB_EXHAUSTED)
        {
            // iterate over entries in A(i,:)
            kount2++ ;
            GrB_Index i = GxB_rowIterator_getRowIndex (iterator) ;
            Assert (i >= 0 && i < nrows) ;
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index j = GxB_rowIterator_getColIndex (iterator) ;
                Assert (j >= 0 && j < ncols) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(i,:)
                info = GxB_rowIterator_nextCol (iterator) ;
                OK (info) ;
            }
            // move to the next row, A(i+1,:)
            info = GxB_rowIterator_nextRow (iterator) ;
            OK (info) ;
        }

        Assert (kount1 == kount2) ;

        // check the return value when the iterator is exhausted
        GrB_Index i = GxB_rowIterator_getRowIndex (iterator) ;
        Assert (i == nrows) ;

    }
    else if (kind == 1)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a col iterator
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_colIterator_attach (iterator, A, NULL)) ;
        // get the kount
        GrB_Index kount2 = 0 ;
        GrB_Index kount1 = GxB_colIterator_kount (iterator) ;
        // seek to A(:,0)
        info = GxB_colIterator_seekCol (iterator, 0) ;
        OK (info) ;
        while (info != GxB_EXHAUSTED)
        {
            // iterate over entries in A(:,j)
            kount2++ ;
            GrB_Index j = GxB_colIterator_getColIndex (iterator) ;
            Assert (j >= 0 && j < ncols) ;
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index i = GxB_colIterator_getRowIndex (iterator) ;
                Assert (i >= 0 && i < nrows) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(:,j)
                info = GxB_colIterator_nextRow (iterator) ;
                OK (info) ;
            }
            // move to the next column, A(:,j+1)
            info = GxB_colIterator_nextCol (iterator) ;
            OK (info) ;
        }

        Assert (kount1 == kount2) ;

        // check the return value when the iterator is exhausted
        GrB_Index j = GxB_rowIterator_getRowIndex (iterator) ;
        Assert (j == ncols) ;

    }
    else if (kind == 2)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a matrix iterator
        //----------------------------------------------------------------------

        // useless descriptor, just to test nthreads extraction for GB_wait
        if (GB_IS_SPARSE (A) || GB_IS_HYPERSPARSE (A)) A->jumbled = true ;

        // attach it to the matrix A
        OK (GxB_Matrix_Iterator_attach (iterator, A, GrB_DESC_S)) ;
        // seek to the first entry
        OK (GxB_Matrix_Iterator_seek (iterator, 0)) ;
        while (info != GxB_EXHAUSTED)
        {
            // get the entry A(i,j)
            GrB_Index i, j ;
            GxB_Matrix_Iterator_getIndex (iterator, &i, &j) ;
            Assert (i >= 0 && i < nrows) ;
            Assert (j >= 0 && j < ncols) ;
            // Y (i) += A(i,j) * X (j)
            MULTADD ;
            // move to the next entry in A
            info = GxB_Matrix_Iterator_next (iterator) ;
            OK (info);
        }

        GrB_Index p = GxB_Matrix_Iterator_getp (iterator) ;
        GrB_Index pmax = GxB_Matrix_Iterator_getpmax (iterator) ;
        Assert (p == pmax) ;

    }
    else if (kind == 3)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a row iterator, but backwards with kseek
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_rowIterator_attach (iterator, A, NULL)) ;
        // get the kount
        GrB_Index kount = GxB_rowIterator_kount (iterator) ;
        for (int k = kount-1 ; k >= 0 ; k--)
        {
            // seek to A(k,:)
            info = GxB_rowIterator_kseek (iterator, (GrB_Index) k) ;
            Assert (info == GrB_SUCCESS || info == GrB_NO_VALUE) ;

            // get the row index
            GrB_Index i = GxB_rowIterator_getRowIndex (iterator) ;
            Assert (i >= 0 && i <= nrows) ;

            // iterate over entries in A(i,:)
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index j = GxB_rowIterator_getColIndex (iterator) ;
                Assert (j >= 0 && j < ncols) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(i,:)
                info = GxB_rowIterator_nextCol (iterator) ;
                OK (info) ;
            }
        }

    }
    else if (kind == 4)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a col iterator, but backwards with kseek
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_colIterator_attach (iterator, A, NULL)) ;
        // get the kount
        GrB_Index kount = GxB_colIterator_kount (iterator) ;
        for (int k = kount-1 ; k >= 0 ; k--)
        {
            // seek to A(:,k)
            info = GxB_colIterator_kseek (iterator, (GrB_Index) k) ;
            Assert (info == GrB_SUCCESS || info == GrB_NO_VALUE) ;

            // get the col index
            GrB_Index j = GxB_colIterator_getColIndex (iterator) ;
            Assert (j >= 0 && j <= ncols) ;

            // iterate over entries in A(:,j)
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index i = GxB_colIterator_getRowIndex (iterator) ;
                Assert (i >= 0 && i < nrows) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(:,j)
                info = GxB_colIterator_nextRow (iterator) ;
                OK (info) ;
            }
        }

    }
    else if (kind == 5)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a row iterator, but with seekRow
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_rowIterator_attach (iterator, A, NULL)) ;

        for (int k = 0 ; k < nrows ; k++)
        {
            // seek to A(k,:)
            info = GxB_rowIterator_seekRow (iterator, (GrB_Index) k) ;
            Assert (info >= GrB_SUCCESS) ;

            // get the row index
            GrB_Index i = GxB_rowIterator_getRowIndex (iterator) ;
            Assert (i >= 0 && i <= nrows) ;

            // if the matrix is hypersparse, seekRow can skip
            if (i != k) continue ;

            // iterate over entries in A(i,:)
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index j = GxB_rowIterator_getColIndex (iterator) ;
                Assert (j >= 0 && j < ncols) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(i,:)
                info = GxB_rowIterator_nextCol (iterator) ;
                OK (info) ;
            }
        }

        // try exhaustion
        info = GxB_rowIterator_seekRow (iterator, nrows) ;
        Assert (info == GxB_EXHAUSTED) ;

    }
    else if (kind == 6)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a col iterator, but with seekCol
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_colIterator_attach (iterator, A, NULL)) ;
        for (int k = 0 ; k < ncols ; k++)
        {
            // seek to A(:,k)
            info = GxB_colIterator_seekCol (iterator, (GrB_Index) k) ;
            Assert (info >= GrB_SUCCESS) ;

            // get the col index
            GrB_Index j = GxB_colIterator_getColIndex (iterator) ;
            Assert (j >= 0 && j <= ncols) ;

            // if the matrix is hypersparse, seekCol can skip
            if (j != k) continue ;

            // iterate over entries in A(:,j)
            while (info == GrB_SUCCESS)
            {
                // get the entry A(i,j)
                GrB_Index i = GxB_colIterator_getRowIndex (iterator) ;
                Assert (i >= 0 && i < nrows) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
                // move to the next entry in A(:,j)
                info = GxB_colIterator_nextRow (iterator) ;
                OK (info) ;
            }
        }

        // try exhaustion
        info = GxB_colIterator_seekCol (iterator, ncols) ;
        Assert (info == GxB_EXHAUSTED) ;

    }
    else if (kind == 7)
    {

        //----------------------------------------------------------------------
        // Y = A*X using a matrix iterator but with seek
        //----------------------------------------------------------------------

        // attach it to the matrix A
        OK (GxB_Matrix_Iterator_attach (iterator, A, NULL)) ;
        GrB_Index pmax = GxB_Matrix_Iterator_getpmax (iterator) ;

        for (GrB_Index p = 0 ; p < pmax ; p++)
        {
            // seek to the pth entry
            OK (GxB_Matrix_Iterator_seek (iterator, p)) ;
            // check if the pth entry exists (for bitmap case)
            if (p == GxB_Matrix_Iterator_getp (iterator))
            {
                // get the entry A(i,j)
                GrB_Index i, j ;
                GxB_Matrix_Iterator_getIndex (iterator, &i, &j) ;
                Assert (i >= 0 && i < nrows) ;
                Assert (j >= 0 && j < ncols) ;
                // Y (i) += A(i,j) * X (j)
                MULTADD ;
            }
        }
        // try exhaustion
        info = GxB_Matrix_Iterator_seek (iterator, pmax) ;
        Assert (info == GxB_EXHAUSTED) ;
    }
}