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