File: GB_transpose_op.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, 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 (402 lines) | stat: -rw-r--r-- 16,375 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
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
//------------------------------------------------------------------------------
// GB_transpose_op: transpose, typecast, and apply an operator to a matrix
//------------------------------------------------------------------------------

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

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

// C = op (A')

// The values of A are typecasted to op->xtype and then passed to the unary
// operator.  The output is assigned to C, which must be of type op->ztype; no
// output typecasting done with the output of the operator.

// If the op is positional, it has been replaced with the unary op
// GxB_ONE_INT64, as a placeholder, and C_code_iso is GB_ISO_1.  The true op
// is applied later, in GB_transpose.

// If A is sparse or hypersparse (but not as-is-full)
//      The pattern of C is constructed.  C is sparse.
//      Workspaces and A_slice are non-NULL.
//      This method is parallel, but not highly scalable.  It uses only
//      nthreads = nnz(A)/(A->vlen) threads.

// If A is full or as-if-full:
//      The pattern of C is not constructed.  C is full.
//      Workspaces and A_slice are NULL.
//      This method is parallel and fully scalable.

// If A is bitmap:
//      C->b is constructed.  C is bitmap.
//      Workspaces and A_slice are NULL.
//      This method is parallel and fully scalable.

#include "transpose/GB_transpose.h"
#include "binaryop/GB_binop.h"
#include "jitifyer/GB_stringify.h"
#ifndef GBCOMPACT
#include "GB_control.h"
#include "FactoryKernels/GB_uop__include.h"
#include "FactoryKernels/GB_ew__include.h"
#endif

