Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Parallel Graph Algorithms Spring 2015 Implementing Parallel Graph Algorithms Lecture 3: Galois Primer Roman Manevich Ben-Gurion University.

Similar presentations


Presentation on theme: "Implementing Parallel Graph Algorithms Spring 2015 Implementing Parallel Graph Algorithms Lecture 3: Galois Primer Roman Manevich Ben-Gurion University."— Presentation transcript:

1 Implementing Parallel Graph Algorithms Spring 2015 Implementing Parallel Graph Algorithms Lecture 3: Galois Primer Roman Manevich Ben-Gurion University based on slides by Donald Lenharth and Donald Nguysn

2 Agenda Parallel programming with the Galois system – High-level concepts and constructs Operators Loops Graph data structures Scheduling – Measuring performance – Optimizing programs – Converting between graph formats How to conduct an experimental evaluation 2

3 Parallel programming with the Galois system 3

4 Galois System Parallel Program = Operator + Schedule + Parallel Data Structure OperatorsSchedulesData Structure API Calls SchedulersData Structures AllocatorThread Primitives Multicore User Program Galois System 4

5 Galois in Practice C++ library – Galois::for_each(begin, end, functor) – Galois::Graph::*, Galois::Bag, … Currently supports – Cautious operators (i.e., no undos) 5

6 Building Galois Programs Requirements – Linux, modern compiler, Boost headers Partial support for Solaris, Windows Partial support for Intel MIC, Arm, Power – Hugepages (optional) As easy as gcc… g++ -I${GDIR}/include –L${GDIR}/lib *.cpp –lgalois Galois distribution uses CMake to simplify build cmake ${GDIR}; make 6

7 Baseline Runtime System Speculative execution-based runtime Provides hooks (Joe++) to allow user to optimize performance Once program works correctly in parallel, then optimize 7

8 “Hello graph” Galois Program #include “Galois/Galois.h” #include “Galois/Graphs/LCGraph.h” struct Data { int value; float f; }; typedef Galois::Graph::LC_CSR_Graph Graph; typedef Galois::Graph::GraphNode Node; Graph graph; struct P { void operator()(Node n, Galois::UserContext & ctx) { graph.getData(n).value += 1; } }; int main(int argc, char** argv) { graph.structureFromGraph(argv[1]); Galois::for_each(graph.begin(), graph.end(), P()); return 0; } 8 Data structure Declarations Galois Iterator Operator Includes

9 A Galois Program Operator – The Context Iterator – Topology-Driven – Data-Driven Data Structures – API for graphs, etc Scheduling – Priorities, etc Miscellaneous directives

10 Example Operator //Operators are any valid C++ functor with the correct signature struct P { Graph& g; P(Graph& g) :g(g) {} void operator()(const Node& n, UserContext & ctx) { graph.getData(n).value += 1; } }; Galois::for_each(ii,ee,P(graph)); //Or as a lambda Galois::for_each(ii,ee, [&graph] (const Node& n, UserContext & ctx) { graph.getData(n).value += 1; });

11 The Operator Context void operator()(const Node& n, UserContext & ctx); Context is a handle to the loop-runtime UserContext has – breakLoop(); //Break out of the current parallel loop (eventually) – PerIterAllocTy& getPerIterAlloc(); //A per-iteration region allocator – void push(Args&&... args); //Add a new item to the worklist (forwards args to WorkItemType constructor)

