1 //------------------------------------------------------------------------------
2 // GB_mex_ewise_alias4: C<M> = M+M
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_ewise_alias4 (C, M, op, desc)"
13 
14 #define FREE_ALL                            \
15 {                                           \
16     GrB_Matrix_free_(&M) ;                  \
17     GrB_Matrix_free_(&C) ;                  \
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, M = NULL ;
33     GrB_Descriptor desc = NULL ;
34 
35     // check inputs
36     if (nargout > 1 || nargin < 3 || nargin > 4)
37     {
38         mexErrMsgTxt ("Usage: " USAGE) ;
39     }
40 
41     // get C (make a deep copy)
42     #define GET_DEEP_COPY \
43     C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
44     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
45     GET_DEEP_COPY ;
46     if (C == NULL)
47     {
48         FREE_ALL ;
49         mexErrMsgTxt ("C failed") ;
50     }
51 
52     // get M (shallow copy)
53     M = GB_mx_mxArray_to_Matrix (pargin [1], "M input", false, true) ;
54     if (M == NULL)
55     {
56         FREE_ALL ;
57         mexErrMsgTxt ("M failed") ;
58     }
59 
60     // get op
61     bool user_complex = (Complex != GxB_FC64) && (C->type == Complex) ;
62     GrB_BinaryOp op ;
63     if (!GB_mx_mxArray_to_BinaryOp (&op, pargin [2], "op",
64         C->type, user_complex) || op == NULL)
65     {
66         FREE_ALL ;
67         mexErrMsgTxt ("op failed") ;
68     }
69 
70     // get desc
71     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (3), "desc"))
72     {
73         FREE_ALL ;
74         mexErrMsgTxt ("desc failed") ;
75     }
76 
77     // C<M> = M+M
78     METHOD (GrB_Matrix_eWiseAdd_BinaryOp_(C, M, NULL, op, M, M, desc)) ;
79 
80     // return C to MATLAB as a struct and free the GraphBLAS C
81     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
82 
83     FREE_ALL ;
84 }
85 
86