1 //------------------------------------------------------------------------------
2 // GB_mex_apply_maskalias: C<C> = accum(C,op(A)) or op(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 // Apply a unary operator to a matrix, with C aliased to the Mask
11
12 #include "GB_mex.h"
13
14 #define USAGE "C = GB_mex_apply_maskalias (C, accum, op, A, desc)"
15
16 #define FREE_ALL \
17 { \
18 GrB_Matrix_free_(&C) ; \
19 GrB_Matrix_free_(&A) ; \
20 GrB_Descriptor_free_(&desc) ; \
21 GB_mx_put_global (true) ; \
22 }
23
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])24 void mexFunction
25 (
26 int nargout,
27 mxArray *pargout [ ],
28 int nargin,
29 const mxArray *pargin [ ]
30 )
31 {
32
33 bool malloc_debug = GB_mx_get_global (true) ;
34 GrB_Matrix C = NULL ;
35 GrB_Matrix A = NULL ;
36 GrB_Descriptor desc = NULL ;
37
38 // check inputs
39 if (nargout > 1 || nargin < 4 || nargin > 5)
40 {
41 mexErrMsgTxt ("Usage: " USAGE) ;
42 }
43
44 // get C (make a deep copy)
45 #define GET_DEEP_COPY \
46 C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
47 #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
48 GET_DEEP_COPY ;
49 if (C == NULL)
50 {
51 FREE_ALL ;
52 mexErrMsgTxt ("C failed") ;
53 }
54
55 // get A (shallow copy)
56 A = GB_mx_mxArray_to_Matrix (pargin [3], "A input", false, true) ;
57 if (A == NULL)
58 {
59 FREE_ALL ;
60 mexErrMsgTxt ("A failed") ;
61 }
62
63 // get accum, if present
64 bool user_complex = (Complex != GxB_FC64)
65 && (C->type == Complex || A->type == Complex) ;
66 GrB_BinaryOp accum ;
67 if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [1], "accum",
68 C->type, user_complex))
69 {
70 FREE_ALL ;
71 mexErrMsgTxt ("accum failed") ;
72 }
73
74 // get op
75 GrB_UnaryOp op ;
76 if (!GB_mx_mxArray_to_UnaryOp (&op, pargin [2], "op",
77 A->type, user_complex) || op == NULL)
78 {
79 FREE_ALL ;
80 mexErrMsgTxt ("UnaryOp failed") ;
81 }
82
83 // get desc
84 if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (4), "desc"))
85 {
86 FREE_ALL ;
87 mexErrMsgTxt ("desc failed") ;
88 }
89
90 // C<C> = accum(C,op(A))
91 if (GB_NCOLS (C) == 1 && (desc == NULL || desc->in0 == GxB_DEFAULT))
92 {
93 // this is just to test the Vector version
94 METHOD (GrB_Vector_apply_((GrB_Vector) C, (GrB_Vector) C, accum, op,
95 (GrB_Vector) A, desc)) ;
96 }
97 else
98 {
99 METHOD (GrB_Matrix_apply_(C, C, accum, op, A, desc)) ;
100 }
101
102 // return C to MATLAB as a struct and free the GraphBLAS C
103 pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
104
105 FREE_ALL ;
106 }
107
108