1 //------------------------------------------------------------------------------
2 // GB_mex_ipagerank: compute pagerank with an integer semiring
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 for testing only.
11
12 #include "GB_mex.h"
13 #include "graphblas_demos.h"
14
15 #define USAGE "[r,irank] = GB_mex_ipagerank (A)"
16
17 #define FREE_ALL \
18 { \
19 if (P != NULL) mxFree (P) ; \
20 GrB_Matrix_free_(&A) ; \
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 GrB_Info info = GrB_SUCCESS ;
34 GrB_Matrix A = NULL ;
35 iPageRank *P = NULL ;
36 GrB_Index n = 0 ;
37 bool malloc_debug = GB_mx_get_global (true) ;
38
39 // check inputs
40 if (nargout > 2 || nargin != 1)
41 {
42 mexErrMsgTxt ("Usage: " USAGE) ;
43 }
44
45 // get A (shallow copy)
46 A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
47 if (A == NULL)
48 {
49 FREE_ALL ;
50 mexErrMsgTxt ("failed") ;
51 }
52
53 GrB_Matrix_nrows (&n, A) ;
54
55 // compute the iPageRank P
56 GB_MEX_TIC ;
57 ipagerank (&P, A) ;
58 GB_MEX_TOC ;
59
60 // return iPageRank to MATLAB
61 pargout [0] = mxCreateDoubleMatrix (1, n, mxREAL) ;
62 pargout [1] = mxCreateDoubleMatrix (1, n, mxREAL) ;
63
64 double *r = mxGetPr (pargout [0]) ;
65 double *irank = mxGetPr (pargout [1]) ;
66
67 // add one to the page ID to convert 0-based to 1-based
68 for (int64_t i = 0 ; i < n ; i++)
69 {
70 r [i] = P [i].pagerank ;
71 irank [i] = P [i].page + 1 ;
72 }
73
74 FREE_ALL ;
75 }
76
77