File: gb_export.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (110 lines) | stat: -rw-r--r-- 3,987 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
//------------------------------------------------------------------------------
// gb_export: export a GrB_Matrix as a built-in matrix or GraphBLAS struct
//------------------------------------------------------------------------------

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

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

// mxArray pargout [0] = gb_export (&C, kind) ; exports C as a built-in matrix
// and frees the remaining content of C.

#include "gb_interface.h"

mxArray *gb_export              // return the exported built-in matrix or struct
(
    GrB_Matrix *C_handle,       // GrB_Matrix to export and free
    kind_enum_t kind            // GrB, sparse, full, or built-in
)
{

    //--------------------------------------------------------------------------
    // determine if all entries in C are present
    //--------------------------------------------------------------------------

    GrB_Index nrows, ncols ;
    bool is_full = false ;
    if (kind == KIND_BUILTIN || kind == KIND_FULL)
    { 
        GrB_Index nvals ;
        OK (GrB_Matrix_nvals (&nvals, *C_handle)) ;
        OK (GrB_Matrix_nrows (&nrows, *C_handle)) ;
        OK (GrB_Matrix_ncols (&ncols, *C_handle)) ;
        is_full = ((double) nrows * (double) ncols == (double) nvals) ;
    }

    if (kind == KIND_BUILTIN)
    { 
        // export as full if all entries present, or sparse otherwise
        kind = (is_full) ? KIND_FULL : KIND_SPARSE ;
    }

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

    if (kind == KIND_SPARSE)
    { 

        //----------------------------------------------------------------------
        // export C as a built-in sparse matrix
        //----------------------------------------------------------------------

        // Typecast to double, if C is integer (int8, ..., uint64)
        return (gb_export_to_mxsparse (C_handle)) ;

    }
    else if (kind == KIND_FULL)
    { 

        //----------------------------------------------------------------------
        // export C as a built-in full matrix, adding explicit zeros if needed
        //----------------------------------------------------------------------

        // No typecasting is needed since built-in full matrices support all
        // the same types.

        GrB_Matrix C = NULL ;
        if (!is_full)
        {
            // expand C with explicit zeros so all entries are present
            C = gb_expand_to_full (*C_handle, NULL, GxB_BY_COL, NULL) ;
            OK (GrB_Matrix_free (C_handle)) ;
            (*C_handle) = C ;
            CHECK_ERROR (GB_is_shallow (*C_handle), "internal error 707")
        }

        if (GB_is_shallow (*C_handle))
        {
            // C is shallow so make a deep copy
            OK (GrB_Matrix_dup (&C, *C_handle)) ;
            OK (GrB_Matrix_free (C_handle)) ;
            (*C_handle) = C ;
        }

        CHECK_ERROR (GB_is_shallow (*C_handle), "internal error 717")

        // export as a full matrix, held by column, not uniform-valued
        void *Cx = NULL ;
        GrB_Type ctype = NULL ;
        GrB_Index Cx_size ;
        OK (GxB_Matrix_export_FullC (C_handle, &ctype, &nrows, &ncols,
            &Cx, &Cx_size, NULL, NULL)) ;

        return (gb_export_to_mxfull (&Cx, nrows, ncols, ctype)) ;

    }
    else // kind == KIND_GRB
    { 

        //----------------------------------------------------------------------
        // export C as a built-in struct containing a verbatim GrB_Matrix
        //----------------------------------------------------------------------

        // No typecasting is needed since the built-in struct can hold all of
        // the opaque content of the GrB_Matrix.
        return (gb_export_to_mxstruct (C_handle)) ;
    }
}