12 Fast Local Memory void operator()(const Node& n, UserContext & ctx) { // This vector uses scalable allocation std::vector ::other> vec(ctx.getPerIterAlloc()); for (…) { vec.push_back(graph.getEdgeDst(ii)); } }

13 Applying an Operator: Topology //Standard Topology driven fixedpoint while (!fixedpoint()) { //Apply op to each node in the graph Galois::for_each(graph.begin(), graph.end(),Op(graph)); } //Standard Topology driven initialization Galois::for_each(graph.begin(), graph.end(), [&graph] (const Node& n, UserContext & ctx) { graph.getData(n).value = 0; });

14 Applying an Operator: Data-driven struct P { void operator()(int n, UserContext & ctx) { if (n < 100) { ctx.push(n+1); ctx.push(n+2); } }; //For_each has a single work item form //1 is the initial work item //Yes, you can work on abstract iteration spaces Galois::for_each(1,P());

15 Data Structures In Galois/Graph/* General Graph: FirstGraph.h Specialized graphs: LC_*.h – No edge/node creation/removal – Variants for different memory layouts – Except LC_Morph: allows new nodes with declared number of edges Others: Trees, Bags, Reducers

16 LC_CSR_Graph Local Computation, Compressed Sparse Row Key Typedefs: – GraphNode: node handle – edge_iterator – iterator Key Functions: – nodeData& getData(GraphNode) – edgeData& getEdgeData(edge_iterator) – GraphNode getEdgeDst(edge_iterator) – iterator begin() – iterator end() – edge_iterator edge_begin(GraphNode) – edge_iterator edge_end(GraphNode)

17 LC_CSR_Graph template struct LC_CSR_Graph { typedef … GraphNode; typedef … edge_iterator; typedef … iterator; iterator begin(); iterator end(); edge_iterator edge_begin(GraphNode); edge_iterator edge_end(GraphNode); NodeData& getData(GraphNode); EdgeData& getEdgeData(edge_iterator); GraphNode getEdgeDst(edge_iterator); };

18 LC_CSR_Graph Example //Sum values on edges and nodes typedef LC_CSR_Graph Graph; typedef Graph::iterator iterator; typedtypedef ef Graph::edge_iterator edge_iterator; Graph g; Galois::Graph::readGraph(graph, filename); for (iterator ii = g.begin(), ei = g.end(); ii != ei; ++ii) { double sum = g.getData(*ii); for (edge_iterator jj = g.edge_begin(*ii), ej = g.edge_end(*ii); jj != ej; ++jj) { sum += g.getEdgeData(jj); } } //C++11 for (auto n : g) { double sum = g.getData(n); for (auto edge : g.out_edges(n)) { sum += graph.getEdgeData(edge); } }

19 scheduling 19

20 Scheduling Abstractly, iterations of for_each loop are placed in an unordered collection of tasks Often, programmers do not need to worry about the scheduling of tasks to threads But, more explicit control is available through scheduling interface 20

21 Galois schedulers Various scheduling policies available – In namespace Galois::WorkList – In include/Galois/WorkList/ 21 template struct LIFO {}; template struct FIFO {}; template struct ChunkedLIFO {}; template struct dChunkedLIFO {}; template struct AltChunkedLIFO {}; template struct StableIterator {}; template struct BulkSynchronous {}; template struct LocalQueue {}; template struct OrderedByIntegerMetric {};

22 Using schedulers using namespace Galois::WorkList; typedef dChunkedLIFO Sched; Galois::for_each(g.begin(), g.end, Op(), Galois::wl ());

23 Standard Scheduling Options Lifo (Fifo) Like: – LIFO, ChunkedLIFO, dChunkedLIFO – AltChunkedLIFO No worklist pushes: – StableIterator Round Based: – BulkSynchronous New Work stays local: – LocalQueue Priority Scheduling: – OrderedByIntegerMetric Most have options (including sub-schedulers)

24 Useful Directives Loopname: report statistics by loop – for_each(…, loopname(“name”)); Timers: Galois::StatTimer – May be named PAPI measurements reportpageAlloc: report pages allocated setActiveThreads(n) : limit threads to n

25 Extended example: sssp 25

26 Example: SSSP Find the shortest distance from source node to all other nodes in a graph – Label nodes with tentative distance – Assume non-negative edge weights Algorithms – Chaotic relaxation O(2 V ) – Bellman-Ford O(VE) – Dijkstra’s algorithm O(E log V) Uses priority queue – Δ-stepping Uses sequence of bags to prioritize work Δ=1, O(E log V) Δ=∞, O(VE) Different algorithms are different schedules for applying relaxations – SSSP needs priority scheduling for work efficiency 1 9 2 3 1 ∞ ∞ 0 Active edge Neighborhood Activity 1 9 4 3 1 3 Operator Edge relaxation ∞ 2 26

27 Algorithmic Variants == Scheduling Chaotic Relaxation: – Specify a non-priority scheduler E.g. dChunkedFIFO Dijkstra: – Use Ordered Executor Delta-Stepping Like: – Specify OBIM priority scheduler Bellman-Ford – Push every edge in non-priority scheduler – Execute – Repeat #nodes times

28 Simple (PUSH) SSSP in Galois struct SSSP { void operator()(UpdateRequest& req, Galois::UserContext & ctx) const { unsigned& data = graph.getData(req.second); if (req.first > data) return; for (Graph::edge_iterator ii=graph.edge_begin(req.second), ee = graph.edge_end(req.second); ii != ee; ++ii) relax_edge(data, ii, ctx); } };

29 Relax Edge (PUSH) void relax_edge(unsigned src_data, Graph::edge_iterator ii, Galois::UserContext & ctx) { GNode dst = graph.getEdgeDst(ii); unsigned int edge_data = graph.getEdgeData(ii); unsigned& dst_data = graph.getData(dst); unsigned int newDist = dst_data + edge_data; if (newDist < dst_data) { dst_data = newDist; ctx.push(std::make_pair(newDist, dst)); }

30 Specifying Schedule and Running Galois::Graph::readGraph(graph, filename); Galois::for_each(graph.begin(), graph.end(), Init()); using namespace Galois::WorkList; typedef dChunkedLIFO dChunk; typedef OrderedByIntegerMetric OBIM; graph.getData(*graph.begin()) = 0; Galois::for_each(std::make_pair(0U, *graph.begin()), SSSP(), Galois::wl ()); WorkList Load SSSP

31 Implementation Variants: Push V.S. Pull Simple optimization to control concurrency costs, locks, etc. Push: Look at node and update neighbors Pull: Look at neighbors and update self Pull seems “obviously” better, but in practice it depends on algorithm, scheduling, and data

32 Pull SSSP struct SSSP { void operator()(GNode req, Galois::UserContext & ctx) { //update self for (auto ii = graph.edge_begin(req), ee = graph.edge_end(req); ii != ee; ++ii) { auto edist = graph.getEdgeData(ii), ndist = graph.getData(graph.getEdgeDst(ii)); if (edist + ndist < data) data = edist + ndist; } //push higher neighbors for (auto ii = graph.edge_begin(req), ee = graph.edge_end(req); ii != ee; ++ii) { auto edist = graph.getEdgeData(ii), ndist = graph.getData(graph.getEdgeDst(ii)); if (ndist > data + edist) ctx.push(graph.getEdgeDst(ii)); } };

33 SSSP Demo Start with chaotic algorithm and vary scheduling policy – Different policies give different amounts of work and scalability but all policies produce correct executions Policies – FIFO – ChunkedFIFO FIFO of fixed size chunks of items – dChunkedFIFO A ChunkedFIFO per package with stealing between ChunkedFIFOs – OBIM Generalization of sequence of bags when sequence is sparse 33

34 Algorithmic Variants == Scheduling Chaotic Relaxation: – Specify a non-priority scheduler E.g. dChunkedFIFO Dijkstra: – Use Ordered Executor Delta-Stepping Like: – Specify OBIM priority scheduler Bellman-Ford – Push every edge in non-priority scheduler – Execute – Repeat #nodes times

35 scheduling 35

36 Best Scheduling Policies 1.Exploit locality 2.Control the total amount of work 3.Use architecture-aware concurrent data structures that must scale to many threads 4.Vary according to application 36 Require sophisticated implementations

37 Contribution A language for scheduling policies – Declarative: sophisticated schedulers w/o writing code – Effective: performance comparable to hand- written and often better than previous schedulers 37 Get good performance without users writing (serial or concurrent) scheduling code

38 38 FIFO LIFOChunkedLIFO(k)Ordered(f) ChunkedFIFO(k)OrderedByMetric(g)Random 2 3 1 1 1 2 3 3 2 ChunkedFIFO(2)FIFOOrderedByMetric(g) order Rules and their composition

39 AppOrderScheduling Policy PFPFIFO [Goldberg88] PFPHL OrderedByMetric(  n. -n.height) FIFO [Cherkassy95] SSSP D -stepping OrderedByMetric(  n.  n.w / D  + …) FIFO [Meyer98] SSSPDijkstra Ordered(  a,b. a.w  b.w) [Dijkstra59] DMRLocal stack ChunkedFIFO(k) Local: LIFO [Kulkarni08] DTBRIO OrderedByMetric(  p. p.rnd) ChunkedFIFO(k) [Amenta03] MATCHINGABMP OrderedByMetric(  n. n.lvl) FIFO [ABMP91] BPRBP Ordered(  a,b. a.old-a.new  b.old-b.new) [Elidan06] Application-specific Policies 39

40 Tuning a Galois program 40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57 What’s in the Galois distribution 57

58 Lonestar Collection of irregular algorithms

59 Graph Analytics DSLs Easy to implement their APIs on top of Galois system – Galois implementations called PowerGraph-g, Ligra- g, etc. – About 200-300 lines of code each 59  GraphLabLow et al. (UAI ’10)  PowerGraphGonzalez et al. (OSDI ’12)  GraphChiKyrola et al. (OSDI ’12)  LigraShun and Blelloch (PPoPP ’13)  PregelMalewicz et al. (SIGMOD ‘10)  …

60 Conducting experimental evaluation 60

61 Performance Metrics 61 #include “Galois/Galois.h” #include “Galois/Statistics.h” #include int main(int argc, char** argv) { Galois::StatManager stats; //Set number of threads Galois::setActiveThreads(4); //Report statistics by loop name Galois::for_each(…, Galois::loopname(“MyLoop”)); //Insert own timers Galois::StatTimer timer(“Phase2”); timer.start(); … timer.stop(); std::cout << “Phase 2 took “ << timer.get() << “ milliseconds\n”; //Report on memory activity Galois::reportPageAlloc(“AfterPhase2”); return 0; }

62 Evaluation Platform – 40-core system 4 socket, Xeon E7-4860 (Westmere) – 128 GB RAM Applications – Breadth-first search (bfs) – Connected components (cc) – Approximate diameter (dia) – PageRank (pr) – Single-source shortest paths (sssp) Inputs – twitter50 (50 M nodes, 2 B edges, low-diameter) – road (20 M nodes, 60 M edges, high-diameter) Comparison with – Ligra (shared memory) – PowerGraph (distributed) Runtimes with 64 16-core machines (1024 cores) does not beat one 40-core machine using Galois 62 “A lightweight infrastructure for graph analytics” Nguyen, Lenharth, Pingali (SOSP 2013)

63 Conclusion 63

64 Next assignment Serial implementation – Due date: 15/4 – See details on web-page Next lecture tentatively on 24/6 – Project presentations 64


Download ppt "Implementing Parallel Graph Algorithms Spring 2015 Implementing Parallel Graph Algorithms Lecture 3: Galois Primer Roman Manevich Ben-Gurion University."

Similar presentations


Ads by Google