1 //------------------------------------------------------------------------------
2 // GB_mex_assign_alias_mask: C<A> = A
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 #include "GB_mex.h"
11 
12 #define USAGE "C = GB_mex_assign_alias_mask (C, A, desc)"
13 
14 #define FREE_ALL                            \
15 {                                           \
16     GrB_Matrix_free_(&C) ;                  \
17     GrB_Matrix_free_(&A) ;                  \
18     GrB_Descriptor_free_(&desc) ;           \
19     GB_mx_put_global (true) ;               \
20 }
21 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])22 void mexFunction
23 (
24     int nargout,
25     mxArray *pargout [ ],
26     int nargin,
27     const mxArray *pargin [ ]
28 )
29 {
30 
31     bool malloc_debug = GB_mx_get_global (true) ;
32     GrB_Matrix C = NULL, A = NULL ;
33     GrB_Descriptor desc = NULL ;
34 
35     // check inputs
36     if (nargout > 1 || nargin < 2 || nargin > 3)
37     {
38         mexErrMsgTxt ("Usage: " USAGE) ;
39     }
40 
41     // get A C (make a deep copy)
42     #define GET_DEEP_COPY       \
43         C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;   \
44         GxB_Matrix_Option_set (C, GxB_SPARSITY_CONTROL, C->sparsity) ;      \
45         A = GB_mx_mxArray_to_Matrix (pargin [1], "A input", true, true) ;   \
46         GxB_Matrix_Option_set (A, GxB_SPARSITY_CONTROL, A->sparsity) ;
47     #define FREE_DEEP_COPY      \
48         GrB_Matrix_free_(&C) ;  \
49         GrB_Matrix_free_(&A) ;
50     GET_DEEP_COPY ;
51     if (C == NULL || A == NULL)
52     {
53         FREE_ALL ;
54         mexErrMsgTxt ("C or A failed") ;
55     }
56 
57     // get desc
58     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (2), "desc"))
59     {
60         FREE_ALL ;
61         mexErrMsgTxt ("desc failed") ;
62     }
63 
64     GrB_Index nrows, ncols ;
65     GrB_Matrix_nrows (&nrows, C) ;
66     GrB_Matrix_ncols (&ncols, C) ;
67 
68     // C<A> = A
69     METHOD (GxB_Matrix_subassign_(C, A, NULL, A,
70         GrB_ALL, nrows, GrB_ALL, ncols, desc)) ;
71 
72     // return C to MATLAB as a struct and free the GraphBLAS C
73     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
74 
75     FREE_ALL ;
76 }
77 
78