1 / 14

CS 240A : Breadth-first search in Cilk ++

CS 240A : Breadth-first search in Cilk ++. Thanks to Charles E. Leiserson for some of these slides. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 16. 17. 18. 19. Breadth First Search. Level-by-level graph traversal Serially complexity: Θ (m+n). Graph: G(E,V)

berit
Download Presentation

CS 240A : Breadth-first search in Cilk ++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 240A : Breadth-first search in Cilk++ Thanks to Charles E. Leiserson for some of these slides

  2. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) Graph: G(E,V) E: Set of edges (size m) V: Set of vertices (size n)

  3. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1

  4. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1 5 Level 2 2

  5. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1 5 Level 2 2 6 3 Level 3 9

  6. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1 5 Level 2 2 6 3 Level 3 9 4 16 Level 4 10 7

  7. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1 5 Level 2 2 6 3 Level 3 9 4 4 16 16 Level 4 10 7 11 8 Level 5 17

  8. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Level-by-level graph traversal • Serially complexity:Θ(m+n) 1 Level 1 5 Level 2 2 6 3 Level 3 9 4 4 16 16 Level 4 10 7 11 8 Level 5 17 18 Level 6 12

  9. 1 2 3 4 5 6 7 8 9 10 11 12 16 17 18 19 Breadth First Search • Who is parent(19)? • If we use a queue for expanding the frontier? • Does it actually matter? 1 Level 1 5 Level 2 2 6 3 Level 3 9 4 4 16 16 Level 4 10 7 11 8 Level 5 17 18 Level 6 12

  10. Parallel BFS Bag<T> has an associative reduce function that merges two sets • Way #1: A custom reducer void BFS(Graph *G, Vertex root) { Bag<Vertex> frontier(root); while ( ! frontier.isEmpty() ) { cilk::hyperobject< Bag<Vertex> > succbag(); cilk_for (int i=0; i< frontier.size(); i++) { for( Vertex v in frontier[i].adjacency() ) { if( ! v.unvisited() ) succbag() += v; } } frontier = succbag.getValue(); } } operator+=(Vertex & rhs) also marks rhs “visited”

  11. Parallel BFS • Way #2: Concurrent writes + List reducer void BFS(Graph *G, Vertex root) { list<Vertex> frontier(root); Vertex * parent = new Vertex[n]; while ( ! frontier.isEmpty() ) { cilk_for (int i=0; i< frontier.size(); i++) { for( Vertex v in frontier[i].adjacency() ) { if ( ! v.visited() ) parent[v] = frontier[i]; } } ... } } An intentional data race How to generate the new frontier?

  12. Parallel BFS Run cilk_for loop again void BFS(Graph *G, Vertex root) { ... while ( ! frontier.isEmpty() ) { ... hyperobject< reducer_list_append<Vertex> > succlist(); cilk_for (int i=0; i< frontier.size(); i++) { for( Vertex v in frontier[i].adjacency() ) { if ( parent[v] == frontier[i] ) { succlist.push_back(v); v.visit(); // Mark “visited” } } } frontier = succlist.getValue(); } } !v.visited() check is not necessary. Why?

  13. Work: T1(n) = Θ(m+n) Span: T∞(n) = Θ(d) Parallelism: T1(n) = Θ((m+n)/d) T∞(n) Parallel BFS • Each level is explored with Θ(1) span • Graph G has at most d, at least d/2 levels • Depending on the location of root • d=diameter(G)

  14. Parallel BFS Caveats • d is usually small • d = lg(n) for scale-free graphs • But the degrees are not bounded  • Parallel scaling will be memory-bound • Lots of burdened parallelism, • Loops are skinny • Especially to the root and leaves of BFS-tree • You are not “expected” to parallelize BFS part of Homework #4 • You may do it for extra credit though 

More Related