[CS61C FA20] Lecture 33.3 - Thread-Level Parallelism I: Threads — Transcript
Full transcript
- 0:00and welcome back now let's try to figure
- 0:03out the solution to the problem we posed
- 0:05last lecture which was
- 0:06how do we make use of this incredible
- 0:08incredibly powerful machine
- 0:092021 we know how to deal with the wide
- 0:12vectors
- 0:13but i've got multiple cores how do i
- 0:15think about from a software point of
- 0:16view to be able to make this machine
- 0:17screen make this machine
- 0:19do all the you know all the performance
- 0:21all the compute on some
- 0:23huge data set how do i make use of
- 0:24multiple cores attacking at the same
- 0:26time
- 0:27let's get started so i ran
- 0:30i go to my unix machine i type ps minus
- 0:33x
- 0:33and please try find the equivalent you
- 0:35know the equivalent command on your
- 0:36computer usually it's ps
- 0:38with some kind of command used to be
- 0:39dash ef now it's x
- 0:41here's what i see i've got 156
- 0:45different programs running at the same
- 0:46time right now i mean not right now but
- 0:49at that at the time of that of that
- 0:50slide
- 0:51uh 156 how does my laptop do that how's
- 0:53my laptop somehow
- 0:54my fan's not running it's quiet how is
- 0:57it running 156 programs all the same
- 0:59time happily
- 1:00the fan isn't even going you know the
- 1:02over oh my gosh i'm being overheated
- 1:03the fan it's not this listen to the
- 1:05microphone no fan very quiet
- 1:07happy how does it do this how does it
- 1:09how are you imagine
- 1:10this is the analogy imagine doing 156
- 1:13different assignments at the same time
- 1:15it's crazy so here's the idea first
- 1:18first name new name for this lecture a
- 1:20thread
- 1:21a thread stands for a thread of
- 1:23execution it is
- 1:25a single stream of instructions think of
- 1:28a
- 1:29you know old-school program a single pc
- 1:32you load in a program at out and you're
- 1:34loaded and you're running it
- 1:36okay you're living in that out just
- 1:37think about that for a moment okay and
- 1:38you don't ever
- 1:39bring you know there's there's some
- 1:41control there's some control
- 1:42which is being pat you make a function
- 1:44call now it goes here then it comes back
- 1:45and it comes back here it's a single
- 1:48thread of execution okay that's a thread
- 1:52a program within it at out could
- 1:55split or fork itself to have multiple
- 1:58threads of execution
- 1:59all running at the same time and then it
- 2:01might have a way to join them back
- 2:03together
- 2:03to have a result so these are kind of
- 2:05analogies here so it's an easy way to
- 2:07think about parallelism it's a single
- 2:08thread of execution i'm just kind of
- 2:09following
- 2:10as it sounds like a finger single finger
- 2:12and say i'm on this line now i'm on this
- 2:13line now i make a function call now i
- 2:15return
- 2:16single finger single thread of execution
- 2:18okay
- 2:19i imagine i have old school days i've
- 2:20got a single cpu and a single core
- 2:23how do i handle multiple threads how did
- 2:26your computer
- 2:27in the day in the days long gone that
- 2:30had a single core
- 2:31and had a had and had didn't a single
- 2:34single cpu in a single core
- 2:35how is it able to run multiple programs
- 2:37the os is a program i was able to run
- 2:39the os and anything else
- 2:40well here's how it always did it time
- 2:43sharing
- 2:44the idea is the single cpu single core
- 2:47can
- 2:48essentially by the way i make this
- 2:49analogy before i make this analogy again
- 2:52if you if you're a parent with multiple
- 2:54kids
- 2:55and if you have more kids than parents
- 2:56or if you're somehow your spouse
- 2:58leaves and you're in charge of all the
- 2:59kids and even more than one you have to
- 3:01give a lot of love to more
- 3:02than just a one-on-one one-on-one
- 3:05defense doesn't work anymore
- 3:06you should play zone defense as parents
- 3:08a lot of kids talk about it
- 3:09so what does that mean you give a little
- 3:11you can put one you put the youngest to
- 3:13bed
- 3:13and then you hang out with the middle
- 3:14one you put that to bed get some kisses
- 3:16then you put the older one get that
- 3:17right then you keep going you give a
- 3:18little love to every single one
- 3:20okay so it's a little like a little
- 3:21slice of time for each particular one
- 3:24each one thinks so you're giving all the
- 3:25love to me
- 3:26and then they go around they read a book
- 3:27or something they don't realize that
- 3:28you're actually giving love to other
- 3:29people at the same time
- 3:30that's what the cpu is doing cpu says
- 3:32let me go to the first thread and give
- 3:34it a little bit of time
- 3:34a little bit of time it's running it's
- 3:36processing something and then put it
- 3:37like
- 3:38pause it go to the next one and by the
- 3:41way it's not
- 3:42unlike the analogy of having children
- 3:44where they go off read a book it's not
- 3:45to anything when you're not giving
- 3:46attention to it's not doing anything
- 3:47because
- 3:47you're the only one who can actually
- 3:48make you know make it compute
- 3:50so you're the next one give it a little
- 3:52love the next one you keep rotating and
- 3:53then you go back to the first one
- 3:55and if you do this fast enough if you
- 3:56time share fast enough slice through
- 3:58time fast enough
- 3:59you will never notice it it'll just seem
- 4:01like your computer is a little and the
- 4:02more you have
- 4:03the less slice you get but you're like
- 4:05oh my computer's a little slower
- 4:06and now my videos maybe dropping frames
- 4:09or something or
- 4:10or maybe zoom isn't doing the right
- 4:11thing if you have a really overworked
- 4:12machine
- 4:14maybe it's not able to to the youtube
- 4:16sometimes can give you a very high
- 4:19well this might be also transfer of data
- 4:20but sometimes even the processing of
- 4:22data
- 4:23can't um not let's say drop pixels but
- 4:26it can't
- 4:26have the highest resolution oh i'm i
- 4:28need to lower my resolution on youtube
- 4:30not because the pipe isn't big enough in
- 4:32the terms of data but i just don't have
- 4:34time the compute time
- 4:35to take all that in and do this i'm
- 4:36going to i'm going to say i'm failing
- 4:38right now i'm going to now
- 4:39pinch down the pipe and say give me a
- 4:40smaller it might actually do this you
- 4:42might ask a system that says
- 4:44i have a big enough pipe to handle a
- 4:451080p or 4k stream or an 8k stream
- 4:47but i don't have cpu cycles so i'm going
- 4:49to pinch it down just give me 360 or 720
- 4:51or something smaller than that
- 4:53you know to be able to handle that okay
- 4:55time sharing
- 4:56slice it through okay if you have a
- 4:57single thread single cpu multiple
- 5:00threads you time share between them and
- 5:01by the way i'll give you a little
- 5:0330 second story when i was uh
- 5:04undergraduate we worked on a time
- 5:06sharing system it was called multix
- 5:10uh back at about back at mit and
- 5:13we had a system where i would we would
- 5:16all uh
- 5:16the week before finals we would all be
- 5:18writing our papers so i was i was i went
- 5:19to the lab the computer lab to write a
- 5:21paper paper didn't have a laptop laptop
- 5:23or a personal computer at the time
- 5:24and i was my freshman year and i'm
- 5:26writing a paper and
- 5:28the slice of time that i got was so
- 5:30small okay with all the
- 5:32mit other undergraduates doing it i
- 5:34would type a whole line of characters
- 5:38and not see my cursor update imagine a
- 5:40computer so slow
- 5:42it doesn't even update the cursor
- 5:44imagine right and then
- 5:46i remember this i remember being so
- 5:47frustrated because it would then
- 5:49give me i was like and give me give me a
- 5:51little slice it would type all of them
- 5:52and i have a typo
- 5:54then i said oh my gosh i have to like
- 5:55i'd count how many characters to go back
- 5:57i would
- 5:58go back 60 change in a to an e and
- 6:01but i wouldn't update but my cursor
- 6:03wouldn't even update okay imagine this
- 6:04world where time sharing was so
- 6:06stretch thin in terms of resources it
- 6:09couldn't even
- 6:09update the screen in a text editor
- 6:12that's what it was
- 6:13writing a paper and this was incredibly
- 6:14painful we all learned to work in the
- 6:16middle of the night to do this because
- 6:17you know during five o'clock six o'clock
- 6:19middle of the afternoon you couldn't
- 6:20even type 80 characters without having
- 6:22it paused until it gives a whole line
- 6:25okay so time sharing can go bad
- 6:27if you have too many too many things
- 6:29asking for work
- 6:30for for one cpu how many 500 kids each
- 6:33kid be like ah
- 6:34all crying at the same time wouldn't we
- 6:35be bad um
- 6:38so in threads more detail threads are a
- 6:40sequential flow of instructions that
- 6:42perform some tasks
- 6:43and we've been calling this a program
- 6:44for now but now we're going to know that
- 6:45we're called it's really called a thread
- 6:46a lot of times
- 6:47we start with an abstraction we kind of
- 6:48reveal the abstraction later it's really
- 6:50not
- 6:50you know it's really called a virtual
- 6:52address space not not just an address
- 6:53space so
- 6:54we're doing the same thing here with
- 6:55with a program program was a program now
- 6:57program really talking about a thread
- 6:58for now
- 7:00each thread has a dedicated program
- 7:01counter that knows for that thread what
- 7:03you're doing
- 7:04separate registers ooh interesting so
- 7:06now you're thinking how's that different
- 7:07from
- 7:08okay well separate registers you can
- 7:10still access the shared memory we saw
- 7:11that before
- 7:13each visit now we have to talk about
- 7:14here this is actually important the
- 7:16distinction between a hardware thread
- 7:18and a software thread okay
- 7:21each physical core so each core the
- 7:24element is
- 7:24a core that's the unit now provides one
- 7:27or more
- 7:28hardware threads so a hardware thread is
- 7:30a thread
- 7:31running on that core okay
- 7:34so each is executing a hardware thread
- 7:36so when the thread
- 7:37is kind of loaded in a way onto the core
- 7:40it's a hardware thread now
- 7:44the operating system supports and can
- 7:46multiplex between
- 7:47multiple software threads and the idea
- 7:50is i might have a program that divides
- 7:52itself into a hundred different
- 7:54threads those would be software threads
- 7:56but i only have a four
- 7:58core machine so now what does that tell
- 8:00me
- 8:01only four of those hundred software
- 8:03threads can be hardware threads if it's
- 8:05running
- 8:06on a core live it's kind of loaded then
- 8:09it's mapping it onto that
- 8:10hardware thread that's mapped into that
- 8:12core it becomes a hardware thread
- 8:14100 software threads four hardware
- 8:18threads on a four core machine i might
- 8:19be able to actually be clever
- 8:20to get more hardware threads running on
- 8:22these four cores but for now
- 8:24we don't know anything about that so
- 8:25we're going to say only four for a four
- 8:26core machine
- 8:27i only have four hardware threads okay
- 8:29and by the way if you're
- 8:30still a software thread and have been
- 8:31mapped to a hardware thread you're
- 8:33waiting
- 8:34because because nobody can process you
- 8:35just like the kid can't read if you're
- 8:37not being actively
- 8:38processed by our into our hardware
- 8:40thread if you're a software thread just
- 8:41sitting there
- 8:42you're not processing you're just idle
- 8:43you're waiting okay
- 8:45that's the idea hardware threads are
- 8:47running on the on the core software
- 8:48threads are all the ones that you
- 8:49created all the ones that are kind of
- 8:50there waiting all the ones in the total
- 8:52space are all software threads the ones
- 8:53rating on cores
- 8:54are hardware threats okay just some
- 8:56names some some nomenclature
- 8:59is professor edward lee recently
- 9:01emeritus i believe
- 9:03he had this wonderful quote about
- 9:06threads
- 9:07um i'll just read it to you normally i
- 9:09don't read slides but it's just too good
- 9:11although threads seem to be a small step
- 9:13from sequential computation
- 9:15in fact they represent a huge step they
- 9:18discard
- 9:18the most essential and appealing
- 9:20properties of sequential computation
- 9:22understandability you just you know
- 9:24process now
- 9:26again you could have code that's hard to
- 9:27read but in general understandability
- 9:30predictability and determinism
- 9:33threads as a model of computation are
- 9:36wildly
- 9:37non-deterministic it means kind of
- 9:39random in a way
- 9:40and the job of the programmer becomes
- 9:42one of proving that
- 9:44non-determinism determinism means that i
- 9:45can basically promise you what the
- 9:48output would be there is no there's no
- 9:49i'm not rolling a dice in there i'm not
- 9:51shuffling anything there's no randomness
- 9:52there
- 9:53non-determinism means i often don't know
- 9:55the order that these threads are gonna
- 9:56these threads get out there and what
- 9:58order they come back
- 9:59i don't know and that's the hard part
- 10:02about programming with threads is how do
- 10:04you manage
- 10:04all these threads taking different
- 10:06amounts of time one thread went over
- 10:07there just
- 10:08got spun in the loop and maybe it's
- 10:09going to come back in a year maybe never
- 10:10come back
- 10:11so who knows right how many what does my
- 10:13program does managing
- 10:15all and a program a single threading
- 10:16program could do that too but not
- 10:18if i send all it's like send all the i
- 10:20hire all these workers go out and do
- 10:21some stuff
- 10:22and go like i pay all these b's go do
- 10:24something and then
- 10:25some of them haven't come back some of
- 10:26them have some come back in different
- 10:28orders
- 10:28what do i do how do i manage that is
- 10:30what ed was talking about
- 10:33so here's the idea the abstraction is
- 10:37they're all simultaneously active
- 10:38remember i can rotate the time sharing
- 10:40lets them all rotate so i've got
- 10:42even so from the point of view of
- 10:43software in a way
- 10:46i don't care how many hardware threads
- 10:48can ever be run i'm just going to say
- 10:50well you know what makes sense to break
- 10:51this up into 100 pieces just because
- 10:53that's the way
- 10:53logically this problem breaks up so
- 10:55break up into 100 pieces
- 10:56and let them all go and you're going to
- 10:59notice that
- 11:01if there actually are only force
- 11:02hardware threads possible that may be
- 11:04100
- 11:07i'll say this way it might be faster to
- 11:09break if there are only four hardware
- 11:11threads
- 11:12and i have a problem to break it up into
- 11:14more than
- 11:16four software threads are you gonna say
- 11:18why why would that
- 11:19why why why do this here's why let's say
- 11:22one part of it gets stalled so one of
- 11:25those hardware just gets stalled okay
- 11:27so one quarter of your whole thing is
- 11:29just being idle stalled because some
- 11:31some part of the code is there and it's
- 11:32needed to do extra work who knows
- 11:34it's just stalled that's gonna
- 11:35eventually return but it's just stalled
- 11:38imagine if however broken to a hundred
- 11:41of these guys
- 11:42okay and 26 number 26
- 11:45this is the one that's going to stall
- 11:46like out of the 100 pieces there's one
- 11:48thing that's just a particularly hard
- 11:50computation let's just say so when that
- 11:52comes in
- 11:53it's going here and imagine if it's a
- 11:55hundred now let's go back to this model
- 11:58if only one of those hundred or if it's
- 12:01only four one of those four
- 12:02is going to be stalled and can take a
- 12:04long time if i break into a hundred
- 12:06pieces
- 12:07then basically i can compute all 99 of
- 12:09them like
- 12:10let's say it's a lot of work so all 99
- 12:12of them can finish
- 12:14and that one guy is still solved still
- 12:16okay still still competing still
- 12:17okay finally it returns
- 12:21in the four model i
- 12:24these three finished and then this is
- 12:26still waiting for that first one that
- 12:28first stalled guy is still stuck still
- 12:30stuck on stuff okay now it finishes
- 12:31and then sells 25 more to do but if i
- 12:34broke into 100
- 12:35it's like those other 24 things that are
- 12:38now waiting because the first guy got
- 12:40stuck
- 12:41can be processed over here if i break it
- 12:43into 100 pieces
- 12:44only that one guy that gets stuck was
- 12:46stalled there so you could actually
- 12:48imagine
- 12:49that it makes sense for a particular
- 12:51problem to divide up into
- 12:53more software threads than you have
- 12:55hardware threads that's what i'm saying
- 12:57it could just be that and i don't think
- 12:58if i described that very well but the
- 12:59idea is
- 13:00you're just stalling with yourself and
- 13:02the other 25 24
- 13:04of the problem but in the other in my
- 13:06world these guys are computing all those
- 13:08guys and you're stuck on 100th over the
- 13:09problem and then when that gets done
- 13:10you're done
- 13:11rather than have to then compute the
- 13:12other 24 undone guys that's what i'm
- 13:14trying to say and who knows what's
- 13:16happening in bad particular problem but
- 13:17it could make sense to and there's a
- 13:19little knob there's a no i have a
- 13:20problem how
- 13:21how much do i slice this up into for the
- 13:23given amount of hardware threads i can
- 13:24run it on a given amount of cores i have
- 13:26given them out of machining them
- 13:27dispatches two on the cloud
- 13:29how much resolution how small a slice
- 13:32should i do and so you
- 13:33play with this curve and you play with
- 13:35it and you see what the what the
- 13:36knee and the curve is what the low point
- 13:37of the curve is in terms of time okay
- 13:40so point number one you get this
- 13:42abstraction of software threads
- 13:44okay you're gonna multiplex software
- 13:46threads under hardware threads right the
- 13:47idea
- 13:48here's the pool and now you're gonna
- 13:49grab some of them and pull them in here
- 13:51pull them in pull them in work okay pull
- 13:52it out put another one in you're gonna
- 13:53try to get them all to be
- 13:55matched in there um you could how do you
- 13:58do that how do you bring them in and out
- 13:59well
- 14:00you can decide whenever you've got a
- 14:02block thread you've got a cache miss
- 14:04user input network access there's some
- 14:06reason that thread is stalled for
- 14:07whatever reason as we call that blocked
- 14:09it could be a lot of those things right
- 14:10cache miss i've got to go to command
- 14:11it's a thousand cycles well
- 14:13get that guy out of here while you're
- 14:14going to sacramento make that kind of a
- 14:15request that goes out there
- 14:16pull it out and then bring somebody else
- 14:18in to get some work done while the
- 14:19sacramento returned it and maybe it's
- 14:20even farther than sacramento maybe now
- 14:22you know a virtual memory and maybe i
- 14:23need to go to disk maybe it means a page
- 14:25miss
- 14:25oh my gosh how many is that a million
- 14:28clock cycles for a page miss
- 14:29possibly so get this guy out while it's
- 14:32waiting on it while that's happening
- 14:34do all the work okay you could also have
- 14:36a timer like a little time slice so you
- 14:38say
- 14:38well okay they're all fine no cache
- 14:40misses let's say i'm all doing you know
- 14:41just
- 14:42ads and r type instructions adds and
- 14:44subtracts and xor it's just simple stuff
- 14:45not even a memory access just just i'm
- 14:47computing i'm like raw compute mode well
- 14:50give some other give give some other
- 14:51people a chance get some love to other
- 14:52people so let some other people so maybe
- 14:54slice out
- 14:54after a couple of timers so you can
- 14:56multiplex them in different ways
- 14:59how do you remove it how do you remove a
- 15:00software thread from a hardware thread
- 15:02um so it's so here we go how do i take
- 15:05it out how do i
- 15:06unplug it and put it back in the kind of
- 15:08waiting stage well i have to interrupt
- 15:09execution i got to stop running i need
- 15:11to save its state
- 15:12i'm going to save its state so we did
- 15:14this we did this we learned a little bit
- 15:16in virtual memory how you
- 15:17move things around uh for multiple
- 15:19processes so you have to
- 15:21save its register save its pc to memory
- 15:24you got to pull it out save to memory
- 15:25and now you've got it
- 15:26okay so that's important um so i can
- 15:28reinstate it so now all the things that
- 15:30are part of that
- 15:31part of that world of computation have
- 15:33to be have to be saved and that's
- 15:34obviously the registers in the pc
- 15:37how do you do the same thing how do you
- 15:38start a different software thread
- 15:40how do you load that onto a hardware
- 15:41thread well you go go to memory
- 15:44grab its previously saved registers
- 15:46until the hardware's registers
- 15:47and you jump to its pc and you keep
- 15:49going so basically registers in pc is
- 15:50the piece
- 15:51that's needed for these guys pull it out
- 15:53pull it in pull it out pull it in and
- 15:54you're doing this for
- 15:55removing and adding different software
- 15:57threads so here's an example very simple
- 15:59i got a thread pool there's my pool of
- 16:01threads
- 16:02here's the list over here with a couple
- 16:03of all the processes all the
- 16:05threads i need to be to be running and
- 16:08the os is going to map those threads
- 16:10to the cores so that's the idea and
- 16:12you're going to schedule that to make
- 16:13that happen there are four cores
- 16:15each core is actively running one
- 16:17instruction stream at a time and that's
- 16:18it so i've got this huge pool
- 16:20and they're being mapped to the four
- 16:22hardware threads if here i have only
- 16:24one hardware thread per core and i'm
- 16:26running on that and i'm just kind of
- 16:27doing this until
- 16:28until the program finishes or and by the
- 16:30way you're seeing here many of these are
- 16:31daemons many of these are programs that
- 16:32just continue to run
- 16:33if the if the name ends in a d as a the
- 16:36list here you know user
- 16:37s even user node d the d means daemon it
- 16:40means it's running all the time
- 16:42it doesn't stop it's not like well shoot
- 16:44that was hard we're all done we're done
- 16:45yet no
- 16:46some of the programs never stop and so
- 16:48the computer is always just processing
- 16:49your computer
- 16:50idle even you know even a computer
- 16:52that's running anything like i'm just in
- 16:54the
- 16:54mac in the finder i'm just in the os
- 16:56doing nothing i'm not running any
- 16:57programs nothing's running but what
- 16:58yes things are running it's listening
- 17:00for hardware connections it's somebody's
- 17:01running to be able to handle
- 17:03like the keyboard that's your os uh
- 17:06are there some network things there are
- 17:07many things that are happening in the
- 17:08background
- 17:09who's up into the clock all those things
- 17:10are running even though you don't
- 17:11realize it as part of the os so
- 17:13even if you're running no user programs
- 17:15many things are running in your in your
- 17:16system now you should check that out by
- 17:18the way do i type ps minus
- 17:19minus x and you'll see the list of all
- 17:21the things running even when you're
- 17:22running nothing else or maybe just run
- 17:23terminal and then type that you'll see
- 17:25wait i'm only running terminal
- 17:26and there's a ton of things being run at
- 17:27the same time okay all those are
- 17:29processed
- 17:30all that is complicated this is but the
- 17:32nice thing is the os handles it for you
- 17:34so far i haven't talked about it all
- 17:35explicitly loading this thing in we even
- 17:37talked about how to even split and fork
- 17:38and join
- 17:39fork myself and join it back we've done
- 17:40any of that so we're just talking about
- 17:41the big picture how about the oh
- 17:43mostly we'll talk about what the os has
- 17:44been doing all along and that's what's
- 17:45happening all right
- 17:46we're going to see how to make this work
- 17:49we're getting lower and lower in the
- 17:50abstraction level of being able to do
- 17:51this ourself
- 17:52explicitly you know touching some code
- 17:54that actually splits this stuff and
- 17:55joins it up
- 17:56in a couple lectures all right we'll see
- 17:57you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 33.3 - Thread-Level Parallelism I: Threads by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 4,043 words across 646 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.