File: pagerank_demo.c

package info (click to toggle)
suitesparse 1%3A5.8.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 152,716 kB
  • sloc: ansic: 774,385; cpp: 24,213; makefile: 6,310; fortran: 1,927; java: 1,826; csh: 1,686; ruby: 725; sh: 535; perl: 225; python: 209; sed: 164; awk: 60
file content (145 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//------------------------------------------------------------------------------
// GraphBLAS/Demo/Program/pagerank_demo.c: PageRank via various semirings
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2020, All Rights Reserved.
// http://suitesparse.com   See GraphBLAS/Doc/License.txt for license.

//------------------------------------------------------------------------------

// Read a graph from a file and compute the pagerank of its nodes, using
// dpagerank and ipagerank.

// usage:  ./pagerank_demo   < file
//         ./pagerank_demo 0 < file
//         ./pagerank_demo 1 < file
//
// default is 0-based, for the matrices in the Matrix/ folder
//
// The infile has one line per edge in the graph; these have the form
//
//  i j x
//
// where A(i,j)=x is performed by GrB_Matrix_build, to construct the matrix.
// The dimensions of A are assumed to be the largest row and column indices,
// plus one (in read_matrix.c).
//
// A must be square.

// macro used by OK(...) to free workspace if an error occurs
#define FREE_ALL                    \
{                                   \
    GrB_Matrix_free (&A) ;          \
    if (Pd != NULL) free (Pd) ;     \
    if (Pi != NULL) free (Pi) ;     \
    if (P2 != NULL) free (P2) ;     \
}

#include "graphblas_demos.h"

int main (int argc, char **argv)
{

    //--------------------------------------------------------------------------
    // initializations
    //--------------------------------------------------------------------------

    GrB_Info info ;
    GrB_Matrix A = NULL ;
    PageRank *Pd = NULL, *P2 = NULL ;
    iPageRank *Pi = NULL ;

    double tic [2], t ;
    OK (GrB_init (GrB_NONBLOCKING)) ;
    int nthreads ;
    OK (GxB_Global_Option_get (GxB_GLOBAL_NTHREADS, &nthreads)) ;
    fprintf (stderr, "\npagerank_demo: nthreads: %d\n", nthreads) ;
    printf  (        "\npagerank_demo: nthreads: %d\n", nthreads) ;

    //--------------------------------------------------------------------------
    // read a matrix from stdin
    //--------------------------------------------------------------------------

    bool one_based = false ;
    if (argc > 1) one_based = strtol (argv [1], NULL, 0) ;

    OK (read_matrix (&A,
        stdin,      // read matrix from stdin
        false,      // unsymmetric
        false,      // self edges OK
        one_based,  // 0-based or 1-based, depending on input arg
        true,       // read input file as Boolean
        true)) ;    // print status to stdout

    GrB_Index n, nvals ;
    OK (GrB_Matrix_nrows (&n, A)) ;
    OK (GrB_Matrix_nvals (&nvals, A)) ;

    //--------------------------------------------------------------------------
    // compute the page rank via a real semiring
    //--------------------------------------------------------------------------

    simple_tic (tic) ;
    OK (dpagerank (&Pd, A)) ;
    t = simple_toc (tic) ;

    fprintf (stderr, "n %g edges %g  dpagerank time : %14.6f iters: 20\n",
        (double) n, (double) nvals, t) ;
    printf  (        "n %g edges %g  dpagerank time : %14.6f iters: 20\n",
        (double) n, (double) nvals, t) ;

    //--------------------------------------------------------------------------
    // compute the page rank via an integer semiring
    //--------------------------------------------------------------------------

    simple_tic (tic) ;
    OK (ipagerank (&Pi, A)) ;
    t = simple_toc (tic) ;

    fprintf (stderr, "n %g edges %g  ipagerank time : %14.6f iters: 20\n",
        (double) n, (double) nvals, t) ;
    printf  (        "n %g edges %g  ipagerank time : %14.6f iters: 20\n",
        (double) n, (double) nvals, t) ;

    //--------------------------------------------------------------------------
    // compute the page rank via an extreme semiring
    //--------------------------------------------------------------------------

    int iters ;
    simple_tic (tic) ;
    OK (dpagerank2 (&P2, A, 100, 1e-5, &iters, GxB_DEFAULT)) ;
    t = simple_toc (tic) ;

    fprintf (stderr, "n %g edges %g  dpagerank time : %14.6f iters: %d\n",
        (double) n, (double) nvals, t, iters) ;
    printf  (        "n %g edges %g  dpagerank time : %14.6f iters: %d\n",
        (double) n, (double) nvals, t, iters) ;

    //--------------------------------------------------------------------------
    // print results
    //--------------------------------------------------------------------------

    int64_t limit = MIN (n, 5000) ;
    printf ("Top %g nodes:\n", (double) limit) ;
    for (int64_t i = 0 ; i < limit ; i++)
    {
        printf ("%5g d:[%6g : %16.8e] i:[%6g : %16.8e] x:[%6g : %16.8e]",
            (double) i,
            (double) Pd [i].page, (double) Pd [i].pagerank,
            (double) Pi [i].page, (double) Pi [i].pagerank,
            (double) P2 [i].page, (double) P2 [i].pagerank) ;
        if (Pd [i].page != Pi [i].page || Pd [i].page != P2 [i].page)
        {
            printf ("mismatch") ;
        }
        printf ("\n") ;
    }

    //--------------------------------------------------------------------------
    // free all workspace
    //--------------------------------------------------------------------------

    FREE_ALL ;
    GrB_finalize ( ) ;
}