virtual void execute(sgraph& output, const std::vector<sgraph*>& parents) { output.copy_vertex_field(field, new_field, group); }
void triple_apply_pagerank(sgraph& g, size_t& num_iter, double& total_pagerank, double& total_delta) { typedef sgraph_compute::sgraph_engine<flexible_type>::graph_data_type graph_data_type; typedef sgraph::edge_direction edge_direction; // initialize every vertex with core id kmin g.init_vertex_field(PAGERANK_COLUMN, reset_probability); g.init_vertex_field(PREV_PAGERANK_COLUMN, 1.0); g.init_vertex_field(DELTA_COLUMN, 0.0); // Initialize degree count sgraph_compute::sgraph_engine<flexible_type> ga; auto degrees = ga.gather( g, [=](const graph_data_type& center, const graph_data_type& edge, const graph_data_type& other, edge_direction edgedir, flexible_type& combiner) { combiner += 1; }, flexible_type(0), edge_direction::OUT_EDGE); g.add_vertex_field(degrees, OUT_DEGREE_COLUMN); num_iter = 0; total_delta = 0.0; total_pagerank = 0.0; timer mytimer; // Triple apply double w = (1 - reset_probability); const size_t degree_idx = g.get_vertex_field_id(OUT_DEGREE_COLUMN); const size_t pr_idx = g.get_vertex_field_id(PAGERANK_COLUMN); const size_t old_pr_idx = g.get_vertex_field_id(PREV_PAGERANK_COLUMN); sgraph_compute::triple_apply_fn_type apply_fn = [&](sgraph_compute::edge_scope& scope) { auto& source = scope.source(); auto& target = scope.target(); scope.lock_vertices(); target[pr_idx] += w * source[old_pr_idx] / source[degree_idx]; scope.unlock_vertices(); }; table_printer table({{"Iteration", 0}, {"L1 change in pagerank", 0}}); table.print_header(); for (size_t iter = 0; iter < max_iterations; ++iter) { if(cppipc::must_cancel()) { log_and_throw(std::string("Toolkit cancelled by user.")); } mytimer.start(); ++num_iter; g.init_vertex_field(PAGERANK_COLUMN, reset_probability); sgraph_compute::triple_apply(g, apply_fn, {PAGERANK_COLUMN}); // compute the change in pagerank auto delta = sgraph_compute::vertex_apply( g, flex_type_enum::FLOAT, [&](const std::vector<flexible_type>& vdata) { return std::abs((double)(vdata[pr_idx]) - (double)(vdata[old_pr_idx])); }); // make the current pagerank the old pagerank g.copy_vertex_field(PAGERANK_COLUMN, PREV_PAGERANK_COLUMN); g.replace_vertex_field(delta, DELTA_COLUMN); total_delta = sgraph_compute::vertex_reduce<double>(g, DELTA_COLUMN, [](const flexible_type& v, double& acc) { acc += (flex_float)v; }, [](const double& v, double& acc) { acc += v; }); table.print_row(iter+1, total_delta); // check convergence if (total_delta < threshold) { break; } } // end of pagerank iterations table.print_footer(); // cleanup g.remove_vertex_field(PREV_PAGERANK_COLUMN); g.remove_vertex_field(OUT_DEGREE_COLUMN); total_pagerank = sgraph_compute::vertex_reduce<double>(g, PAGERANK_COLUMN, [](const flexible_type& v, double& acc) { acc += (flex_float)v; }, [](const double& v, double& acc) { acc += v; }); }