File: GB_cast_array.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 (122 lines) | stat: -rw-r--r-- 4,433 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
//------------------------------------------------------------------------------
// GB_cast_array: typecast an array
//------------------------------------------------------------------------------

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

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

// Casts an input array A->x to an output array Cx with a different type.  The
// two types are always different, so this does not need to handle user-defined
// types.  The iso case is not handled; A->x and Cx must be the same size and no
// iso expansion is done.

#include "GB.h"
#include "apply/GB_apply.h"
#include "jitifyer/GB_stringify.h"
#ifndef GBCOMPACT
#include "FactoryKernels/GB_uop__include.h"
#endif

GrB_Info GB_cast_array              // typecast an array
(
    GB_void *Cx,                // output array
    const GB_Type_code ccode,   // type code for Cx
    GrB_Matrix A,
    const int A_nthreads        // number of threads to use
)
{

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

    ASSERT_MATRIX_OK (A, "A for cast_array", GB0) ;
    const GB_void *restrict Ax = A->x ;
    const int8_t *restrict Ab = A->b ;
    const int64_t anz = GB_nnz_held (A) ;
    const GB_Type_code acode = A->type->code ;

    if (anz == 0 || Cx == Ax)
    { 
        // no work to do
        return (GrB_SUCCESS) ;
    }

    ASSERT (Cx != NULL) ;
    ASSERT (Ax != NULL) ;
    ASSERT (anz > 0) ;
    ASSERT (GB_code_compatible (ccode, acode)) ;
    ASSERT (ccode != acode) ;
    ASSERT (ccode != GB_UDT_code) ;
    ASSERT (!A->iso) ;

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

    GrB_Info info = GrB_NO_VALUE ;

    #ifndef GBCOMPACT
    GB_IF_FACTORY_KERNELS_ENABLED
    { 

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

        const GB_Type_code code1 = ccode, code2 = acode ;

        #define GB_uop_apply(zname,xname)                                   \
            GB (_uop_apply__identity ## zname ## xname)

        #define GB_WORKER(ignore1,zname,ztype,xname,xtype)                  \
        {                                                                   \
            info = GB_uop_apply (zname,xname) (Cx, Ax, Ab, anz,             \
                A_nthreads) ;                                               \
        }                                                                   \
        break ;

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

        #define GB_EXCLUDE_SAME_TYPES
        #include "apply/factory/GB_twotype_factory.c"
    }
    #endif

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

    if (info == GrB_NO_VALUE)
    { 
        GrB_Type ctype = GB_code_type (ccode, NULL) ;
        GB_Operator op = GB_unop_identity (ctype, NULL) ;
        ASSERT_OP_OK (op, "id op for cast_array", GB0) ;
        info = GB_apply_unop_jit (Cx, ctype, op, false, A, NULL, NULL, 0,
            A_nthreads) ;
    }

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

    if (info == GrB_NO_VALUE)
    { 
        GB_BURBLE_N (anz, "(generic cast array) ") ;
        int64_t csize = GB_code_size (ccode, 0) ;
        int64_t asize = GB_code_size (acode, 0) ;
        GB_cast_function cast_A_to_C = GB_cast_factory (ccode, acode) ;
        #define GB_A_IS_BITMAP (Ab != NULL)
        #define GB_APPLY_OP(pC,pA) \
            cast_A_to_C (Cx +((pC)*csize), Ax +((pA)*asize), asize)
        #include "apply/template/GB_apply_unop_ip_template.c"
        info = GrB_SUCCESS ;
    }

    return (info) ;
}