GrB_Info GB_transpose_op // transpose, typecast, and apply operator to a matrix
(
    GrB_Matrix C,                       // output matrix
    const GB_iso_code C_code_iso,       // iso code for C
        const GB_Operator op,           // unary/idxunop/binop to apply
        const GrB_Scalar scalar,        // scalar to bind to binary operator
        bool binop_bind1st,             // if true, binop(x,A) else binop(A,y)
    const GrB_Matrix A,                 // input matrix
    // for sparse or hypersparse case:
    void **Workspaces,                  // Workspaces, size nworkspaces
    const int64_t *restrict A_slice,    // how A is sliced, size nthreads+1
    int nworkspaces,                    // # of workspaces to use
    // for all cases:
    int nthreads                        // # of threads to use
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    ASSERT (!GB_ZOMBIES (A)) ;
    ASSERT (GB_JUMBLED_OK (A)) ;
    ASSERT (!GB_PENDING (A)) ;

    GrB_Info info = GrB_NO_VALUE ;
    GrB_Type Atype = A->type ;
    ASSERT (op != NULL) ;
    GB_Opcode opcode = op->opcode ;

    // positional operators and idxunop are applied after the transpose
    // future:: extend this method to handle positional and idxunop operators
    ASSERT (!GB_OPCODE_IS_POSITIONAL (opcode)) ;
    ASSERT (!GB_IS_INDEXUNARYOP_CODE (opcode)) ;

    // for the generic kernels below:
    bool Cp_is_32 = C->p_is_32 ;
    #define GB_Cp_IS_32 Cp_is_32
    #define GB_A_TYPE GB_void
    #define GB_C_TYPE GB_void

    //--------------------------------------------------------------------------
    // transpose the matrix and apply the operator
    //--------------------------------------------------------------------------

    if (C->iso)
    { 

        //----------------------------------------------------------------------
        // via the iso kernel
        //----------------------------------------------------------------------

        // if C is iso, only the pattern is transposed.  The numerical work
        // takes O(1) time

        // Cx [0] = unop (A), binop (scalar,A), or binop (A,scalar)
        GB_unop_iso ((GB_void *) C->x, C->type, C_code_iso, op, A, scalar) ;

        // C = transpose the pattern
        #define GB_ISO_TRANSPOSE
        #include "transpose/template/GB_transpose_template.c"
        info = GrB_SUCCESS ;

    }
    else if (GB_IS_UNARYOP_CODE (opcode))
    {

        //----------------------------------------------------------------------
        // apply the unary operator to all entries
        //----------------------------------------------------------------------

        ASSERT_OP_OK (op, "op for transpose", GB0) ;

        //----------------------------------------------------------------------
        // via the factory kernel
        //----------------------------------------------------------------------

        #ifndef GBCOMPACT
        GB_IF_FACTORY_KERNELS_ENABLED
        { 
            if (Atype == op->xtype || opcode == GB_IDENTITY_unop_code)
            { 

                // The switch factory is used if the unop is IDENTITY, or if no
                // typecasting is being done.  The IDENTITY operator can do
                // arbitrary typecasting.

                //--------------------------------------------------------------
                // define the worker for the switch factory
                //--------------------------------------------------------------

                #define GB_uop_tran(opname,zname,aname) \
                    GB (_uop_tran_ ## opname ## zname ## aname)

                #define GB_WORKER(opname,zname,ztype,aname,atype)            \
                {                                                            \
                    info = GB_uop_tran (opname,zname,aname)                  \
                        (C, A, Workspaces, A_slice, nworkspaces, nthreads) ; \
                }                                                            \
                break ;

                //--------------------------------------------------------------
                // launch the switch factory
                //--------------------------------------------------------------

                #include "apply/factory/GB_unop_factory.c"
            }
        }
        #endif

        //----------------------------------------------------------------------
        // via the JIT or PreJIT kernel
        //----------------------------------------------------------------------

        if (info == GrB_NO_VALUE)
        { 
            info = GB_transpose_unop_jit (C, op, A, Workspaces, A_slice,
                nworkspaces, nthreads) ;
        }

        //----------------------------------------------------------------------
        // via the generic kernel
        //----------------------------------------------------------------------

        if (info == GrB_NO_VALUE)
        {
            GB_BURBLE_MATRIX (A, "(generic transpose: %s) ", op->name) ;

            size_t asize = Atype->size ;
            size_t zsize = op->ztype->size ;
            size_t xsize = op->xtype->size ;
            GB_cast_function
                cast_A_to_X = GB_cast_factory (op->xtype->code, Atype->code) ;
            GxB_unary_function fop = op->unop_function ;

            ASSERT_TYPE_OK (op->ztype, "unop ztype", GB0) ;
            ASSERT_TYPE_OK (op->xtype, "unop xtype", GB0) ;
            ASSERT_TYPE_OK (C->type, "C type", GB0) ;
            ASSERT (C->type->size == zsize) ;
            ASSERT (C->type == op->ztype) ;

            // Cx [pC] = unop ((xtype) Ax [pA])
            #undef  GB_APPLY_OP
            #define GB_APPLY_OP(pC,pA)                                      \
            {                                                               \
                /* xwork = (xtype) Ax [pA] */                               \
                GB_void xwork [GB_VLA(xsize)] ;                             \
                cast_A_to_X (xwork, Ax +((pA)*asize), asize) ;              \
                /* Cx [pC] = fop (xwork) ; Cx is of type op->ztype */       \
                fop (Cx +((pC)*zsize), xwork) ;                             \
            }

            #include "transpose/template/GB_transpose_template.c"
            info = GrB_SUCCESS ;
        }

    }
    else
    {

        //----------------------------------------------------------------------
        // apply a binary operator (bound to a scalar)
        //----------------------------------------------------------------------

        ASSERT_OP_OK (op, "binop for transpose", GB0) ;

        GB_Type_code xcode, ycode, zcode ;
        ASSERT (opcode != GB_FIRST_binop_code) ;
        ASSERT (opcode != GB_SECOND_binop_code) ;
        ASSERT (opcode != GB_PAIR_binop_code) ;
        ASSERT (opcode != GB_ANY_binop_code) ;

        size_t asize = Atype->size ;
        size_t ssize = scalar->type->size ;
        size_t zsize = op->ztype->size ;
        size_t xsize = op->xtype->size ;
        size_t ysize = op->ytype->size ;

        GB_Type_code scalar_code = scalar->type->code ;
        xcode = op->xtype->code ;
        ycode = op->ytype->code ;

        // typecast the scalar to the operator input
        size_t ssize_cast ;
        GB_Type_code scalar_code_cast ;
        if (binop_bind1st)
        { 
            ssize_cast = xsize ;
            scalar_code_cast = xcode ;
        }
        else
        { 
            ssize_cast = ysize ;
            scalar_code_cast = ycode ;
        }
        GB_void swork [GB_VLA(ssize_cast)] ;
        GB_void *scalarx = (GB_void *) scalar->x ;
        if (scalar_code_cast != scalar_code)
        { 
            // typecast the scalar to the operator input, in swork
            GB_cast_function cast_s =
                GB_cast_factory (scalar_code_cast, scalar_code) ;
            cast_s (swork, scalar->x, ssize) ;
            scalarx = swork ;
        }

        GB_Type_code acode = Atype->code ;
        GxB_binary_function fop = op->binop_function ;
        ASSERT (fop != NULL) ;
        GB_cast_function cast_A_to_Y = GB_cast_factory (ycode, acode) ;
        GB_cast_function cast_A_to_X = GB_cast_factory (xcode, acode) ;

        if (binop_bind1st)
        {

            //------------------------------------------------------------------
            // C = op(scalar,A') via the factory kernel
            //------------------------------------------------------------------

            #ifndef GBCOMPACT
            GB_IF_FACTORY_KERNELS_ENABLED
            { 
                if (GB_binop_builtin (op->xtype, false, Atype, false,
                    (GrB_BinaryOp) op, false, &opcode, &xcode, &ycode, &zcode))
                { 

                    //----------------------------------------------------------
                    // define the worker for the switch factory
                    //----------------------------------------------------------

                    #define GB_bind1st_tran(op,xname) \
                        GB (_bind1st_tran_ ## op ## xname)

                    #define GB_BINOP_WORKER(op,xname)                       \
                    {                                                       \
                        info = GB_bind1st_tran (op, xname) (C, scalarx, A,  \
                            Workspaces, A_slice, nworkspaces, nthreads) ;   \
                    }                                                       \
                    break ;

                    //----------------------------------------------------------
                    // launch the switch factory
                    //----------------------------------------------------------

                    #define GB_NO_FIRST
                    #define GB_NO_SECOND
                    #define GB_NO_PAIR
                    #include "binaryop/factory/GB_binop_factory.c"
                }
            }
            #endif

            //------------------------------------------------------------------
            // via the JIT or PreJIT kernel
            //------------------------------------------------------------------

            if (info == GrB_NO_VALUE)
            { 
                info = GB_transpose_bind1st_jit (C, (GrB_BinaryOp) op,
                    scalarx, A, Workspaces, A_slice, nworkspaces, nthreads) ;
            }

        }
        else
        {

            //------------------------------------------------------------------
            // C = op(A',scalar) via the factory kernel
            //------------------------------------------------------------------

            #ifndef GBCOMPACT
            GB_IF_FACTORY_KERNELS_ENABLED
            { 
                if (GB_binop_builtin (Atype, false, op->ytype, false,
                    (GrB_BinaryOp) op, false, &opcode, &xcode, &ycode, &zcode))
                { 

                    //----------------------------------------------------------
                    // define the worker for the switch factory
                    //----------------------------------------------------------

                    #define GB_bind2nd_tran(op,xname) \
                        GB (_bind2nd_tran_ ## op ## xname)
                    #undef  GB_BINOP_WORKER
                    #define GB_BINOP_WORKER(op,xname)                       \
                    {                                                       \
                        info = GB_bind2nd_tran (op, xname) (C, A, scalarx,  \
                            Workspaces, A_slice, nworkspaces, nthreads) ;   \
                    }                                                       \
                    break ;

                    //----------------------------------------------------------
                    // launch the switch factory
                    //----------------------------------------------------------

                    #define GB_NO_FIRST
                    #define GB_NO_SECOND
                    #define GB_NO_PAIR
                    #include "binaryop/factory/GB_binop_factory.c"
                }
            }
            #endif

            //------------------------------------------------------------------
            // via the JIT or PreJIT kernel
            //------------------------------------------------------------------

            if (info == GrB_NO_VALUE)
            { 
                info = GB_transpose_bind2nd_jit (C, (GrB_BinaryOp) op,
                    A, scalarx, Workspaces, A_slice, nworkspaces, nthreads) ;
            }

        }

        //----------------------------------------------------------------------
        // via the generic kernel
        //----------------------------------------------------------------------

        if (info == GrB_NO_VALUE)
        {
            GB_BURBLE_MATRIX (A, "(generic transpose: %s) ", op->name) ;

            if (binop_bind1st)
            { 
                // Cx [pC] = binaryop ((xtype) scalar, (ytype) Ax [pA])
                #undef  GB_APPLY_OP
                #define GB_APPLY_OP(pC,pA)                                  \
                {                                                           \
                    /* ywork = (ytype) Ax [pA] */                           \
                    GB_void ywork [GB_VLA(ysize)] ;                         \
                    cast_A_to_Y (ywork, Ax +(pA)*asize, asize) ;            \
                    /* Cx [pC] = fop (xwork) ; Cx is of type op->ztype */   \
                    fop (Cx +((pC)*zsize), scalarx, ywork) ;                \
                }
                #include "transpose/template/GB_transpose_template.c"
            }
            else
            { 
                // Cx [pC] = binaryop ((xtype) Ax [pA], (ytype) scalar)
                #undef  GB_APPLY_OP
                #define GB_APPLY_OP(pC,pA)                                  \
                {                                                           \
                    /* xwork = (xtype) Ax [pA] */                           \
                    GB_void xwork [GB_VLA(xsize)] ;                         \
                    cast_A_to_X (xwork, Ax +(pA)*asize, asize) ;            \
                    /* Cx [pC] = fop (xwork) ; Cx is of type op->ztype */   \
                    fop (Cx +(pC*zsize), xwork, scalarx) ;                  \
                }
                #include "transpose/template/GB_transpose_template.c"
            }
            info = GrB_SUCCESS ;
        }
    }

    return (info) ;
}