1 //------------------------------------------------------------------------------
2 // GB_mex_tricount: count the number of triangles in a graph
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 // Usage: ntri = GB_mex_tricount (method, A, E, L, U) ;
9 // see tricount.c for a description of the inputs
10 
11 // Not all methods use all matrices.  If a matrix is not required, pass in
12 // sparse(1) for that parameter.  This is ugly, but this function is not meant
13 // to have a clean API.  It is simply for testing.
14 
15 //------------------------------------------------------------------------------
16 
17 #include "GB_mex.h"
18 
19 #define USAGE "[ntri t] = GB_mex_tricount (method, A, E, L, U)"
20 
21 #define FREE_ALL                        \
22 {                                       \
23     GrB_Matrix_free_(&A) ;              \
24     GrB_Matrix_free_(&E) ;              \
25     GrB_Matrix_free_(&L) ;              \
26     GrB_Matrix_free_(&U) ;              \
27     GB_mx_put_global (true) ;           \
28 }
29 
mexFunction(int nargout,mxArray * pargout[],int nargin,const mxArray * pargin[])30 void mexFunction
31 (
32     int nargout,
33     mxArray *pargout [ ],
34     int nargin,
35     const mxArray *pargin [ ]
36 )
37 {
38 
39     bool malloc_debug = GB_mx_get_global (true) ;
40     GrB_Matrix A = NULL, E = NULL, L = NULL, U = NULL ;
41 
42     // check inputs
43     if (nargout > 2 || nargin != 5)
44     {
45         mexErrMsgTxt ("Usage: " USAGE) ;
46     }
47 
48     #define GET_DEEP_COPY ;
49     #define FREE_DEEP_COPY ;
50 
51     // get the method.  Default is Sandia method (outer product)
52     int GET_SCALAR (0, int, method, 3) ;
53 
54     // get A, E, L, and U
55     A = GB_mx_mxArray_to_Matrix (pargin [1], "A", false, true) ;
56     E = GB_mx_mxArray_to_Matrix (pargin [2], "E", false, true) ;
57     L = GB_mx_mxArray_to_Matrix (pargin [3], "L", false, true) ;
58     U = GB_mx_mxArray_to_Matrix (pargin [4], "U", false, true) ;
59 
60     // count the triangles
61     double t [2] ;
62     int64_t ntri ;
63     METHOD (tricount (&ntri, method, A, E, L, U, t)) ;
64 
65     // return ntri to MATLAB
66     pargout [0] = mxCreateDoubleScalar ((double) ntri) ;
67 
68     // return t to MATLAB (compute time)
69     if (nargout > 0)
70     {
71         pargout [1] = mxCreateDoubleScalar (t [0] + t [1]) ;
72     }
73 
74     FREE_ALL ;
75 }
76 
77