1 //------------------------------------------------------------------------------
2 // GB_mex_Vector_eWiseAdd: w<mask> = accum(w,u+v)
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 "w = GB_mex_Vector_eWiseAdd (w, mask, accum, add, u, v, desc)"
13 
14 #define FREE_ALL                    \
15 {                                   \
16     GrB_Vector_free_(&w) ;          \
17     GrB_Vector_free_(&u) ;          \
18     GrB_Vector_free_(&v) ;          \
19     GrB_Descriptor_free_(&desc) ;   \
20     GrB_Vector_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_Vector w = NULL ;
35     GrB_Vector u = NULL ;
36     GrB_Vector v = NULL ;
37     GrB_Vector 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 w (make a deep copy)
47     #define GET_DEEP_COPY \
48     w = GB_mx_mxArray_to_Vector (pargin [0], "w input", true, true) ;
49     #define FREE_DEEP_COPY GrB_Vector_free_(&w) ;
50     GET_DEEP_COPY ;
51     if (w == NULL)
52     {
53         FREE_ALL ;
54         mexErrMsgTxt ("w failed") ;
55     }
56 
57     // get mask (shallow copy)
58     mask = GB_mx_mxArray_to_Vector (pargin [1], "mask", false, false) ;
59     if (mask == NULL && !mxIsEmpty (pargin [1]))
60     {
61         FREE_ALL ;
62         mexErrMsgTxt ("mask failed") ;
63     }
64 
65     // get u (shallow copy)
66     u = GB_mx_mxArray_to_Vector (pargin [4], "u input", false, true) ;
67     if (u == NULL)
68     {
69         FREE_ALL ;
70         mexErrMsgTxt ("u failed") ;
71     }
72 
73     // get v (shallow copy)
74     v = GB_mx_mxArray_to_Vector (pargin [5], "v input", false, true) ;
75     if (v == NULL)
76     {
77         FREE_ALL ;
78         mexErrMsgTxt ("v failed") ;
79     }
80 
81     // get add operator
82     bool user_complex = (Complex != GxB_FC64)
83         && (u->type == Complex || v->type == Complex) ;
84     GrB_BinaryOp add ;
85     if (!GB_mx_mxArray_to_BinaryOp (&add, pargin [3], "add",
86         w->type, user_complex) || add == NULL)
87     {
88         FREE_ALL ;
89         mexErrMsgTxt ("add failed") ;
90     }
91 
92     // get accum, if present
93     user_complex = (Complex != GxB_FC64)
94         && (w->type == Complex || add->xtype == Complex) ;
95     GrB_BinaryOp accum ;
96     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [2], "accum",
97         w->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     // w<mask> = accum(w,u+v)
111     METHOD (GrB_Vector_eWiseAdd_BinaryOp_(w, mask, accum, add, u, v, desc)) ;
112 
113     // return w to MATLAB as a struct and free the GraphBLAS w
114     pargout [0] = GB_mx_Vector_to_mxArray (&w, "w output", true) ;
115 
116     FREE_ALL ;
117 }
118 
119