Functionality & Performance in Milestone 1

Slides:



Advertisements
Similar presentations
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Advertisements

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Graphs, Continued ECE 297.
Chapter 3 Working with Batches of Data. Objectives Understand vector class and how it can be used to collect, store and manipulate data. Become familiar.
Week 7 Part I Kyle Dewey. Overview Code from last time Array initialization Pointers vs. arrays Structs typedef Bubble sort (if time)
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
Lecture 5 – Function (Part 2) FTMK, UTeM – Sem /2014.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Module 20/21/22: The Standard Template Library
Working with Batches of Data
Review 1.
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
4. Java language basics: Function
Chapter 7: User-Defined Functions II
C++ Templates.
Motivation and Overview
Chapter 2 - Introduction to C Programming
HKN ECE 220: Fall 2017 Midterm 1 AJ Schroeder, Evan Lissoos, Utsav Kawrani 23rd September, 2017.
© Craig Zilles (adapted from slides by Howard Huang)
C Programming Tutorial – Part I
Administrative Items ECE 297.
Chapter 2 - Introduction to C Programming
Pointers and References
Stack Data Structure, Reverse Polish Notation, Homework 7
This pointer, Dynamic memory allocation, Constructors and Destructor
Hash Tables Part II: Using Buckets
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Functions Inputs Output
Object Oriented Programming COP3330 / CGS5409
Chapter 14 - Advanced C Topics
CMPE 152: Compiler Design October 4 Class Meeting
HKN ECE 220: Spring 2018 Midterm 1
Containers, Iterators, Algorithms, Thrust
Constructors and Other Tools
STL Iterators Separating Container from Data Access.
The Standard Template Library
STL Containers Some other containers in the STL.
Explaining issues with DCremoval( )
mdul-cntns-sub-cmmnts2.f90
Data Structures Unsorted Arrays
ECE 103 Engineering Programming Chapter 18 Iteration
Lab4 problems More about templates Some STL
Lecture 14: Problems with Lots of Similar Data
Functionality & Performance in Milestone 1
STL List.
Hashing.
Class rational part2.
The Three Attributes of an Identifier
© Craig Zilles (adapted from slides by Howard Huang)
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
SPL – PS1 Introduction to C++.
STL List.
A dictionary lookup mechanism
Presentation transcript:

Functionality & Performance in Milestone 1

Streets Database Library Milestone 1 at a Glance Main Executable Streets Database Library Street Map Library Tester Executable

Streets Database Library Milestone 1 at a Glance Cannot Modify! Main Executable Streets Database Library Street Map Library Tester Executable

Streets Database Library Milestone 1 at a Glance Already Implemented Cannot Modify! Main Executable To be implemented load_map close_map Streets Database Library Street Map Library Tester Executable load_map close_map

Streets Database Library Milestone 1 at a Glance Already Implemented Cannot Modify! Main Executable To be implemented load_map close_map Streets Database Library Street Map Library Tester Executable load_map Load, close, queries, etc. queries close_map

What you will need… To read the handout Seriously… At least one global variable (to pass performance tests) Why? Containers from the STL std::vector, std::list std::map, std::unordered_map, std::multimap Which containers should we choose?

What you are given…

Recommended Steps for m1 Compile Run ece297exercise 1

Recommended Steps for m1 Compile Run ece297exercise 1 /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:185: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:190: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:195: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:200: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:205: undefined reference to `find_intersection_street_names(unsigned int)' /tmp/ccVscFJr.o:/cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:210: more undefined references to `find_intersection_street_names(unsigned int)' follow collect2: error: ld returned 1 exit status Build Error: Failed to build unit tests. Compiler returned non-zero exit status 1

Recommended Steps for m1 Compile Run ece297exercise 1 Create “empty” implementations of functions in m1.h Make sure you return something if the function requires it No more build errors with ece297exercise

Recommended Steps for m1 Compile Run ece297exercise 1 Create “empty” implementations of functions in m1.h Fail all the tests Your goal for m1: Fail 0 of the tests

Breaking Maps Up Into Parts

Breaking Maps Up Into Parts Intersection Street Segment

Find Intersection Street Segments Return a vector of all the street segments connected to an intersection Our only input is an unsigned number Need to use something that is outside the scope of the function StreetsDatabaseAPI.h has what we need

Street Segments in StreetDatabaseAPI.h

Other Functions in StreetDatabaseAPI.h

