1 / 24

Memory Model

Memory Model. For this course: Assume Uniform Access Time All elements in an array accessible with same time cost Reality is somewhat different Memory Hierarchy (in order of decreasing speed) Registers On (cpu) chip cache memory Off chip cache memory Main memory

brittons
Télécharger la présentation

Memory Model

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Memory Model • For this course: Assume Uniform Access Time • All elements in an array accessible with same time cost • Reality is somewhat different • Memory Hierarchy (in order of decreasing speed) • Registers • On (cpu) chip cache memory • Off chip cache memory • Main memory • Virtual memory (automatically managed use of disk) • Explicit disk I/O • All but last managed by system • Need to be aware, but can do little to manipulate others directly • Promote locality?

  2. Cost of Disk I/O • Disk access 10,000 to 100,000 times slower than memory access • Do almost anything (almost!) in terms of computation to avoid an extra disk access • Performance penalty is huge • B-Trees designed to be used with disk storage • Typically used with database application • Many different variations • Will present basic ideas here • Want broad, not deep trees • Even log N disk accesses can be too many

  3. External Methods • Disk use requires special consideration • Timing Considerations (already mentioned) • Writing pointers to disk? • What do values mean when read in at a different time/different machine? • General Properties of B-Trees • All Leaves Have Same Depth • Degree of Node > 2 • (maybe hundreds or thousands) • Not a Binary Tree, but is a Search Tree • There are many implementations... • Will use examples with artificially smallnumbers to illustrate

  4. Rules of B-Trees • Rules • Every node (except root) has at least MINIMUM entries • The MAXIMUM number of node entries is 2*MINIMUM • The entries of each B-tree are stored, sorted • The number of sub-trees below a non-leaf node is one greater than the number of node entries • For non leaves: • Entry at index k is greater than all entries in sub-tree k • Entry at index k is less than all entries in sub-tree k+1 • Every leaf in a B-tree has the same depth

  5. Example Example B Tree (MAX = 2) [6] * * * * * * [2 4] [9] * * * * * * * * * * * * * * * [1] [3] [5] [7 8] [10]

  6. Search in B-Tree • Every Child is Also the Root of a Smaller B-Tree • Possible internal node implementation class BTNode { //ignoring ref on disk issue int myDataCount; int myChildCount; KeyType[] myKeys[MAX+1]; BTNode[] myChild[MAX+2] } • Search: boolean isInBTree(BTNode t, KeyType key); • Search through myKeys until myKeys[k] >= key • If t.myData[k] == key, return true • If isLeaf(t) return false • return isInBtree(t.myChild[k])

  7. Find Example Example Find in B-Tree (MAX = 2) Finding 10 [6 17] * * * * * * * * * * * * * * * [4] [12] [19 22] * * * * * * * * * * * * * * * * * * * * * [2 3] [5] [10] [16] [18] [20] [25]

  8. B-Tree Insertion • Insertion Gets a Little Messy • Insertion may cause rule violation • “Loose” Insertion (leave extra space) (+1) • Fixing Excess Entries • Insert Fix • Split • Move up middle • Height gained only at root • Look at some examples

  9. Insertion Fix • (MAX = 4) Fixing Child with Excess Entry [9 28] BEFORE * * * * * * * * * * * * [3 4] [13 16 19 22 25] [33 40] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * [2 3][4 5][7 8][11 12][14 15][17 18][20 21][23 24][26 27][31 32][34 35][50 51] [9 19 28] AFTER * * * * * * * * * * * * * * * * [3 4] [13 16] [22 25] [33 40] * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * [2 3][4 5][7 8][11 12][14 15][17 18][20 21][23 24][26 27][31 32][34 35][50 51]

  10. [6 17] BEFORE * * * * * * * * * [4] [12] [18 19 22] [ ] STEP 2 * * [6 17 19] * * * * * * * * * * * * [4] [12] [18] [22] [6 17 19] STEP 1 * * * * * * * * * * * * [4] [12] [18] [22] [17] AFTER * * * * [6] [19] * * * * * * * * * * * * [4] [12] [18] [22] Insertion Fix (MAX= 2) Another Fix

  11. B-Tree Removal • Remove • Loose Remove • If rules violated: Fix • Borrow (rotation) • Join • Examples left to the “reader”

  12. B-Trees • Many variations • Leaf node often different from internal node • Only leaf nodes carry all data (internal nodes: keys only) • Examples didn’t distinguish keys from data • Design to have nodes fit disk block • The Big Picture • Details can be worked out • Can do a lot of computation to avoid a disk access

  13. Balanced Binary Search Trees • Pathological BST • Insert nodes from ordered list • Search: O(___) ? • The Balanced Tree • Binary Tree is balanced if height of left and right subtree differ by no more than one, recursively for all nodes. • (Height of empty tree is -1) • Examples

  14. Balanced Binary Search Trees • Keeping BSTrees Balanced • Keeps find, insert, delete O(log(N)) worst case. • Pay small extra amount at each insertion (and deletion) to keep it balanced • Several Well-known Systems Exist for This • AVL Trees • Red-Black Trees • . . . • Will look at AVL Trees

  15. AVL Trees • AVL Trees • Adelson-Velskii and Landis • Discovered ways to keep BSTrees Balanced • Insertions • Insert into BST in normal way • If tree no longer balanced, perform a “rotation” • Rotations restore balance to the tree

  16. AVL Trees • Single Rotation • An insertion into the left subtree of the left child of tree • Adapted from Weiss, pp 567-568 // Used if it has caused loss of balance) // (Also used as part of double rotation operations) Tnode rotateWithLeftChild(TNode k2) //post: returns root of adjusted tree { TNode k1 = k2.left; k2.left = k1.right; k1.right = k2; return k1; }

  17. AVL Trees • Single Rotation

  18. AVL Trees • Single Rotation k1 k2 before after k2 k1 C B B C A A • Also: mirror image

  19. AVL Trees • Single Rotation • Mirror image case TNode rotateWithRightChild(TNode k2) //post: returns root of adjusted tree { TNode k1 = k2.right; k2.right = k1.left; k1.left = k2; return k1; }

  20. AVL Tree • Double Rotation • An insertion into the right subtree of the left child of tree • Adapted from Weiss, p 57 // Used after insertion into right subtree, k2, // of left child, k1, of k3 (if it has caused // loss of balance) TNode doubleRotateWithLeftChild(TNode k3) //post: returns root of adjusted tree { k3.left = rotateWithRightChild(k3.left); return rotateWithLeftChild(k3); }

  21. AVL Tree • Double Rotation

  22. AVL Trees • Double Rotation k3 after before k2 k3 k1 k1 k2 C B D A D A B C • Also: mirror image

  23. AVL Tree • Double Rotation • An insertion into the right subtree of the left child of tree • Adapted from Weiss, p 571 // Mirror Image TNode doubleRotateWithRightChild(TNode k3) //post: returns root of adjusted tree { k3.right = rotateWithLeftChild(k3.right); return rotateWithRightChild(k3); }

  24. AVL Trees • Deletions can also cause imbalance • Use similar rotations to restore balance • Big Oh?

More Related