File: GB_mex_split.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 (113 lines) | stat: -rw-r--r-- 3,386 bytes parent folder | download | duplicates (3)
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
//------------------------------------------------------------------------------
// GB_mex_split: C = split (Tiles)
//------------------------------------------------------------------------------

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

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

#include "GB_mex.h"

#define USAGE "C = GB_mex_split (A, ms, ns)"

#define FREE_ALL                        \
{                                       \
    GrB_Matrix_free_(&A) ;              \
    mxFree (Tiles) ;                    \
    GB_mx_put_global (true) ;           \
}

#define OK(method)                      \
{                                       \
    info = method ;                     \
    if (info != GrB_SUCCESS)            \
    {                                   \
        printf ("%d at %d\n", info, __LINE__) ;  \
        mexErrMsgTxt ("failed") ;       \
    }                                   \
}

void mexFunction
(
    int nargout,
    mxArray *pargout [ ],
    int nargin,
    const mxArray *pargin [ ]
)
{

    GrB_Info info ;
    bool malloc_debug = GB_mx_get_global (true) ;
    GrB_Matrix A = NULL ;
    GrB_Matrix *Tiles = NULL ;

    // check inputs
    if (nargout > 1 || nargin != 3)
    {
        mexErrMsgTxt ("Usage: " USAGE) ;
    }

    // get A (shallow copy)
    A = GB_mx_mxArray_to_Matrix (pargin [0], "A input", false, true) ;
    if (A == NULL)
    {
        FREE_ALL ;
        mexErrMsgTxt ("A failed") ;
    }

    // get ms (deep copy)
    if (mxGetClassID (pargin [1]) != mxDOUBLE_CLASS)
    {
        mexErrMsgTxt ("ms must be double") ;
    }
    double *ms_double = mxGetDoubles (pargin [1]) ;
    GrB_Index m = mxGetNumberOfElements (pargin [1]) ;
    GrB_Index *Tile_nrows = mxMalloc (m * sizeof (GrB_Index)) ;
    for (int64_t k = 0 ; k < m ; k++)
    {
        Tile_nrows [k] = (GrB_Index) (ms_double [k]) ;
    }

    // get ns (deep copy)
    if (mxGetClassID (pargin [2]) != mxDOUBLE_CLASS)
    {
        mexErrMsgTxt ("ns must be double") ;
    }
    double *ns_double = mxGetDoubles (pargin [2]) ;
    GrB_Index n = mxGetNumberOfElements (pargin [2]) ;
    GrB_Index *Tile_ncols = mxMalloc (n * sizeof (GrB_Index)) ;
    for (int64_t k = 0 ; k < n ; k++)
    {
        Tile_ncols [k] = (GrB_Index) (ns_double [k]) ;
    }

    // create Tiles
    Tiles = mxCalloc (m * n, sizeof (GrB_Matrix)) ;

    // construct the empty Tiles array
    #define GET_DEEP_COPY                                   \
        memset (Tiles, 0, m * n * sizeof (GrB_Matrix)) ;
    #define FREE_DEEP_COPY                                  \
        for (int64_t k = 0 ; k < m*n ; k++)                 \
        {                                                   \
            GrB_Matrix_free (&(Tiles [k])) ;                \
        }

    // Tiles = split (A, ms, ns)
    METHOD (GxB_Matrix_split (Tiles, m, n, Tile_nrows, Tile_ncols, A, NULL)) ;

    // return C as a cell array and free the GraphBLAS tiles
    pargout [0] = mxCreateCellMatrix (m, n) ;
    for (int64_t j = 0 ; j < n ; j++)
    {
        for (int64_t i = 0 ; i < m ; i++)
        {
            mxArray *T = GB_mx_Matrix_to_mxArray (&(Tiles [i*n+j]),
                "Tile output", true) ;
            mxSetCell (pargout [0], i+j*m, T) ;
        }
    }
    FREE_ALL ;
}