Understanding the Functions Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 Street Segment ID: 352

Understanding the Functions Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352

Understanding the Functions getIntersectionStreetSegment(232, {0, 1, 2}); will return {311, 321, 352} Which street segment IDs correspond to 0, 1, or 2 is irrelevant. StreetsDatabaseAPI will ensure it is consistent for a given map Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352

Understanding the Functions getIntersectionStreetSegment(232, {0, 1, 2}); will return one of {311, 321, 352} Which street segment IDs correspond to 0, 1, or 2 is irrelevant. StreetsDatabaseAPI will ensure it is consistent for a given map Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352 getStreetSegmentInfo({311, 321, 352}) will get the struct we saw earlier containing additional information for the street segment ID

Other Functions in StreetDatabaseAPI.h Be very careful: IntersectionIndex, StreeSegmentIndex, etc. are all typedefs to unsigned. If you illogically compare an intersection ID with a street segment ID, you will not get a compile error. Use appropriate variable names to avoid this issue.

Implement the Function You will likely need one or more of these function calls: getIntersectionStreetSegmentCount(…) getIntersectionStreetSegment(…) getStreetSegmentInfo(…) Write your implementation here. Add street segment IDs to the ss_ids vector.

Run only the functionality tests for intersections: Test The Function Run only the functionality tests for intersections: Only 3 of 4 failed, which means we passed the functionality test for find_intersection_street_segments.

Complexity of Our Function Assume these are O(1): getIntersectionStreetSegmentCount( …) getIntersectionStreetSegment(…) getStreetSegmentInfo(…) Complexity: O(n) What is n? Can we reduce the complexity of our function?

Massive hint in m1.cpp

Massive hint in m1.cpp What data structure can we use for intersection names?

What are the inputs/outputs of our function? intersection_id Type: unsigned Output std::vector<unsigned>

Viewing STL data-structures as input/output STL Container Input Output Example vector unsigned integer Templated vector<MyObject> objects; … unsigned input = 5; MyObject output = objects[input]; list (similar to vector) map Templated Key Templated Value map<unsigned, MyObject> objects; unsigned input = 3; MyObject output = objects[input] unordered_map (similar to map)

Potential Data Structures Which data structures can be indexed with an unsigned intersection_id? What can these data structures contain? What is the complexity of accessing elements in the data structure?

Potential Data Structures Which data structures can be indexed with an unsigned intersection_id? std::vector, std::list, std::map, std::unordered_map What can these data structures contain? Anything, they are templated What is the complexity of accessing elements in the data structure? std::vector – O(1), std::list – O(n) std::map – O(log n), std::unordered_map – O(1) We would want something less complex than O(n)

Complexity versus Performance std::vector std::unordered_map Accessing an element takes O(1) Accessing an element takes O(1)

Complexity versus Performance std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1)

Complexity versus Performance std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1) An access involves: A call to a hash function to get an index A direct call to memory

Complexity versus Performance std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1) An access involves: A call to a hash function to get an index A direct call to memory The hash function takes time!

When do I need a hash function? Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? What does unordered_map’s hash function require?

When do I need a hash function? Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? std::string Custom class What does unordered_map’s hash function require?

When do I need a hash function? Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? std::string Custom class What does unordered_map’s hash function require? An operator== for the data type being used

Loading Data First Notice the global variable. Populate intersection_street_segments with the street segment IDs for each intersection.

Loading Data First – Option 1 First we loop through all intersections. Remember to use a useful variable name!

Loading Data First – Option 1 Then we add an empty “nested” vector into our global variable. This empty vector will contain the street segment IDs, which is the output of the function we were implementing.

Loading Data First – Option 1 Now, just like before, we loop through the number of street segments connected to the intersection.

Loading Data First – Option 1 … and we add the street segment IDs to our “nested” vector, which we can access using the operator[] of the global variable.

Loading Data First – Option 2 In Option 2, we remove the need to push back empty vectors into our global variable. Instead, we use vector’s “resize” function. This will change how many elements the vector holds, and allows us to use operator[] with a value of up to the number of intersections.

How does this impact the find function? Since the output of our global variable is a vector of street segment IDs, we can just index into it using the intersection ID. Our function is now one line long, but faster since it only has to do an array look up.

How does this impact performance? We moved the complexity to load_map What is the complexity of the load_map function? What is the complexity of find_intersection_street_segments? There is a performance test for load_map too! You have three seconds to load everything into your data structures