Skip to content

Commit cb753b7

Browse files
committed
Merge pull request #98 from erikzenker/topic-pagerank
Pagerank example
2 parents 7e5c8c3 + 6223115 commit cb753b7

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ add_test(graybat_check_build "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --ta
9090
add_test(graybat_example_build "${CMAKE_COMMAND}" --build ${CMAKE_BINARY_DIR} --target example)
9191
add_test(graybat_test_run mpiexec --allow-run-as-root -n 2 check )
9292
add_test(graybat_gol_run mpiexec --allow-run-as-root gol 90 4 )
93+
add_test(graybat_pr_run mpiexec --allow-run-as-root pagerank )
9394
set_tests_properties(graybat_test_run PROPERTIES DEPENDS graybat_check_build)
9495
set_tests_properties(graybat_gol_run PROPERTIES DEPENDS graybat_example_build)
96+
set_tests_properties(graybat_pr_run PROPERTIES DEPENDS graybat_example_build)
9597

9698
# Install
9799
file(GLOB CMAKE_FILES "${CMAKE_CURRENT_SOURCE_DIR}/*.cmake")

example/pagerank.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* @example chain.cpp
3+
*
4+
* @brief Data is send through a chain of compute
5+
* nodes and every node increments the value.
6+
*
7+
*/
8+
9+
// STL
10+
#include <iostream> /* std::cout */
11+
#include <vector> /* std::vector */
12+
13+
// GRAYBAT
14+
#include <graybat/Cage.hpp>
15+
#include <graybat/communicationPolicy/BMPI.hpp>
16+
#include <graybat/graphPolicy/BGL.hpp>
17+
18+
// GRAYBAT mappings
19+
#include <graybat/mapping/Consecutive.hpp>
20+
21+
// GRAYBAT pattern
22+
#include <graybat/pattern/Random.hpp>
23+
24+
struct PageRank {
25+
PageRank() : pageRank({1}){ }
26+
std::vector<float> pageRank;
27+
};
28+
29+
int exp() {
30+
/***************************************************************************
31+
* Configuration
32+
****************************************************************************/
33+
34+
// CommunicationPolicy
35+
typedef graybat::communicationPolicy::BMPI CP;
36+
typedef CP::Config Config;
37+
38+
// GraphPolicy
39+
typedef graybat::graphPolicy::BGL<PageRank> GP;
40+
41+
// Cage
42+
typedef graybat::Cage<CP, GP> Cage;
43+
typedef typename Cage::Event Event;
44+
typedef typename Cage::Vertex Vertex;
45+
typedef typename Cage::Edge Edge;
46+
47+
/***************************************************************************
48+
* Initialize Communication
49+
****************************************************************************/
50+
size_t const n = 1000;
51+
size_t const min = 1;
52+
size_t const max = 10;
53+
size_t const seed = 1234;
54+
Config config;
55+
Cage cage(config);
56+
57+
cage.setGraph(graybat::pattern::Random<GP>(n, min, max, seed));
58+
59+
// Distribute vertices
60+
cage.distribute(graybat::mapping::Consecutive());
61+
62+
/***************************************************************************
63+
* Run Simulation
64+
****************************************************************************/
65+
std::vector<Event> events;
66+
size_t const nIterations = 20;
67+
float const dampingFactor = 0.5;
68+
69+
for(size_t iteration = 0; iteration < nIterations; iteration++){
70+
71+
// Calculate relative pagerank and spread it to
72+
// adjacent vertices
73+
for(Vertex &v : cage.hostedVertices){
74+
assert(cage.getAdjacentVertices(v).size() > 0);
75+
std::vector<float> relativePageRank{1};
76+
relativePageRank[0] = v().pageRank[0] / static_cast<float>(cage.getAdjacentVertices(v).size());
77+
v.spread(relativePageRank, events);
78+
}
79+
80+
// Calculate vertex page rank based on
81+
// relative page rank of adjacent vertices
82+
for(Vertex &v : cage.hostedVertices){
83+
std::vector<float> relativePageRank{1};
84+
float relativePageRankSum = 0;
85+
86+
for(Edge e : cage.getInEdges(v)){
87+
cage.recv(e, relativePageRank);
88+
relativePageRankSum += relativePageRank[0];
89+
}
90+
v().pageRank[0] = (1 - dampingFactor) + dampingFactor * relativePageRankSum;
91+
}
92+
93+
for(unsigned i = 0; i < events.size(); ++i){
94+
events.back().wait();
95+
events.pop_back();
96+
}
97+
}
98+
99+
// Print page final page rank
100+
for(Vertex &v : cage.hostedVertices){
101+
std::cout << "Pagerank[" <<v.id << "] : " << v().pageRank[0] << std::endl;
102+
}
103+
104+
return 0;
105+
106+
}
107+
108+
int main(){
109+
exp();
110+
return 0;
111+
}

include/graybat/pattern/Random.hpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
// CLIB
4+
#include <assert.h>
5+
6+
// STL
7+
#include <utility> /* std::make_pair */
8+
#include <cstdlib> /* std::rand */
9+
10+
// GRAYBAT
11+
#include <graybat/graphPolicy/Traits.hpp>
12+
13+
namespace graybat {
14+
15+
namespace pattern {
16+
17+
template<typename T_GraphPolicy>
18+
struct Random {
19+
20+
using GraphPolicy = T_GraphPolicy;
21+
using VertexDescription = graybat::graphPolicy::VertexDescription<GraphPolicy>;
22+
using EdgeDescription = graybat::graphPolicy::EdgeDescription<GraphPolicy>;
23+
using GraphDescription = graybat::graphPolicy::GraphDescription<GraphPolicy>;
24+
using EdgeProperty = graybat::graphPolicy::EdgeProperty<GraphPolicy>;
25+
using VertexProperty = graybat::graphPolicy::VertexProperty<GraphPolicy>;
26+
27+
size_t const nVertices;
28+
size_t const minEdgesPerVertex;
29+
size_t const maxEdgesPerVertex;
30+
31+
Random(size_t const nVertices, size_t const minEdgesPerVertex, size_t const maxEdgesPerVertex, size_t const seed) :
32+
nVertices(nVertices),
33+
minEdgesPerVertex(minEdgesPerVertex),
34+
maxEdgesPerVertex(maxEdgesPerVertex)
35+
{
36+
assert(minEdgesPerVertex <= maxEdgesPerVertex);
37+
std::srand(seed);
38+
}
39+
40+
GraphDescription operator()(){
41+
std::vector<VertexDescription> vertices;
42+
std::vector<EdgeDescription> edges;
43+
44+
for(size_t vertex_i = 0; vertex_i < nVertices; ++vertex_i){
45+
vertices.push_back(std::make_pair(vertex_i, VertexProperty()));
46+
}
47+
48+
for(size_t vertex_i = 0; vertex_i < nVertices; ++vertex_i){
49+
size_t nEdges = std::max(minEdgesPerVertex, (std::rand() % maxEdgesPerVertex));
50+
for(size_t j = 0; j < nEdges; ++j){
51+
size_t adjacentVertex_i = std::rand() % nVertices;
52+
edges.push_back(std::make_pair(std::make_pair(vertices[vertex_i].first, vertices[adjacentVertex_i].first), EdgeProperty()));
53+
}
54+
}
55+
56+
return std::make_pair(vertices, edges);
57+
}
58+
59+
};
60+
61+
} /* pattern */
62+
63+
} /* graybat */

0 commit comments

Comments
 (0)