lecture 37: real-world use of graphs
DESCRIPTION
CSC 213 – Large Scale Programming. Lecture 37: Real-World USE OF Graphs. Today’s Goals. Consider what new does & how Java works What are traditional means of managing memory? Why did they change how this was done for Java? What are the benefits & costs of these changes? - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/1.jpg)
LECTURE 37:REAL-WORLD USE OF GRAPHS
CSC 213 – Large Scale Programming
![Page 2: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/2.jpg)
Today’s Goals
Consider what new does & how Java works What are traditional means of managing
memory? Why did they change how this was done for
Java? What are the benefits & costs of these
changes? Examine real-world use of graphs & its
benefits How do all of those graph algorithms get
used? Can we take advantage of this knowledge
somehow? What occurs in real-world we have not
covered? And why is beer ALWAYS answer to life’s
problems
![Page 3: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/3.jpg)
Explicit Memory Management
Traditional form of memory management Used a lot, but fallen out of favor
malloc / new Commands used to allocate space for an
object free / delete
Return memory to system using these command
Simple to use
![Page 4: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/4.jpg)
Explicit Memory Management
Traditional form of memory management Used a lot, but fallen out of favor
malloc / new Commands used to allocate space for an
object free / delete
Return memory to system using these command
Simple to use, but tricky to get right Forget to free memory leak free too soon dangling pointer
![Page 5: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/5.jpg)
Dangling Pointers
Node x = new Node(“happy”);
![Page 6: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/6.jpg)
Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;
![Page 7: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/7.jpg)
Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;delete x; // But I’m not dead yet!
![Page 8: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/8.jpg)
Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;delete x; // But I’m not dead yet!Node y = new Node(“sad”);
![Page 9: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/9.jpg)
Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;delete x; // But I’m not dead yet!Node y = new Node(“sad”);cout << ptr.data << endl; // sad
![Page 10: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/10.jpg)
Creates insidious, hard-to-find bugs
Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;delete x; // But I’m not dead yet!Node y = new Node(“sad”);cout << ptr.data << endl; // sad
![Page 11: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/11.jpg)
Solution: Garbage Collection Allocate objects into program’s heap
No relation to heap implementing a priority queue
This heap is simply a “pile of memory” Garbage collector scans objects on
heap Starts at references in program stack &
static fields Finds objects reachable from those program
roots We consider the unreachable objects
“garbage” Cannot be used again, so safe to remove
from heap Need to include free command is
eliminated
![Page 12: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/12.jpg)
No More Dangling Pointers
Node x = new Node(“happy”);
![Page 13: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/13.jpg)
No More Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;
![Page 14: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/14.jpg)
No More Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;// x reachable through ptr so cannot
reclaim!
![Page 15: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/15.jpg)
No More Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;// x reachable through ptr so cannot
reclaim!Node y = new Node(“sad”);
![Page 16: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/16.jpg)
No More Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;// x reachable through ptr so cannot
reclaim!Node y = new Node(“sad”);cout << ptr.data << endl; // happy!
![Page 17: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/17.jpg)
Eliminates one mistake programmers make!
But how do we perform garbage collection?
No More Dangling Pointers
Node x = new Node(“happy”);Node ptr = x;// x reachable through ptr so cannot
reclaim!Node y = new Node(“sad”);cout << ptr.data << endl; // happy!
![Page 18: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/18.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 19: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/19.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 20: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/20.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 21: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/21.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 22: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/22.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 23: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/23.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 24: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/24.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 25: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/25.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 26: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/26.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 27: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/27.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 28: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/28.jpg)
Static & locals are called root references
Must compute objects in their transitive closure
Garbage CollectionHEAP
![Page 29: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/29.jpg)
Garbage CollectionHEAP
Remove unmarked objects from the heap
![Page 30: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/30.jpg)
Garbage CollectionHEAP
Remove unmarked objects from the heap
![Page 31: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/31.jpg)
Garbage CollectionHEAP
Remove unmarked objects from the heap
New objects allocated into empty spaces
![Page 32: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/32.jpg)
Why Not Always Use GC?
Garbage collection has obvious benefits Eliminates some errors that often occurs Added benefit: also makes programming
easier
![Page 33: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/33.jpg)
Why Not Always Use GC?
Garbage collection has obvious benefits Eliminates some errors that often occurs Added benefit: also makes programming
easier Also easier to update code when GC used
for memory GC also has several drawbacks
Reachable objects could, not will, be used again
More memory needed to hold the extra objects
It takes time to compute reachable objects
![Page 34: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/34.jpg)
Cost of Accessing Memory
How long memory access takes is also important Will make a major difference in time
program takes Imaginary scenario used to consider
this effect:
![Page 35: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/35.jpg)
Cost of Accessing Memory
How long memory access takes is also important Will make a major difference in time
program takes Imaginary scenario used to consider
this effect:I want a beer
![Page 36: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/36.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers
![Page 37: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/37.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers
![Page 38: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/38.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers Very, very fast but… … number of beers held is limited
![Page 39: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/39.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers
Use caches at next level for dearest memory
![Page 40: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/40.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers
Use caches at next level for dearest memory
![Page 41: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/41.jpg)
Registers and Caches
Inside the CPU, find first levels of memory
At the lowest level, are processor’s registers
Use caches at next level for dearest memory More space than registers, but… … not as fast (walk across room) Will need more beer if party is good
![Page 42: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/42.jpg)
Horrors!
Processor does its best to keep memory local Caches organized to hold memory needed
soon Makes guesses, since this requires
predicting future Will eventually drink all beer in house
![Page 43: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/43.jpg)
Horrors!
Processor does its best to keep memory local Caches organized to hold memory needed
soon Makes guesses, since this requires
predicting future Will eventually drink all beer in house
![Page 44: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/44.jpg)
Horrors!
Processor does its best to keep memory local Caches organized to hold memory needed
soon Makes guesses, since this requires
predicting future Will eventually drink all beer in house
30MB is largest cache size at the moment Many programs need more than this What do we do?
![Page 45: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/45.jpg)
When the House Runs Dry…
What do you normally do when all beer gone? Must go to store to get more… … but do not want a DUI so we must walk
to store Processor uses RAM to store data that
cannot fit RAM sizes are much, much larger than
caches 100x slower to access, however
![Page 46: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/46.jpg)
When Store Is Out Of Beer...
![Page 47: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/47.jpg)
When Store Is Out Of Beer...
![Page 48: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/48.jpg)
Ein Glass Bier, Bitte
Get SCUBA gear ready for WALK to Germany Should find enough beer to handle any
situation But buzz destroyed by the very long wait
per glass If Germany runs out, you're drinking too
much
![Page 49: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/49.jpg)
Walking To Germany Is Slow…
60M 80M 100M 120M 140M 160M 180M1
1.2
1.4
1.6
1.8
2
2.2
2.4pseudoJBB
GenMS
GenImmix
GenMS + Leadered
Heap Size
Tim
e Re
lati
ve to
Gen
MS
+ L
eade
red
at 6
0MB
![Page 50: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/50.jpg)
Maintaining Your Buzz
Prevent long pauses by maintaining locality Repeatedly access those objects in fast
memory Access objects in sequential order they are
in memory Both of properties take advantage of
caching Limit data used to size of cache (temporal
locality) (Spatial locality) Exploit knowing how cache
works Limiting data is not easy (or would
have done it) So taking advantage of spatial locality is
our best bet
![Page 51: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/51.jpg)
Cache Replacement Algorithms When we access memory, add its block
to cache May need to evict a block if the cache
already full 2+1 approaches used to select evicted
block FIFO maintains blocks in Queue and evicts
oldest Track each use and evict block least
recently used (Randomly choose a block to evict)
For good performance want to avoid worst case But what is it?
![Page 52: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/52.jpg)
Cache Replacement Workings
Access Order During Program Execution0 1 2 3 4 5 0 1 0 1 2 5 3 2 4
LRU
FIFO
![Page 53: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/53.jpg)
What Does This Mean?
Large data sets require more thought & care Start with, but do not end at, big-Oh
notation Consider memory costs and how to limit
them Most data structures do not grow this
large STACK, QUEUE, SEQUENCE rarely get above
1GB Using very, very large GRAPH is not typical
Databases are largest data sets anywhere Which data structures & implementations
affected?
![Page 54: Lecture 37: Real-World USE OF Graphs](https://reader035.vdocuments.us/reader035/viewer/2022062310/56815eb3550346895dcd3adf/html5/thumbnails/54.jpg)
For Next Lecture
Remember, tests for your program #3 due Think before submitting; do tests make
sense? Reading on memory hierarchy for
Monday How can we use experience of wanting a
beer? Organize searchable collections to help
performance I am taking students to conference on
Friday Will not be here, since I cannot be in two
places at once