27 #ifndef _H_GRB_PAGERANK 28 #define _H_GRB_PAGERANK 39 namespace algorithms {
132 typename IOType,
typename NonzeroT
140 const IOType alpha = 0.85,
141 const IOType conv = 0.0000001,
142 const size_t max = 1000,
143 size_t *
const iterations =
nullptr,
144 double *
const quality =
nullptr 157 const size_t n =
nrows( L );
158 const IOType zero = realRing.template getZero< IOType >();
162 if( n !=
ncols( L ) ) {
165 if(
size( pr ) != n ) {
168 if(
size( pr ) != n ) {
171 if(
size( pr_next ) != n ||
172 size( pr_nextnext ) != n ||
187 if( alpha <= 0 || alpha >= 1 ) {
200 if(
nnz( pr ) != n ) {
201 ret =
set( pr, static_cast< IOType >( 1 ) / static_cast< IOType >( n ) );
206 ret = ret ? ret :
set( pr_nextnext, zero );
217 ret = ret ? ret :
set( pr_next, 1 );
218 ret = ret ? ret :
set( row_sum, 0 );
220 vxm< descr | descriptors::dense | descriptors::transpose_matrix >(
221 row_sum, pr_next, L, pattern_ring
227 std::cout <<
"Prelude to iteration 0:\n";
229 [ &row_sum, &pr_next, &pr ](
const size_t i ) {
232 std::cout << i <<
": " << row_sum[ i ] <<
"\t" << pr[ i ] <<
"\t" 233 << pr_next[ i ] <<
"\n";
236 pr, pr_next, row_sum );
242 [ &row_sum, &alpha, &zero ](
const size_t i ) {
243 assert( row_sum[ i ] >= zero );
244 if( row_sum[ i ] > zero ) {
245 row_sum[ i ] = alpha / row_sum[ i ];
253 std::cout <<
"Row sum array:\n";
255 [ &row_sum ](
const size_t i ) {
257 std::cout << i <<
": " << row_sum[ i ] <<
"\n";
264 IOType dangling = zero;
266 IOType residual = zero;
272 residual = dangling = 0;
275 std::cout <<
"Current PR array:\n";
277 [ &pr ](
const size_t i ) {
280 std::cout << i <<
": " << pr[ i ] <<
"\n";
292 [ &pr_next, &row_sum, &dangling, &pr ](
const size_t i ) {
294 if( row_sum[ i ] == 0 ) {
299 pr_next[ i ] = pr[ i ] * row_sum[ i ];
301 }, row_sum, pr, pr_next
312 ret = foldl< grb::descriptors::invert_mask >(
313 dangling, pr, row_sum, addM
318 ret = ret ? ret :
set( pr_next, 0 );
320 pr_next, pr, row_sum,
327 #if defined _DEBUG && defined _GRB_WITH_LPF 328 for(
size_t dbg = 0; dbg < spmd<>::nprocs(); ++dbg ) {
331 std::cout <<
"Next PR array (under construction):\n";
333 [ &pr_next, s ](
const size_t i ) {
336 std::cout << i <<
", " << s <<
": " << pr_next[ i ] <<
"\n";
347 std::cout << s <<
": dangling (1) = " << dangling <<
"\n";
351 dangling = ( alpha * dangling + 1 - alpha ) / static_cast< IOType >( n );
354 std::cout << s <<
": dangling (2) = " << dangling <<
"\n";
360 ret = ret ? ret :
set( pr_nextnext, 0 ); assert( ret ==
SUCCESS );
361 ret = ret ? ret : vxm< descr >( pr_nextnext, pr_next, L, realRing );
363 assert( n ==
grb::nnz( pr_nextnext ) );
365 #if defined _DEBUG && defined _GRB_WITH_LPF 366 for(
size_t dbg = 0; dbg < spmd<>::nprocs(); ++dbg ) {
368 std::cout << s <<
": nextnext PR array (after vxm):\n";
370 [ &pr_nextnext, s ](
const size_t i ) {
373 std::cout << i <<
", " << s <<
": " << pr_nextnext[ i ] <<
"\n";
379 for(
size_t k = 0; k < spmd<>::nprocs(); ++k ) {
381 std::cout <<
"old pr \t scaled input \t alpha * pr * H at PID " 384 [ &pr, &pr_next, &pr_nextnext ](
const size_t i ) {
387 std::cout << pr[ i ] <<
"\t" << pr_next[ i ] <<
"\t" 388 << pr_nextnext[ i ] <<
"\n";
390 }, pr, pr_next, pr_nextnext
403 [ &pr, &pr_nextnext, &dangling, &residual, &zero ](
const size_t i ) {
405 const IOType oldval = pr[ i ];
407 pr[ i ] = pr_nextnext[ i ] + dangling;
409 if( oldval > pr[ i ] ) {
410 residual += oldval - pr[ i ];
412 residual += pr[ i ] - oldval;
414 pr_nextnext[ i ] = zero;
428 ret = foldl< descriptors::dense >( pr_nextnext, dangling, addM );
433 ret = dot< descriptors::dense >(
442 std::swap( pr, pr_nextnext );
451 if( conv != zero && residual <= conv ) {
break; }
455 std::cout <<
"Iteration " << iter <<
", " 456 <<
"residual = " << residual << std::endl;
460 }
while( ret ==
SUCCESS && iter < max );
463 if( iterations !=
nullptr ) {
466 if( quality !=
nullptr ) {
473 std::cerr <<
"Error while running simple pagerank algorithm: " 477 }
else if( residual <= conv ) {
480 std::cerr <<
"Info: simple pagerank converged after " << iter
488 std::cout <<
"Info: simple pagerank did not converge after " 489 << iter <<
" iterations.\n";
500 #endif // end _H_GRB_PAGERANK RC simple_pagerank(Vector< IOType > &pr, const Matrix< NonzeroT > &L, Vector< IOType > &pr_next, Vector< IOType > &pr_nextnext, Vector< IOType > &row_sum, const IOType alpha=0.85, const IOType conv=0.0000001, const size_t max=1000, size_t *const iterations=nullptr, double *const quality=nullptr)
The canonical PageRank algorithm.
Definition: simple_pagerank.hpp:134
RC eWiseApply(Vector< OutputType, backend, Coords > &z, const InputType1 alpha, const InputType2 beta, const OP &op=OP(), const Phase &phase=EXECUTE, const typename std::enable_if< !grb::is_object< OutputType >::value &&!grb::is_object< InputType1 >::value &&!grb::is_object< InputType2 >::value &&grb::is_operator< OP >::value, void >::type *const =nullptr)
Computes , out of place, operator version.
Definition: blas1.hpp:208
RC set(Vector< DataType, backend, Coords > &x, const T val, const Phase &phase=EXECUTE, const typename std::enable_if< !grb::is_object< DataType >::value &&!grb::is_object< T >::value, void >::type *const =nullptr) noexcept
Sets all elements of a vector to the given value.
Definition: io.hpp:858
Standard identity for numerical addition.
Definition: identities.hpp:57
A call to a primitive has determined that one of its arguments was illegal as per the specification o...
Definition: rc.hpp:143
An ALP/GraphBLAS matrix.
Definition: matrix.hpp:72
RC
Return codes of ALP primitives.
Definition: rc.hpp:47
A GraphBLAS vector.
Definition: vector.hpp:64
Standard identity for numerical multiplication.
Definition: identities.hpp:79
size_t nnz(const Vector< DataType, backend, Coords > &x) noexcept
Request the number of nonzeroes in a given vector.
Definition: io.hpp:479
static constexpr Descriptor no_operation
Indicates no additional pre- or post-processing on any of the GraphBLAS function arguments.
Definition: descriptors.hpp:63
This operator multiplies the two input parameters and writes the result to the output variable.
Definition: ops.hpp:208
unsigned int Descriptor
Descriptors indicate pre- or post-processing for some or all of the arguments to an ALP/GraphBLAS cal...
Definition: descriptors.hpp:54
size_t nrows(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the row size of a given matrix.
Definition: io.hpp:286
static RC allreduce(IOType &inout, const Operator op=Operator())
Schedules an allreduce operation of a single object of type IOType per process.
Definition: collectives.hpp:121
This operator assigns the left-hand input if the right-hand input evaluates true.
Definition: ops.hpp:85
size_t ncols(const Matrix< InputType, backend, RIT, CIT, NIT > &A) noexcept
Requests the column size of a given matrix.
Definition: io.hpp:339
static size_t pid() noexcept
Definition: spmd.hpp:61
Indicates when one of the grb::algorithms has failed to achieve its intended result,...
Definition: rc.hpp:154
Standard identity for the logical AND operator.
Definition: identities.hpp:178
Collection of various properties on the given ALP/GraphBLAS backend.
Definition: properties.hpp:52
This operator takes the sum of the two input parameters and writes it to the output variable.
Definition: ops.hpp:175
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
The main header to include in order to use the ALP/GraphBLAS API.
For backends that support multiple user processes this class defines some basic primitives to support...
Definition: spmd.hpp:51
RC eWiseLambda(const Func f, const Vector< DataType, backend, Coords > &x, Args...)
Executes an arbitrary element-wise user-defined function f on any number of vectors of equal length.
Definition: blas1.hpp:3746
size_t size(const Vector< DataType, backend, Coords > &x) noexcept
Request the size of a given vector.
Definition: io.hpp:235
Indicates the primitive has executed successfully.
Definition: rc.hpp:54
size_t capacity(const Vector< InputType, backend, Coords > &x) noexcept
Queries the capacity of the given ALP/GraphBLAS container.
Definition: io.hpp:388
A generalised semiring.
Definition: semiring.hpp:190
One or more of the ALP/GraphBLAS objects passed to the primitive that returned this error have mismat...
Definition: rc.hpp:90
This operator returns the absolute difference between two numbers.
Definition: ops.hpp:541
std::string toString(const RC code)
A generalised monoid.
Definition: monoid.hpp:54