File: GB_AxB_saxpy_generic_method.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 (285 lines) | stat: -rw-r--r-- 10,145 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
//------------------------------------------------------------------------------
// GB_AxB_saxpy_generic_method: C=A*B, C<M>=A*B, or C<!M>=A*B
//------------------------------------------------------------------------------

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

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

// GB_AxB_saxpy_generic_method computes C=A*B, C<M>=A*B, or C<!M>=A*B.  with
// arbitrary types and operators.  C can be hyper, sparse, or bitmap, but not
// full.  For all cases, the four matrices C, M (if present), A, and B have the
// same format (by-row or by-column), or they represent implicitly transposed
// matrices with the same effect.  This method does not handle the dot-product
// methods, which compute C=A'*B if A and B are held by column, or equivalently
// A*B' if both are held by row.

// This method uses GB_AxB_saxpy_generic_template.c to implement two
// meta-methods, each of which can contain further specialized methods (such as
// the fine/coarse x Gustavson/Hash, mask/no-mask methods in saxpy3):

// saxpy3: general purpose method, where C is sparse or hypersparse,
//          via GB_AxB_saxpy3_template.c.  SaxpyTasks holds the (fine/coarse x
//          Gustavson/Hash) tasks constructed by GB_AxB_saxpy3_slice*.

// saxbit: general purpose method, where C is bitmap, via
//          GB_AxB_saxbit_template.c.  The method constructs its own
//          tasks in workspace defined and freed in that template.

// C is not iso, nor is it full.

// This template is used to construct the following methods, all of which
// are called by GB_AxB_saxpy_generic:

//      GB_AxB_saxpy3_generic_first
//      GB_AxB_saxpy3_generic_second
//      GB_AxB_saxpy3_generic_flipped
//      GB_AxB_saxpy3_generic_unflipped

//      GB_AxB_saxbit_generic_first
//      GB_AxB_saxbit_generic_second
//      GB_AxB_saxbit_generic_flipped
//      GB_AxB_saxbit_generic_unflipped

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

#include "mxm/GB_AxB_saxpy.h"
#include "binaryop/GB_binop.h"
#include "assign/GB_bitmap_assign_methods.h"
#include "mxm/include/GB_mxm_shared_definitions.h"
#include "mxm/GB_AxB_saxpy_generic.h"
#include "generic/GB_generic.h"

