File: GB_sort.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 (867 lines) | stat: -rw-r--r-- 32,166 bytes parent folder | download
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
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
//------------------------------------------------------------------------------
// GB_sort: sort all vectors in a matrix
//------------------------------------------------------------------------------

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

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

#include "sort/GB_sort.h"
#include "transpose/GB_transpose.h"
#include "jitifyer/GB_stringify.h"

//  macros:

//  GB_SORT (func)      defined as GB_sort_XX_func_TYPE_ascend or _descend,
//                      or GB_sort_XX_func_UDT where XX = 32 or 64.
//  GB_C_TYPE           bool, int8_, ... or GB_void for UDT
//  GB_Ci_TYPE          the type of C->i (uint32_t or uint64_t)
//  GB_ADDR(A,p)        A+p for builtin, A + p * GB_SIZE otherwise
//  GB_SIZE             size of each entry: sizeof (GB_C_TYPE) for built-in
//  GB_GETX(x,X,i)      x = (op->xtype) X [i]
//  GB_COPY(A,i,C,k)    A [i] = C [k]
//  GB_SWAP(A,i,k)      swap A [i] and A [k]
//  GB_LT               compare two entries, x < y

//------------------------------------------------------------------------------
// macros for all built-in types
//------------------------------------------------------------------------------

#define GB_SORT_UDT         0
#define GB_ADDR(A,i)        ((A) + (i))
#define GB_GETX(x,A,i)      GB_C_TYPE x = A [i]
#define GB_COPY(A,i,B,j)    A [i] = B [j]
#define GB_SIZE             sizeof (GB_C_TYPE)
#define GB_SWAP(A,i,j)      \
{                           \
    GB_C_TYPE t = A [i] ;   \
    A [i] = A [j] ;         \
    A [j] = t ;             \
}

//------------------------------------------------------------------------------
// ascending sort for built-in types
//------------------------------------------------------------------------------

#define GB_LT(less,a,i,b,j)  \
    less = (((a) < (b)) ? true : (((a) == (b)) ? ((i) < (j)) : false))

#define GB_Ci_TYPE          uint32_t

#define GB_C_TYPE           bool
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_BOOL)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_INT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_INT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_INT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_INT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_UINT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_UINT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_UINT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_UINT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           float
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_FP32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           double
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _ascend_FP64)
#include "sort/template/GB_sort_template.c"

#undef  GB_Ci_TYPE
#define GB_Ci_TYPE          uint64_t

#define GB_C_TYPE           bool
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_BOOL)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_INT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_INT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_INT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_INT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_UINT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_UINT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_UINT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_UINT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           float
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_FP32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           double
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _ascend_FP64)
#include "sort/template/GB_sort_template.c"


//------------------------------------------------------------------------------
// descending sort for built-in types
//------------------------------------------------------------------------------

#undef  GB_LT
#define GB_LT(less,a,i,b,j)  \
    less = (((a) > (b)) ? true : (((a) == (b)) ? ((i) < (j)) : false))

#undef  GB_Ci_TYPE
#define GB_Ci_TYPE          uint32_t

#define GB_C_TYPE           bool
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_BOOL)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_INT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_INT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_INT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_INT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_UINT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_UINT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_UINT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_UINT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           float
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_FP32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           double
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _descend_FP64)
#include "sort/template/GB_sort_template.c"

#undef  GB_Ci_TYPE
#define GB_Ci_TYPE          uint64_t

#define GB_C_TYPE           bool
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_BOOL)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_INT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_INT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_INT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           int64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_INT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint8_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_UINT8)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint16_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_UINT16)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint32_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_UINT32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           uint64_t
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_UINT64)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           float
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_FP32)
#include "sort/template/GB_sort_template.c"

#define GB_C_TYPE           double
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _descend_FP64)
#include "sort/template/GB_sort_template.c"

//------------------------------------------------------------------------------
// macros for the generic kernel
//------------------------------------------------------------------------------

#undef  GB_ADDR
#undef  GB_GETX
#undef  GB_COPY
#undef  GB_SIZE
#undef  GB_SWAP
#undef  GB_LT

#define GB_ADDR(A,i)        ((A) + (i) * csize)
#define GB_GETX(x,A,i)      GB_void x [GB_VLA(xsize)] ;                     \
                            fcast (x, GB_ADDR (A, i), csize)
