Download presentation
Presentation is loading. Please wait.
Published byAubrey Barber Modified over 8 years ago
1
Traveling Courier / Milestone 4 Continued
2
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: 11900 calls to your find_path If 0.1 s per call 1190 s too slow
3
Dijkstra’s Algorithm What do we know at this point? Shortest path to every intersection in the blue circle
4
Dijkstra’s Algorithm Keep Going!
5
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
6
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!
7
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
8
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
9
Algorithm Challenge
10
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
11
Multithreading Why & How
12
Intel 8086 First PC microprocessor 1978 29,000 transistors 5 MHz ~10 clocks / instruction ~500,000 instructions / s
13
Intel Core i7 – “Skylake” 2015 1.5 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
14
CPU Scaling: Past & Future 1978 to 2015 –50,000x more transistors –16,000x more instructions / s The future: –Still get 2X transistors every 2 - 3 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
15
A Single-Threaded Program Instructions (code) Memory Global Variables Heap Variables (new) Stack (local variables)... Program Counter Stack Pointer CPU / Core
16
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
17
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
18
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
19
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
20
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
21
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
22
Possible Timing? Thread Time main “Main thread starting” “Main thread continues” myThread do_func_b () Exit Program Main thread starting Main thread continues Output
23
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; }
24
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
25
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.