Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functionality & Performance in Milestone 1

Similar presentations


Presentation on theme: "Functionality & Performance in Milestone 1"— Presentation transcript:

1 Functionality & Performance in Milestone 1

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

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

4 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

5 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

6 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?

7 What you are given… #include "m1.h“ #include "StreetsDatabaseAPI.h“
bool load_map(std::string map_path) { bool load_successful = false; //Load your map related data structures here return load_successful; } void close_map() { //Clean-up your map related data structures here

8 Recommended Steps for m1
Compile Run ece297exercise 1

9 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(int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:190: undefined reference to `find_intersection_street_names(int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:195: undefined reference to `find_intersection_street_names(int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:200: undefined reference to `find_intersection_street_names(int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:205: undefined reference to `find_intersection_street_names(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(int)' follow collect2: error: ld returned 1 exit status Build Error: Failed to build unit tests. Compiler returned non-zero exit status 1

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

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

12 Breaking Maps Up Into Parts

13 Breaking Maps Up Into Parts
Intersection Street Segment

14 Find Intersection Street Segments
Return a vector of all the street segments connected to an intersection Our only input is an int number Need to use something that is outside the scope of the function StreetsDatabaseAPI.h has what we need std::vector<int> find_intersection_street_segments(int intersection_id) { std::vector<int> ss_ids; //... return ss_ids; }

15 Street Segments in StreetDatabaseAPI.h
struct InfoStreetSegment { OSMID wayOSMID; //OSM ID of the source way IntersectionIndex from, to; //intersection ID this //segment runs from/to bool oneWay; //if true, then can only travel in //from->to direction int curvePointCount; //number of curve points //between the ends float speedLimit; //in km/h StreetIndex streetID; //index of street this segment //belongs to };

16 Other Functions in StreetDatabaseAPI.h
// access the street segments incident on the intersection (get the count // Nss first, then iterate through segmentNumber=0..Nss-1) int getIntersectionStreetSegmentCount(IntersectionIndex intersectionIdx); StreetSegmentIndex getIntersectionStreetSegment(int segmentNumber, IntersectionIndex intersectionIdx); InfoStreetSegment getInfoStreetSegment( StreetSegmentIndex streetSegmentIdx);

17 StreetDatabaseAPI: Naming Convention
int getIntersectionStreetSegmentCount(IntersectionIndex id); Lookup Usually O(1) Entity Attribute Often matches 1st argument

18 Understanding the Data
getIntersectionStreetSegment(0, 232) → returns 311 getIntersectionStreetSegment(1, 232) → returns 321 getIntersectionStreetSegment(2, 232) → returns 352 Ordering is irrelevant, but StreetsDatabaseAPI ensures consistent Intersection ID: 232 Street Segment ID: 311 Street Segment ID: 321 Street Segment ID: 352 getIntersectionStreetSegmentCount(232) → returns 3 getInfoStreetSegment({311, 321, 352}) will get a struct InfoStreetSegment with data about the street segment

19 Other Functions in StreetDatabaseAPI.h
// access the street segments incident on the intersection (get the count // Nss first, then iterate through segmentNumber=0..Nss-1) int getIntersectionStreetSegmentCount(IntersectionIndex intersectionIdx); StreetSegmentIndex getIntersectionStreetSegment(int segmentNumber, IntersectionIndex intersectionIdx); InfoStreetSegment getInfoStreetSegment( StreetSegmentIndex streetSegmentIdx); Be very careful: IntersectionIndex, StreetSegmentIndex, etc. are typedefs to int. You will not get a compiler error if you accidentally swap/compare them! Use descriptive variable names to avoid this.

20 Implement the Function
std::vector<int> find_intersection_street_segments(int intersection_id) { std::vector<int> ss_ids; for(int i = 0; i < getIntersectionStreetSegmentCount(intersection_id); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection_id); ss_ids.push_back(ss_id); } return ss_ids; Implement Function Here You will likely need one or more of these function calls: getIntersectionStreetSegmentCount(…) getIntersectionStreetSegment(…) getStreetSegmentInfo(…)

21 Run intersection tests only:
Test the Function std::vector<int> find_intersection_street_segments(int intersection_id) { std::vector<int> ss_ids; for(int i = 0; i < getIntersectionStreetSegmentCount(intersection_id); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection_id); ss_ids.push_back(ss_id); } return ss_ids; Run intersection tests only: ece297exercise 1 --run_tester ‘*Intersection*’ Passed Functionality Failed Performance

22 Complexity of our Function
std::vector<int> find_intersection_street_segments(int intersection_id) { std::vector<int> ss_ids; for(int i = 0; i < getIntersectionStreetSegmentCount(intersection_id); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection_id); ss_ids.push_back(ss_id); } return ss_ids; Assume these are O(1): getIntersectionStreetSegmentCount(…) getIntersectionStreetSegment(…) getStreetSegmentInfo(…) Complexity? O(n) What is n? Can we reduce the complexity of our function?

23 Massive hint in m1.cpp bool load_map(std::string map_path) { bool load_successful = false; //Load your map related data structures here return load_successful; } What data structure can we use for intersection street segments?

24 What are the Function’s Inputs & Outputs?
std::vector<int> find_intersection_street_segments(int intersection_id) { std::vector<int> ss_ids; for(int i = 0; i < getIntersectionStreetSegmentCount(intersection_id); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection_id); ss_ids.push_back(ss_id); } return ss_ids; Input Intersection ID int Output Collection of Street Segment IDs std::vector<int>

25 Viewing STL data-structures as input/output
STL Container Input Output Example vector integer Templated std::vector<MyObject> objects; input = 5; MyObject output = objects[input]; list iterator std::list<MyObject> objects; int input = 5; auto it = std::advance(objects.begin(), input); MyObject output = *it; map Templated Key Templated Value std::map<int, MyObject> objects; input = 3; MyObject output = objects[input] unordered_map (similar to map)

26 Potential Data Structures
Which data structures can be accessed with an int 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)

27 Complexity versus Performance
std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct memory access (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 memory access The hash function takes time!

28 When do I need a hash function?
Arrays 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

29 Loading Data First Note global variable
std::vector<std::vector<int>> intersection_street_segments; bool load_map(std::string map_name) { bool load_successful = loadStreetsDatabaseBIN(map_name); intersection_street_segments.resize(getNumberOfIntersections()); for(int intersection = 0; intersection < getNumberOfIntersections(); ++intersection) { for(int i = 0; i < getIntersectionStreetSegmentCount(intersection); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection); intersection_street_segments[intersection].push_back(ss_id); } return load_successful;

30 Nested Vectors std::vector<std::vector<int>> intersection_street_segments; std::vector<int> intersction_street_segments [0] [1] [2] [size() - 1] Intersection 1‘s Street Segment IDs int int int int

31 Populate intersection_street_segments here
Loading Data First Note global variable std::vector<std::vector<int>> intersection_street_segments; bool load_map(std::string map_name) { bool load_successful = loadStreetsDatabaseBIN(map_name); intersection_street_segments.resize(getNumberOfIntersections()); for(int intersection = 0; intersection < getNumberOfIntersections(); ++intersection) { for(int i = 0; i < getIntersectionStreetSegmentCount(intersection); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection); intersection_street_segments[intersection].push_back(ss_id); } return load_successful; Notice the global variable. Populate intersection_street_segments here

32 Loading Data First std::vector<std::vector<int>> intersection_street_segments; bool load_map(std::string map_name) { bool load_successful = loadStreetsDatabaseBIN(map_name); intersection_street_segments.resize(getNumberOfIntersections()); for(int intersection = 0; intersection < getNumberOfIntersections(); ++intersection) { for(int i = 0; i < getIntersectionStreetSegmentCount(intersection); ++i) { int ss_id = getIntersectionStreetSegment(i, intersection); intersection_street_segments[intersection].push_back(ss_id); } return load_successful; Notice the global variable. //Create empty //vector for each //intersection //Iterate through all intersections //Iterate through //all segments at //intersection //Memorize/save segment connected to intersection

33 How does this impact the find function?
std::vector<int> find_intersection_street_segments(int intersection_id) { return intersection_street_segments[intersection_id]; } Just one line! Looks up answer from data structure Index into std::vector of street segments with intersection_id Much faster: Single array look-up O(1)

34 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 several seconds to load everything into your data structures A long time!


Download ppt "Functionality & Performance in Milestone 1"

Similar presentations


Ads by Google