File: GB_convert_bitmap_to_sparse.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 (134 lines) | stat: -rw-r--r-- 4,846 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
//------------------------------------------------------------------------------
// GB_convert_bitmap_to_sparse: convert a matrix from bitmap to sparse
//------------------------------------------------------------------------------

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

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

#include "GB.h"

#define GB_FREE_ALL                     \
{                                       \
    GB_FREE_MEMORY (&Cp, Cp_size) ;            \
    GB_FREE_MEMORY (&Ci, Ci_size) ;            \
    GB_FREE_MEMORY (&Cx, Cx_size) ;            \
}

GrB_Info GB_convert_bitmap_to_sparse    // convert matrix from bitmap to sparse
(
    GrB_Matrix A,               // matrix to convert from bitmap to sparse
    GB_Werk Werk
)
{

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

    GrB_Info info ;
    ASSERT_MATRIX_OK (A, "A converting bitmap to sparse", GB0) ;
    ASSERT (!GB_IS_FULL (A)) ;
    ASSERT (GB_IS_BITMAP (A)) ;
    ASSERT (!GB_IS_SPARSE (A)) ;
    ASSERT (!GB_IS_HYPERSPARSE (A)) ;
    ASSERT (!GB_PENDING (A)) ;      // bitmap never has pending tuples
    ASSERT (!GB_JUMBLED (A)) ;      // bitmap is never jumbled
    ASSERT (!GB_ZOMBIES (A)) ;      // bitmap never has zomies

    //--------------------------------------------------------------------------
    // allocate Cp, Ci, and Cx
    //--------------------------------------------------------------------------

    const int64_t anvals = A->nvals ;
    GB_BURBLE_N (anvals, "(bitmap to sparse) ") ;
    const int64_t anzmax = GB_IMAX (anvals, 1) ;
    int64_t cnvec_nonempty ;
    const int64_t avdim = A->vdim ;
    const size_t asize = A->type->size ;
    void *Cp = NULL ; size_t Cp_size = 0 ;
    void *Ci = NULL ; size_t Ci_size = 0 ;
    void *Cx = NULL ; size_t Cx_size = 0 ;

    bool Cp_is_32, Cj_is_32, Ci_is_32 ;
    GB_determine_pji_is_32 (&Cp_is_32, &Cj_is_32, &Ci_is_32,
        GxB_AUTO_SPARSITY, anzmax, A->vlen, avdim, Werk) ;

    size_t psize = Cp_is_32 ? sizeof (uint32_t) : sizeof (uint64_t) ;
    size_t isize = Ci_is_32 ? sizeof (uint32_t) : sizeof (uint64_t) ;

    Cp = GB_MALLOC_MEMORY (avdim+1, psize, &Cp_size) ;
    Ci = GB_MALLOC_MEMORY (anzmax,  isize, &Ci_size) ;
    if (Cp == NULL || Ci == NULL)
    { 
        // out of memory
        GB_FREE_ALL ;
        return (GrB_OUT_OF_MEMORY) ;
    }

    const bool A_iso = A->iso ;
    if (!A_iso)
    { 
        // A is not iso.  Allocate new space for Cx, which is filled by
        // GB_convert_b2s.
        Cx = GB_MALLOC_MEMORY (anzmax, asize, &Cx_size) ;
        if (Cx == NULL)
        { 
            // out of memory
            GB_FREE_ALL ;
            return (GrB_OUT_OF_MEMORY) ;
        }
    }

    //--------------------------------------------------------------------------
    // convert to sparse format (Cp, Ci, and Cx)
    //--------------------------------------------------------------------------

    // Cx and A->x always have the same type.
    // The values are not converted if A is iso (Cx is NULL).
    GB_OK (GB_convert_b2s (Cp, Ci, NULL, Cx, &cnvec_nonempty,
        Cp_is_32, Ci_is_32, false, A->type, A, Werk)) ;

    //--------------------------------------------------------------------------
    // free prior content of A and transplant the new content
    //--------------------------------------------------------------------------

    bool Cx_shallow = false ;
    if (A_iso)
    { 
        // A is iso.  Remove A->x from the matrix so it is not freed by
        // GB_phybix_free; it is transplanted back again just below.
        Cx = A->x ;
        Cx_size = A->x_size ;
        Cx_shallow = A->x_shallow ;
        A->x = NULL ;
    }

    GB_phybix_free (A) ;        // clears A->nvals
    A->p = Cp ; A->p_size = Cp_size ; A->p_shallow = false ;
    A->i = Ci ; A->i_size = Ci_size ; A->i_shallow = false ;
    A->x = Cx ; A->x_size = Cx_size ; A->x_shallow = Cx_shallow ;
    A->p_is_32 = Cp_is_32 ;
    A->j_is_32 = Cj_is_32 ;
    A->i_is_32 = Ci_is_32 ;
    A->iso = A_iso ;
    A->nvals = anvals ;
    A->plen = avdim ;
    A->nvec = avdim ;
//  A->nvec_nonempty = cnvec_nonempty ;
    GB_nvec_nonempty_set (A, cnvec_nonempty) ;
    A->magic = GB_MAGIC ;

    //--------------------------------------------------------------------------
    // return result
    //--------------------------------------------------------------------------

    ASSERT_MATRIX_OK (A, "A converted from to bitmap to sparse", GB0) ;
    ASSERT (GB_IS_SPARSE (A)) ;
    ASSERT (!GB_ZOMBIES (A)) ;
    ASSERT (!GB_JUMBLED (A)) ;
    ASSERT (!GB_PENDING (A)) ;
    return (GrB_SUCCESS) ;
}