1 //------------------------------------------------------------------------------
2 // GB_mex_mxm: 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_mxm (C, Mask, accum, semiring, 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_Matrix_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_Matrix A = NULL ;
43     GrB_Matrix B = NULL ;
44     GrB_Matrix C = NULL ;
45     GrB_Matrix 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 C (make a deep copy)
56     #define GET_DEEP_COPY \
57     C = GB_mx_mxArray_to_Matrix (pargin [0], "C input", true, true) ;
58     #define FREE_DEEP_COPY GrB_Matrix_free_(&C) ;
59     GET_DEEP_COPY ;
60     if (C == NULL)
61     {
62         FREE_ALL ;
63         mexErrMsgTxt ("C failed") ;
64     }
65 
66     // get Mask (shallow copy)
67     Mask = GB_mx_mxArray_to_Matrix (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 B (shallow copy)
83     B = GB_mx_mxArray_to_Matrix (pargin [5], "B input", false, true) ;
84     if (B == NULL)
85     {
86         FREE_ALL ;
87         mexErrMsgTxt ("B failed") ;
88     }
89 
90     bool user_complex = (Complex != GxB_FC64) && (C->type == Complex) ;
91 
92     // get semiring
93     if (!GB_mx_mxArray_to_Semiring (&semiring, pargin [3], "semiring",
94         C->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         C->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     // C<Mask> = accum(C,A*B)
117     METHOD (GrB_mxm (C, Mask, accum, semiring, A, B, desc)) ;
118 
119     // return C to MATLAB as a struct and free the GraphBLAS C
120     pargout [0] = GB_mx_Matrix_to_mxArray (&C, "C output from GrB_mxm", true) ;
121     FREE_ALL ;
122 }
123 
124