GrB_Info GB_AXB_SAXPY_GENERIC_METHOD
(
    GrB_Matrix C,                   // any sparsity except full
    const GrB_Matrix M,
    bool Mask_comp,
    const bool Mask_struct,
    const bool M_in_place,          // ignored if C is bitmap
    const GrB_Matrix A,
    bool A_is_pattern,
    const GrB_Matrix B,
    bool B_is_pattern,
    const GrB_Semiring semiring,    // semiring that defines C=A*B
    const int ntasks,
    const int nthreads,

    #if GB_GENERIC_C_IS_SPARSE_OR_HYPERSPARSE
    // for saxpy3 only:
    GB_saxpy3task_struct *restrict SaxpyTasks,
    const int nfine,
    const int do_sort,              // if true, sort in saxpy3
    GB_Werk Werk
    #else
    // for saxbit only:
    const int nfine_tasks_per_vector,
    const bool use_coarse_tasks,
    const bool use_atomics,
    const int64_t *restrict M_ek_slicing,
    const int M_nthreads,
    const int M_ntasks,
    const int64_t *restrict A_slice,
    const int64_t *restrict H_slice,
    GB_void *restrict Wcx,
    int8_t *restrict Wf
    #endif
)
{

    //--------------------------------------------------------------------------
    // get operators, functions, workspace, contents of A, B, and C
    //--------------------------------------------------------------------------

    GrB_BinaryOp mult = semiring->multiply ;
    GrB_Monoid add = semiring->add ;
    ASSERT (mult->ztype == add->op->ztype) ;
    ASSERT (mult->ztype == C->type) ;

    GxB_binary_function fmult = mult->binop_function ;    // NULL if positional
    GxB_index_binary_function fmult_idx = mult->idxbinop_function ;
    GxB_binary_function fadd  = add->op->binop_function ;
    GB_Opcode opcode = mult->opcode ;

    size_t csize = C->type->size ;
    size_t asize = A_is_pattern ? 0 : A->type->size ;
    size_t bsize = B_is_pattern ? 0 : B->type->size ;

    size_t xsize = mult->xtype->size ;
    size_t ysize = mult->ytype->size ;

    // scalar workspace: because of typecasting, the x/y types need not
    // be the same as the size of the A and B types.
    // GB_GENERIC_FLIPXY false: aik = (xtype) A(i,k) and bkj = (ytype) B(k,j)
    // GB_GENERIC_FLIPXY true:  aik = (ytype) A(i,k) and bkj = (xtype) B(k,j)
    size_t aik_size = GB_GENERIC_FLIPXY ? ysize : xsize ;
    size_t bkj_size = GB_GENERIC_FLIPXY ? xsize : ysize ;

    GB_cast_function cast_A, cast_B ;
    #if GB_GENERIC_FLIPXY
    { 
        // A is typecasted to y, and B is typecasted to x
        cast_A = A_is_pattern ? NULL : 
                 GB_cast_factory (mult->ytype->code, A->type->code) ;
        cast_B = B_is_pattern ? NULL : 
                 GB_cast_factory (mult->xtype->code, B->type->code) ;
    }
    #else
    { 
        // A is typecasted to x, and B is typecasted to y
        cast_A = A_is_pattern ? NULL :
                 GB_cast_factory (mult->xtype->code, A->type->code) ;
        cast_B = B_is_pattern ? NULL :
                 GB_cast_factory (mult->ytype->code, B->type->code) ;
    }
    #endif

    int nthreads_max = GB_Context_nthreads_max ( ) ;
    double chunk = GB_Context_chunk ( ) ;

    //--------------------------------------------------------------------------
    // C = A*B via saxpy3 or bitmap method, function pointers, and typecasting
    //--------------------------------------------------------------------------

    // This is before typecast to GB_B2TYPE, so it is the size of the
    // entries in the B matrix, not as typecasted to GB_B2TYPE.
    #define GB_B_SIZE bsize

    // definitions for GB_AxB_saxpy_generic_template.c
    #include "mxm/include/GB_AxB_saxpy3_template.h"

    // aik = A(i,k), located in Ax [A_iso ? 0:pA]
    #undef  GB_A_IS_PATTERN
    #define GB_A_IS_PATTERN 0
    #undef  GB_DECLAREA
    #define GB_DECLAREA(aik)                                            \
        GB_void aik [GB_VLA(aik_size)] ;
    #undef  GB_GETA
    #define GB_GETA(aik,Ax,pA,A_iso)                                    \
        if (!A_is_pattern)                                              \
        {                                                               \
            cast_A (aik, Ax +((A_iso) ? 0:((pA)*asize)), asize) ;       \
        }

    // bkj = B(k,j), located in Bx [B_iso ? 0:pB]
    #undef  GB_B_IS_PATTERN
    #define GB_B_IS_PATTERN 0
    #undef  GB_DECLAREB
    #define GB_DECLAREB(bkj)                                            \
        GB_void bkj [GB_VLA(bkj_size)] ;
    #undef  GB_GETB
    #define GB_GETB(bkj,Bx,pB,B_iso)                                    \
        if (!B_is_pattern)                                              \
        {                                                               \
            cast_B (bkj, Bx +((B_iso) ? 0:((pB)*bsize)), bsize) ;       \
        }

    // define t for each task
    #undef  GB_CIJ_DECLARE
    #define GB_CIJ_DECLARE(t) GB_void t [GB_VLA(csize)]

    // address of Cx [p]
    #undef  GB_CX
    #define GB_CX(p) (Cx +((p)*csize))

    // Cx [p] = t
    #undef  GB_CIJ_WRITE
    #define GB_CIJ_WRITE(p,t) memcpy (GB_CX (p), t, csize)

    // address of Hx [i]
    #undef  GB_HX
    #define GB_HX(i) (Hx +((i)*csize))

    // Hx [i] = t
    #undef  GB_HX_WRITE
    #define GB_HX_WRITE(i,t) memcpy (GB_HX (i), t, csize)

    // Cx [p] = Hx [i]
    #undef  GB_CIJ_GATHER
    #define GB_CIJ_GATHER(p,i) memcpy (GB_CX (p), GB_HX(i), csize)

    // Cx [p:p+len=-1] = Hx [i:i+len-1]
    // via memcpy (&(Cx [p]), &(Hx [i]), len*csize)
    #undef  GB_CIJ_MEMCPY
    #define GB_CIJ_MEMCPY(p,i,len) memcpy (GB_CX (p), GB_HX (i), (len)*csize)

    // Cx [p] += Hx [i]
    #undef  GB_CIJ_GATHER_UPDATE
    #define GB_CIJ_GATHER_UPDATE(p,i) fadd (GB_CX (p), GB_CX (p), GB_HX (i))

    // Cx [p] += t
    #undef  GB_CIJ_UPDATE
    #define GB_CIJ_UPDATE(p,t) fadd (GB_CX (p), GB_CX (p), t)

    // Hx [i] += t
    #undef  GB_HX_UPDATE
    #define GB_HX_UPDATE(i,t) fadd (GB_HX (i), GB_HX (i), t)

    // generic types for C and Z
    #undef  GB_C_TYPE
    #define GB_C_TYPE GB_void

    #undef  GB_Z_TYPE
    #define GB_Z_TYPE GB_void

    #undef  GB_C_SIZE
    #define GB_C_SIZE csize

    #if GB_GENERIC_OP_IS_FIRST
    { 
        // t = A(i,k)
        ASSERT (B_is_pattern) ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) memcpy (t, aik, csize)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #elif GB_GENERIC_OP_IS_SECOND
    { 
        // t = B(i,k)
        ASSERT (A_is_pattern) ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) memcpy (t, bkj, csize)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #elif GB_GENERIC_FLIPXY
    { 
        // t = B(k,j) * A(i,k)
        ASSERT (fmult != NULL) ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) fmult (t, bkj, aik)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #elif GB_GENERIC_NOFLIPXY
    { 
        // t = A(i,k) * B(k,j)
        ASSERT (fmult != NULL) ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) fmult (t, aik, bkj)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #elif GB_GENERIC_IDX_FLIPXY
    { 
        // t = B(k,j) * A(i,k)
        ASSERT (fmult_idx != NULL) ;
        const void *theta = mult->theta ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) \
            fmult_idx (t, bkj, j, k, aik, k, i, theta)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #elif GB_GENERIC_IDX_NOFLIPXY
    { 
        // t = A(i,k) * B(k,j)
        ASSERT (fmult_idx != NULL) ;
        const void *theta = mult->theta ;
        #undef  GB_MULT
        #define GB_MULT(t, aik, bkj, i, k, j) \
            fmult_idx (t, aik, i, k, bkj, k, j, theta)
        #include "mxm/factory/GB_AxB_saxpy_generic_template.c"
    }
    #endif

    return (GrB_SUCCESS) ;
}