[CS61C FA20] Lecture 34.2 - Thread-Level Parallelism II: OpenMP — Transcript
Full transcript
- 0:01and welcome back now let's see a
- 0:03wonderful abstraction called
- 0:04openmp that can allow us work with those
- 0:06multiple cores very efficiently
- 0:09and in an easy way without having to add
- 0:11a lot of time a lot of code in c
- 0:13so if you had a parallel loop here's a
- 0:15very simple
- 0:16loop has a hundred iterations is going
- 0:18to have the value 0 through 99
- 0:20inclusive and you just have one thread
- 0:23just doing all of that
- 0:25if you had four helpers how would you do
- 0:26it in parallel well the smart way to do
- 0:28it if you know about caches
- 0:30in which you know that it makes sense to
- 0:33have maybe one worker work with one
- 0:35particular cache block and have
- 0:37if you have at least a wider cache block
- 0:39when you bring the block in
- 0:40you can work with all your neighbors uh
- 0:43again
- 0:44we don't want to have four different
- 0:45workers all working on the same
- 0:47particular word or
- 0:48the same particular block it'd be nice
- 0:49to kind of separate them out divide it
- 0:51up
- 0:52so we break them up into zero through
- 0:53twenty five seven
- 0:55twenty four twenty five through forty
- 0:56nine 50 through 74
- 0:58and 75 through 99 and so now you have
- 1:00four different workers
- 1:01all working on one part of the of the
- 1:03loop so i'm going to work on this part
- 1:04of the array you go away from me
- 1:05i'm going to work on this part of the
- 1:06array and you do the same thing so there
- 1:08are four of us all
- 1:09kind of owning a space of the array
- 1:11that's the cleanest way to do this
- 1:12you could also have slices up a
- 1:14different way which would have been
- 1:14really bad for the cache in which
- 1:17each one of them is going to work on i
- 1:20mod 4 and i'll be imod 4 equals 0.
- 1:23so 0 4 8c 12 16
- 1:27and you want you work on i mod 4
- 1:31equals 1 so 1 5 9. you know you could do
- 1:35it that way like kind of like how the
- 1:36way direct map cache maps every other
- 1:38color to that
- 1:39but that's not the way we want to do
- 1:40this in terms of parallel execution you
- 1:41want to own a space of it
- 1:43i'll load the whole cache block in i
- 1:44work on this and i don't bother me i'm
- 1:46going to break this whole part of the
- 1:47array so you want to kind of separate
- 1:48them out in memory so they're not
- 1:50stepping into those toes if that makes
- 1:51sense so this is how we divide it up
- 1:52this a smart way to divide it up
- 1:55how to do this in openmp it's not that
- 1:57bad i first have to include the header
- 1:59file the omp
- 2:00then i just have to say a pragma pragma
- 2:02is a way kind of a directive to see to
- 2:04say i want to do something special
- 2:05that's above and beyond
- 2:06pragma omp parallel 4 and it's done
- 2:10that loop will be paralyzed that's all
- 2:13you got to do that's amazing and
- 2:14loops are so common this is a very
- 2:17effective way to do this so here's an
- 2:18example
- 2:19i grab a pen i'll walk you through how
- 2:21this entire thing works
- 2:22um there are different compilers that'll
- 2:24support this
- 2:25you've got clang here's an example you
- 2:27got gcc you've got your cc
- 2:30dash five and i would recommend the top
- 2:31one gcc-5 because
- 2:33it lets you only have a very simple line
- 2:36look dash f openmp that's all you need
- 2:38to say is dash f openmp
- 2:40and here's your here's your source code
- 2:42and you get it out and the a dot works
- 2:44if you want to use gcc you have to add
- 2:46dash l openmp
- 2:48and then you could also name it but i
- 2:49plan with the x preprocessor much
- 2:51cleaner just to say
- 2:52use gcc five which is really nice all
- 2:55right how does this code what is this
- 2:56code doing all this code essentially
- 2:57doing is it has an array of ten elements
- 3:01whose value is the same as the index so
- 3:03array element one is the
- 3:05index one every element nine is index
- 3:07nine really easy okay
- 3:09i'm going to tell omp openmp that i want
- 3:11to use four
- 3:12different software threads i don't know
- 3:14how many i have no idea how many
- 3:16hardware threads i'm allowed how many
- 3:18cores i have whether i'm hyper 30 on
- 3:20hyper threading on or off i don't know
- 3:21that i'm just going to say i want to use
- 3:22four software threads let's work with
- 3:24that
- 3:25this says n is going to be size of a
- 3:27over size of int
- 3:29essentially that's just 10 so n is 10
- 3:31elements
- 3:32here's my pragma i talked about a second
- 3:34ago omp
- 3:35parallel 4 that means this for loop is
- 3:37going to be paralyzed here we go
- 3:39so let's take a look at that what
- 3:40happens here well
- 3:42for i o 0 i is less than 10 what happens
- 3:46i'm going to print
- 3:46the following i'm going to print the
- 3:48thread number and here's get thread
- 3:50number
- 3:51and i'm going to print the value of i so
- 3:53what i'm essentially doing is saying
- 3:55which of the three there are four
- 3:56threads zero one two and three they're
- 3:58numbered zero through three for four
- 4:00threads and i'm going to then assign the
- 4:03value of a of i'm going to overwrite it
- 4:05before
- 4:05a of i was just zero through nine i'm
- 4:06going to replace it and add it
- 4:09zero through nine obviously has only uh
- 4:11single digits so
- 4:12that's only one's values i'm going to
- 4:14add a tens column and the tens column is
- 4:16going to be
- 4:17the thread number that's what this is
- 4:18basically put the thread stop the thread
- 4:20number clobber the thread the tens
- 4:22column with the thread number what
- 4:24that's going to give me then at the end
- 4:25when i'm all done
- 4:26so only this part is paralyzed then at
- 4:28the end it says we'll go through
- 4:30all the array and print the values of
- 4:31the array that's all this is doing
- 4:32nothing really magical here
- 4:34but here's what's really i'm gonna show
- 4:35you a demo in a second it's gonna be fun
- 4:37just like we predicted the i've color
- 4:40color-coded this if this helps a little
- 4:42bit so i'm going to
- 4:43show you this here so this
- 4:46single digit the tens digit says all of
- 4:48these were owned by thread zero
- 4:50zero one and two the le the first three
- 4:52this is like zero to twenty four
- 4:54here it's only ten elements and by the
- 4:55way this automatically happens
- 4:57works even though ten isn't uh a
- 5:00multiple of four
- 5:01it works beautifully and just you know
- 5:02all that details of well it's not a
- 5:04multiple of four
- 5:05so you can't it just it just handles it
- 5:07dynamically
- 5:08beautiful thread number one
- 5:12handles the values three through five so
- 5:15this is zero through two is thread zero
- 5:17uh thread number two handles six and
- 5:20seven
- 5:21and thread number three handles eight
- 5:23and nine so it's
- 5:24neat look at this three of them three of
- 5:26them two of them and two of them it did
- 5:28as well as it could in terms of dividing
- 5:29them up into
- 5:30four equal parts amazing the interesting
- 5:33part
- 5:34is when you look at what gets printed
- 5:36out part of what you know professor lee
- 5:38was talking about in that quote
- 5:40was that it's unpredictable when things
- 5:43will return when those
- 5:44software threads get mapped to hardware
- 5:45threads and run and then how fast
- 5:47they finish and complete let's look at
- 5:50this this happens to be a very
- 5:52pretty thing look zero one two zero one
- 5:55sorry zero one two three and then oh
- 5:57look
- 5:57zero one two three and then zero
- 6:00and one so it looks like they did it in
- 6:03order
- 6:04now let's run it twice i have this
- 6:05queued up this is the same code nothing
- 6:07different this code is available to you
- 6:09i'm going to run this code now let's run
- 6:10the for loop
- 6:12oh look how nice it is all the zeros hit
- 6:15first zero and two then all the ones
- 6:16three four five
- 6:17then look at this then the threes hit
- 6:20look at this
- 6:21then the threes the threes came in and
- 6:23then they finished
- 6:24and then you got a two and the twos now
- 6:27let's run it one more time
- 6:28watch what happens the twos
- 6:31the ones the threes the zeros zero
- 6:34three two one look at this zero one two
- 6:38three zero one two three zero one two
- 6:41look at this okay
- 6:45zero one this is uh oh oh look at this
- 6:48one
- 6:49the three zeros finished then the one
- 6:51started then two twos finished
- 6:54then two more ones finished and then the
- 6:56threes got in the game
- 6:58so this is really interesting you have
- 7:00no idea as i'm running this
- 7:03who's going to be divided up the three
- 7:05now they're all together we can just
- 7:06keep trying this
- 7:06now the one look there's a first one
- 7:08came in there then the three then
- 7:09another zero
- 7:11i'm just showing you this is a this is
- 7:12what the realities of this are so
- 7:14as you're thinking about writing code
- 7:16you have to write code that's
- 7:17impervious to how long it takes these
- 7:20workers to finish that's the hardest
- 7:22thing to do
- 7:23as you're stepping back and point how do
- 7:24i divide this up and also
- 7:26i don't care how long i have to have
- 7:28code
- 7:29that is able to handle naturally just
- 7:32resilient in terms of handling this and
- 7:35this particular thing
- 7:36doesn't if i care about the order of
- 7:38printing out that doesn't any side
- 7:39effect like this is going to affect
- 7:40be affected by the order so you have to
- 7:42have your computation
- 7:44be again impervious to how those threads
- 7:47get
- 7:48loaded into hardware threads and return
- 7:49and finish
- 7:52in summary let's do just two slides for
- 7:53summary it's a c extension no new
- 7:55language to learn
- 7:56well let's learn go and make you do a
- 7:58project and go people have thought about
- 8:00doing that and that's a lot harder than
- 8:01just
- 8:02living in c getting better at c but
- 8:04using all the what you know how to see
- 8:06all that you know about c to make c
- 8:09parallel
- 8:10so it's a wonderful extension we're
- 8:11really really a fan of multi-threaded
- 8:13shared memory parallelism
- 8:14you add a compile directory with the
- 8:16pragma you've got this runtime library
- 8:17with without h files that includes all
- 8:19the
- 8:19headers that you need here's the nice
- 8:22thing the pragma
- 8:24any pragma you have is ignored by
- 8:26compilers who don't know about openmp
- 8:28um so it's wonderful and it's the same
- 8:31source code for
- 8:32multiple architectures one core 16 cores
- 8:35hyper threading on or off 28 cores
- 8:38doesn't matter same source code will
- 8:40just work you gotta
- 8:41it's just it's just beautiful that way
- 8:42um it uh it also
- 8:44only works with shared memory so here's
- 8:46the programming model
- 8:49you've got a main thread we call the
- 8:50main thread or master i prefer the word
- 8:52main thread you've got and then it's
- 8:53going to fork
- 8:54it's going to fork its way and you have
- 8:56then multiple streams in the parallel
- 8:58region so all these are multiple streams
- 9:00all working together this parallel for
- 9:02this fork is a way
- 9:04one of these this these are called
- 9:05patterns this is one of the software
- 9:07patterns for parallel programming
- 9:09the idea you fork into these multiple
- 9:11threads and they go through and then
- 9:12there's some join at the end and that's
- 9:14impli all this isn't you used to be able
- 9:16to have you used to have to
- 9:18explicitly call a fork and explicitly
- 9:20call to join this parallel four does all
- 9:23that for you which is we
- 9:24which we really like but if you wanted
- 9:25to have this explicitness you can get to
- 9:27that
- 9:28but four conjoint is another option you
- 9:30know that's another model to think about
- 9:31this so
- 9:32there's a parallel region and then you
- 9:34have some serial region
- 9:36then a parallel region and this might be
- 9:37this then another one it might only have
- 9:40three
- 9:40and you divide this up and so you have
- 9:42these serial regions
- 9:43and you have these parallel regions we
- 9:46love that
- 9:47so they begin they begin in a single
- 9:49process i call it the main thread
- 9:51this is the sequential execution then
- 9:53when a parallel region is encountered
- 9:55it forks it into parallel threads they
- 9:58execute simultaneously as much as you
- 9:59can as much as the hardware will allow
- 10:01and at the end of that there's a join
- 10:02which brings it back to the serial
- 10:04portion again
- 10:05um we're going to see amdahl's law keep
- 10:07talking about amdahl's law we're not we
- 10:08didn't talk to amdahl's law yet when we
- 10:10teach you realize
- 10:11the point of amble's law is it's really
- 10:12painful being these serial portions as
- 10:14much as you try to speed up the code
- 10:16the longer you spend on these serial
- 10:17portions the harder it is to speed that
- 10:19whole thing up uh because that's that's
- 10:20that's what dominates over time so what
- 10:23kind of threads are we talking about
- 10:25remember i mentioned before these are
- 10:26all software threads i general
- 10:28thank you so much i generate these
- 10:30software threads
- 10:33the os is job it's not my job don't
- 10:35worry about it not my job man
- 10:36to multiplex these onto the hardware
- 10:39thread so
- 10:40this oh the operating system does that
- 10:42hard work to figure out who's idle
- 10:44who's stalled who's blocked or who's
- 10:46making memory access to go to sacramento
- 10:48okay get this guy out
- 10:49and get the next one in all that's
- 10:51handled by by the os it's wonderful
- 10:56let me look at other things i want to
- 10:58say here um
- 11:01you're certainly competing for hardware
- 11:03threads you certainly have a fixed
- 11:04amount of hardware and all those
- 11:05softwares are competing for that space
- 11:07there um the key the key thing that's
- 11:10actually
- 11:10really hard is that be careful when
- 11:12you're doing timing
- 11:14um timing is a complicated thing we
- 11:16encouraged
- 11:17people all in 621a 621b cs10 not to use
- 11:20clock timing don't use clock timing
- 11:22figure out what this is so we have to be
- 11:24able to share with you some of the
- 11:26hardware support some of the software
- 11:27support to do timing because i i almost
- 11:29feel like i need to do timing on this
- 11:31well let me just have a stopwatch and
- 11:33start do it
- 11:34have a single thread and time that and
- 11:36then compare that with a hundred threads
- 11:38and then stopwatch we kept telling
- 11:40people don't use a stopwatch in timing
- 11:41but in some sense we know we need to
- 11:43think about
- 11:43uh what support we have so that i don't
- 11:45have to rely on my my broken stopwatch
- 11:47for measuring that
- 11:48we'll learn more about openmp and lots
- 11:50more examples in the next couple of
- 11:51lectures we'll see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 34.2 - Thread-Level Parallelism II: OpenMP by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,520 words across 394 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.