[CS61C FA20] Lecture 35.1 - Thread-Level Parallelism III: Hardware Synchronization — Transcript
Full transcript
- 0:00and welcome back in this last series of
- 0:02lectures we're going to talk about a
- 0:04couple of details
- 0:05uh we titled this thread level parallels
- 0:08in part three
- 0:09hardware synchronization how do we deal
- 0:11with race conditions
- 0:12that's essentially that first part of it
- 0:14what's deadlocked there's some more
- 0:15details of openmp we'll have some fun
- 0:17here so
- 0:17let's jump right in hardware
- 0:20synchronization
- 0:24as a review openmp has a beautiful way
- 0:27to
- 0:28abuse beautiful abstraction to be able
- 0:30to add parallelism to your c code
- 0:32you have a couple of lines of code maybe
- 0:34there's a header file and you say i want
- 0:36a parallel
- 0:36a pragma that says i want to parallel
- 0:38this for loop and all of a sudden this
- 0:39beautiful for loop
- 0:41that used to be a full serial thing is
- 0:42now paralyzed across all the threads and
- 0:44just works and it works in a really nice
- 0:45clever way
- 0:47um what this means in some sense is it's
- 0:50doing what we talked about
- 0:51a couple of lectures ago it takes a for
- 0:53loop and it says all right for i equals
- 0:540 to some max value here is max
- 0:57it says let me just chop that that up so
- 0:58maybe if it's two threads it's gonna say
- 1:00well zero to
- 1:01half of it so if it's a hundred zero to
- 1:0349 is one of them and 50 to 99 is the
- 1:05second one
- 1:05so it kind of keeps each of the each of
- 1:07the threads
- 1:09each of the cores is going to be working
- 1:10on a contiguous part of memory rather
- 1:12than well one from here and one from
- 1:14there you could do it that way but it's
- 1:15a really bad way to do that for your
- 1:16caches
- 1:17so you have a contiguous area of memory
- 1:18that each of the cores is going to be
- 1:20able to process
- 1:22you should also have so this is the
- 1:24third point says you should have a
- 1:25simple shape
- 1:26um to be able to paralyze something you
- 1:28have to sometimes even even a doubly
- 1:29nested loop doesn't paralyze very well
- 1:31it makes more sense to
- 1:32kind of see if you can unwrap that to be
- 1:34a single top level
- 1:35parallelization rather than two loops
- 1:37inside of it it gets complicated so make
- 1:39sure you
- 1:40you you practice it you'll see it you'll
- 1:41see this as you practice with some of
- 1:43this maybe for your project
- 1:44as well you're not allowed to have a
- 1:46premature exit in any of these so if you
- 1:47have this
- 1:48special code there like a break a return
- 1:50and exit to go to is
- 1:51you don't put this uh you know don't
- 1:53don't don't jump outside of any pragma
- 1:55within that
- 1:56you can move around inside there but
- 1:57don't jump outside of the pragma you can
- 1:59mess with things that's not that's not
- 2:00appropriate
- 2:02we talked about this if you have two
- 2:04memory access of a shared memory system
- 2:06and two memory accesses form can form a
- 2:08data race you can have two memory
- 2:10accesses
- 2:10both trying to read and update a
- 2:12particular variable it doesn't even
- 2:14necessarily
- 2:14in c code doesn't necessarily look like
- 2:16that variable is going to be
- 2:18uh in memory but that that variable a
- 2:20sum
- 2:21we've tried to sum up pi you can't uh
- 2:23sometimes
- 2:24depending on how you write your code you
- 2:26cannot have people
- 2:27affecting a shared variable that that
- 2:29could be trouble and that's a data race
- 2:30and that's a race condition you want to
- 2:32try to prevent that with some kind of
- 2:33synchronization
- 2:34so you want to you want to do this you
- 2:36want to you want to be able to
- 2:38not have to serialize all of it could
- 2:40you actually have
- 2:41um everyone contribute to some shared
- 2:44space as they're all at the end of a
- 2:45thousand different software cores
- 2:47software course software threads and you
- 2:49want to be able to have them all
- 2:50add their contribution to an eventual a
- 2:53sum that's going to be pi hopefully very
- 2:55close approximation
- 2:56how do you do that without having to do
- 2:57it in a serial way because you could
- 2:59imagine this
- 2:59this being something with now i have a
- 3:01million different software threads
- 3:03you know it'd be nice if you didn't have
- 3:05to serialize that
- 3:06whole part of it and make it in the
- 3:08serial part how could you paralyze right
- 3:09to that so that's important
- 3:11we're going to see you can't do it at c
- 3:13you can't write this in c you've got to
- 3:14have lower level support
- 3:15at the hardware level to make this
- 3:17happen hardware synchronization is the
- 3:19secret to this
- 3:21so the secret and you're going to see
- 3:23this in every solution of any any any
- 3:25particular hardware device that has this
- 3:27problem
- 3:27it happens at the upper level language
- 3:29level you need to have hardware support
- 3:31to fix it
- 3:32and the support the solution is
- 3:34something called atomic read and write
- 3:36this means you can read and write in a
- 3:38single instruction
- 3:39and nobody else is permitted to to have
- 3:42no other access to that is permitted
- 3:44between that read and write um
- 3:46and the idea is this is in a shared
- 3:48memory space so this is in a shared
- 3:50memory space so
- 3:51a common implementation here's how we do
- 3:53this in a very regular way
- 3:54um the this atomic is a swap between
- 3:57registers and memory
- 3:58um so in some sense you're you're you
- 4:00then link
- 4:01a read and a write with this uh and the
- 4:04right would fail the memorization has
- 4:05been tampered with as it says here in a
- 4:07slide
- 4:07um and risk five has has variations of
- 4:10both we'll talk about that so
- 4:11we're gonna call these uh atomic memory
- 4:13operations
- 4:15or amos and the idea is
- 4:18you perform an operation on an operand
- 4:20in memory
- 4:21and set a destination register to the
- 4:23original memory value you remember back
- 4:25in the day and
- 4:26here's a picture of what this looks like
- 4:27there's a couple of instructions that
- 4:28are that are
- 4:29support this uh and you can see them
- 4:31here add and
- 4:32swap is the one we're gonna see in a
- 4:34second this is what it looks like if you
- 4:36if you look at the the way it's broken
- 4:37down
- 4:38uh in risk five and here's what's doing
- 4:42you remember i'll just i'll give you a
- 4:43summary of this slide um
- 4:45you remember if i want to add something
- 4:48to a memory location so a memory
- 4:50location has a value there's a memory
- 4:51location that you know
- 4:52a of 5 there's a value there and i want
- 4:54to add something i want to add 10 to
- 4:55that value
- 4:56i can't do that there's no operation to
- 4:58add 10 to that value
- 5:00the only way i can do this is to bring
- 5:01that value into a register
- 5:03add something to that register and put
- 5:04it back it's a three-step process
- 5:06what i'm telling you about this atomic
- 5:08memory operation is you're allowed to do
- 5:09that you now they're providing you a
- 5:11hardware support to do all three of
- 5:12those
- 5:13and now we can think about how you might
- 5:14even build the controlling data path to
- 5:15make it work
- 5:16all three of those happen at once and
- 5:19nothing can be interrupted in this
- 5:21process
- 5:21so what it means is here's an example
- 5:24for the ad
- 5:25i'm here's an example of amo ad so let's
- 5:27take a look at that real quick and
- 5:28what what that looks like here and this
- 5:31amo ad
- 5:33here we go ammo add rd
- 5:37and rs1 here's what's going to do
- 5:41i have a value in rs1 and i want to add
- 5:44it
- 5:45to the memory locate to the value at the
- 5:48memory location
- 5:49pointed to sorry i have a value in rs2
- 5:52and i want to add it
- 5:53to the value in the memory location in
- 5:56rs1
- 5:58and i'd also be nice if i
- 6:01then read the old value of rs1 and
- 6:04stuffed it in rd so it's kind of a
- 6:06two-step process
- 6:07think about this so this is what's
- 6:08happening so let's let's look at this
- 6:10so first i read in this is a pointer
- 6:13okay this is a pointer this is star p
- 6:14this is a pointer p
- 6:16i'm going to read this says read in this
- 6:19is a load word into t
- 6:20into a local variable t i'm now
- 6:24storing that value the old value in that
- 6:26in that register
- 6:28the old value in the memory location
- 6:29pointed to by rs1
- 6:31and i write it into rd that's what this
- 6:33says
- 6:34x of r d is t so i read i read the old
- 6:36value put it into t
- 6:38and then i update that value here's the
- 6:41here's the x of rs2
- 6:45which is what's the value i want to add
- 6:46to i want to add 10 to the thing to a of
- 6:485.
- 6:49this so this is my star p it's basically
- 6:52like 10
- 6:52plus star p this stores my 10
- 6:56and i'm going to say this says rd
- 7:00gets star p that's what's happening with
- 7:02this line
- 7:03okay x of rd gets star p
- 7:06and then i say star p equals star p
- 7:09plus 10 and 10 is stored here
- 7:13this says star p equals star p
- 7:16which is the t see t is star p plus
- 7:19there's my ten
- 7:20okay so it's both so in one operation
- 7:23it's doing read the old value then do an
- 7:27add
- 7:27on what the old value was and then put
- 7:29it back as the new value that's
- 7:31in one operation as an atomic value and
- 7:34swap
- 7:34is this swap idea so let's do let's look
- 7:37at this how we do if we do a swap
- 7:38what happens here okay the lock is going
- 7:41to be a register
- 7:42stored the lock is going to be in memory
- 7:44location stored in register a0 so a0 is
- 7:46a pointer
- 7:47to where my lock is remember that okay
- 7:48so a0 is kind of the
- 7:50address the p here got that right
- 7:52remember setting is
- 7:531 and unset is is is 0. free is 0.
- 7:57so first i'm gonna set t zero to one
- 7:59okay t
- 8:00zero equals one now what does this do
- 8:04amo swap aq stands for acquire rl stands
- 8:07for release
- 8:08this is i'm gonna acquire the lock what
- 8:10this says this says
- 8:12get t1 gets the old lock value okay so
- 8:15t1 is going to get remember
- 8:16this is the older this if you remember
- 8:18the add this guy gets the old value
- 8:21and this is the new value i'm going to
- 8:22try to put in there okay that's the idea
- 8:24here so
- 8:25a0 is going to now get that memory
- 8:27location is going to get my t0
- 8:29and t1 is going to get the old value of
- 8:31it okay
- 8:32the old value of whatever that lock was
- 8:34a star this star
- 8:35star a0 if you think about it okay
- 8:39now i'm going to spin i'm going to spin
- 8:40on this branch not equal to zero so
- 8:43if if t one meaning what i got what i
- 8:46what was there
- 8:47is already a one branch not equal to
- 8:49zero
- 8:50so if it's a one meaning it was already
- 8:52grabbed oh it's already busy
- 8:53i just go back here i branch back up to
- 8:55there and i spin on this i spin on here
- 8:58okay i spin weight on that but the key
- 9:00is the amo swap that makes this work
- 9:04here we go if i fall through the branch
- 9:07not equal to zero that means it was zero
- 9:08so now
- 9:09i can do my thing and here's the key
- 9:11it's not like remember this is the whole
- 9:13thing
- 9:14if i fell through it it meant that i
- 9:16grabbed it this is the whole beauty of
- 9:17this ammo swap
- 9:19only it's not like well we both could
- 9:21read before if
- 9:23i follow through the branch not equal to
- 9:24zero
- 9:26this says that i have it now because i
- 9:29wrote the one
- 9:30nobody else wrote the one this is this
- 9:31is atomic operation
- 9:33so only i was writing the one if there
- 9:34are two competing guys and it falls
- 9:36through only i'm gonna be the one that
- 9:38falls through
- 9:39the bench not equal to zero i'd be the
- 9:40one that actually writes the value
- 9:42so now i'll follow through if if i don't
- 9:45take the branch that means
- 9:46it was zero okay this takes the branch
- 9:49if it's
- 9:49one which means if it's zero that means
- 9:51it was free and i just grabbed it
- 9:53that's the idea this swap stuffed the
- 9:55one in there not
- 9:56anybody's won my one only so now it's
- 9:59mine
- 10:00and now i go through now the critical
- 10:01side now i own the lock that's the key
- 10:04if it wasn't if i was doing this if i
- 10:06somehow break the ammo swap if you
- 10:08remember the bmw swap had like
- 10:09four three things you were doing the
- 10:11critical idea is only
- 10:13if um how do i say it because it
- 10:15happened atomically it happened without
- 10:16any interruption
- 10:17that's the reason this succeeds now if i
- 10:19get to that critical section line i know
- 10:21that number one was written to by me and
- 10:22me alone
- 10:23that was the problem before two people
- 10:25wrote the one thinking they both had it
- 10:27only i had it in this case so now i do
- 10:30my critical section in red i do it
- 10:32and then i want to release it how do i
- 10:34release it i release it by swapping out
- 10:36x0 i don't care when i do the swap i
- 10:38don't care what the old value is i know
- 10:40it was a 1. i know that star a0 is a 1
- 10:42because i owned it
- 10:43this value the old value is going to go
- 10:45into x0 that's ignored nothing happens
- 10:47there
- 10:48and i basically take my x0 which is zero
- 10:50and i stuff this into there and now i
- 10:52reset it to zero
- 10:53and now it's free i release the lock i'm
- 10:56good this is it
- 10:57the critical part about it was that amo
- 11:00was an atomic operation
- 11:02i can read and write at the same time
- 11:05so the old way the broken
- 11:06synchronization is this wild lock
- 11:08and we're both spinning and two people
- 11:09could be in a while lock and they both
- 11:10say oh great the lock is free boom and
- 11:12they both right there one thinking
- 11:14i own it can't happen this can't happen
- 11:17now with this ammo swap
- 11:19this is the same idea so this is the
- 11:20same idea by the way if you try to
- 11:21translate
- 11:22one to one if i just take this by this
- 11:24is broken okay
- 11:25if i take this one to one and write this
- 11:27in risk five it's not going to work
- 11:29same the same problem happens risk five
- 11:30without this you need the amo swat to
- 11:32make this work
- 11:34so this is in a way my piece of
- 11:37keep trying until i get it and once i
- 11:39get it i know it's his mind now i'm
- 11:40guaranteed this is mine i've got the
- 11:42lock i've got this
- 11:43and i unlock with the simple there so
- 11:45all this kind of process through so all
- 11:47this
- 11:47this is this and this is this but done
- 11:50correctly
- 11:51this is broken this works okay that's
- 11:53the idea
- 11:54pretty powerful stuff how does this work
- 11:57in openmp do i have to
- 11:59now do wait dan i can't use c anymore i
- 12:01have to now write risk five
- 12:02no you can do this in openmp2 here's how
- 12:04it's done openmp let's take a look
- 12:06so first we're going to declare a
- 12:09abstract data type called a lock
- 12:11omp lock sub t who knows what that is i
- 12:14don't have any is it a number
- 12:15is it a whole struct i don't care i just
- 12:17have to reserve one of them i have a
- 12:18lock
- 12:20here is my parallel section i say get
- 12:22thread numbers ids thread numbers in my
- 12:24parallel section and now i want to have
- 12:25a piece
- 12:26that only i do the sequential section so
- 12:29i first say
- 12:30omp set lock and i say address of lock
- 12:33and now
- 12:34only when i get it do i proceed into
- 12:36that section knowing it's only me who's
- 12:38in that
- 12:38sequential section by the way all a
- 12:40thousand threads are all saying the same
- 12:41thing
- 12:42if they get to that at the same time and
- 12:43only one of them is going to grab it and
- 12:45the second one will grab it then the
- 12:46third one will grab it
- 12:48i print some id and then i end the
- 12:50sequential section by say
- 12:51omp unset the lock and i have to put
- 12:54address of lock so i pass it in
- 12:56the pointer to that lock both times okay
- 12:59and then when i'm all done i don't know
- 13:01whether that that required this
- 13:02omp the lock there was space there i'm
- 13:05gonna have to free it
- 13:06so uh i had go back to my parallels i
- 13:07had a parallel section before and a
- 13:08parallel section afterwards and i'm
- 13:10pretty good and at the end i'm gonna
- 13:11destroy that lock
- 13:12then i'm all done pretty clean pretty
- 13:15nice and pretty clean
- 13:17so that hardware synchronization was key
- 13:19that amo was key to make this work
- 13:22normally you have um libraries that
- 13:25this is true anytime you're above the
- 13:27lowest level normally you have to be
- 13:29able to
- 13:30well in c that's how you do it because
- 13:32openmp does any other language
- 13:34has to have a way every language has to
- 13:35have a way to synchronize these parallel
- 13:37things to say
- 13:38nobody only one person can own these as
- 13:40i said locked or semi-four at a time
- 13:42you have to support that so this is
- 13:44gonna be supported in almost every
- 13:45language that i know of that supports
- 13:46parallel programming there's an idea of
- 13:48a semaphore of idea of a lock
- 13:49that's built in the system and by the
- 13:51way the way it builds it in is by going
- 13:53down to and actually making call to the
- 13:54amo levels below that it'll actually
- 13:56compile or interpret down to that that's
- 13:58the key here
- 13:59oh openmp also has other pregnant for
- 14:02other things critical cases atomic
- 14:04barrier ordered there are other things
- 14:06please read the manuals in the bottom
- 14:07there's many more features private
- 14:08variables reductions a lot of stuff in
- 14:10there
- 14:11if you're going to really dive deep into
- 14:13openmp
- 14:14there's a link that uh at the openmp.org
- 14:17site that has
- 14:18uh there and there's a nice hands-on
- 14:20documentation which is useful to play
- 14:21with there's a tutorial there
- 14:23so here's an example of a critical
- 14:24section here's an example of another way
- 14:26to do this
- 14:27mutual exclusive set mutual exclusion
- 14:29says that only one thread at a time can
- 14:31be in the critical section
- 14:32and they're going to wait their term
- 14:33into that it make their wait their turn
- 14:35so let's go back into our
- 14:36you know adding up uh trying to
- 14:38approximate the value of pi
- 14:40i can just say here look at this look
- 14:41how clean and beautiful this is i can do
- 14:43the lock i certainly can do the lock and
- 14:44that would be fine i can
- 14:45work with that but it's a lot more piece
- 14:47i have to make a lock and reserve it and
- 14:49or i can just say omp critical very
- 14:52beautiful very clean
- 14:53so we've got omp parallel here's the
- 14:55open and close here omp parallel open
- 14:57actually open and close here this is
- 15:00important
- 15:01this is an open and close here for that
- 15:03o p parallel and within this little
- 15:05range is open p
- 15:06critical this this is the loop that
- 15:08closes here
- 15:09so this is the line there and right
- 15:11inside the parallel i'll say i'm saying
- 15:13this is a critical section within the
- 15:14parallel section saying this guy better
- 15:16be only one thread at a time and there
- 15:18it is
- 15:18pi plus equals some id and it works so
- 15:21we started
- 15:22we started by if you remember by the way
- 15:24there's a thousand threads here software
- 15:25threads
- 15:26we started by saying well let's just
- 15:27have a couple threads four would be fine
- 15:29then we said well why don't we have a
- 15:30thousand threads well then it's kind of
- 15:32annoying at the end i have to wait till
- 15:33the thousand things
- 15:35they're all done to be able to add them
- 15:36up that's a little annoying
- 15:38it's not really a big deal for a
- 15:39thousand but if i had that number of
- 15:40threads is a lot bigger that'd be it may
- 15:42be a problem
- 15:42how can i synchronize that well let's
- 15:44just let's just put that pi plus equals
- 15:45and then we introduced we introduced
- 15:47explicitly a race condition
- 15:48just to teach what race conditions were
- 15:50and we said well how can we get back and
- 15:51so now i'm at the point of
- 15:52like right paren i'm now closing the
- 15:54thought the thread
- 15:56the conversation about uh how to do this
- 15:58and so i introduced a problem to then
- 15:59fix it to be able to teach you what this
- 16:01critical section was
- 16:02and how you can do this with hardware
- 16:03synchronization and that's that's the
- 16:05key piece here
- 16:06now now that i bring up this idea of
- 16:09locks i now have to bring up the second
- 16:11kind of problem that you have
- 16:12introduced with with parallel code
- 16:16which is deadlock we talked about race
- 16:17conditions and how to deal with race
- 16:19conditions with these locks but once you
- 16:20introduce
- 16:21lock or simmer for you now have the
- 16:23possibility of deadlock
- 16:25what is deadlock the deadlock is a
- 16:27situation where
- 16:30multiple actors are each waiting for
- 16:32each other and
- 16:33we're frozen livelock by the way is the
- 16:36same idea
- 16:37but people are moving there's motion but
- 16:39still you're kind of stuck
- 16:40um so deadlock is up you're lurched and
- 16:42i'm waiting for you waiting for me to
- 16:43know when nothing moves
- 16:44um here's a beautiful picture just to
- 16:46show you
- 16:47of deadlock and i believe this is uh a
- 16:50traffic jam
- 16:51i think this is in china i'm not sure
- 16:53where this is uh within that but
- 16:55somebody took a picture of
- 16:56a beautiful case of deadlock that
- 16:58computer scientists all around the world
- 16:59said ah
- 17:00deadlocked and we all grabbed that
- 17:01photograph and are using it in our in
- 17:02our slides
- 17:03to teach deadlock i mean that's amazing
- 17:05all it takes is
- 17:06is one car if just like one car could
- 17:09could l i mean look at this
- 17:10thing no one can move they're all the
- 17:13problem is
- 17:13you know you get in the autumn just back
- 17:14up well people behind you now nobody can
- 17:16move at all because if this if you're
- 17:17actually
- 17:18packing in sonoma moves you're literally
- 17:19stuck no way to do this
- 17:21um if you just judge a if this guy can
- 17:23just move here move here then these guys
- 17:25can get through and then it all frees up
- 17:26but you can have this kind of situation
- 17:28if you don't have the traffic light set
- 17:29up right
- 17:31the most famous deadlock by the way is
- 17:33called the dining philosopher's problem
- 17:34and here's the problem you have these
- 17:36philosophers uh around the table
- 17:38um and each of them in parallel this is
- 17:41a parallel system
- 17:42thinks a little bit because philosophers
- 17:44they think about something and then
- 17:46they grab a left fork it's available if
- 17:47it is pick it up and then
- 17:49there's a fork on both sides by the way
- 17:51five people and five forks is the idea
- 17:54you can do this again with like two
- 17:55chopsticks one on each side but let's
- 17:56just do this one okay
- 17:58think until the left fork is available
- 17:59if one is pick it up think until the
- 18:00right fork is available when it is pick
- 18:02it up
- 18:03when both forks are held i don't know
- 18:05who eats with two forks but
- 18:07eat for a fixed amount of time so two
- 18:08forks and you're like maybe you're
- 18:09pulling apart some meat i don't know
- 18:10putting apart some piece of tofu or
- 18:11something okay
- 18:12and then when you're done put the right
- 18:14fork down put the left fork down repeat
- 18:15from the beginning
- 18:16well what can happen is everybody goes
- 18:20and picks up the left fork
- 18:22and everyone is now spin waiting on the
- 18:25right fork but everyone picked up a left
- 18:26fork
- 18:27and so this looks actually works better
- 18:28with chopsticks to be honest so
- 18:30everyone goes to the right one and
- 18:32there's no right one so all five
- 18:33are stuck in this deadlock scenario
- 18:36where there's no
- 18:37and just like the parking situation no
- 18:40no no traffic jam
- 18:41no one can grab their right fork so
- 18:43nothing happens
- 18:44here's an example of live lock you walk
- 18:47past somebody in the hallway
- 18:48and you say oh i'm so sorry you're like
- 18:49this but that person also walks like
- 18:51that you're
- 18:51sorry and you walk like this and you do
- 18:53this and the person follows your mirrors
- 18:55trying to
- 18:55do this and if you if these are kind of
- 18:57robots you can imagine a scenario where
- 18:59each robot pauses for
- 19:00the same amount of time and then moves
- 19:02to the right and then pauses for the
- 19:03same amount of time and move to the left
- 19:04and never pass each other ever you can
- 19:07imagine a little simulation where they
- 19:08just do this wiggle back and forth that
- 19:10would be a problem
- 19:12so all this is deadlock we have to think
- 19:14about how to how to prevent that what
- 19:15are some solutions to think about
- 19:16preventing deadlock we'll let you think
- 19:18about that but that is something certain
- 19:19we need to think about
- 19:22we also want to talk about timing we
- 19:23want to be able to think
- 19:25how do we prove that this wonderful how
- 19:28do i how do i adjust the parameters to
- 19:29make this parallel
- 19:31program work faster normally if i were
- 19:34looking at 621b 61a in an algorithm i'd
- 19:36do
- 19:37algorithm analysis and count the number
- 19:38of basic primitive operations and i have
- 19:39what's called a running time which is
- 19:41the number of primitive steps
- 19:42it's not time by the way running time is
- 19:43not time you learn this in cs10
- 19:45629 and 610b it's not time it's
- 19:47primitive operations account it's a
- 19:49count really and it's a count
- 19:51so it's a you know how does how do
- 19:52things grow as the size of the input
- 19:54grows that's what running time is
- 19:55we said don't use the clock don't use
- 19:57wall clock time or stop watch time
- 20:00well when you're running parallel code
- 20:02often you do use wall clock time because
- 20:04you have
- 20:05a thousand things you might you care
- 20:07less about how many steps each of these
- 20:09threads does
- 20:09but how much faster is this
- 20:11parallelization compared to before
- 20:12so in some sense you are using wall
- 20:14clock time so let's go back to
- 20:16let me undo the idea that you never use
- 20:17wall clock time to actually do that
- 20:20omp or openmp provides uh some support
- 20:23some software support to be able to help
- 20:24you with that what they do is they
- 20:25provide something called
- 20:26open omp get w time which is a void
- 20:29uh returns a double doesn't take any
- 20:31arguments the idea is it returns the
- 20:34elapsed wall clock time in seconds
- 20:37from some other time in the past and the
- 20:39way you can
- 20:40then figure this out is you have two
- 20:43calls you
- 20:44assign at the time maybe it's like
- 20:45seconds since 1900 who knows second
- 20:47since
- 20:48the year five who knows what that is
- 20:51it's some random
- 20:51at some random it's some value it's some
- 20:54some value
- 20:55of the number of uh of some time in the
- 20:58past
- 20:58boom and now i then run my code
- 21:02i then split it 14 ways i join i split
- 21:04and fork and join
- 21:05and i come back and i stop it and now i
- 21:07can say and some there are some
- 21:08parameters to this so maybe it was the
- 21:10number of threads i have or how i do
- 21:11something or maybe the algorithm
- 21:12whatever i'm doing i'm doing something i
- 21:14want to kind of measure this better
- 21:16than this as the number of threads goes
- 21:18up say or this is i run this on
- 21:19different machines
- 21:20or machines that have a different number
- 21:22of logical or
- 21:23or or physical cpus so then you have a
- 21:27second
- 21:28you have the end time and then you
- 21:29subtract these two so this is the number
- 21:32of seconds since say 1900
- 21:33this is the number of seconds in the
- 21:35future and if i subtract these two
- 21:37then the only thing i'm left with is the
- 21:38difference in time between start and end
- 21:40so that's how you use omp get w time as
- 21:42a way to measure wall clock time
- 21:44to see how some parallel analysis works
- 21:46okay
- 21:48that's the end of this mini lecture
- 21:49we'll see the next one thanks so much
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 35.1 - Thread-Level Parallelism III: Hardware Synchronization by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 4,873 words across 758 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.