File: GB_cast_matrix.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 (93 lines) | stat: -rw-r--r-- 3,153 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
//------------------------------------------------------------------------------
// GB_cast_matrix: copy or typecast the values from A into C
//------------------------------------------------------------------------------

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

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

// The values of C must already be allocated, of size large enough to hold
// the values of A.  The pattern of C must match the pattern of A, but the
// pattern is not accessed (except to compute GB_nnz_held (A)).

// Note that A may contain zombies, or entries not in the bitmap pattern of A
// if A is bitmap, and the values of these entries might be uninitialized
// values in A->x.  All entries are typecasted or memcpy'ed from A->x to C->x,
// including zombies, non-entries, and live entries alike.  valgrind may
// complain about typecasting these uninitialized values, but these warnings
// are false positives.

#include "GB.h"
#define GB_FREE_ALL ;

GrB_Info GB_cast_matrix     // copy or typecast the values from A into C
(
    GrB_Matrix C,
    GrB_Matrix A
)
{

    //--------------------------------------------------------------------------
    // determine the number of threads to use
    //--------------------------------------------------------------------------

    GrB_Info info ;
    const int64_t anz = GB_nnz_held (A) ;
    int nthreads_max = GB_Context_nthreads_max ( ) ;
    double chunk = GB_Context_chunk ( ) ;
    int nthreads = GB_nthreads (anz, chunk, nthreads_max) ;
    ASSERT (GB_IMPLIES (anz > 1, A->iso == C->iso)) ;
    if (anz == 0)
    { 
        // nothing to do
        return (GrB_SUCCESS) ;
    }

    //--------------------------------------------------------------------------
    // copy or typecast from A into C
    //--------------------------------------------------------------------------

    GB_void *Cx = (GB_void *) C->x ;
    GB_void *Ax = (GB_void *) A->x ;
    if (C->type == A->type)
    {

        //----------------------------------------------------------------------
        // copy A->x into C->x
        //----------------------------------------------------------------------

        if (A->iso)
        { 
            // A iso, ctype == atype
            memcpy (Cx, Ax, C->type->size) ;
        }
        else
        { 
            // copy all the values, no typecast
            GB_memcpy (Cx, Ax, anz * C->type->size, nthreads) ;
        }

    }
    else
    {

        //----------------------------------------------------------------------
        // typecast Ax into C->x
        //----------------------------------------------------------------------

        if (A->iso)
        { 
            // Cx [0] = (ctype) Ax [0]
            GB_unop_iso (Cx, C->type, GB_ISO_A, NULL, A, NULL) ;
        }
        else
        { 
            // typecast all the values from A to Cx
            ASSERT (GB_IMPLIES (anz > 0, Cx != NULL)) ;
            GB_OK (GB_cast_array (Cx, C->type->code, A, nthreads)) ;
        }
    }
    return (GrB_SUCCESS) ;
}