[CS61C FA20] Lecture 25.2 - Caches II: Direct Mapped Example — Transcript
Full transcript
- 0:00and welcome back now let's work on a
- 0:02problem let's work on an
- 0:03actual problem where we learn about
- 0:05direct map caches
- 0:06with some actual numbers and some actual
- 0:08problems so here we go
- 0:09i got eight bytes of data in a direct
- 0:12map cache with two byte blocks
- 0:14okay oh eight bytes of data overall area
- 0:18of the problem you might have seen this
- 0:19before there eight bytes of my total guy
- 0:22two by blocks okay two bytes across i
- 0:25got that
- 0:26okay now we're gonna ask you some
- 0:27questions how big is the tag
- 0:29index and offset fields if we're doing a
- 0:3132-bit architecture
- 0:33okay so that's usually the setup so
- 0:35let's figure out the offset first let's
- 0:36do the offset first
- 0:37well the offset is the number of total
- 0:40bits i need
- 0:42to if i'm looking at how am i dividing
- 0:43up that set of bits
- 0:45how am i his 32-bit address sum's got to
- 0:48be some of those 32-bits is going to be
- 0:49the t and the i and the o
- 0:51so let's do the o first well the o tells
- 0:54me
- 0:54which column i'm in that's the first
- 0:55thing we do so how many columns do i
- 0:57have how many bytes
- 0:58it's always in bytes how many bytes do i
- 1:00have in a block
- 1:02i figured it out it said it it says two
- 1:04by blocks well
- 1:05so two to the what two to the number of
- 1:07bits is that total number of bytes
- 1:10in that block well two to the blank is
- 1:13two so blank is one so i need one bit
- 1:16for my
- 1:16and that's what we saw before one bit to
- 1:19determine my offset
- 1:21what's my index well it's the same idea
- 1:24i just did this before kind of when i
- 1:26was talking about the the
- 1:28larger cash problem here we go total
- 1:30cash is what's the area
- 1:32well i told you eight two to the three
- 1:35what's a block a block is two to the one
- 1:38bytes two to the three over two to the
- 1:41one is three minus two is one
- 1:43therefore two to the two blocks per
- 1:46cache
- 1:46therefore i need two bits to specify
- 1:49that number of blocks
- 1:50so then i go to my tag tag is everybody
- 1:54else
- 1:55that basically said three bits are
- 1:56needed to access
- 1:58a cache i could by the way i could have
- 2:00told you the tag size already
- 2:01because you told me the cache size
- 2:02that's three because sorry you told me
- 2:04the cache is eight bytes
- 2:05that's three bits to access eight bytes
- 2:07so therefore three can be borrowed
- 2:09for whatever if i'm even if i'm confused
- 2:11some of them are indexed some of them
- 2:13are
- 2:13offset i don't know which is which i do
- 2:15know that three of them are to tell me
- 2:16about the
- 2:17place it inside the cache well the three
- 2:20are for there
- 2:20the rest of them 29 are my tag and
- 2:23that's it so i need a 29 bit tag
- 2:26and three bits to tell me which row the
- 2:29left two bits
- 2:30of that the left two bits are the index
- 2:33that tells me what row and one bit is my
- 2:35offset which is what column
- 2:36that's it again why not the first full
- 2:3932 bits for the tag
- 2:40well why waste those those rightmost
- 2:43three bits are never used they're always
- 2:45you know they're always going to be
- 2:46uh uh uh redundant so we don't want to
- 2:48have them in that system
- 2:51so let's do this now a big picture this
- 2:54is a really big picture idea
- 2:56taking a computer without a cache and
- 2:58then adding a cache
- 2:59doesn't change the process it changes
- 3:01the process doesn't change the value
- 3:03doesn't change the program i told you
- 3:05you should take the same code
- 3:06with or without cash it does the same
- 3:08thing now it may not be it may be faster
- 3:10if you
- 3:11knew that there were cash there but the
- 3:12same thing should happen effectively
- 3:14so i want to load a word i got some
- 3:17memory
- 3:19t1 is a pointer to memory i want to load
- 3:21that and store it into t0
- 3:23clobber whatever t0 was and put that
- 3:25memory in there and let's say t1
- 3:27contains
- 3:28uh one zero two two okay 1022
- 3:31is the is the pointer say and what's in
- 3:34that address
- 3:3599. so what happens those are the steps
- 3:38without the case just the days without a
- 3:39cash
- 3:39back in the days the simple the simple
- 3:42halcyon days before caches
- 3:46processor issues an address 1022 to
- 3:48memory it says i want to get you know
- 3:49what is that what is lw
- 3:51t0 0 t1 where t1 is 10 22 it says
- 3:54look can you please go to memory at
- 3:57address 2 10 22 and get something there
- 3:59yeah sure i can get you that how much
- 4:00you're asking for oh it's the lw it's a
- 4:02word all right please i'll get you back
- 4:03no problem okay here's 99.
- 4:07the memory reads it returns 99. remember
- 4:10he sends it back to the processor and
- 4:11the processor then
- 4:12loads that into the register and by the
- 4:14way at this point you know how the whole
- 4:15thing works
- 4:16you know that how the data you
- 4:20you i could ask you for a data path of
- 4:21what what lights up and how it actually
- 4:23does it and you can even show me the
- 4:24control lines to make it work at this
- 4:26stage in the course
- 4:27you can explain everything about how a
- 4:29load work works it's really very
- 4:30powerful
- 4:31to understand how that whole thing works
- 4:32to run on a risk 5 machine
- 4:34this lights up this this goes there the
- 4:36index the offset there's a zero at the
- 4:38zero uh
- 4:39uh immediate there that zero gets added
- 4:41to that memory location
- 4:42doesn't change it's still 10 22. so
- 4:44that's an immediate had to be extended
- 4:46all those things you know how to do and
- 4:48you know how to route it you know what
- 4:49turns on you know what muxes turn on
- 4:50what the signal lines that go to the mux
- 4:52it's really very cool
- 4:53at this point your understanding of the
- 4:55whole system very powerful i hope you
- 4:57walk a little bit taller now that you
- 4:58understand how that works
- 4:59but again this is without a cache what
- 5:02happens with the cache what's the
- 5:04algorithm with the cache
- 5:05again i got my same load word and t once
- 5:07contains the same thing
- 5:09well with the cache it's similar to like
- 5:10what a hash function does
- 5:12it says well you know i could save time
- 5:15if it's in my cache then go into memory
- 5:17okay so first i do is i have to see if
- 5:20it has a copy
- 5:21do you have a copy of that data at is
- 5:24that 99
- 5:25somehow copied in because i'm remember
- 5:27i'm only reading for now i'm never
- 5:28writing i'm only reading for now okay so
- 5:30is that 99 somewhere in my cache so if
- 5:33it is
- 5:34i say it's a hit we got it whoo and i
- 5:36returned that and
- 5:37i never had to go to sacramento
- 5:38sacramento was really far away
- 5:40is it in my room is that 99 somewhere in
- 5:42my room well i got to check it if it is
- 5:44if not oh man well then i got to go to
- 5:46memory and then here's what i have to do
- 5:48here's a step forward
- 5:49i have to memory resident to the address
- 5:53memory sends it back to the cash so the
- 5:55cash is in there
- 5:57when it wasn't there the cash has to
- 5:59still store it for the next time
- 6:01so it doesn't well i don't have it sorry
- 6:02i'm going to sleep don't go to sleep you
- 6:03got to store it for the next time that's
- 6:05the whole purpose of the cache is that
- 6:06right you have the first time you'll
- 6:07have it for the next time i ask for it
- 6:09it replaces the spot that word with 99
- 6:12and then it sends that idea back to it
- 6:14so it still has to do with storage and
- 6:15update itself and then it sends it back
- 6:17and from the process point of view it
- 6:18didn't didn't know it didn't have to i
- 6:20just
- 6:21asked for it and if it was there it was
- 6:22much faster return value if it's not
- 6:24magic things happen behind the scenes as
- 6:27it moves to the right place and updates
- 6:29update updates and then
- 6:30finally i just get my 99 and i work with
- 6:32it and it sets it to
- 6:33zero so in some sense it's the same
- 6:35thing from the processor's point of view
- 6:36abstractly i don't know what happened
- 6:38below the hood and now the cache is
- 6:39moving around and putting it here
- 6:41and doing its thing so that it does the
- 6:43right thing
- 6:44this is the last slide on this uh on
- 6:46this particular uh
- 6:48mini lecture but i wanted to i want to
- 6:49show talk more about
- 6:51how to think about i i created this
- 6:53because i realized as i was working with
- 6:55many students in office hours they
- 6:56weren't
- 6:57visualizing it right and i'm a visual
- 6:58guy i got my phd in graphics so
- 7:00pictures always help me learn and maybe
- 7:02some others are like that
- 7:04so i wanted to show you how to solve
- 7:06cash problems in general
- 7:08we always as i mentioned before draw our
- 7:11memory the same width
- 7:12as the cache okay always the same
- 7:15however however many
- 7:16whatever that block size is of my cache
- 7:19draw your memory
- 7:20the same size the width of it at least
- 7:24and now as i have my t i and o
- 7:27and i have a value all zeros where is
- 7:30all zeros
- 7:31in cache well i always start
- 7:34in the upper right there it is
- 7:38where is it in memory same spot
- 7:41like these are these are like mirrors
- 7:44it's almost like a mirror this is
- 7:46there's this
- 7:46wonderful there's this wonderful marx
- 7:48brothers where they're
- 7:50kind of like the two brothers are
- 7:51pretending to be he he walks in and he
- 7:53thinks he's looking at a mirror but it's
- 7:54actually his brother his brother goes
- 7:56like this it's like maybe if i can grab
- 7:59a copy i'll show you this over here but
- 8:00it does the hand and the hand in there
- 8:02it's a mirror
- 8:03that's what's happening as i'm asking
- 8:05for the memory the cache is exactly the
- 8:07same spot so wherever you see this arrow
- 8:08pointing to memory it's literally the
- 8:10same spot
- 8:10in that cache okay next one
- 8:14of interest oh i just increment that guy
- 8:17my increment my
- 8:18binary odometer by one which means all
- 8:20i'm going to do is change my offset
- 8:22i move my offset by one what am i doing
- 8:24i just move over by one
- 8:26it's the next one over so it's like i'm
- 8:27starting from the top right and read
- 8:28across to the left
- 8:30i'm just increasing the bytes that i'm
- 8:31going to ask for as i'm asking for my
- 8:33address i change my one i'm getting the
- 8:35next byte then the next byte and the
- 8:36next byte and for now we're just doing
- 8:37load bytes we're just moving by one okay
- 8:40the next interesting thing the next kind
- 8:42of significant number is when it gets to
- 8:44all ones
- 8:45i want you to draw think before i give
- 8:47you the answer where is that
- 8:48where is that thing if i say all zero
- 8:49zero zeros all zeros in my tag
- 8:52all zeros are my index but all ones are
- 8:54my offset where is that in the picture
- 8:55of memory circle it
- 8:57circle i should i'm gonna make an exam
- 8:59question circle the box where it is in
- 9:01memory in there
- 9:02i'll tell you where it is it means i got
- 9:05to the last spot
- 9:08before i wrapped into the next index so
- 9:11it is in the top
- 9:12left okay got that so as i read it
- 9:16across
- 9:17offset continues to move across until i
- 9:19get to all ones
- 9:20that's the top left then what happens
- 9:22then it wraps
- 9:24to make the index up by one and where is
- 9:26that
- 9:28it's the next row it's still in that
- 9:30it's still in cache zero this is
- 9:32tag zero or cache number zero
- 9:36and so again that's here that's the next
- 9:38row down that's my
- 9:39okay so start to think about and see
- 9:41kind of visualize where this thing is
- 9:43now the next thing is what what's what
- 9:45happens when that maxes out when that
- 9:47maxes out
- 9:48and offset max is out where am i you
- 9:50probably can guess where i'm going to be
- 9:52you're exactly right it is in the bottom
- 9:54left
- 9:55of the memory the bottom left of your
- 9:57cache and your bottom left of memory
- 9:59same exact idea
- 10:00i hope this is helpful by the way when
- 10:03that wraps what happens next
- 10:05well as you might imagine i now go
- 10:08back to the top left of my cache
- 10:14top right of my cache but now the tag
- 10:17has
- 10:18changed now i'm in the next cache the
- 10:20cashflow bloop
- 10:21and because this is the next box this is
- 10:23the next box this tells me
- 10:25what my cache number so my tag is now a
- 10:27one there so
- 10:29one in all zeros is in the top right of
- 10:32the next cache size down in memory or if
- 10:35i my cache i just go back to the top
- 10:37right
- 10:37because i basically go across go across
- 10:39go zigzag and then i just jump back up
- 10:41there
- 10:41that's what happens so i've jumped back
- 10:43up there when i had all zeros
- 10:44because again in this i only am looking
- 10:48at these to tell me where i am in here
- 10:50i'm using this one to tell me which one
- 10:52of these guys i go to that's all this is
- 10:55it really isn't that bad
- 10:57the next thing that's most important is
- 10:59all ones whoa that's the biggest memory
- 11:01location of all time
- 11:04where is it you're exactly right it's
- 11:06the bottom left
- 11:07of here and because this maps to that
- 11:10it'd be the bottom left of that cache
- 11:13with tag number whatever the maximum
- 11:15value is
- 11:16that's it that's the big idea of how to
- 11:18solve cash problems
- 11:20take your t ino theoden gracias
- 11:23thanks to dan for teaching me this the
- 11:26idea is
- 11:27you are going to be able to look i could
- 11:28just tell my
- 11:30i give you some bits and they look
- 11:31complicated no make your columns there's
- 11:33my t
- 11:34there's my i there's my o if you want if
- 11:36you're really fancy have three different
- 11:38colors red green and blue like i use
- 11:39here
- 11:40and then start to make use of this
- 11:43i don't know this kind of visual way to
- 11:45think about this as you start to have
- 11:46problems
- 11:47and as i start to have some piece here's
- 11:48a piece of code i might give you what's
- 11:50happening in this loop oh
- 11:51maybe it's just look at this maybe the
- 11:53loop is dressed let me let me go maybe
- 11:54maybe go back
- 11:55maybe this loop is just in here in the
- 11:57cache maybe just reading this
- 11:58oh okay so i can think about that that
- 12:01means it's running it maybe it keeps
- 12:02going
- 12:02it just goes on here right on the right
- 12:04okay i don't know what my stride is
- 12:06maybe
- 12:07read this memory address and then jump
- 12:09another if it's memory address not the
- 12:11neighbor
- 12:11if i'm reading by ones i'll be reading
- 12:14i'm reading a cross
- 12:15i'm reading a cross what if i jump by
- 12:17some stride i'd read this memory and
- 12:18then i jump to the next one
- 12:20how big is your stride well what if your
- 12:22stride is exactly a block size
- 12:24well that means i start some place and
- 12:26i'm going down here because i'm jumping
- 12:28by a block i jump by a whole block which
- 12:30means i go to the next block
- 12:32what if i'm my what if my stride is the
- 12:34cash size
- 12:35whatever memory address i have i jump
- 12:37back what what happens if it's not even
- 12:38on the edge
- 12:39here's my first access here and i
- 12:42stride by my cache size what's your next
- 12:46location there it is because i moved by
- 12:50a cache size
- 12:51and by the way if this is the first one
- 12:53if that's the first one
- 12:54where's the second one same spot because
- 12:57if i jump by the cache size
- 12:59it doesn't move in the cache right it
- 13:00does this is just a copy of what that
- 13:02looks like
- 13:03that's not saying the whole thing is a
- 13:04copy of the whole thing but i'm just
- 13:05saying the location in the cache
- 13:07is parallel to where it is in that box
- 13:09moved over
- 13:10so i could stride i can make a memory
- 13:13access and then stride by some amount
- 13:15and if i stride by a byte i move across
- 13:17the top
- 13:18if i stride by a block size i move down
- 13:20wherever i start with
- 13:22wherever i start with i'm moving down
- 13:23that same row if i stride by a cache
- 13:26size
- 13:26i store wherever i start with and i'm
- 13:28jumping to the next guy the same spot
- 13:31okay so these are common strides we
- 13:33might have an example
- 13:34what if i stride by a little less than a
- 13:36block oh let's play with that
- 13:37here we go a little less than a block
- 13:39i'm here a little less than a block
- 13:41means
- 13:42i'm actually going to go this way it's a
- 13:44little less than a block
- 13:46what if it's a little more than a block
- 13:48well then i'm going to go this way
- 13:50okay what if i'm what if i'm not exactly
- 13:52a cash
- 13:53but a little more than a cash what
- 13:56happens
- 13:57here and i move over a little bit and i
- 14:00move
- 14:01over a little bit what if i'm a little
- 14:03less than
- 14:04a cash size i'm here
- 14:07in the same spot but a little's in here
- 14:11what if i'm not just a little less than
- 14:13the cash's what if i'm a whole block
- 14:14size let's
- 14:15lessen the cache size starting to hurt
- 14:18here's my memory
- 14:19exactly one block size less than the
- 14:21cache size if i start with this
- 14:22m that means the next one is b up one
- 14:26and up one it means i'm going up one i'm
- 14:28kind of not completely
- 14:29getting there what if i my stride is
- 14:32and by the way play this slower and
- 14:34rewind this to make sure you understand
- 14:35this what if my stride is
- 14:36cache size plus a block well i start on
- 14:39this m
- 14:40and the next one would be the same m but
- 14:41down one and the next
- 14:43m but down two okay so think about as we
- 14:47give you some code and maybe have a
- 14:48stride as i'm accessing not just
- 14:51continuous
- 14:51version of memory but like a stride and
- 14:54then another one and another one
- 14:56how you're moving through this space to
- 14:58understand
- 14:59as you're solving these problems what
- 15:01how many hits do you get how many times
- 15:03do you get it how many times did you
- 15:04miss it
- 15:05all those things about in terms of
- 15:07performance of a problem
- 15:08okay phew i hope this was useful i kind
- 15:10of i really
- 15:12visually always go here you give me any
- 15:14problem i say excuse me give me time
- 15:15hold on
- 15:16i draw my picture okay now now let me
- 15:18read the problem and i label it i draw
- 15:19my p
- 15:20i draw my this and then i say okay
- 15:21what's the width of the cache what's the
- 15:23block size that's the width of both
- 15:24cache and memory because i've drawn them
- 15:25the same
- 15:26and now i start with my here's my t i
- 15:28drop my t's out my tio
- 15:29and i drive them out and i understand
- 15:30that that's i do so
- 15:33basically just sit down take your time
- 15:35on these problems and draw the pictures
- 15:37and you will get them right
- 15:38okay see the next lecture
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 25.2 - Caches II: Direct Mapped Example by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,371 words across 509 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.