1 //------------------------------------------------------------------------------
2 // GB_mex_mxv: w<mask> = accum(w,A*u)
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_mxv (w, mask, accum, semiring, A, u, desc)"
13 
14 #define FREE_ALL                                    \
15 {                                                   \
16     GrB_Vector_free_(&w) ;                          \
17     GrB_Vector_free_(&u) ;                          \
18     GrB_Matrix_free_(&A) ;                          \
19     GrB_Vector_free_(&mask) ;                       \
20     if (semiring != Complex_plus_times)             \
21     {                                               \
22         if (semiring != NULL)                       \
23         {                                           \
24             GrB_Monoid_free_(&(semiring->add)) ;    \
25         }                                           \
26         GrB_Semiring_free_(&semiring) ;             \
27     }                                               \
28     GrB_Descriptor_free_(&desc) ;                   \
29     GB_mx_put_global (true) ;                       \
30 }
31 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])32 void mexFunction
33 (
34     int nargout,
35     mxArray *pargout [ ],
36     int nargin,
37     const mxArray *pargin [ ]
38 )
39 {
40 
41     bool malloc_debug = GB_mx_get_global (true) ;
42     GrB_Vector w = NULL ;
43     GrB_Vector u = NULL ;
44     GrB_Matrix A = NULL ;
45     GrB_Vector mask = NULL ;
46     GrB_Semiring semiring = NULL ;
47     GrB_Descriptor desc = NULL ;
48 
49     // check inputs
50     if (nargout > 1 || nargin < 6 || nargin > 7)
51     {
52         mexErrMsgTxt ("Usage: " USAGE) ;
53     }
54 
55     // get w (make a deep copy)
56     #define GET_DEEP_COPY \
57     w = GB_mx_mxArray_to_Vector (pargin [0], "w input", true, true) ;
58     #define FREE_DEEP_COPY GrB_Vector_free_(&w) ;
59     GET_DEEP_COPY ;
60     if (w == NULL)
61     {
62         FREE_ALL ;
63         mexErrMsgTxt ("w failed") ;
64     }
65 
66     // get mask (shallow copy)
67     mask = GB_mx_mxArray_to_Vector (pargin [1], "mask", false, false) ;
68     if (mask == NULL && !mxIsEmpty (pargin [1]))
69     {
70         FREE_ALL ;
71         mexErrMsgTxt ("mask failed") ;
72     }
73 
74     // get A (shallow copy)
75     A = GB_mx_mxArray_to_Matrix (pargin [4], "A input", false, true) ;
76     if (A == NULL)
77     {
78         FREE_ALL ;
79         mexErrMsgTxt ("A failed") ;
80     }
81 
82     // get u (shallow copy)
83     u = GB_mx_mxArray_to_Vector (pargin [5], "A input", false, true) ;
84     if (u == NULL)
85     {
86         FREE_ALL ;
87         mexErrMsgTxt ("u failed") ;
88     }
89 
90     bool user_complex = (Complex != GxB_FC64) && (w->type == Complex) ;
91 
92     // get semiring
93     if (!GB_mx_mxArray_to_Semiring (&semiring, pargin [3], "semiring",
94         w->type, user_complex))
95     {
96         FREE_ALL ;
97         mexErrMsgTxt ("semiring failed") ;
98     }
99 
100     // get accum, if present
101     GrB_BinaryOp accum ;
102     if (!GB_mx_mxArray_to_BinaryOp (&accum, pargin [2], "accum",
103         w->type, user_complex))
104     {
105         FREE_ALL ;
106         mexErrMsgTxt ("accum failed") ;
107     }
108 
109     // get desc
110     if (!GB_mx_mxArray_to_Descriptor (&desc, PARGIN (6), "desc"))
111     {
112         FREE_ALL ;
113         mexErrMsgTxt ("desc failed") ;
114     }
115 
116     // w<mask> = accum(w,A*u)
117     METHOD (GrB_mxv (w, mask, accum, semiring, A, u, desc)) ;
118 
119     // return w to MATLAB as a struct and free the GraphBLAS w
120     pargout [0] = GB_mx_Vector_to_mxArray (&w, "w output", true) ;
121 
122     FREE_ALL ;
123 }
124 
125