YouTube2Text

[CS61C FA20] Lecture 25.1 - Caches II: Direct Mapped Caches — Transcript

by CS 61C Departmental · 5,889 words · 892 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's finally do it
  2. 0:03let's teach you how the basics of the
  3. 0:04simplest kind of cache we can come up
  4. 0:06with
  5. 0:07the direct map cache so
  6. 0:10in a direct map cache each memory
  7. 0:13address one of the questions we were
  8. 0:14asking of a cache is
  9. 0:16how do you know when i go to memory
  10. 0:18where it lives in the cache
  11. 0:20okay where does it go do i put it
  12. 0:22anywhere and then search for it linearly
  13. 0:24or log we never do that we know exactly
  14. 0:27where it is and where it's going to be
  15. 0:28is a very clear location we'll talk
  16. 0:30about when we show example the idea is
  17. 0:32every memory address is associated with
  18. 0:34exactly one block there's a new word you
  19. 0:36haven't heard before block
  20. 0:37that block is the amount of data that
  21. 0:41you move in and out of caches
  22. 0:42so that's it if i'm talking about a hard
  23. 0:45drive i talk about
  24. 0:46files what's the unit of data files
  25. 0:51if it's registers it's a word move
  26. 0:53things in and out in a word
  27. 0:54in a cache the thing you move in and out
  28. 0:56is called a block that's the unit of
  29. 0:58transfer
  30. 0:59so which block it's going to be in
  31. 1:03is always defined okay and very clearly
  32. 1:06defined it's not a search there's no
  33. 1:07search needed to find it i know exactly
  34. 1:08by
  35. 1:09what the bits happen to be let's
  36. 1:10actually look at an example to see what
  37. 1:11look like
  38. 1:12there is your first cache
  39. 1:16one of the abstractions of memory is to
  40. 1:17think of it as
  41. 1:19basically infinite we know it's 2 to the
  42. 1:2132 it starts at 0 and moves
  43. 1:24to 2 to the 32 minus 1 possible bytes i
  44. 1:27could write
  45. 1:28and here we're showing it here each
  46. 1:29memory address is a byte wide
  47. 1:31so we're showing that we're drawing this
  48. 1:32memory as a byte wide and by the way you
  49. 1:35can draw this picture memory different
  50. 1:36ways memory is
  51. 1:37i can draw it a byte wide and 32
  52. 1:412 to the 32 or 4 billion and actually we
  53. 1:44now know the word for it don't we
  54. 1:47for gibby look at this 432 is for gibby
  55. 1:50remember that
  56. 1:51so zero i could draw four gibby
  57. 1:54rows where each row
  58. 1:57uh is one bite wide
  59. 2:01or i could draw it a word wide
  60. 2:04so four bites wide and only one gibby
  61. 2:07high
  62. 2:08so four times one four you know one
  63. 2:09gibby times four is four gibby total
  64. 2:12or one bite times four gibby is four
  65. 2:15give me total so i can draw the memory
  66. 2:17and change the aspect ratio thinking of
  67. 2:19it different ways
  68. 2:21so it's the same memory i'm just drawing
  69. 2:22in different ways and thinking of it
  70. 2:24different ways
  71. 2:24and in fact i'm going to encourage you
  72. 2:26from now on
  73. 2:28from now from this point on if you ever
  74. 2:30draw memory
  75. 2:32slowly i'm just going to say this very
  76. 2:33slowly so you so we get it all
  77. 2:35draw memory so that it's the same
  78. 2:40width as your cash that's all you got to
  79. 2:43do
  80. 2:44from now on when you're thinking about
  81. 2:45one of these cash problems oh turn the
  82. 2:47page on the final exam it's a cash
  83. 2:49problem cash says
  84. 2:50the block remember the block is the unit
  85. 2:53of transfer that's the width of the cash
  86. 2:56is some amount okay whatever that amount
  87. 2:58is draw your memory the exact same width
  88. 3:01the amount member is going to be
  89. 3:02more rows because the number is bigger
  90. 3:04than the cache but always draw in fact i
  91. 3:06always draw them right above each other
  92. 3:07here's the cache his memory so the same
  93. 3:09width is like there
  94. 3:10with the cache with your memory okay so
  95. 3:12do that i'm drawing them here
  96. 3:14two separate two separate pictures not
  97. 3:15like this but so the same width see i
  98. 3:17did it same width okay
  99. 3:19okay so here we go
  100. 3:23first problem notice the color coding
  101. 3:26before i even tell you what's happening
  102. 3:27the color coding
  103. 3:27tells you the answer which is
  104. 3:31every four elements knows exactly where
  105. 3:34to go
  106. 3:36if you look at this picture all the
  107. 3:38blues go to the blue area
  108. 3:40so this is a four byte cache
  109. 3:44i'm starting to use some words that now
  110. 3:45it might make some sense
  111. 3:47what's our block size what's the unit of
  112. 3:49transfer it's a byte
  113. 3:51and that's the width so this is the
  114. 3:53block size and it's only one byte
  115. 3:55so i'm drawing memory one byte wide so
  116. 3:58how high is it
  117. 3:594 gb 32 to the 32
  118. 4:02total bytes i have access to okay and by
  119. 4:05the way sometimes we draw our memory
  120. 4:06from zero in the bottom to zero on the
  121. 4:08top i actually prefer to draw it
  122. 4:09top down but it means lowest number here
  123. 4:12increasing number there
  124. 4:14but sometimes the picture we flip it for
  125. 4:15convenience sake like when we talk about
  126. 4:17the picture of
  127. 4:18if you remember picture where stuff
  128. 4:19lives in code zero is the code
  129. 4:22and then above is the static and above
  130. 4:23it is the heap and above it there's a
  131. 4:25stack at the higher level so we actually
  132. 4:26flip and we talk about what the code
  133. 4:28does
  134. 4:28when we actually run cache problems we
  135. 4:30flip it to have zero be up top and down
  136. 4:32there
  137. 4:32okay all right
  138. 4:36so this is going to be one byte wide and
  139. 4:39if this is a four byte cache that must
  140. 4:41be
  141. 4:41that this is four there are four of
  142. 4:44these guys because this
  143. 4:45times this is going to be the total size
  144. 4:48of the cache
  145. 4:49okay it's like the area think about the
  146. 4:51area we'll actually have a picture of
  147. 4:52two
  148. 4:52later we show that but that's the idea
  149. 4:55width times height
  150. 4:56is the area of the cache okay but that's
  151. 4:58the idea
  152. 4:59so every four so i can tell without even
  153. 5:01thinking there's no search anymore
  154. 5:03well let's see where how about nine
  155. 5:05where does nine live really it's like
  156. 5:07modulo modulo four well what's nine mod
  157. 5:10four
  158. 5:10one well no that's what colors i didn't
  159. 5:12know that i didn't know that okay i know
  160. 5:13i know my my
  161. 5:14nine mod four is one let's just see if
  162. 5:17that's true
  163. 5:18well nine is this red color and one is
  164. 5:20the red color see
  165. 5:21did it for me so in a way this cache is
  166. 5:23doing modulus for us and that's how we
  167. 5:24do it
  168. 5:26another way to think about it here's
  169. 5:27another way to think about it
  170. 5:31what are the uh memory what's nine what
  171. 5:33are the memory addresses here let's look
  172. 5:34at let's get nine here
  173. 5:36101. which of these bits
  174. 5:40is telling me what row of the cache it
  175. 5:43is
  176. 5:44which one can you guys tell me
  177. 5:47yeah isn't that lower bits telling me
  178. 5:50what that is
  179. 5:51so already i can tell if you just tell
  180. 5:53me the number oh it's uh what's your
  181. 5:55what's your
  182. 5:56money address oh it's uh fqs no fq
  183. 5:59these have to be hex values good q good
  184. 6:01dan q that's a nice hex value
  185. 6:03f e d nine seven three four
  186. 6:07six oh six the last one was six
  187. 6:10six is oh one one oh i bet you you're
  188. 6:13in spot two which is the third spot but
  189. 6:17it's spot
  190. 6:18labeled by two okay so all you do is
  191. 6:20look at the low order bits and tell you
  192. 6:22what row you're in we're gonna see it's
  193. 6:23a little more complicated with that but
  194. 6:24that's kind of neat to be able to
  195. 6:25quickly go and figure out where things
  196. 6:27are immediately just by looking at those
  197. 6:29bits
  198. 6:30so cash flows kickstart this i said some
  199. 6:33of these things but cash flow
  200. 6:34location zero can come from zero
  201. 6:37four eight or anybody that has two zeros
  202. 6:40in the lower bits
  203. 6:41basically anybody that's divisible by
  204. 6:43four
  205. 6:46zero four 8 c keep going
  206. 6:490 4 8 c in fact if you look at any
  207. 6:52memory address
  208. 6:54this is all you need to know look at
  209. 6:55that hex look at the last look at the
  210. 6:57nibble
  211. 6:58the least significant nibble and that's
  212. 7:01basically it zero four eight c
  213. 7:03you're at zero sorry so okay that's it
  214. 7:06like a sheet
  215. 7:06all right zero four eight c you're
  216. 7:08you're in bunk zero
  217. 7:10next let's do it together one five
  218. 7:13nine d you're in bunk one get it
  219. 7:17okay two let's do it two six come on
  220. 7:21a keep going e you're in bunk two
  221. 7:25those guys are mod for r2 and let's do
  222. 7:28it together ready
  223. 7:30fast three can you do it seven
  224. 7:33keep going b f you're in bunk three
  225. 7:36enjoy yourselves
  226. 7:38pretty cool all right and all i was
  227. 7:41doing look at the lower two bits of
  228. 7:42those numbers to figure out what where
  229. 7:43they go
  230. 7:47so these four blocks
  231. 7:50by the way remember we can't stop i
  232. 7:52should remember here stopping at f
  233. 7:54remember it could be all the way down to
  234. 7:5532 gb
  235. 7:57i mean four gibby forgive me i got 32 in
  236. 7:59there four give me 232 is four gibby
  237. 8:01i go to 42 for gibby this could be four
  238. 8:03gimme high and it would still work
  239. 8:05all the blues all the blues one quarter
  240. 8:08of the whole guys are blues
  241. 8:09they all map to that blue spot it's kind
  242. 8:11of cyani
  243. 8:12but that's it now what if we wanted a
  244. 8:16block
  245. 8:17to be bigger than one byte because if
  246. 8:19you think about it now this
  247. 8:20you know what this really means if i i
  248. 8:22mean this kind of doesn't work at this
  249. 8:23level
  250. 8:24because if i had any 32-bit value
  251. 8:27it means the 32-bit values are being
  252. 8:29spread across
  253. 8:30the different cache spot like if i load
  254. 8:32in a float float 30 bits wide
  255. 8:34um well that means that the lower eight
  256. 8:36or here and then the next one is at the
  257. 8:38red
  258. 8:39and the next that doesn't make so much
  259. 8:40sense so usually we think of caches
  260. 8:42being at least
  261. 8:43a worldwide just like a load of float in
  262. 8:46that spot otherwise the float's being
  263. 8:47split across that doesn't i don't like
  264. 8:48that so much so
  265. 8:49we want to think about larger larger one
  266. 8:51bites but we're going to start teaching
  267. 8:52with cash is
  268. 8:53at a very basic level so you understand
  269. 8:54what this is from so for now
  270. 8:56we're in this beautiful abstract padded
  271. 8:59world
  272. 9:00model right now padded room i could just
  273. 9:02walk around i'm going to i can't hurt
  274. 9:03myself and padded room
  275. 9:05in which we're just talking about bytes
  276. 9:06we're just reading writing bytes don't
  277. 9:07worry about anything bigger than that
  278. 9:08for now just to be easy
  279. 9:10so now again we're just reading reading
  280. 9:12buying bytes for now okay easy at work
  281. 9:14look at how it happens now this direct
  282. 9:17map cache
  283. 9:18now says i'm going to have an 8 i've
  284. 9:20doubled my size look my block size is
  285. 9:22now two bytes that's
  286. 9:23wide four rows two bytes each that's
  287. 9:26four times two is eight that's an eight
  288. 9:28byte cache
  289. 9:29okay it's direct map still the easiest
  290. 9:31kind of caches we have
  291. 9:32but you see how we're doing it and this
  292. 9:34is critical by the way
  293. 9:36we're going to label the lowest byte is
  294. 9:39the top right it's almost like
  295. 9:41um kind of a right right to left view or
  296. 9:43maybe eating corn like this it's the
  297. 9:44opposite how you write read a page or
  298. 9:46something
  299. 9:47here is the lowest bite of all of memory
  300. 9:50then it goes across that's the next byte
  301. 9:53and then it goes back to down here
  302. 9:54and that's the next one so you kind of
  303. 9:56do this little ziggy zaggy thing as we
  304. 9:57read and as
  305. 9:58as the block size gets bigger remember
  306. 10:00block size is the width of the cache and
  307. 10:01it's still again drawn
  308. 10:02our memory the same width as the cache
  309. 10:05as i widen that block
  310. 10:06size i'm going to do the same thing how
  311. 10:08i don't care how it is the top
  312. 10:10right is going to be the the least bite
  313. 10:12the lowest byte of the lowest word
  314. 10:13of the whole thing and i walk my way
  315. 10:15across i pack them in till i get however
  316. 10:17wide that block
  317. 10:18size is and i jump back it's always
  318. 10:19going to be this way this kind of ziggy
  319. 10:21zaggity guy
  320. 10:22so that's that's not different as i
  321. 10:23change my block size that won't that
  322. 10:24won't change
  323. 10:26so again the whole blue thing moves up
  324. 10:28there but now when i ask for a bite
  325. 10:31the controller has to figure out where
  326. 10:32it is so now it's a little bit more
  327. 10:34complicated is it always the lowest bit
  328. 10:35let's see is it the lowest bit anymore
  329. 10:37now
  330. 10:38i don't know if it is anymore how do i
  331. 10:39figure out which one
  332. 10:41which goes where we can almost have a
  333. 10:43design question here
  334. 10:45zero zero well that that's in zero
  335. 10:48and and zero one that's that's this spot
  336. 10:51that's this guy right here okay
  337. 10:53that's also in zero but now one
  338. 10:57zero that's in one and then one
  339. 11:00one is in one you see what i'm doing so
  340. 11:03it's not so much i'm looking at let's
  341. 11:05look here that that somehow the lower
  342. 11:07bits are telling me where it is
  343. 11:09it's almost like i'm ignoring this one
  344. 11:11isn't it this isn't telling me anything
  345. 11:13it's kind of like this is telling me but
  346. 11:15in fact there are obviously four of them
  347. 11:17so it sounds like i need to be looking
  348. 11:18at these two bits
  349. 11:19so just as we're looking at this it
  350. 11:21looks like it isn't always the lowest
  351. 11:23bits that tell me where to go
  352. 11:24as i as i have more larger block size
  353. 11:27than one
  354. 11:28i feel like i have to somehow skip some
  355. 11:31amount
  356. 11:31until i start counting and we're
  357. 11:32actually going to look at this in a
  358. 11:33second
  359. 11:34how that actually works so i try to lead
  360. 11:37you like wow it's pretty simple looking
  361. 11:38at the bits
  362. 11:39it is simple but it's not as trivial as
  363. 11:41looking at the least significant bits to
  364. 11:42tell you what row you're in
  365. 11:43okay so when we ask for a byte again the
  366. 11:47controller does it
  367. 11:48finds out what's the right block and
  368. 11:49loads it for us that's what's beautiful
  369. 11:50about these things it's automatic
  370. 11:52how does it know the right one we kind
  371. 11:53of talked about that so that we talked
  372. 11:54about
  373. 11:55how do we select the right bite so now
  374. 11:57once i ask for it how do i know
  375. 11:59which is it the right blue if i've ever
  376. 12:01got a guy in blue
  377. 12:03is it the right blue guy is the left guy
  378. 12:05blue if i'm trying to return a byte
  379. 12:06which are these two that you just loaded
  380. 12:07the controller did something for us but
  381. 12:08which of the two blue guys do i want
  382. 12:11that's we got to figure that out here um
  383. 12:12and in fact that might have helped
  384. 12:14our numbers actually that column we
  385. 12:16crossed off
  386. 12:17maybe that was a secret for what bite it
  387. 12:19was that's i'm giving you a little hint
  388. 12:20of what
  389. 12:21what's there so for example memory
  390. 12:23address
  391. 12:2411101 okay well what's that by the way
  392. 12:28what's 1101 really fast really fast see
  393. 12:31you got to know that faster than that
  394. 12:32you
  395. 12:33were too slow here's how i do this 1 1 1
  396. 12:36is 7.
  397. 12:38one one one oh means seven has been
  398. 12:40shifted to the left by two spots
  399. 12:41that's times four seven times four is
  400. 12:43twenty-eight plus one is twenty-nine
  401. 12:46so in my head you give me one one one
  402. 12:47one i'd say twenty-nine and you're like
  403. 12:48how do you do that so fast
  404. 12:49i was not adding 16 and 8 and i wasn't
  405. 12:52doing that
  406. 12:54i got to 29 by saying oh that's 7 times
  407. 12:564 28 plus 1 is 29.
  408. 12:58so think about how to do this faster so
  409. 13:00you get to
  410. 13:02some interview and they they give you
  411. 13:03some bit and you're playing with bits
  412. 13:04much more fluently by looking at how to
  413. 13:06think of this so 1-101
  414. 13:0729 really fast right pretty cool neat 7
  415. 13:10times 4
  416. 13:10plus 1. okay so what's actually
  417. 13:14happening
  418. 13:14so how do i know which color block
  419. 13:161-1-1-1 this is kind of the
  420. 13:18main problem one one on one how do we
  421. 13:20know well i was kind of telling you that
  422. 13:22somehow we're gonna skip this lower guy
  423. 13:25and we know what row it is from
  424. 13:26the the one o but then maybe this is the
  425. 13:29one which is the which left or right guy
  426. 13:31that does that so that's
  427. 13:32part of what we're trying to get to is
  428. 13:34how do you figure out how to do this
  429. 13:36and and here's the interesting thing i
  430. 13:39think the bigger question
  431. 13:40isn't which blue but the bigger question
  432. 13:42i mean which blew
  433. 13:43from the left or right point of view but
  434. 13:45the bigger question is
  435. 13:46how do i know which blue it came from
  436. 13:50so remember the memory controller loaded
  437. 13:52a blue into my and up up it says
  438. 13:54blue's filled i look at the bits and
  439. 13:56it's filled with bits remember i can't
  440. 13:57tell by looking at a cache
  441. 13:58bits or bits it could be garbage it
  442. 14:00could be values so you can't just look
  443. 14:02there so there's got to be something
  444. 14:03else that tells me that it's whether it
  445. 14:04was empty or not we're going to get to
  446. 14:05that in a couple of slides
  447. 14:08but there's got to be a way to know
  448. 14:09which of these blues did i mean look
  449. 14:11this blue is full of data yep filled
  450. 14:13with good data very happy
  451. 14:14maybe that's even a picture so there's a
  452. 14:16picture there it could be garbage that
  453. 14:18happens to be a picture or could be a
  454. 14:19picture whatever
  455. 14:19somehow there's data there but i'm
  456. 14:21asking myself was it from this guy
  457. 14:23or was it from this i have to know which
  458. 14:25one that is to be able to know
  459. 14:27did i save myself time or do i have to
  460. 14:29go grab another blue and replace that
  461. 14:30one
  462. 14:31so what do you do a baggage claim it's a
  463. 14:34great sketch
  464. 14:35which is a funny sketch i saw on youtube
  465. 14:38in which a person says i'd like to
  466. 14:39find my bag and the person says you have
  467. 14:41to describe your bag yeah it's a
  468. 14:42it's a roll-on it's black um like
  469. 14:46every other bag in the world oh yeah
  470. 14:48sorry it's got a toothbrush in there
  471. 14:50uh again like every other bag in the
  472. 14:52world
  473. 14:53oh yeah sorry it's got two wheels uh
  474. 14:55yeah again right it's very funny sketch
  475. 14:57so part of what i'm asking is
  476. 15:00how do you distinguish your bags when
  477. 15:02you pick them up the airport you know
  478. 15:02you're flying back from
  479. 15:04home welcome back to cal it's you know
  480. 15:06late august welcome back first week of
  481. 15:08classes
  482. 15:09your bank get your bag they're all
  483. 15:10identical black bags there's some law
  484. 15:12that says you have to have the same
  485. 15:13boring rectangular black roll-on bag
  486. 15:16carry-on well not a carry-on because you
  487. 15:18want to check it but the point is you're
  488. 15:19checking your bag you're getting it
  489. 15:20how do you have what's the secret in the
  490. 15:22airport all that story was to tell you
  491. 15:24it's a tag you have a little tag that
  492. 15:27tells me oh it's this one no no it's
  493. 15:29this one
  494. 15:30so each of these blues should have a tag
  495. 15:33that tells you
  496. 15:34which where did it come from which of
  497. 15:35those kind of rows which of those
  498. 15:37floors you think of this memory it's
  499. 15:39like floors which floor did it
  500. 15:40originally come from
  501. 15:41to end up being in my having a copy in
  502. 15:43my cache
  503. 15:44that tag is the key just like a tag i
  504. 15:46remember on luggage
  505. 15:47the tag is the key so
  506. 15:51i introduce a tag and this is an eight
  507. 15:54byte
  508. 15:55direct map cache with a tag by the way
  509. 15:58those words meant nothing to you about
  510. 15:5930 minutes ago so it's kind of cool that
  511. 16:01you're like oh now i know what this
  512. 16:02means
  513. 16:04so here's what happens i get all the
  514. 16:06blues mapped to the blue
  515. 16:08and what should go in the tag well
  516. 16:11let's think about what should we put it
  517. 16:12there let's how about this let's make up
  518. 16:14a new
  519. 16:15set of bits for each blue and just make
  520. 16:17them up so everybody okay here we go
  521. 16:18everybody all the blues all the colors
  522. 16:20every row in the original remember make
  523. 16:22up a new special code
  524. 16:25that we could do that maybe you would
  525. 16:26have designed that one that's not as
  526. 16:28efficient as
  527. 16:29well they kind of already have their
  528. 16:30address
  529. 16:32at the very least which is the address
  530. 16:33comes with it right let's just use that
  531. 16:35that doesn't make any sense so
  532. 16:36let's just do the addresses and then
  533. 16:39you're going to say to yourself well
  534. 16:40do we do we actually need the entire
  535. 16:43address
  536. 16:44and we didn't some of the address tell
  537. 16:46us
  538. 16:47we were already blue maybe two of those
  539. 16:49bits told us that we were blue anyway
  540. 16:51right if i'm blue i know the two of
  541. 16:52those bits are zeros
  542. 16:54and we know that it's not the least
  543. 16:56significant two but the one over
  544. 16:58so i don't need those two bits right and
  545. 17:00you remember the least significant bit
  546. 17:02actually was the one that maybe told me
  547. 17:04which of the blues
  548. 17:05is it is it this guy is it this guy or
  549. 17:08is it this guy that's the least
  550. 17:09significant bit tells me
  551. 17:10which which column i'm in in my cache
  552. 17:14so i don't need that but either because
  553. 17:16that's just telling me there but i
  554. 17:18probably need everybody else
  555. 17:19so in fact here's why i think about it
  556. 17:22it's really interesting
  557. 17:26when we were we go let's go back in time
  558. 17:28again
  559. 17:29we were branch addressing and remember
  560. 17:32you can branch address
  561. 17:33and branch to every other byte and
  562. 17:36that's neat you can jump to any
  563. 17:3716-bit value every other place and
  564. 17:40memory you could branch to
  565. 17:42not everyone but every other one did we
  566. 17:44store that did we start the raw
  567. 17:46did we store when we stored the branch
  568. 17:48the actual amount a branch
  569. 17:49to restore to the byte level we didn't
  570. 17:52we stored it
  571. 17:53so that the number one means the next
  572. 17:55valid thing i could go to
  573. 17:57two is two over from the next valid
  574. 17:59thing i could go to
  575. 18:00so i didn't store all the bits i i
  576. 18:02didn't store all of them i threw one
  577. 18:03away i threw
  578. 18:04anything that was redundant they all
  579. 18:05bothered that would have been zeros if
  580. 18:07i'd stored all the bytes
  581. 18:08it would have been a zeros in that
  582. 18:09column why would i store all zeros i
  583. 18:10never store anything if it's all the
  584. 18:11same value that's silly
  585. 18:13so here all of these guys have something
  586. 18:15consistent which is
  587. 18:17they all have everybody here everybody
  588. 18:20here this everybody that's ever here
  589. 18:23has the lowest three bytes i don't
  590. 18:26really care about
  591. 18:27because the lowest byte is either zero
  592. 18:30and one telling me did you really want
  593. 18:31this one or this one
  594. 18:32and the next two bytes tell me it was
  595. 18:34blue so everybody has the next two bytes
  596. 18:36the same
  597. 18:37and this byte is either a zero or one
  598. 18:39either way i don't care about those guys
  599. 18:41so is there another way to do this where
  600. 18:44if i don't care
  601. 18:46i kind of in a way this is a fun way to
  602. 18:48think about this
  603. 18:50what if i just count by cash number like
  604. 18:52what do you mean cash number i
  605. 18:53i think i know i think i invented this
  606. 18:55term cache number it says
  607. 18:57take the cache here's my cache whatever
  608. 19:00size it is
  609. 19:01draw the same size here and label
  610. 19:04from zero it's zero and the next one
  611. 19:09try another cache and label it one and
  612. 19:11keep doing this keep
  613. 19:12right for the whole for the rest of the
  614. 19:13whole of all memory
  615. 19:15go ahead i'll give you time 32 gibby
  616. 19:17keep coming
  617. 19:184 gb keep going 232 or 4 gibby
  618. 19:21go to the bottom and literally make
  619. 19:23these squares and label them all
  620. 19:25the guy here here i'm calling the
  621. 19:29cash number and i claim
  622. 19:32that everybody in the first cash
  623. 19:36everybody from zero to seven would have
  624. 19:39everybody from zero to seven
  625. 19:40would have cache number zero i claim
  626. 19:43that and i say i claim that meaning
  627. 19:44the bits that are left after i throw
  628. 19:46away
  629. 19:48the the rows we'll call it an index
  630. 19:51flavor those
  631. 19:52two bits that tell me which row i'm in
  632. 19:53and whatever bits i tell me what column
  633. 19:55i'm in if i throw those away
  634. 19:56i'm left with i call the cache number
  635. 20:00and that's what i want to have my tag
  636. 20:01the cache number
  637. 20:03so that's the idea and i again i just
  638. 20:06mentioned this it's useful to draw
  639. 20:07memory the same width as
  640. 20:09if you do this you will be in a great
  641. 20:11space for doing a problem on caches to
  642. 20:13understand this
  643. 20:14and so i cross those off so
  644. 20:17and i write whatever bits are left which
  645. 20:20is the cache number where do they come
  646. 20:21from
  647. 20:22if you look at this you know 2 is here
  648. 20:24look this was 2
  649. 20:26that's cash 0. so that's 0. 14 was here
  650. 20:30here's 14. somewhere in there that's
  651. 20:32cash 2.
  652. 20:33see that i just keep doing that etc so
  653. 20:36you end up having cache number as your
  654. 20:38tag
  655. 20:39and that is the bits that you care about
  656. 20:40because other bits were either what row
  657. 20:42or what column but i'm still in the
  658. 20:43cache i don't care about those
  659. 20:45so that's it i'm left
  660. 20:50i now have the idea that if i look at it
  661. 20:53a generic
  662. 20:54address i can now tell you in a generic
  663. 20:57direct map way which bits went
  664. 21:00where and remember
  665. 21:04sometimes the block size is more than
  666. 21:05one when the block size is one
  667. 21:07and some of these values go away for a
  668. 21:11generic problem i have a block size
  669. 21:12the previous previous problem had a
  670. 21:14block size two or two bytes maybe it's
  671. 21:16much larger than that so i need some
  672. 21:17number of bits tell me which column in
  673. 21:19there
  674. 21:20i gotta know which row i'm in and the
  675. 21:22rest of them is my tag my
  676. 21:24cache number that's the idea i divide
  677. 21:27the memory into three fields
  678. 21:29tag which is the upper bits tag is
  679. 21:31always the upper bits okay that's the
  680. 21:33ones
  681. 21:33the lower guys will tell me which row
  682. 21:34and column upper bits are my tag that's
  683. 21:37my
  684. 21:37cache number from the last probably
  685. 21:40remember the index was which row i'm in
  686. 21:42and that's which block i'm in
  687. 21:44and the lowest set of bits tell me which
  688. 21:46byte i'm in
  689. 21:47which byte or which word i want and
  690. 21:49that's which column i'm in
  691. 21:51and that's it that's the big idea each
  692. 21:53of these fields are read as
  693. 21:55unsigned values there's no sign value
  694. 21:57here it goes from zero to all ones
  695. 22:00and we usually start by the way if you
  696. 22:02look at this it's it's listed from left
  697. 22:03to right t
  698. 22:04i and o but we don't think it that way
  699. 22:07we typically think of working with the
  700. 22:09cache by going with the i first we go to
  701. 22:11the middle first
  702. 22:12which is weird because i like ignore the
  703. 22:13upper bits and the middle bits and kind
  704. 22:14of spread them out okay
  705. 22:15by the way the good part of the pie is
  706. 22:17right here take the side take the crust
  707. 22:19off and i get the stuff in the middle
  708. 22:21my index and what row i'm in that's
  709. 22:23where the action is so i go to my index
  710. 22:25what
  711. 22:26what's the action what row am i in okay
  712. 22:28so what what block am i
  713. 22:30in then once i'm there
  714. 22:34i'm going to see which byte i want and
  715. 22:37that's the offset
  716. 22:38now that's not the order usually it's
  717. 22:40index tag offset we'll get to that in a
  718. 22:41second
  719. 22:42the algorithm for this and the tag is
  720. 22:44the remaining bits the offset tells me
  721. 22:46which column which byte or word i want i
  722. 22:49mean in general it's what byte i am
  723. 22:50but if i'm reading about only words at a
  724. 22:52time the lower two guys would be zero if
  725. 22:54i want that
  726. 22:55index is which row i'm in offset is what
  727. 22:57column i'm in and the tag is the
  728. 22:59remaining part always the upper set of
  729. 23:00bits to check to see
  730. 23:02where did it come from did it come from
  731. 23:03the place i wanted to yes therefore it's
  732. 23:05there already
  733. 23:05and you'll see this when we do an
  734. 23:07example all these things will become
  735. 23:08crystal clear hopefully okay so the tag
  736. 23:11is the remaining bits after i've taken
  737. 23:12the index and the offset away from them
  738. 23:14and i came up with this mnemonic i think
  739. 23:17i don't know maybe other people did but
  740. 23:19i came up with i don't know anybody else
  741. 23:20use this mnemonic so i kind of i want to
  742. 23:22take credit for that
  743. 23:23and in spanish
  744. 23:26the word for uncle is theo hey
  745. 23:30theoden so and i always think of myself
  746. 23:32as a fair bit of unclear
  747. 23:34like an uncle so remember this
  748. 23:38theoden's mnemonic theoden tio theoden
  749. 23:41okay uncle dan tag
  750. 23:45index offset left to right t i o tag
  751. 23:48index offset
  752. 23:49and here's a way to think about caches
  753. 23:50in general here we go
  754. 23:54the lowest set of bits the index and
  755. 23:57offset
  756. 23:58are what determine the area that's the
  757. 24:01overall area
  758. 24:02why because the offset tells you the
  759. 24:05width
  760. 24:06i've tried to color code this so if you
  761. 24:07call it if you're color blind the i'm
  762. 24:09trying to i'll show i'll show this here
  763. 24:10but this is blue here
  764. 24:11for this and this is a little green and
  765. 24:13it says this is the index index tells me
  766. 24:16how many rows i have
  767. 24:18okay so that's and that is just pure
  768. 24:20number of blocks that's the unit
  769. 24:22a number of blocks the unit of the
  770. 24:25offset is
  771. 24:27the bytes per block i'm thinking about
  772. 24:29how many columns i have well
  773. 24:30it's bytes per block is telling me that
  774. 24:33and if i take the multiple take the
  775. 24:34number of
  776. 24:34number of blocks which is the height two
  777. 24:37bytes per block which is the width
  778. 24:40blocks times bytes per blocks the blocks
  779. 24:42cancel i get bytes
  780. 24:44so i multiply them together i get the
  781. 24:46area and bytes
  782. 24:47that's it that's how that's how we work
  783. 24:49with it and the tag is whatever is left
  784. 24:51so that's the big idea the big idea by
  785. 24:54the way this is important
  786. 24:55don't forget that if i take 2 to the h
  787. 24:59and multiply by 2 to the w i get 2 to
  788. 25:02the
  789. 25:02quantity h plus w he's like dan i get
  790. 25:05that yeah but we're going to be talking
  791. 25:06about
  792. 25:07height as not just a number like 5 12
  793. 25:11i'm going to say something like 2 to the
  794. 25:12ninth
  795. 25:14you're like oh i should draw like a nine
  796. 25:16i'm gonna say well what's the width well
  797. 25:17i might say two to the sixth
  798. 25:19and i'd say quickly tell me the area
  799. 25:22tell me how big your cache is
  800. 25:24and you quickly in your head say nine
  801. 25:26plus six is
  802. 25:272 to the 15 is 32 kibi you got a 32 kb
  803. 25:31cash there dan
  804. 25:32so i want you to be really fast by that
  805. 25:34i gave you a lecture
  806. 25:35several lectures ago on being able to be
  807. 25:36fast by that two to ninth it's 5 12.
  808. 25:382 to the 6 is 64. and i don't want you
  809. 25:40to sit down all right 512
  810. 25:42times 64. let's say carry the one i'm
  811. 25:45going to
  812. 25:46have to kind of like to take your pencil
  813. 25:48and kind of like carry the 1.
  814. 25:49i don't want you to do that 9 plus 6 is
  815. 25:5215 2 to the 15
  816. 25:5332 kibi it's a 32 kb cash i want you to
  817. 25:56be fast like that which is why
  818. 25:58i mentioned this and that's also why i
  819. 26:01mentioned
  820. 26:01um the idea of being able to be fluent
  821. 26:04with these numbers
  822. 26:052 to the 15 you should be very fast okay
  823. 26:08and by the way you can imagine i might
  824. 26:10ask a question like oh i don't know
  825. 26:13if my cache size is uh my cache size is
  826. 26:1632kb
  827. 26:17and i have an index oh and see now we're
  828. 26:20playing with this
  829. 26:21i have uh nine bits of index
  830. 26:25what's my block size oh see i just asked
  831. 26:28that i just flipped the whole thing
  832. 26:29so i'm thinking okay two to the fifteen
  833. 26:32divided by
  834. 26:32nine bits that's true to the ninth rows
  835. 26:35fifteen to the fifteenth
  836. 26:37to the 15th by the way there's the
  837. 26:39equivalent just of this of like
  838. 26:40well then true to the h plus w minus
  839. 26:44you know uh divided sorry divided by 2
  840. 26:46to the h
  841. 26:48if i divide both sides by this h2 to the
  842. 26:50w so i say
  843. 26:51okay i got a 32 kb cash i've got
  844. 26:54nine bits of index that's two to the
  845. 26:56ninth i'm thinking 32 kb
  846. 26:58or k to the 15 divided by two to the
  847. 27:00nine 15 minus nine is six
  848. 27:01two to the six oh i got 64 bytes wide
  849. 27:05block fast that's the kind of thing that
  850. 27:08we're going to ask you to do and be able
  851. 27:09to be fluent in so
  852. 27:10hopefully this lecture gives you a
  853. 27:11little bit of a this lecture combined
  854. 27:13with the other lecture where i told you
  855. 27:14to be fluent in those
  856. 27:15numbers let you realize that we're going
  857. 27:16to be talking about 2 to the power for
  858. 27:18these things
  859. 27:19and the number of bits i have tells me i
  860. 27:21have two of those rows if i have
  861. 27:22n bits uh i bits for the index is true
  862. 27:24to the i
  863. 27:25different rows if i have o bits for the
  864. 27:28for the offset it's two to the o bytes
  865. 27:31in that block
  866. 27:32that's the idea so each of these guys is
  867. 27:35going to be a function
  868. 27:36of that you're going to be i'm going to
  869. 27:38ask you to be fluent between the bits
  870. 27:40to the number of rows and number of
  871. 27:42columns and then overall
  872. 27:43again product of that is the overall
  873. 27:45size of my cache
  874. 27:47because that we're going to be a lot
  875. 27:48that's right you cannot be at an exam
  876. 27:50and this is back in the days when we
  877. 27:51didn't have open book exams and untimed
  878. 27:53exams
  879. 27:53but you can't be at a cash question
  880. 27:55right let's say 2 to the 9th then you're
  881. 27:56writing 2 to the 9's okay 64 times 2 to
  882. 27:58the 5
  883. 27:59look it up multiply it no fast fast fast
  884. 28:02so we're encouraging all of our folks
  885. 28:03you didn't really need it before now but
  886. 28:05as we start working cash problems
  887. 28:07you're gonna need it and it definitely
  888. 28:08helps it definitely helps not to be
  889. 28:09stalled on kind of some of the simple
  890. 28:10arithmetic
  891. 28:11okay that's it we'll see at the next
  892. 28:13lecture thanks so much

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 25.1 - Caches II: Direct Mapped Caches by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 5,889 words across 892 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.