Traveling Courier / Milestone 4 Continued. Recall Pre-compute all shortest paths you might need? –Then just look up delays during pertubations How many.

Slides:



Advertisements
Similar presentations
Recursion CS 367 – Introduction to Data Structures.
Advertisements

Dijkstra’s Algorithm Keep Going!. Pre-Computing Shortest Paths How many paths to pre-compute? Recall: –Using single-source to single-dest find_path: Need.
Threads. What do we have so far The basic unit of CPU utilization is a process. To run a program (a sequence of code), create a process. Processes are.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Computer Science 1620 Loops.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Chocolate Bar! luqili. Milestone 3 Speed 11% of final mark 7%: path quality and speed –Some cleverness required for full marks –Implement some A* techniques.
Milestone 3: Finding Routes ECE 297. Directions: How?
CS352-Week 2. Topics Heap allocation References Pointers.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
Lecture 1: Performance EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2013, Dr. Rozier.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
February 11, 2005 More Pointers Dynamic Memory Allocation.
10/27: Lecture Topics Survey results Current Architectural Trends Operating Systems Intro –What is an OS? –Issues in operating systems.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
1 Web Based Programming Section 8 James King 12 August 2003.
Pointers OVERVIEW.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
MULTICORE PROCESSOR TECHNOLOGY.  Introduction  history  Why multi-core ?  What do you mean by multicore?  Multi core architecture  Comparison of.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Writing Functions in Assembly
Repetitive Structures
Measuring Where CPU Time Goes
Overview 4 major memory segments Key differences from Java stack
Chapter 3: Process Concept
Depth First Seach: Output Fix
Operating Systems (CS 340 D)
Bill Tucker Austin Community College COSC 1315
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
Threads in C Caryl Rahn.
CS399 New Beginnings Jonathan Walpole.
Process Management Presented By Aditya Gupta Assistant Professor
A Lecture for the c++ Course
The University of Adelaide, School of Computer Science
CPU Efficiency Issues.
Writing Functions in Assembly
Dynamic Memory CSCE 121 J. Michael Moore.
Operating Systems (CS 340 D)
CSC 253 Lecture 8.
CMSC 341 Prof. Michael Neary
CSC 253 Lecture 8.
Discussion section #2 HW1 questions?
Overview 4 major memory segments Key differences from Java stack
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Given the code to the left:
Dynamic Memory A whole heap of fun….
Dynamic Memory A whole heap of fun….
Explaining issues with DCremoval( )
Milestone 3: Finding Routes
Multithreading Why & How.
Arrays an array of 5 ints is filled with 3,2,4,1,7
M4 and Parallel Programming
Mental Health and Wellness Resources
Programming with Shared Memory
Chapter 12 Pipelining and RISC
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Efficiently Estimating Travel Time
Pointers and dynamic objects
Passing Arguments and The Big 5
The Stack.
CMSC 202 Lesson 6 Functions II.
A Level Computer Science Topic 5: Computer Architecture and Assembly
Dynamic Memory CSCE 121.
Pointers, Dynamic Data, and Reference Types
Pointer analysis John Rollinson & Kaiyuan Li
Presentation transcript:

Traveling Courier / Milestone 4 Continued

Recall Pre-compute all shortest paths you might need? –Then just look up delays during pertubations How many shortest paths to pre-compute? –Using single-source to single-dest find_path: Need any delivery location to any other travel time: –2N * (2N-1)  9900 calls for N = 50 Plus any depot to any delivery location –M * 2N  1000 calls for N = 50, M = 10 Plus any delivery location to any depot –2N * M  1000 calls –Total: calls to your find_path If 0.1 s per call  1190 s  too slow

Dijkstra’s Algorithm What do we know at this point? Shortest path to every intersection in the blue circle

Dijkstra’s Algorithm Keep Going!

Pre-Computing Travel Time Paths Using single-source to all destinations –Need any delivery location to any other 2N calls  100 if N = 50 –Plus any depot to any delivery location M calls  10 –Plus any delivery location to any depot 0 calls –Total: 110 calls Is this the minimum? –No, with small change can achieve: 101 calls Get from earlier call

Is This Fast Enough? Recall: –Dijkstra’s algorithm can search whole graph –Especially with multiple destinations –O(N) items to put in wavefront –Using heap / priority_queue: O (log N) to add / remove 1 item from wavefront Total: –N log N –Can execute in well under a second –OK!

How Do I Finish by the Time Limit? #include #define TIME_LIMIT 30 // m4: 30 second time limit int main ( ) { clock_t startTime = clock (); // Clock “ticks” do { myOptimizer (); clock_t currentTime = clock (); float timeSecs = ((float) (currentTime – startTime)) / CLOCKS_PER_SEC; // Keep optimizing until within 10% of time limit } while (timeSecs < 0.9 * TIME_LIMIT);... } Simple, but gives CPU time