#define GB_COPY(A,i,B,j)    memcpy (GB_ADDR (A, i), GB_ADDR (B, j), csize)
#define GB_SIZE             csize

#define GB_SWAP(A,i,j)                                                      \
{                                                                           \
    GB_void t [GB_VLA(csize)] ;         /* declare the scalar t */          \
    memcpy (t, GB_ADDR (A, i), csize) ; /* t = A [i] */                     \
    GB_COPY (A, i, A, j) ;              /* A [i] = A [j] */                 \
    memcpy (GB_ADDR (A, j), t, csize) ; /* A [j] = t */                     \
}

#define GB_LT(less,a,i,b,j)                                                 \
{                                                                           \
    flt (&less, a, b) ;         /* less = (a < b) */                        \
    if (!less)                                                              \
    {                                                                       \
        /* check for equality and tie-break on index */                     \
        bool more ;                                                         \
        flt (&more, b, a) ;     /* more = (b < a) */                        \
        less = (more) ? false : ((i) < (j)) ;                               \
    }                                                                       \
}

#undef  GB_SORT_UDT
#define GB_SORT_UDT 1

#undef  GB_Ci_TYPE
#define GB_Ci_TYPE          uint32_t
#define GB_C_TYPE           GB_void
#define GB_SORT(func)       GB_EVAL3 (GB(sort_32_), func, _UDT)
#include "sort/template/GB_sort_template.c"

#undef  GB_Ci_TYPE
#define GB_Ci_TYPE          uint64_t
#define GB_C_TYPE           GB_void
#define GB_SORT(func)       GB_EVAL3 (GB(sort_64_), func, _UDT)
#include "sort/template/GB_sort_template.c"

//------------------------------------------------------------------------------
// GB_sort
//------------------------------------------------------------------------------

#undef  GB_FREE_WORKSPACE
#define GB_FREE_WORKSPACE                   \
{                                           \
    GB_WERK_POP (C_ek_slicing, int64_t) ;   \
    GB_Matrix_free (&T) ;                   \
}

#undef  GB_FREE_ALL
#define GB_FREE_ALL                         \
{                                           \
    GB_FREE_WORKSPACE ;                     \
    if (!C_is_NULL) GB_phybix_free (C) ;    \
    GB_phybix_free (P) ;                    \
}

