Esempio n. 1
0
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)) ;
    fprintf (stderr, "\npagerank_demo:\n") ;
    printf  (        "\npagerank_demo:\n") ;

    //--------------------------------------------------------------------------
    // 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 ( ) ;
}
Esempio n. 2
0
GrB_Info ktruss_graphblas       // compute the k-truss of a graph
(
    GrB_Matrix *p_C,            // output k-truss subgraph, C
    GrB_Matrix A,               // input adjacency matrix, A, not modified
    const int64_t k,            // find the k-truss, where k >= 3
    int64_t *p_nsteps           // # of steps taken
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    // ensure k is 3 or more
    if (k < 3) return (GrB_INVALID_VALUE) ;

    if (p_C == NULL || p_nsteps == NULL) return (GrB_NULL_POINTER) ;

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

    GrB_Info info ;
    GxB_SelectOp supportop = NULL ;

    GrB_Index n ;
    GrB_Matrix C = NULL ;
    OK (GrB_Matrix_nrows (&n, A)) ;
    OK (GrB_Matrix_new (&C, GrB_INT64, n, n)) ;

    // select operator
    int64_t support = (k-2) ;
    OK (GxB_SelectOp_new (&supportop, support_function, GrB_INT64)) ;

    // last_cnz = nnz (A)
    GrB_Index cnz, last_cnz ;
    OK (GrB_Matrix_nvals (&last_cnz, A)) ;

    //--------------------------------------------------------------------------
    // find the k-truss of A
    //--------------------------------------------------------------------------

    double tmult = 0 ;
    double tsel  = 0 ;

    for (int64_t nsteps = 1 ; ; nsteps++)
    {

        //----------------------------------------------------------------------
        // C<C> = C*C
        //----------------------------------------------------------------------

        GrB_Matrix Cin = (nsteps == 1) ? A : C ;
        double t1 = omp_get_wtime ( ) ;
        OK (GrB_mxm (C, Cin, NULL, GxB_PLUS_LAND_INT64, Cin, Cin, NULL)) ;
        double t2 = omp_get_wtime ( ) ;
        printf ("C<C>=C*C time: %g\n", t2-t1) ;
        tmult += (t2-t1) ;

        //----------------------------------------------------------------------
        // C = C .* (C >= support)
        //----------------------------------------------------------------------

        OK (GxB_select (C, NULL, NULL, supportop, C, &support, NULL)) ;

        double t3 = omp_get_wtime ( ) ;
        printf ("select time: %g\n", t3-t2) ;
        tsel += (t3-t2) ;

        //----------------------------------------------------------------------
        // check if the k-truss has been found
        //----------------------------------------------------------------------

        OK (GrB_Matrix_nvals (&cnz, C)) ;
        if (cnz == last_cnz)
        {
            printf ("ktruss_grb done: tmult %g tsel %g\n", tmult, tsel) ;
            (*p_C) = C ;                        // return the output matrix C
            (*p_nsteps) = nsteps ;              // return # of steps
            OK (GrB_free (&supportop)) ;        // free the select operator
            return (GrB_SUCCESS) ;
        }
        last_cnz = cnz ;
    }
}