Better Time Limit #include // Time utilities using namespace std; #define TIME_LIMIT 30 // m4: 30 second time limit int main ( ) { auto startTime = chrono::high_resolution_clock::now(); bool timeOut = false; while (!timeOut) { myOptimizer (); auto currentTime = chrono::high_resolution_clock::now(); auto wallClock = chrono::duration_cast > ( currentTime - startTime); // Keep optimizing until within 10% of time limit if (wallClock.count() > 0.9 * TIME_LIMIT) timeOut = true; }... } Inside namespace chrono Static member function: can call without an object Better, gives actual elapsed time no matter how many CPUs you are using Time difference Time difference in seconds

Algorithm Challenge

Algorithms: Challenge Question Frank likes what he calls “cool” numbers For cool numbers, there are integers x and y such that –Cool number = x 2 = y 3 –For example, 1 is cool (= 1 2 = 1 3 ) and 64 is cool (= 8 2 = 4 3 ) –25 is not cool (= 5 2, but no integer cubed = 25) 1.Write program to print all cool numbers between 1 and N 2.Calculate the computational complexity 3.Mail me program & complexity: first 5 of lowest complexity  chocolate bar in class Monday Source: ACM Programming Competition

Multithreading Why & How

Intel 8086 First PC microprocessor ,000 transistors 5 MHz ~10 clocks / instruction ~500,000 instructions / s

Intel Core i7 – “Skylake” billion transistors 4.0 GHz ~15 clocks / instruction, but ~30 instructions in flight  Average ~2 instructions completed / clock ~8 billion instructions / s die photo: courtesy techpowerup.com

CPU Scaling: Past & Future 1978 to 2015 –50,000x more transistors –16,000x more instructions / s The future: –Still get 2X transistors every years –But transistors not getting much faster  CPU clock speed saturating –~30 instructions in flight Complexity & power to go beyond this climbs rapidly Slow growth in instructions / cycle –Impact: CPU speed no longer increasing rapidly But can fit many processors (cores) on a single chip Using multiple cores now important Multithreading: one program using multiple cores at once

A Single-Threaded Program Instructions (code) Memory Global Variables Heap Variables (new) Stack (local variables)... Program Counter Stack Pointer CPU / Core

A Multi-Threaded Program Instructions (code) Memory Global Variables Heap Variables (new) Stack1 (local variables)... Program Counter Stack Pointer Core1 Program Counter Stack Pointer Core2 Stack2 (local variables) thread 1 thread 2 Shared by all threads Each thread gets own local variables

Thread Basics Each thread has own program counter –Can be executing a different function –Is (almost always) executing a different instruction from other threads Each thread has own stack –Has its own copy of local variables (all different) Each thread sees same global variables Dynamically allocated memory –Shared by all threads –Any thread with a pointer to it can access

Implications Threads can communicate through memory –Global variables –Dynamically allocated memory –Fast communication! Must be careful threads don’t conflict in reads/write to same memory –What if two threads update the same global variable at the same time? –Not clear which update wins! Can have more threads than CPUs –Time share the CPUs

Multi-Threading Program start: 1 “main” thread created Need more: construct threads #include #include // C++ 11 feature using namespace std; void start_func_for_thread ( ) { cout << “myThread lives!\n”; do_func_a (); } int main() { cout << “Main thread starting.\n”; thread myThread (start_func_for_thread); cout << “Main thread continues!\n”; do_func_b (); return 0; } Compile: g++ --std=c++11 -pthread main.cpp

Thread Timing Thread Time main “Main thread starting” “Main thread continues” myThread do_func_b () “myThread lives!” do_func_a () Exit Program Main thread starting myThread lives! Main thread continues Output

Another Possible Timing Thread Time main “Main thread starting” “Main thread continues” myThread do_func_b () “myThread lives!” do_func_a () Exit Program Main thread starting Main thread continues myThread lives! Output

Possible Timing? Thread Time main “Main thread starting” “Main thread continues” myThread do_func_b () Exit Program Main thread starting Main thread continues Output

Multi-Threading #include #include // C++ 11 feature using namespace std; void start_func_for_thread ( ) { cout << “myThread lives!\n”; do_func_a (); } int main() { cout << “Main thread starting.\n”; thread myThread (start_func_for_thread); cout << “Main thread continues!\n”; // main thread waits here until myThread finishes myThread.join (); do_func_b (); return 0; }

New Thread Timing Thread Time main “Main thread starting” “Main thread continues” myThread do_func_b () “myThread lives!” do_func_a () Exit Program Main thread starting myThread lives! Main thread continues Output join construct thread

Many Threads #define NUM_THREADS 10 void call_from_thread (int tid) { cout << “Thread “ << tid << “: Godfrey Hounsfield is #” << tid << endl; } int main() { thread myThread[NUM_THREADS]; for (int i = 0; i < NUM_THREADS; i++) // Launch threads myThread[i] = thread(call_from_thread, i); cout << “Main thread: Godfrey’s the main man!\n”; // Main thread waits for all threads to complete for (int i = 0; i < NUM_THREADS; i++) myThread[i].join(); return 0; } Can pass parameters to thread start function