CS 284a, 5 November 97Copyright (c) 1997-98, John Thornley1 CS 284a Lecture Wednesday, 5 November 1997.

Slides:



Advertisements
Similar presentations
SORTING Lecture 12B CS2110 – Spring InsertionSort 2 pre: b 0 b.length ? post: b 0 b.length sorted inv: or: b[0..i-1] is sorted b 0 i b.length sorted.
Advertisements

Lecture 20: 11/12/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 9 Prepared by İnanç TAHRALI.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
 2004 SDU Lecture11- All-pairs shortest paths. Dynamic programming Comparing to divide-and-conquer 1.Both partition the problem into sub-problems 2.Divide-and-conquer.
CS 284a, 18 November 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 18 November, 1997.
All Pairs Shortest Paths and Floyd-Warshall Algorithm CLRS 25.2
Finding Optimal Refueling Policies Shieu-Hong Lin Biola University.
Floyd’s Algorithm (shortest-path problem) Section 8.2.
CS2420: Lecture 37 Vladimir Kulyukin Computer Science Department Utah State University.
Bubble Sort Notes David Beard CS181. Bubble Sort for Strings Double pass algorithm to sort a single dimensional array. Inner loop “bubbles” largest element.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CS 284a, 4 November 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 4 November, 1997.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
CS 284a Lecture Wednesday, 26 November, 1997
Vladimir Kulyukin Computer Science Department Utah State University
Understanding BubbleSort CS-502 (EMC) Fall Understanding BubbleSort CS-502, Operating Systems Fall 2009 (EMC) (Slides include materials from Modern.
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Algorithm Efficiency and Sorting
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
CS 284a, 14 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 14 October, 1997.
CS2420: Lecture 8 Vladimir Kulyukin Computer Science Department Utah State University.
All-Pairs Shortest Paths
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Lecture 4 Loops.
Using Dijkstra’s Algorithm to Find a Shortest Path from a to z 1.
More Dynamic Programming Floyd-Warshall Algorithm.
CSCI-455/552 Introduction to High Performance Computing Lecture 18.
1 Chapter 28 Weighted Graph Applications. 2 Objectives F To represent weighted edges using adjacency matrices and priority queues (§28.2). F To model.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 28 Weighted Graph.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
1 The Floyd-Warshall Algorithm Andreas Klappenecker.
Parallel Programming: All-Pairs Shortest Path CS599 David Monismith Based upon notes from multiple sources.
Algorithms LECTURE 14 Shortest Paths II Bellman-Ford algorithm
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
1 Dijkstra’s Algorithm Dr. Ying Lu RAIK 283 Data Structures & Algorithms.
CS 284a, 29 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 29 October, 1997.
Lecture 13 Algorithm Analysis
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Basic Graph Algorithms Programming Puzzles and Competitions CIS 4900 / 5920 Spring 2009.
Introduction to Algorithms All-Pairs Shortest Paths My T. UF.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Aims: To learn about some simple sorting algorithms. To develop understanding of the importance of efficient algorithms. Objectives: All:Understand how.
CS 284a, 21 October 1997 Copyright (c) , John Thornley1 CS 284a Lecture Tuesday, 21 October, 1997.
CPU Efficiency Issues.
Recursion: The Mirrors
Unweighted Shortest Path Neil Tang 3/11/2010
Shortest Path Graph represents highway system Edges have weights
Data Structures and Algorithms
Graph Search Lecture 17 CS 2110 Spring 2018.
Simulating Reference Parameters in C
Algorithms Lecture # 27 Dr. Sohail Aslam.
Lecture 12 Shortest Path.
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

CS 284a, 5 November 97Copyright (c) , John Thornley1 CS 284a Lecture Wednesday, 5 November 1997

CS 284a, 5 November 97Copyright (c) , John Thornley2 Timing a Multithreaded for Loop What is the overhead of thread creation/termination? Time the following null loop: Time the loop on one processor. Time the loop for different values of n. #pragma multithreadable chunk_size(1) mapping(simple) for (i = 0; i < n, i++) ;

CS 284a, 5 November 97Copyright (c) , John Thornley3 Tempting... but this won’t work #include double elapsed; set_processor_usage(1); start_timing(); #pragma multithreadable chunk_size(1) mapping(simple) for (i = 0; i < n, i++) ; finish_timing(&elapsed); printf(“Multithreaded loop took %f seconds\n”, elapsed); Granularity of clock is milliseconds. Loop may run in small number of milliseconds.

CS 284a, 5 November 97Copyright (c) , John Thornley4 Need to time many iterations #include double elapsed; set_processor_usage(1) start_timing(); for (k = 0; k < num_iterations; k++) #pragma multithreadable chunk_size(1) mapping(simple) for (i = 0; i < n, i++) ; finish_timing(&elapsed); printf(“Multithreaded loop took %f seconds\n”, elapsed/k); Experiment to find large enough num_iterations. In general, should subtract out time for k loop.

CS 284a, 5 November 97Copyright (c) , John Thornley5 Timing a Barrier What is the overhead of threads passing a barrier? Time barrier implementation of the following null loops: Subtract out time for one multithreaded null loop. Time the loops on one processor. Time the loops for different values of n. #pragma multithreadable chunk_size(1) mapping(simple) for (i = 0; i < n, i++) ; #pragma multithreadable chunk_size(1) mapping(simple) for (i = 0; i < n, i++) ;

CS 284a, 5 November 97Copyright (c) , John Thornley6 All-Pairs Shortest-Paths Problem Fully-connected graph with n vertices. Given: for all i, j : edge[i][j] = length of edge from vertex i to vertex j. Want: for all i, j : path[i][j] = length of shortest path from vertex i to vertex j. void seq_shortest_paths( int n, const unsigned int edge[N][N], unsigned int path[N][N]);

CS 284a, 5 November 97Copyright (c) , John Thornley7 All-Pairs Shortest-Paths Example edge path

CS 284a, 5 November 97Copyright (c) , John Thornley8 All-Pairs Shortest-Paths Algorithm void seq_shortest_paths( int n, const unsigned int edge[N][N], unsigned int path[N][N]) { int i, j, k; unsigned int new_path; for (i = 0; i < n; i++) for (j = 0; j < n; j++) path[i][j] = edge[i][j]; for (k = 0; k < n; k++) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { new_path = path[i][k] + path[k][j]; if (new_path < path[i][j]) path[i][j] = new_path; } Invariant: After outer-loop iteration k, for all i, j : path[i][j] =shortest path from vertex i to vertex j with intermediate vertices in the set { 0, 1,..., k }.

CS 284a, 5 November 97Copyright (c) , John Thornley9 Multithreaded All-Pairs Shortest Paths Is This OK? void multi_shortest_paths( int n, const unsigned int edge[N][N], unsigned int path[N][N], int t) { int i, j, k; unsigned int new_path; for (i = 0; i < n; i++) for (j = 0; j < n; j++) path[i][j] = edge[i][j]; #pragma multithreadable chunk_size(1) mapping(blocked(t)) for (k = 0; k < n; k++) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { new_path = path[i][k] + path[k][j]; if (new_path < path[i][j]) path[i][j] = new_path; } No! Erroneous sharing of variables! Different k iterations write to same path[i][j] variables.

CS 284a, 5 November 97Copyright (c) , John Thornley10 Multithreaded All-Pairs Shortest Paths Is This OK? void multi_shortest_paths( int n, const unsigned int edge[N][N], unsigned int path[N][N], int t) { int i, j, k; unsigned int new_path; for (i = 0; i < n; i++) for (j = 0; j < n; j++) path[i][j] = edge[i][j]; for (k = 0; k < n; k++) #pragma multithreadable chunk_size(1) mapping(blocked(t)) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { new_path = path[i][k] + path[k][j]; if (new_path < path[i][j]) path[i][j] = new_path; } No! Erroneous sharing of variables! Different i iterations write/read same path[i][j] variables.

CS 284a, 5 November 97Copyright (c) , John Thornley11 Multithreaded All-Pairs Shortest-Paths This is OK (But Inefficient) void multi_shortest_paths( int n, const unsigned int edge[N][N], unsigned int path[N][N], int t) { int i, j, k; unsigned int old_path[N][N]; unsigned int new_path; for (i = 0; i < n; i++) for (j = 0; j < n; j++) path[i][j] = edge[i][j]; for (k = 0; k < n; k++) { copy(n, old_path, path); /* old_path = path; */ #pragma multithreadable chunk_size(1) mapping(blocked(t)) for (i = 0; i < n; i++) for (j = 0; j < n; j++) { new_path = old_path[i][k] + old_path[k][j]; if (new_path < old_path[i][j]) path[i][j] = new_path; }

CS 284a, 5 November 97Copyright (c) , John Thornley12 Multithreaded All-Pairs Shortest-Paths Outline of Efficient Solution Two arrays, path and temp, as in inefficient solution. Swap pointers on each k iteration, instead of copying arrays. Should we create/destroy new set of threads on each k iteration, or use barrier implementation? Time for n = 1000 is 45 sec. 45 msec per k iteration. Barrier implementation necessary if multithreading overhead is significant fraction of 45 msec.