GrB_Info GB_sort
(
    // output:
    GrB_Matrix C,               // matrix with sorted vectors on output
    GrB_Matrix P,               // matrix with permutations on output
    // input:
    GrB_BinaryOp op,            // comparator for the sort
    GrB_Matrix A,               // matrix to sort
    const bool A_transpose,     // false: sort each row, true: sort each column
    GB_Werk Werk
)
{

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

    GrB_Info info ;
    ASSERT_MATRIX_OK (A, "A for GB_sort", GB0) ;
    ASSERT_BINARYOP_OK (op, "op for GB_sort", GB0) ;

    GrB_Matrix T = NULL ;
    struct GB_Matrix_opaque T_header ;
    GB_WERK_DECLARE (C_ek_slicing, int64_t) ;

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

    bool C_is_NULL = (C == NULL) ;
    if (C_is_NULL && P == NULL)
    { 
        // either C, or P, or both must be present
        return (GrB_NULL_POINTER) ;
    }

    GrB_Type atype = A->type ;
    GrB_Type ctype = (C_is_NULL) ? atype : C->type ;
    GrB_Type ptype = (P == NULL) ? NULL : P->type ;
    bool ptype_ok = true ;

    if (ptype != NULL)
    { 
        // if present, P must have a int32, uint32, int64, uint64 data type
        // that is large enough to hold the row/column indices of C
        int64_t n = GB_IMAX (A->vlen, A->vdim) ;
        int64_t nmax = -1 ;
        switch (ptype->code)
        {
            case GB_INT32_code  : nmax =  INT32_MAX ; break ;
            case GB_UINT32_code : nmax = UINT32_MAX ; break ;
            case GB_INT64_code  : nmax =  INT64_MAX ; break ;
            case GB_UINT64_code : nmax =  INT64_MAX ; break ;
            default             : nmax = -1         ; break ;
        }
        ptype_ok = (n <= nmax) ;
    }

    if (op->ztype != GrB_BOOL || op->xtype != op->ytype || atype != ctype
        || !ptype_ok
        || !GB_Type_compatible (atype, op->xtype)
        || GB_IS_INDEXBINARYOP_CODE (op->opcode))
    { 
        // op must return bool, and its inputs x and y must have the same type.
        // The types of A and C must match exactly.  P must have a valid data
        // type if present.  A and C must be typecasted to the input type of
        // the op.  Positional ops are not allowed.
        return (GrB_DOMAIN_MISMATCH) ;
    }

    int64_t anrows = GB_NROWS (A) ;
    int64_t ancols = GB_NCOLS (A) ;
    if ((C != NULL && (GB_NROWS (C) != anrows || GB_NCOLS (C) != ancols)) ||
        (P != NULL && (GB_NROWS (P) != anrows || GB_NCOLS (P) != ancols)))
    { 
        // C and P must have the same dimensions as A
        return (GrB_DIMENSION_MISMATCH) ;
    }

    bool A_iso = A->iso ;
    bool sort_in_place = (A == C) ;

    // free any prior content of C and P
    if (A == P)
    { 
        return (GrB_NOT_IMPLEMENTED) ;  // A and P cannot be aliased
    }
    GB_phybix_free (P) ;    // this frees A if A == P are aliased
    if (!sort_in_place)
    { 
        GB_phybix_free (C) ;
    }

    //--------------------------------------------------------------------------
    // make a copy of A, unless it is aliased with C
    //--------------------------------------------------------------------------

    if (C_is_NULL)
    { 
        // C is a temporary matrix, which is freed when done
        GB_CLEAR_MATRIX_HEADER (T, &T_header) ;
        C = T ;
    }

    if (A_transpose)
    {
        // ensure C is in sparse or hypersparse CSC format
        if (A->is_csc)
        {
            // A is already CSC
            if (!sort_in_place)
            { 
                // C = A
                GB_OK (GB_dup_worker (&C, A_iso, A, true, atype)) ;
            }
        }
        else
        {
            // A is CSR but C must be CSC
            if (sort_in_place)
            { 
                // C = C'
                GB_OK (GB_transpose_in_place (C, true, Werk)) ;
            }
            else
            { 
                // C = A'
                GB_OK (GB_transpose_cast (C, atype, true, A, false, Werk)) ;
            }
        }
    }
    else
    {
        // ensure C is in sparse or hypersparse CSR format
        if (!A->is_csc)
        {
            // A is already CSR
            if (!sort_in_place)
            { 
                // C = A
                GB_OK (GB_dup_worker (&C, A_iso, A, true, atype)) ;
            }
        }
        else
        {
            // A is CSC but C must be CSR
            if (sort_in_place)
            { 
                // C = C'
                GB_OK (GB_transpose_in_place (C, false, Werk)) ;
            }
            else
            { 
                // C = A'
                GB_OK (GB_transpose_cast (C, atype, false, A, false, Werk)) ;
            }
        }
    }

    // ensure C is sparse or hypersparse
    if (GB_IS_BITMAP (C) || GB_IS_FULL (C))
    { 
        GB_OK (GB_convert_any_to_sparse (C, Werk)) ;
    }

    //--------------------------------------------------------------------------
    // sort C in place
    //--------------------------------------------------------------------------

    int64_t cnz = GB_nnz (C) ;
    int nthreads = GB_nthreads (cnz, chunk, nthreads_max) ;

    GB_Opcode opcode = op->opcode ;
    GB_Type_code acode = atype->code ;

    ASSERT_MATRIX_OK (C, "C to sort", GB0) ;

    if (C->iso || cnz <= 1)
    { 

        //----------------------------------------------------------------------
        // C is iso: nothing to do
        //----------------------------------------------------------------------

        ;

    }
    else if ((op->xtype == atype) && (op->ytype == atype) &&
        (opcode == GB_LT_binop_code || opcode == GB_GT_binop_code) &&
        (acode < GB_UDT_code))
    {

        //----------------------------------------------------------------------
        // no typecasting, using built-in < or > operators, builtin types
        //----------------------------------------------------------------------

        if (opcode == GB_LT_binop_code && C->i_is_32)
        { 
            // ascending sort, 32-bit integers
            switch (acode)
            {
                case GB_BOOL_code : 
                    GB_OK (GB(sort_32_mtx_ascend_BOOL   )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT8_code : 
                    GB_OK (GB(sort_32_mtx_ascend_INT8   )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT16_code : 
                    GB_OK (GB(sort_32_mtx_ascend_INT16  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT32_code : 
                    GB_OK (GB(sort_32_mtx_ascend_INT32  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT64_code : 
                    GB_OK (GB(sort_32_mtx_ascend_INT64  )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT8_code : 
                    GB_OK (GB(sort_32_mtx_ascend_UINT8  )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT16_code : 
                    GB_OK (GB(sort_32_mtx_ascend_UINT16 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT32_code : 
                    GB_OK (GB(sort_32_mtx_ascend_UINT32 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT64_code : 
                    GB_OK (GB(sort_32_mtx_ascend_UINT64 )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP32_code : 
                    GB_OK (GB(sort_32_mtx_ascend_FP32   )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP64_code : 
                    GB_OK (GB(sort_32_mtx_ascend_FP64   )(C, nthreads, Werk)) ;
                    break ;
                default:;
            }
        }
        else if (opcode == GB_GT_binop_code && C->i_is_32)
        { 
            // descending sort
            switch (acode)
            {
                case GB_BOOL_code : 
                    GB_OK (GB(sort_32_mtx_descend_BOOL  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT8_code : 
                    GB_OK (GB(sort_32_mtx_descend_INT8  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT16_code : 
                    GB_OK (GB(sort_32_mtx_descend_INT16 )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT32_code : 
                    GB_OK (GB(sort_32_mtx_descend_INT32 )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT64_code : 
                    GB_OK (GB(sort_32_mtx_descend_INT64 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT8_code : 
                    GB_OK (GB(sort_32_mtx_descend_UINT8 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT16_code : 
                    GB_OK (GB(sort_32_mtx_descend_UINT16)(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT32_code : 
                    GB_OK (GB(sort_32_mtx_descend_UINT32)(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT64_code : 
                    GB_OK (GB(sort_32_mtx_descend_UINT64)(C, nthreads, Werk)) ;
                    break ;
                case GB_FP32_code : 
                    GB_OK (GB(sort_32_mtx_descend_FP32  )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP64_code : 
                    GB_OK (GB(sort_32_mtx_descend_FP64  )(C, nthreads, Werk)) ;
                    break ;
                default:;
            }
        }
        else if (opcode == GB_LT_binop_code && !C->i_is_32)
        { 
            // ascending sort, 64-bit integers
            switch (acode)
            {
                case GB_BOOL_code : 
                    GB_OK (GB(sort_64_mtx_ascend_BOOL   )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT8_code : 
                    GB_OK (GB(sort_64_mtx_ascend_INT8   )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT16_code : 
                    GB_OK (GB(sort_64_mtx_ascend_INT16  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT32_code : 
                    GB_OK (GB(sort_64_mtx_ascend_INT32  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT64_code : 
                    GB_OK (GB(sort_64_mtx_ascend_INT64  )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT8_code : 
                    GB_OK (GB(sort_64_mtx_ascend_UINT8  )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT16_code : 
                    GB_OK (GB(sort_64_mtx_ascend_UINT16 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT32_code : 
                    GB_OK (GB(sort_64_mtx_ascend_UINT32 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT64_code : 
                    GB_OK (GB(sort_64_mtx_ascend_UINT64 )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP32_code : 
                    GB_OK (GB(sort_64_mtx_ascend_FP32   )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP64_code : 
                    GB_OK (GB(sort_64_mtx_ascend_FP64   )(C, nthreads, Werk)) ;
                    break ;
                default:;
            }
        }
        else // if (opcode == GB_GT_binop_code && !C->i_is_32)
        { 
            // descending sort
            switch (acode)
            {
                case GB_BOOL_code : 
                    GB_OK (GB(sort_64_mtx_descend_BOOL  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT8_code : 
                    GB_OK (GB(sort_64_mtx_descend_INT8  )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT16_code : 
                    GB_OK (GB(sort_64_mtx_descend_INT16 )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT32_code : 
                    GB_OK (GB(sort_64_mtx_descend_INT32 )(C, nthreads, Werk)) ;
                    break ;
                case GB_INT64_code : 
                    GB_OK (GB(sort_64_mtx_descend_INT64 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT8_code : 
                    GB_OK (GB(sort_64_mtx_descend_UINT8 )(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT16_code : 
                    GB_OK (GB(sort_64_mtx_descend_UINT16)(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT32_code : 
                    GB_OK (GB(sort_64_mtx_descend_UINT32)(C, nthreads, Werk)) ;
                    break ;
                case GB_UINT64_code : 
                    GB_OK (GB(sort_64_mtx_descend_UINT64)(C, nthreads, Werk)) ;
                    break ;
                case GB_FP32_code : 
                    GB_OK (GB(sort_64_mtx_descend_FP32  )(C, nthreads, Werk)) ;
                    break ;
                case GB_FP64_code : 
                    GB_OK (GB(sort_64_mtx_descend_FP64  )(C, nthreads, Werk)) ;
                    break ;
                default:;
            }
        }

    }
    else
    { 

        //----------------------------------------------------------------------
        // typecasting, user-defined types, or unconventional operators
        //----------------------------------------------------------------------

        // via the JIT kernel
        info = GB_sort_jit (C, op, nthreads, Werk) ;

        // via the generic kernel
        if (info == GrB_NO_VALUE)
        {
            if (C->i_is_32)
            { 
                info = GB (sort_32_mtx_UDT) (C, op, nthreads, Werk) ;
            }
            else
            { 
                info = GB (sort_64_mtx_UDT) (C, op, nthreads, Werk) ;
            }
        }

        GB_OK (info) ;
    }

    //--------------------------------------------------------------------------
    // constuct the final indices
    //--------------------------------------------------------------------------

    int64_t cnvec = C->nvec ;
    GB_MDECL (Ti, ,) ;
    bool Ti_is_32 ;

    if (P == NULL)
    { 
        // P is not constructed; use C->i to construct the new indices
        ASSERT (!C_is_NULL) ;
        Ti = C->i ;
        Ti_is_32 = C->i_is_32 ;
    }
    else
    {
        // allocate P->i and use it to construct the new indices
        size_t pisize = P->i_is_32 ? sizeof (uint32_t) : sizeof (uint64_t) ;
        P->i = GB_MALLOC_MEMORY (cnz, pisize, &(P->i_size)) ;
        if (P->i == NULL)
        { 
            // out of memory
            GB_FREE_ALL ;
            return (GrB_OUT_OF_MEMORY) ;
        }
        Ti = P->i ;
        Ti_is_32 = P->i_is_32 ;
    }

    GB_IPTR (Ti, Ti_is_32) ;

    int C_nthreads, C_ntasks ;
    GB_SLICE_MATRIX (C, 1) ;
    GB_Cp_DECLARE (Cp, ) ; GB_Cp_PTR (Cp, C) ;

    int tid ;
    #pragma omp parallel for num_threads(C_nthreads) schedule(static,1)
    for (tid = 0 ; tid < C_ntasks ; tid++)
    {
        int64_t kfirst = kfirst_Cslice [tid] ;
        int64_t klast  = klast_Cslice  [tid] ;
        for (int64_t k = kfirst ; k <= klast ; k++)
        {
            const int64_t pC0 = GB_IGET (Cp, k) ;
            GB_GET_PA (pC_start, pC_end, tid, k, kfirst, klast, pstart_Cslice,
                pC0, GB_IGET (Cp, k+1)) ;
            for (int64_t pC = pC_start ; pC < pC_end ; pC++)
            { 
                int64_t i = pC - pC0 ;
                GB_ISET (Ti, pC, i) ;   // Ti [pC] = i
            }
        }
    }

    //--------------------------------------------------------------------------
    // construct P
    //--------------------------------------------------------------------------

    bool C_is_hyper = GB_IS_HYPERSPARSE (C) ;
    size_t cisize = (C->i_is_32) ? sizeof (uint32_t) : sizeof (uint64_t) ;
    GB_Type_code cpcode = (C->p_is_32) ? GB_UINT32_code : GB_UINT64_code ;
    GB_Type_code cjcode = (C->j_is_32) ? GB_UINT32_code : GB_UINT64_code ;
    GB_Type_code cicode = (C->i_is_32) ? GB_UINT32_code : GB_UINT64_code ;
    GB_Type_code picode, pjcode ;

    if (P != NULL)
    {
        P->is_csc = C->is_csc ;
        P->nvec = C->nvec ;
//      P->nvec_nonempty = C->nvec_nonempty ;
        GB_nvec_nonempty_set (P, GB_nvec_nonempty_get (C)) ;
        P->iso = false ;
        P->vlen = C->vlen ;
        P->vdim = C->vdim ;

        size_t ppsize = (P->p_is_32) ? sizeof (uint32_t) : sizeof (uint64_t) ;
        size_t pjsize = (P->j_is_32) ? sizeof (uint32_t) : sizeof (uint64_t) ;
        size_t pxsize = ptype->size ;
        GB_Type_code pxcode = ptype->code ;
        GB_Type_code ppcode = (P->p_is_32) ? GB_UINT32_code : GB_UINT64_code ;
        pjcode = (P->j_is_32) ? GB_UINT32_code : GB_UINT64_code ;
        picode = (P->i_is_32) ? GB_UINT32_code : GB_UINT64_code ;

        if (C_is_NULL && pxsize == cisize &&
            P->p_is_32 == C->p_is_32 &&
            P->j_is_32 == C->j_is_32 &&
            P->i_is_32 == C->i_is_32)
        { 
            // C is a temporary matrix T, and its contents are not needed.  The
            // indices of C become the values of P, Cp becomes Pp, and Ch (if
            // present) becomes Ph.
            P->x = C->i ; C->i = NULL ; P->x_size = C->i_size ;
            P->p = C->p ; C->p = NULL ; P->p_size = C->p_size ;
            P->h = C->h ; C->h = NULL ; P->h_size = C->h_size ;
            P->plen = C->plen ;
        }
        else
        {
            // C is required on output, or it has integers of the wrong size.
            // The indices of C are copied and become the values of P.  Cp is
            // copied to Pp, and Ch (if present) is copied to Ph.
            int64_t pplen = GB_IMAX (1, cnvec) ;
            P->plen = pplen ;
            P->x = GB_MALLOC_MEMORY (cnz, pxsize, &(P->x_size)) ;
            P->p = GB_MALLOC_MEMORY (pplen+1, ppsize, &(P->p_size)) ;
            P->h = NULL ;
            if (C_is_hyper)
            { 
                P->h = GB_MALLOC_MEMORY (pplen, pjsize, &(P->h_size)) ;
            }
            if (P->x == NULL || P->p == NULL || (C_is_hyper && P->h == NULL))
            { 
                // out of memory
                GB_FREE_ALL ;
                return (GrB_OUT_OF_MEMORY) ;
            }

            // copy from C to P
//          GB_memcpy (P->x, C->i, cnz * sizeof (int64_t), nthreads_max) ;
            GB_cast_int (P->x, pxcode, C->i, cicode, cnz, nthreads_max) ;
//          GB_memcpy (P->p, C->p, (cnvec+1) * sizeof (int64_t), nthreads_max) ;
            GB_cast_int (P->p, ppcode, C->p, cpcode, cnvec+1, nthreads_max) ;
            if (C_is_hyper)
            { 
//              GB_memcpy (P->h, C->h, cnvec * sizeof (int64_t), nthreads_max) ;
                GB_cast_int (P->h, pjcode, C->h, cjcode, cnvec, nthreads_max) ;
            }
        }

        P->nvals = cnz ;
        P->magic = GB_MAGIC ;
    }

    //--------------------------------------------------------------------------
    // finalize the pattern of C
    //--------------------------------------------------------------------------

    if (!C_is_NULL && P != NULL)
    { 
        // copy P->i into C->i
//      GB_memcpy (C->i, P->i, cnz * sizeof (int64_t), nthreads_max) ;
        GB_cast_int (C->i, cicode, P->i, picode, cnz, nthreads_max) ;
    }

    //--------------------------------------------------------------------------
    // free workspace, and comform/return result
    //--------------------------------------------------------------------------

    GB_FREE_WORKSPACE ;

    if (!C_is_NULL)
    { 
        GB_OK (GB_conform (C, Werk)) ;
        ASSERT_MATRIX_OK (C, "C output of GB_sort", GB0) ;
    }
    if (P != NULL)
    { 
        GB_OK (GB_conform (P, Werk)) ;
        ASSERT_MATRIX_OK (P, "P output of GB_sort", GB0) ;
    }
    return (GrB_SUCCESS) ;
}