1 //------------------------------------------------------------------------------
2 // GB_mex_dpagerank: compute pagerank with a real 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,iters] = GB_mex_dpagerank (A, method)"
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 PageRank *P = NULL ;
36 GrB_Index n = 0 ;
37 bool malloc_debug = GB_mx_get_global (true) ;
38
39 // check inputs
40 if (nargout > 3 || nargin < 1 || nargin > 2)
41 {
42 mexErrMsgTxt ("Usage: " USAGE) ;
43 }
44
45 // get the method
46 int GET_SCALAR (1, int, method, GxB_DEFAULT) ;
47
48 // get A (shallow copy)
49 A = GB_mx_mxArray_to_Matrix (pargin [0], "A", false, true) ;
50 if (A == NULL)
51 {
52 FREE_ALL ;
53 mexErrMsgTxt ("failed") ;
54 }
55
56 GrB_Matrix_nrows (&n, A) ;
57
58 // compute the PageRank P
59 int iters = 0 ;
60 GB_MEX_TIC ;
61 if (nargin > 1)
62 {
63 // printf ("dpagerank2, method %d\n", method) ;
64 info = dpagerank2 (&P, A, 100, 1e-5, &iters, method) ;
65 }
66 else // default method
67 {
68 info = dpagerank (&P, A) ;
69 }
70 GB_MEX_TOC ;
71
72 if (info != GrB_SUCCESS)
73 {
74 FREE_ALL ;
75 mexErrMsgTxt ("failed") ;
76 }
77
78 // return PageRank to MATLAB
79 pargout [0] = mxCreateDoubleMatrix (1, n, mxREAL) ;
80 pargout [1] = mxCreateDoubleMatrix (1, n, mxREAL) ;
81 pargout [2] = mxCreateDoubleScalar ((double) iters) ;
82
83 double *r = mxGetPr (pargout [0]) ;
84 double *irank = mxGetPr (pargout [1]) ;
85
86 // add one to the page ID to convert 0-based to 1-based
87 for (int64_t i = 0 ; i < n ; i++)
88 {
89 r [i] = P [i].pagerank ;
90 irank [i] = P [i].page + 1 ;
91 }
92
93 FREE_ALL ;
94 }
95
96