1 //------------------------------------------------------------------------------
2 // GB_mex_Matrix_eWiseMult: C<Mask> = accum(C,A.*B)
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_Matrix_eWiseMult (C, Mask, accum, mult, A, B, desc)"
13 
14 #define FREE_ALL                    \
15 {                                   \
16     GrB_Matrix_free_(&A) ;          \
17     GrB_Matrix_free_(&B) ;          \
18     GrB_Matrix_free_(&C) ;          \
19     GrB_Descriptor_free_(&desc) ;   \
20     GrB_Matrix_free_(&Mask) ;       \
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 A = NULL ;
35     GrB_Matrix B = NULL ;
36     GrB_Matrix C = NULL ;
37     GrB_Matrix Mask = NULL ;
38     GrB_Descriptor desc = NULL ;
39 
40     // check inputs
41     if (nargout > 1 || nargin < 6 || nargin > 7)
42     {
43         mexErrMsgTxt ("Usage: " USAGE) ;
44     }
45 
46     // get C (make a deep copy)
47     #define GET_DEEP_COPY \
48     C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
49     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
50     GET_DEEP_COPY ;
51     if (C == NULL)
52     {
53         FREE_ALL ;
54         mexErrMsgTxt ("C failed") ;
55     }
56 
57     // get Mask (shallow copy)
58     Mask = GB_mx_mxArray_to_Matrix (pargin [1], "Mask", false, false) ;
59     if (Mask == NULL && !mxIsEmpty (pargin [1]))
60     {
61         FREE_ALL ;
62         mexErrMsgTxt ("Mask failed") ;
63     }
64 
65     // get A (shallow copy)
66     A = GB_mx_mxArray_to_Matrix (pargin [4], "A input", false, true) ;
67     if (A == NULL)
68     {
69         FREE_ALL ;
70         mexErrMsgTxt ("A failed") ;
71     }
72 
73     // get B (shallow copy)
74     B = GB_mx_mxArray_to_Matrix (pargin [5], "B input", false, true) ;
75     if (B == NULL)
76     {
77         FREE_ALL ;
78         mexErrMsgTxt ("B failed") ;
79     }
80 
81     // get mult operator
82     bool user_complex = (Complex != GxB_FC64)
83         && (A->type == Complex || B->type == Complex) ;
84     GrB_BinaryOp mult ;
85     if (!GB_mx_mxArray_to_BinaryOp (&mult, pargin [3], "mult",
86         C->type, user_complex) || mult == NULL)
87     {
88         FREE_ALL ;
89         mexErrMsgTxt ("mult failed") ;
90     }
91 
92     // get accum, if present
93     user_complex = (Complex != GxB_FC64)
94         && (C->type == Complex || mult->ztype == Complex) ;
95     GrB_BinaryOp accum ;
96     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [2], "accum",
97         C->type, user_complex))
98     {
99         FREE_ALL ;
100         mexErrMsgTxt ("accum failed") ;
101     }
102 
103     // get desc
104     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (6), "desc"))
105     {
106         FREE_ALL ;
107         mexErrMsgTxt ("desc failed") ;
108     }
109 
110     // C<Mask> = accum(C,A.*B)
111     METHOD (GrB_Matrix_eWiseMult_BinaryOp_(C, Mask, accum, mult, A, B, desc)) ;
112 
113     // return C to MATLAB as a struct and free the GraphBLAS C
114     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output", true) ;
115 
116     FREE_ALL ;
117 }
118 
119