File: GB_export.c

package info (click to toggle)
suitesparse 1%3A7.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, 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 (280 lines) | stat: -rw-r--r-- 9,008 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
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
//------------------------------------------------------------------------------
// GB_export: export a matrix or vector
//------------------------------------------------------------------------------

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

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

// No conversion is done, except to convert to non-iso if requested, and all
// integers are converted to 64-bits.  The matrix is exported in its current
// sparsity structure and by-row/by-col format.

#include "import_export/GB_export.h"

#define GB_FREE_ALL                     \
{                                       \
    GB_FREE_MEMORY (&Ap_new, Ap_new_size) ;    \
    GB_FREE_MEMORY (&Ah_new, Ah_new_size) ;    \
}

GrB_Info GB_export      // export/unpack a matrix in any format
(
    bool unpacking,     // unpack if true, export and free if false.
                        // The false case is historical; GxB*unpack sets this
                        // flag to true, and GrB*export does not use this
                        // method.

    GrB_Matrix *A,      // handle of matrix to export and free, or unpack
    GrB_Type *type,     // type of matrix to export
    uint64_t *vlen,     // vector length
    uint64_t *vdim,     // vector dimension
    bool is_sparse_vector,      // true if A is a sparse GrB_Vector

    // the 5 arrays:
    uint64_t **Ap,      // pointers
    uint64_t *Ap_size,  // size of Ap in bytes

    uint64_t **Ah,      // vector indices
    uint64_t *Ah_size,  // size of Ah in bytes

    int8_t **Ab,        // bitmap
    uint64_t *Ab_size,  // size of Ab in bytes

    uint64_t **Ai,      // indices
    uint64_t *Ai_size,  // size of Ai in bytes

    void **Ax,          // values
    uint64_t *Ax_size,  // size of Ax in bytes

    // additional information for specific formats:
    uint64_t *nvals,    // # of entries for bitmap format.
    bool *jumbled,      // if true, sparse/hypersparse may be jumbled.
    uint64_t *nvec,     // size of Ah for hypersparse format.

    // information for all formats:
    int *sparsity,      // hypersparse, sparse, bitmap, or full
    bool *is_csc,       // if true then matrix is by-column, else by-row
    bool *iso,          // if true then A is iso and only one entry is returned
                        // in Ax, regardless of nvals(A).
    GB_Werk Werk
)
{

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

    GrB_Info info ;
    int64_t *Ap_new = NULL ; size_t Ap_new_size = 0 ;   // OK; only 64-bit
    int64_t *Ah_new = NULL ; size_t Ah_new_size = 0 ;   // OK; only 64-bit
    ASSERT (A != NULL) ;
    GB_RETURN_IF_NULL (*A) ;

    // ensure the matrix is all-64-bit
    GB_OK (GB_convert_int (*A, false, false, false, false)) ;

    GB_RETURN_IF_NULL_OR_INVALID (*A) ;
    ASSERT_MATRIX_OK (*A, "A to export", GB0) ;
    ASSERT (!GB_ZOMBIES (*A)) ;
    ASSERT (GB_JUMBLED_OK (*A)) ;
    ASSERT (!GB_PENDING (*A)) ;

    GB_RETURN_IF_NULL (type) ;
    GB_RETURN_IF_NULL (vlen) ;
    GB_RETURN_IF_NULL (vdim) ;
    GB_RETURN_IF_NULL (Ax) ;
    GB_RETURN_IF_NULL (Ax_size) ;

    int s = GB_sparsity (*A) ;

    switch (s)
    {
        case GxB_HYPERSPARSE : 
            GB_RETURN_IF_NULL (nvec) ;
            GB_RETURN_IF_NULL (Ah) ; GB_RETURN_IF_NULL (Ah_size) ;
            // fall through to the sparse case

        case GxB_SPARSE : 
            if (is_sparse_vector)
            { 
                GB_RETURN_IF_NULL (nvals) ;
            }
            else
            { 
                GB_RETURN_IF_NULL (Ap) ; GB_RETURN_IF_NULL (Ap_size) ;
            }
            GB_RETURN_IF_NULL (Ai) ; GB_RETURN_IF_NULL (Ai_size) ;
            break ;

        case GxB_BITMAP : 
            GB_RETURN_IF_NULL (nvals) ;
            GB_RETURN_IF_NULL (Ab) ; GB_RETURN_IF_NULL (Ab_size) ;
            // fall through to the full case

        case GxB_FULL : 
            break ;

        default: ;
    }

    //--------------------------------------------------------------------------
    // allocate new space for Ap and Ah if unpacking
    //--------------------------------------------------------------------------

    int64_t avdim = (*A)->vdim ;
    int64_t plen_new, nvec_new ;
    if (unpacking)
    {
        plen_new = (avdim == 0) ? 0 : 1 ;
        nvec_new = (avdim == 1) ? 1 : 0 ;
        Ap_new = GB_CALLOC_MEMORY (plen_new+1, sizeof (int64_t),
            &(Ap_new_size)) ;
        if (avdim > 1)
        { 
            // A is sparse if avdim <= 1, hypersparse if avdim > 1
            Ah_new = GB_CALLOC_MEMORY (1, sizeof (int64_t),
                &(Ah_new_size)) ;
        }
        if (Ap_new == NULL || (avdim > 1 && Ah_new == NULL))
        { 
            // out of memory
            GB_FREE_ALL ;
            return (GrB_OUT_OF_MEMORY) ;
        }
    }

    //--------------------------------------------------------------------------
    // ensure A is non-iso if requested, or export A as-is
    //--------------------------------------------------------------------------

    if (iso == NULL)
    {
        // ensure A is non-iso
        if ((*A)->iso)
        { 
            GBURBLE ("(iso to non-iso export) ") ;
        }
        GB_OK (GB_convert_any_to_non_iso (*A, true)) ;
        ASSERT (!((*A)->iso)) ;
    }
    else
    {
        // do not convert the matrix; export A as-is, either iso or non-iso
        (*iso) = (*A)->iso ;
        if (*iso)
        { 
            GBURBLE ("(iso export) ") ;
        }
    }

    //--------------------------------------------------------------------------
    // export the matrix
    //--------------------------------------------------------------------------

    (*type) = (*A)->type ;
    (*vlen) = (*A)->vlen ;
    (*vdim) = avdim ;

    // export A->x
    GBMDUMP ("export A->x from memtable: %p\n", (*A)->x) ;
    GB_Global_memtable_remove ((*A)->x) ;
    (*Ax) = (*A)->x ; (*A)->x = NULL ;
    (*Ax_size) = (*A)->x_size ;

    switch (s)
    {
        case GxB_HYPERSPARSE : 
            (*nvec) = (*A)->nvec ;

            // export A->h
            GBMDUMP ("export A->h from memtable: %p\n", (*A)->h) ;
            GB_Global_memtable_remove ((*A)->h) ;
            (*Ah) = (uint64_t *) ((*A)->h) ; (*A)->h = NULL ;
            (*Ah_size) = (*A)->h_size ;
            // fall through to the sparse case

        case GxB_SPARSE : 
            if (jumbled != NULL)
            { 
                (*jumbled) = (*A)->jumbled ;
            }

            // export A->p, unless A is a sparse vector in CSC format
            if (is_sparse_vector)
            { 
                uint64_t *restrict Ap = (*A)->p ;       // OK; 64-bit only
                (*nvals) = Ap [1] ;
            }
            else
            { 
                GBMDUMP ("export A->p from memtable: %p\n", (*A)->p) ;
                GB_Global_memtable_remove ((*A)->p) ;
                (*Ap) = (uint64_t *) ((*A)->p) ; (*A)->p = NULL ;
                (*Ap_size) = (*A)->p_size ;
            }

            // export A->i
            GBMDUMP ("export A->i from memtable: %p\n", (*A)->i) ;
            GB_Global_memtable_remove ((*A)->i) ;
            (*Ai) = (uint64_t *) ((*A)->i) ; (*A)->i = NULL ;
            (*Ai_size) = (*A)->i_size ;
            break ;

        case GxB_BITMAP : 
            (*nvals) = (*A)->nvals ;

            // export A->b
            GBMDUMP ("export A->b from memtable: %p\n", (*A)->b) ;
            GB_Global_memtable_remove ((*A)->b) ;
            (*Ab) = (*A)->b ; (*A)->b = NULL ;
            (*Ab_size) = (*A)->b_size ;

        case GxB_FULL : 

        default: ;
    }

    if (sparsity != NULL)
    { 
        (*sparsity) = s ;
    }
    if (is_csc != NULL)
    { 
        (*is_csc) = (*A)->is_csc ;
    }

    //--------------------------------------------------------------------------
    // free or clear the GrB_Matrix
    //--------------------------------------------------------------------------

    // both export and unpack free the hyper_hash, A->Y

    if (unpacking)
    { 
        // unpack: clear the matrix, leaving it hypersparse (or sparse if
        // it is a vector (vdim of 1) or has vdim of zero)
        GB_phybix_free (*A) ;
        (*A)->plen = plen_new ;
        (*A)->nvec = nvec_new ;
        (*A)->p = Ap_new ; (*A)->p_size = Ap_new_size ;
        (*A)->h = Ah_new ; (*A)->h_size = Ah_new_size ;
        (*A)->magic = GB_MAGIC ;
        (*A)->p_is_32 = false ;
        (*A)->j_is_32 = false ;
        (*A)->i_is_32 = false ;
        ASSERT_MATRIX_OK (*A, "A unpacked", GB0) ;
    }
    else
    { 
        // GxB_export: free the header of A, and A->p if A is a sparse
        // GrB_Vector.  This method is historical.
        GB_Matrix_free (A) ;
        ASSERT ((*A) == NULL) ;
    }

    #pragma omp flush
    return (GrB_SUCCESS) ;
}