| 12
 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
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 
 | //------------------------------------------------------------------------------
// GB_apply_op: typecast and apply a unary/binary/idxunop operator to an array
//------------------------------------------------------------------------------
// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2025, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//------------------------------------------------------------------------------
// Cx = op (A)
// Cx and A->x may be aliased.
// This function is CSR/CSC agnostic.  For positional ops, A is treated as if
// it is in CSC format.  The caller has already modified the op if A is in CSR
// format.
#include "apply/GB_apply.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
#define GB_FREE_ALL                         \
{                                           \
    GB_WERK_POP (A_ek_slicing, int64_t) ;   \
}
GrB_Info GB_apply_op        // apply a unary op, idxunop, or binop, Cx = op (A)
(
    GB_void *Cx,                    // output array
    const GrB_Type ctype,           // type of C
    const GB_iso_code C_code_iso,   // C non-iso, or code to compute C iso value
        const GB_Operator op_in,    // unary/index-unary/binop to apply
        const GrB_Scalar scalar,    // scalar to bind to binary operator
        bool binop_bind1st,         // if true, C=binop(s,A), else C=binop(A,s)
        bool flipij,                // if true, flip i,j for user idxunop
    const GrB_Matrix A,             // input matrix
    GB_Werk Werk
)
{
    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------
    GrB_Info info ;
    GB_Operator op = op_in ;
    ASSERT (Cx != NULL) ;
    ASSERT_MATRIX_OK (A, "A input for GB_apply_op", GB0) ;
    ASSERT (GB_JUMBLED_OK (A)) ;        // A can be jumbled
    ASSERT (!GB_ZOMBIES (A)) ;
    GB_WERK_DECLARE (A_ek_slicing, int64_t) ;
    ASSERT (GB_IMPLIES (op != NULL, ctype == op->ztype)) ;
    ASSERT_SCALAR_OK_OR_NULL (scalar, "scalar for GB_apply_op", GB0) ;
    //--------------------------------------------------------------------------
    // get A
    //--------------------------------------------------------------------------
    // A->x is not const since the operator might be applied in-place, if
    // C is aliased to C.
    GB_void *Ax = (GB_void *) A->x ;        // A->x has type A->type
    const int8_t *Ab = A->b ;               // only if A is bitmap
    const GrB_Type Atype = A->type ;        // type of A->x
    const int64_t anz = GB_nnz_held (A) ;   // size of A->x and Cx
    #define GB_A_IS_BITMAP (Ab != NULL)
    //--------------------------------------------------------------------------
    // determine the maximum number of threads to use
    //--------------------------------------------------------------------------
    int nthreads_max = GB_Context_nthreads_max ( ) ;
    double chunk = GB_Context_chunk ( ) ;
    //--------------------------------------------------------------------------
    // get the operator
    //--------------------------------------------------------------------------
    GB_Opcode opcode ;
    bool op_is_unop = false ;
    bool op_is_binop = false ;
    bool opz64 = false ;
    bool opz32 = false ;
    if (op != NULL)
    { 
        ASSERT_OP_OK (op, "op for GB_apply_op", GB0) ;
        opcode = op->opcode ;
        op_is_unop = GB_IS_UNARYOP_CODE (opcode) ;
        op_is_binop = GB_IS_BINARYOP_CODE (opcode) ;
        ASSERT (!GB_IS_INDEXBINARYOP_CODE (opcode)) ;
        opz64 = (op->ztype == GrB_INT64) ;
        opz32 = (op->ztype == GrB_INT32) ;
        if (GB_IS_BUILTIN_BINOP_CODE_POSITIONAL (opcode))
        {
            // rename builtin positional binary ops to positional unary ops
            GrB_UnaryOp op1 = NULL ;
            switch (opcode)
            {
                case GB_FIRSTI_binop_code   : // z = first_i(A(i,j),y) == i
                case GB_SECONDI_binop_code  : // z = second_i(x,A(i,j)) == i
                    // rename FIRSTI and SECONDI to POSITIONI
                    op1 = opz64 ? GxB_POSITIONI_INT64 : GxB_POSITIONI_INT32 ;
                    break ;
                case GB_FIRSTI1_binop_code  : // z = first_i1(A(i,j),y) == i+1
                case GB_SECONDI1_binop_code : // z = second_i1(x,A(i,j)) == i+1
                    // rename FIRSTI1 and SECONDI1 to POSITIONI1
                    op1 = opz64 ? GxB_POSITIONI1_INT64 : GxB_POSITIONI1_INT32 ;
                    break ;
                case GB_FIRSTJ_binop_code   : // z = first_j(A(i,j),y) == j
                case GB_SECONDJ_binop_code  : // z = second_j(x,A(i,j)) == j
                    // rename FIRSTJ and SECONDJ to POSITIONJ
                    op1 = opz64 ? GxB_POSITIONJ_INT64 : GxB_POSITIONJ_INT32 ;
                    break ;
                case GB_FIRSTJ1_binop_code  : // z = first_j1(A(i,j),y) == j+1
                case GB_SECONDJ1_binop_code : // z = second_j1(x,A(i,j)) == j+1
                    // rename FIRSTJ1 and SECONDJ1 to POSITIONJ1
                    op1 = opz64 ? GxB_POSITIONJ1_INT64 : GxB_POSITIONJ1_INT32 ;
                    break ;
                default:;
            }
            ASSERT (op1 != NULL) ;
            op = (GB_Operator) op1 ;
            ASSERT_OP_OK (op, "revised op for GB_apply_op", GB0) ;
            opcode = op->opcode ;
            op_is_unop = true ;
            op_is_binop = false ;
            ASSERT (GB_IS_BUILTIN_UNOP_CODE_POSITIONAL (opcode)) ;
        }
    }
    else
    { 
        // C is iso, with no operator to apply; just call GB_unop_iso below.
        ASSERT (C_code_iso == GB_ISO_1 ||   // C iso value is 1
                C_code_iso == GB_ISO_S ||   // C iso value is the scalar
                C_code_iso == GB_ISO_A) ;   // C iso value is the iso value of A
        opcode = GB_NOP_code ;
    }
    //--------------------------------------------------------------------------
    // determine number of threads to use and slice the A matrix if needed
    //--------------------------------------------------------------------------
    // int64_t anvec = A->nvec ;
    int A_ntasks = 0 ;
    int A_nthreads = GB_nthreads (anz, chunk, nthreads_max) ;
    info = GrB_NO_VALUE ;
    bool depends_on_j = (opcode == GB_USER_idxunop_code) ;
    int64_t thunk = 0 ;
    if (GB_OPCODE_IS_POSITIONAL (opcode))
    { 
        thunk = GB_positional_offset (opcode, scalar, &depends_on_j) ;
    }
    //--------------------------------------------------------------------------
    // apply the operator
    //--------------------------------------------------------------------------
    if (GB_OPCODE_IS_POSITIONAL (opcode))
    {
        //----------------------------------------------------------------------
        // apply a positional op
        //----------------------------------------------------------------------
        ASSERT_OP_OK (op, "positional unop/idxunop: GB_apply_op", GB0) ;
        //----------------------------------------------------------------------
        // positional op via the CUDA kernel
        //----------------------------------------------------------------------
        #if defined ( GRAPHBLAS_HAS_CUDA )
        if (GB_cuda_apply_unop_branch (ctype, A, op))
        {
            info = GB_cuda_apply_unop (Cx, ctype, op, flipij, A,
                (GB_void *) &thunk) ;
        }
        #endif
        //----------------------------------------------------------------------
        // positional op via the CPU factory kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        {
            // get A and C
            GB_Ap_DECLARE (Ap, const) ; GB_Ap_PTR (Ap, A) ;
            GB_Ah_DECLARE (Ah, const) ; GB_Ah_PTR (Ah, A) ;
            GB_Ai_DECLARE (Ai, const) ; GB_Ai_PTR (Ai, A) ;
            int64_t avlen = A->vlen ;
            if (depends_on_j)
            { 
                // slice the entries for each task
                GB_SLICE_MATRIX2 (A, 32) ;
            }
            //------------------------------------------------------------------
            // Cx = positional_op (A)
            //------------------------------------------------------------------
            if (opz64)
            { 
                //--------------------------------------------------------------
                // int64 Cx = positional_op (A)
                //--------------------------------------------------------------
                int64_t *restrict Cz = (int64_t *) Cx ;
                switch (opcode)
                {
                    case GB_POSITIONI_unop_code  : // z = pos_i(A(i,j)) == i
                    case GB_POSITIONI1_unop_code : // z = pos_i1(A(i,j)) == i+1
                    case GB_ROWINDEX_idxunop_code : // z = i+thunk
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (i + thunk) ;
                        #include "apply/template/GB_apply_unop_ip_template.c"
                        break ;
                    case GB_POSITIONJ_unop_code  : // z = pos_j(A(i,j)) == j
                    case GB_POSITIONJ1_unop_code : // z = pos_j1(A(i,j)) == j+1
                    case GB_COLINDEX_idxunop_code : // z = j+thunk
                        #define GB_APPLY_OP(pC,pA)                  \
                            Cz [pC] = (j + thunk) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    case GB_DIAGINDEX_idxunop_code : // z = (j-(i+thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (j - (i+thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    case GB_FLIPDIAGINDEX_idxunop_code : // z = (i-(j+thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (i - (j+thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    default: ;
                }
            }
            else if (opz32)
            { 
                //--------------------------------------------------------------
                // int32 Cx = positional_op (A)
                //--------------------------------------------------------------
                int32_t *restrict Cz = (int32_t *) Cx ;
                switch (opcode)
                {
                    case GB_POSITIONI_unop_code  : // z = pos_i(A(i,j)) == i
                    case GB_POSITIONI1_unop_code : // z = pos_i1(A(i,j)) == i+1
                    case GB_ROWINDEX_idxunop_code : // z = i+thunk
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (int32_t) (i + thunk) ;
                        #include "apply/template/GB_apply_unop_ip_template.c"
                        break ;
                    case GB_POSITIONJ_unop_code  : // z = pos_j(A(i,j)) == j
                    case GB_POSITIONJ1_unop_code : // z = pos_j1(A(i,j)) == j+1
                    case GB_COLINDEX_idxunop_code : // z = j+thunk
                        #define GB_APPLY_OP(pC,pA)                  \
                            Cz [pC] = (int32_t) (j + thunk) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    case GB_DIAGINDEX_idxunop_code : // z = (j-(i+thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (int32_t) (j - (i+thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    case GB_FLIPDIAGINDEX_idxunop_code : // z = (i-(j+thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (int32_t) (i - (j+thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ;
                    default: ;
                }
            }
            else
            { 
                //--------------------------------------------------------------
                // bool Cx = positional_op (A)
                //--------------------------------------------------------------
                ASSERT (op->ztype == GrB_BOOL) ;
                bool *restrict Cz = (bool *) Cx ;
                switch (opcode)
                {
                    case GB_TRIL_idxunop_code : // z = (j <= (i+thunk))
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (j <= (i + thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_TRIU_idxunop_code : // z = (j >= (i+thunk))
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (j >= (i + thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_DIAG_idxunop_code : // z = (j == (i+thunk))
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (j == (i + thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_OFFDIAG_idxunop_code : // z = (j != (i+thunk))
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (j != (i + thunk)) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_COLLE_idxunop_code : // z = (j <= thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            Cz [pC] = (j <= thunk) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_COLGT_idxunop_code : // z = (j > thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            Cz [pC] = (j > thunk) ;
                        #include "apply/template/GB_apply_unop_ijp_template.c"
                        break ; ;
                    case GB_ROWLE_idxunop_code : // z = (i <= thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (i <= thunk) ;
                        #include "apply/template/GB_apply_unop_ip_template.c"
                        break ; ;
                    case GB_ROWGT_idxunop_code : // z = (i > thunk)
                        #define GB_APPLY_OP(pC,pA)                  \
                            int64_t i = GBi_A (Ai, pA, avlen) ;     \
                            Cz [pC] = (i > thunk) ;
                        #include "apply/template/GB_apply_unop_ip_template.c"
                        break ; ;
                    default: ;
                }
            }
            info = GrB_SUCCESS ;
        }
    }
    else if (C_code_iso != GB_NON_ISO)
    {
        //----------------------------------------------------------------------
        // via the iso kernel, in O(1) time
        //----------------------------------------------------------------------
        GBURBLE ("(iso apply) ") ;
        ASSERT_MATRIX_OK (A, "A passing to GB_unop_iso", GB0) ;
        if (anz > 0)
        { 
            // Cx [0] = unop (A), binop (scalar,A), or binop (A,scalar)
            GB_unop_iso (Cx, ctype, C_code_iso, op, A, scalar) ;
        }
        info = GrB_SUCCESS ;
    }
    else if (op_is_unop)
    {
        //----------------------------------------------------------------------
        // apply a unary op
        //----------------------------------------------------------------------
        ASSERT_OP_OK (op, "unop for GB_apply_op", GB0) ;
        ASSERT (!A->iso) ;
        //----------------------------------------------------------------------
        // unary op via the CUDA kernel
        //----------------------------------------------------------------------
        #if defined ( GRAPHBLAS_HAS_CUDA )
        if (GB_cuda_apply_unop_branch (ctype, A, op))
        {
            info = GB_cuda_apply_unop (Cx, ctype, op, flipij, A, NULL) ;
        }
        #endif
        //----------------------------------------------------------------------
        // unary op via the factory kernel
        //----------------------------------------------------------------------
        #ifndef GBCOMPACT
        if (info == GrB_NO_VALUE)
        {
            GB_IF_FACTORY_KERNELS_ENABLED
            { 
                if (Atype == op->xtype || opcode == GB_IDENTITY_unop_code)
                { 
                    // The switch factory is used if the op is IDENTITY, or if
                    // no typecasting.  IDENTITY operator can do arbitrary
                    // typecasting (it is not used if no typecasting is done).
                    //----------------------------------------------------------
                    // define the worker for the switch factory
                    //----------------------------------------------------------
                    #define GB_uop_apply(unop,zname,aname) \
                        GB (_uop_apply_ ## unop ## zname ## aname)
                    #define GB_WORKER(unop,zname,ztype,aname,atype)          \
                    {                                                        \
                        info = GB_uop_apply (unop,zname,aname) (Cx, Ax, Ab,  \
                            anz, A_nthreads) ;                               \
                    }                                                        \
                    break ;
                    //----------------------------------------------------------
                    // launch the switch factory
                    //----------------------------------------------------------
                    #include "apply/factory/GB_unop_factory.c"
                }
            }
        }
        #endif
        //----------------------------------------------------------------------
        // unary op via the JIT or PreJIT kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        { 
            info = GB_apply_unop_jit (Cx, ctype, op, flipij, A,
                NULL, NULL, 0, A_nthreads) ;
        }
        //----------------------------------------------------------------------
        // unary op via the generic kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        { 
            GB_BURBLE_N (anz, "(generic unop apply: %s) ", op->name) ;
            size_t asize = Atype->size ;
            size_t zsize = op->ztype->size ;
            size_t xsize = op->xtype->size ;
            GB_Type_code acode = Atype->code ;
            GB_Type_code xcode = op->xtype->code ;
            GB_cast_function cast_A_to_X = GB_cast_factory (xcode, acode) ;
            GxB_unary_function fop = op->unop_function ;
            #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) */                     \
                fop (Cx +((pC)*zsize), xwork) ;
            #include "apply/template/GB_apply_unop_ip_template.c"
            info = GrB_SUCCESS ;
        }
    }
    else if (op_is_binop)
    { 
        //----------------------------------------------------------------------
        // apply a binary operator (bound to a scalar)
        //----------------------------------------------------------------------
        ASSERT_OP_OK (op, "standard binop for GB_apply_op", GB0) ;
        ASSERT_SCALAR_OK (scalar, "scalar for GB_apply_op", 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) ;
        ASSERT (opcode != GB_USER_idxbinop_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 ;
        }
        if (binop_bind1st)
        {
            //------------------------------------------------------------------
            // binary op (bind 1st) via the CUDA kernel
            //------------------------------------------------------------------
            #if defined ( GRAPHBLAS_HAS_CUDA )
            if (GB_cuda_apply_binop_branch (ctype, (GrB_BinaryOp) op, A))
            {
                info = GB_cuda_apply_binop (Cx, ctype, (GrB_BinaryOp) op, A,
                    scalarx, true) ;
            }
            #endif
            //------------------------------------------------------------------
            // binary op (bind 1st) via the CPU factory kernel
            //------------------------------------------------------------------
            #ifndef GBCOMPACT
            if (info == GrB_NO_VALUE)
            {
                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(binop,xname) \
                            GB (_bind1st_ ## binop ## xname)
                        #define GB_BINOP_WORKER(binop,xname)                   \
                        {                                                      \
                            info = GB_bind1st (binop, xname) (Cx, scalarx, Ax, \
                                Ab, anz, A_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
            //------------------------------------------------------------------
            // binary op (bind 1st) via the JIT or PreJIT kernel
            //------------------------------------------------------------------
            if (info == GrB_NO_VALUE)
            { 
                info = GB_apply_bind1st_jit (Cx, ctype,
                    (GrB_BinaryOp) op, scalarx, A, A_nthreads) ;
            }
        }
        else
        {
            //------------------------------------------------------------------
            // binary op (bind 2nd) via the CUDA kernel
            //------------------------------------------------------------------
            #if defined ( GRAPHBLAS_HAS_CUDA )
            if (GB_cuda_apply_binop_branch (ctype, (GrB_BinaryOp) op, A))
            {
                info = GB_cuda_apply_binop (Cx, ctype, (GrB_BinaryOp) op, A,
                scalarx, false) ;
            }
            #endif
            //------------------------------------------------------------------
            // binary op (bind 2nd) via the CPU factory kernel
            //------------------------------------------------------------------
            #ifndef GBCOMPACT
            if (info == GrB_NO_VALUE)
            {
                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(binop,xname) \
                            GB (_bind2nd_ ## binop ## xname)
                        #undef  GB_BINOP_WORKER
                        #define GB_BINOP_WORKER(binop,xname)                  \
                        {                                                     \
                            info = GB_bind2nd (binop, xname) (Cx, Ax, scalarx,\
                                Ab, anz, A_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
            //------------------------------------------------------------------
            // binary op (bind 2nd) via the JIT or PreJIT kernel
            //------------------------------------------------------------------
            if (info == GrB_NO_VALUE)
            { 
                info = GB_apply_bind2nd_jit (Cx, ctype,
                    (GrB_BinaryOp) op, A, scalarx, A_nthreads) ;
            }
        }
        //----------------------------------------------------------------------
        // binary op (bind 1st or 2nd) via the generic kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        {
            GB_BURBLE_N (anz, "(generic binop apply: %s) ", op->name) ;
            GB_Type_code acode = Atype->code ;
            GxB_binary_function fop = op->binop_function ;
            ASSERT (fop != NULL) ;
            ASSERT (!A->iso) ;
            if (binop_bind1st)
            { 
                // Cx = binop (scalar,Ax) with bind1st
                GB_cast_function cast_A_to_Y = GB_cast_factory (ycode, acode) ;
                #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 (scalarx, ywork) */            \
                    fop (Cx +((pC)*zsize), scalarx, ywork) ;
                #include "apply/template/GB_apply_unop_ip_template.c"
            }
            else
            { 
                // Cx = binop (Ax,scalar) with bind2nd
                GB_cast_function cast_A_to_X = GB_cast_factory (xcode, acode) ;
                #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, scalarx) */            \
                    fop (Cx +((pC)*zsize), xwork, scalarx) ;
                #include "apply/template/GB_apply_unop_ip_template.c"
            }
            info = GrB_SUCCESS ;
        }
    }
    else
    { 
        //----------------------------------------------------------------------
        // apply a user-defined index_unary op
        //----------------------------------------------------------------------
        // All valued GrB_IndexUnaryOps (GrB_VALUE*) have already been
        // renamed to their corresponding binary op (GrB_VALUEEQ_FP32
        // became GrB_EQ_FP32, for example).  The only remaining index
        // unary ops are positional, and user-defined.  Positional ops have
        // been handled above, so only user-defined index unary ops are
        // left.
        ASSERT (opcode == GB_USER_idxunop_code) ;
        size_t ssize = scalar->type->size ;
        size_t ysize = op->ytype->size ;
        GB_Type_code scalar_code = scalar->type->code ;
        GB_Type_code ycode = op->ytype->code ;
        GB_void ywork [GB_VLA(ysize)] ;
        GB_void *ythunk = (GB_void *) scalar->x ;
        if (ycode != scalar_code)
        { 
            // typecast the scalar to the operator input, in ywork
            GB_cast_function cast_s = GB_cast_factory (ycode, scalar_code) ;
            cast_s (ywork, scalar->x, ssize) ;
            ythunk = ywork ;
        }
        //----------------------------------------------------------------------
        // user-defined index-unary op via the CUDA kernel
        //----------------------------------------------------------------------
        #if defined ( GRAPHBLAS_HAS_CUDA )
        if (GB_cuda_apply_unop_branch (ctype, A, op))
        {
            info = GB_cuda_apply_unop (Cx, ctype, op, flipij, A, ythunk) ;
        }
        #endif
        //----------------------------------------------------------------------
        // user-defined index-unary op via the JIT or PreJIT kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        { 
            if (depends_on_j)
            { 
                // slice the entries for each task
                GB_SLICE_MATRIX2 (A, 32) ;
            }
            info = GB_apply_unop_jit (Cx, ctype, op, flipij, A,
                ythunk, A_ek_slicing, A_ntasks, A_nthreads) ;
        }
        //----------------------------------------------------------------------
        // user-defined index-unary op via the generic kernel
        //----------------------------------------------------------------------
        if (info == GrB_NO_VALUE)
        { 
            GB_BURBLE_N (anz, "(generic apply: user-defined idxunop) ") ;
            // get A and C
            GB_Ap_DECLARE (Ap, const) ; GB_Ap_PTR (Ap, A) ;
            GB_Ah_DECLARE (Ah, const) ; GB_Ah_PTR (Ah, A) ;
            GB_Ai_DECLARE (Ai, const) ; GB_Ai_PTR (Ai, A) ;
            int64_t avlen = A->vlen ;
            GB_Type_code acode = Atype->code ;
            // A can be iso-valued, but C is not
            bool A_iso = A->iso ;
            ASSERT (C_code_iso == GB_NON_ISO) ;
            if (A_iso)
            { 
                GB_BURBLE_N (anz, "(A iso; C non-iso) ") ;
            }
            GxB_index_unary_function fop = op->idxunop_function ;
            size_t asize = Atype->size ;
            size_t zsize = op->ztype->size ;
            size_t xsize = op->xtype->size ;
            GB_Type_code xcode = op->xtype->code ;
            GB_cast_function cast_A_to_X = GB_cast_factory (xcode, acode) ;
            // Cx [pC] = op (Ax [A_iso ? 0 : pA], i, j, ythunk)
            #define GB_APPLY_OP(pC,pA)                                      \
                int64_t i = GBi_A (Ai, pA, avlen) ;                         \
                GB_void xwork [GB_VLA(xsize)] ;                             \
                cast_A_to_X (xwork, Ax +(A_iso ? 0 : (pA))*asize, asize) ;  \
                fop (Cx +((pC)*zsize), xwork,                               \
                    flipij ? j : i, flipij ? i : j, ythunk) ;
            #include "apply/template/GB_apply_unop_ijp_template.c"
            info = GrB_SUCCESS ;
        }
    }
    //--------------------------------------------------------------------------
    // free workspace and return result
    //--------------------------------------------------------------------------
    GB_FREE_ALL ;
    return (info) ;
}
 |