YouTube2Text

[CS61C FA20] Lecture 34.3 - Thread-Level Parallelism II: Computing Pi — Transcript

by CS 61C Departmental · 3,111 words · 489 segments · language en · Watch on YouTube

Full transcript

  1. 0:00and welcome back now let's see a really
  2. 0:02nice example
  3. 0:04in which we try to compute pi first
  4. 0:06we'll try it in cereal and then we'll
  5. 0:08try it in parallel and see we can do
  6. 0:09this
  7. 0:09we like this problem because we call it
  8. 0:11embarrassingly parallel
  9. 0:13it slices up really neatly into
  10. 0:15different slices of problems
  11. 0:16that can be farmed out to workers and
  12. 0:18they come back and each of the workers
  13. 0:19doesn't need the neighbor's
  14. 0:20value there's not a lot of communication
  15. 0:22between them i'll just do my job and
  16. 0:23then give it back and then you kind of
  17. 0:24put it all together at the end
  18. 0:26it's a nice four conjoined model
  19. 0:29there are two ways to do this this is
  20. 0:31the traditional way the
  21. 0:33if you were look at x squared plus y
  22. 0:35squared equals one
  23. 0:36that's a circle of area one and if you
  24. 0:39looked at a quarter of that
  25. 0:41so the top right corner if you just
  26. 0:43solve that for y you get y equals
  27. 0:451 uh minus x squared square root of 1
  28. 0:48minus x squared that's the
  29. 0:49that's the upper right value that's the
  30. 0:51upper right value here if i do this
  31. 0:52and look at this this is an area one
  32. 0:54this is sorry area pi
  33. 0:56radius 1 and the area equals pi
  34. 1:02this is simply y equals square root of
  35. 1:061 minus x squared from and if i
  36. 1:08integrate this from 0 to pi i'm going to
  37. 1:10get a quarter because i'm
  38. 1:12going to get a quarter of pi 0 to 1 i'm
  39. 1:14going to get a quarter pi if i multiply
  40. 1:15that by 4
  41. 1:16i get pi so this is just integrating
  42. 1:19this equation just from here just this
  43. 1:21curve just this curve
  44. 1:22and i say what's this area in here and
  45. 1:25this is 1
  46. 1:26and i integrate this and this is height
  47. 1:281 also this is
  48. 1:29square root of 1 minus 1 minus x squared
  49. 1:32but i multiply that by 4
  50. 1:34and i integrate between 0 and 1 i get pi
  51. 1:36so this is mathematically telling me i
  52. 1:38have just pi there's nothing it doesn't
  53. 1:39tie today it just
  54. 1:40says pi is a number and there there's
  55. 1:42another analogy which
  56. 1:43this curve this is beautiful this is
  57. 1:46also if you integrate this from zero to
  58. 1:47one
  59. 1:48pi which is a nice curve it looks like
  60. 1:50that picture on the right you see
  61. 1:52what we like about this over the top one
  62. 1:54is that
  63. 1:56square root can be slow from a floating
  64. 1:58point calculation point of view
  65. 1:59but squaring is really easy so we can we
  66. 2:02can do multiplication really fast much
  67. 2:03better than square root so we're gonna
  68. 2:04try this one
  69. 2:05and we see we can integrate this again
  70. 2:06from zero to one and do this so remember
  71. 2:07it's 4 over
  72. 2:091 plus x squared and here's a piece of
  73. 2:11code
  74. 2:12that does this serially here's what
  75. 2:14we're trying to get to at the top
  76. 2:20three 3.14159.26535.897932 uh three
  77. 2:22eight four et cetera
  78. 2:24so let's look at what this code does i
  79. 2:26have only ten steps i'm going to ten
  80. 2:28different rectangles to approximate my
  81. 2:30area
  82. 2:31and the step size is one over the number
  83. 2:33of steps that's the width of each of
  84. 2:34those rectangles as if i remember look
  85. 2:36at this
  86. 2:37what's the width of that rectangles that
  87. 2:39step
  88. 2:41okay what's the height well remember
  89. 2:43this before the height is 4 over 1 plus
  90. 2:45x squared
  91. 2:46and so this is 4 over 1 plus x
  92. 2:49squared wherever the value of x is x is
  93. 2:51a value from 0 to 1.
  94. 2:53so i initialize my accumulator sum as
  95. 2:56zero and then i'm going to go through
  96. 3:00you always want to iterate on integer
  97. 3:02values you want to be you don't want to
  98. 3:03be iterating on floating point values so
  99. 3:05i'm going to count how many steps
  100. 3:07because you always have a very clean cut
  101. 3:08line
  102. 3:09floating point has this noise right
  103. 3:11rounding noise so you don't want to have
  104. 3:12well sometimes i
  105. 3:13i ended up having some number of
  106. 3:15iterations another some other number if
  107. 3:17based on the noise i want to have a
  108. 3:18clean number of iterations now i have
  109. 3:19exactly here numsteps
  110. 3:21iterations x is going to continue to
  111. 3:25move to the right
  112. 3:26so here's the value oops here's the
  113. 3:27value of x as it continues to move to
  114. 3:29the right
  115. 3:29whoops as i continue to move to the
  116. 3:31right here
  117. 3:33sum starts at zero starts at zero and is
  118. 3:36going to be
  119. 3:37the area of that rectangle this is the
  120. 3:40width
  121. 3:41and this is the height okay so none of
  122. 3:44that none of that's
  123. 3:45none of that's magic and i'm doing that
  124. 3:47here and when i'm all done
  125. 3:48i print out the value of pi let's see
  126. 3:50what i get
  127. 3:51well i'm trying to get three point four
  128. 3:53one five nine two six five three five
  129. 3:55well i got to the two well it's supposed
  130. 3:58to be a one three one three one four one
  131. 3:59i didn't even get there so i got two
  132. 4:01significant figures if i increase num
  133. 4:04steps i'd do very well
  134. 4:05i would you know increase some steps to
  135. 4:07the biggest number i possibly can handle
  136. 4:09i would do really well take a long time
  137. 4:11but i do very well not very accurate
  138. 4:14so let's increase the numb steps and
  139. 4:16let's paralyze let's actually play with
  140. 4:17this and see how close we can get
  141. 4:19parallelization version boy i wish there
  142. 4:21were a way i wish certainly our way to
  143. 4:23just
  144. 4:24have a one-line change and make this
  145. 4:26work
  146. 4:27we do that pragma we talked about before
  147. 4:29that pragma parallel 4
  148. 4:31and i'm now done so i'm not going to
  149. 4:32paralyze that for loop openmp does all
  150. 4:35the hard work i obviously have to add
  151. 4:36the include file but that's all i'm
  152. 4:37going to add i love this isn't it nice
  153. 4:40two lines i add and this thing is now
  154. 4:41paralyzed
  155. 4:43num steps is still 10. and we can see
  156. 4:46what happens
  157. 4:47here we go here's my parallel four i'm
  158. 4:49going to sum plus equals let's see what
  159. 4:51happens here all right it looks pretty
  160. 4:52good
  161. 4:53and uh oh i've got a problem
  162. 4:57why is there a problem let me see what
  163. 4:58the problem is here
  164. 5:01each thread needs access to this shared
  165. 5:04element
  166. 5:05the whole idea of parallelization one of
  167. 5:06the core ideas of paralyzing
  168. 5:08anything just in life anything is
  169. 5:12what are any shared resources if i've
  170. 5:14got a shared resource i really have to
  171. 5:15be very careful about the shared
  172. 5:17resource
  173. 5:17if i have multiple workers all working
  174. 5:20on and reading and writing to a shared
  175. 5:21resource
  176. 5:22imagine you know imagine a google doc
  177. 5:24and they're all reading and writing and
  178. 5:25copying and grabbing a thing and
  179. 5:27grabbing their own version
  180. 5:28which is in their cache and then putting
  181. 5:29it back it could really be ugly google
  182. 5:31doc is nice because it's not really
  183. 5:32their own copy of this
  184. 5:33but if i had a folder and i'm copying a
  185. 5:35version of paper and i'm putting it back
  186. 5:36and then you're grabbing copy
  187. 5:37when i'm writing in parallel but there
  188. 5:39are issues like that
  189. 5:40so this shared value sum is a problem we
  190. 5:43don't like that
  191. 5:44the code's going to run sequentially
  192. 5:46that sum it knows that some is is
  193. 5:48sequentially
  194. 5:49so we got to think about how to do this
  195. 5:50in a way that i don't have this single
  196. 5:52this single sum that's that's going to
  197. 5:54be shared across across these guys
  198. 5:57so let's do this what if i did the same
  199. 6:00thing i did with my for loop and just
  200. 6:01slice it up and say sum
  201. 6:03zero first thread you're going to deal
  202. 6:05with the left side of this
  203. 6:06and you're going to do with the right
  204. 6:07side of this and then i compute
  205. 6:10independently sum of zero and sum of one
  206. 6:12so i'm going to have the sum array
  207. 6:13that i independently compute and it'll
  208. 6:15independently contribute to so
  209. 6:17you know here's just two accumulated one
  210. 6:19accumulator i'm gonna have
  211. 6:21multiple accumulators in parallel and
  212. 6:23then
  213. 6:24when i'm all done i then add them up and
  214. 6:26the join is
  215. 6:27a sequential thing where i go through
  216. 6:29each of the sum all right what was yours
  217. 6:30you know
  218. 6:31it's like all these workers went out and
  219. 6:32it worked for me how much you make
  220. 6:34okay how much you make how much you make
  221. 6:35and i write them all down at the end of
  222. 6:36the day but they all were doing their
  223. 6:37thing in parallel working their job at
  224. 6:39the end of the day
  225. 6:39the boss asked you how much did you make
  226. 6:41and here it's saying how much did you
  227. 6:42accumulate how much area how much
  228. 6:44average you calculate how much average
  229. 6:45did you calculate
  230. 6:46done so that seems like the way we do
  231. 6:48this so let's do a trial run here
  232. 6:50so now what do we change well i have to
  233. 6:53have
  234. 6:54i'm going to hard code the number of
  235. 6:56threads to four we talked before about
  236. 6:57how to ask the total number of threads
  237. 6:59are
  238. 6:59or i can set that somewhere and by the
  239. 7:01way here's the way i'm setting it so num
  240. 7:03threads is this
  241. 7:04constant i'm just saying it to 4 so i
  242. 7:05did that before
  243. 7:07and i'm going to do my parallel loop i
  244. 7:09have to here i go is my sum and i'm
  245. 7:11going to initialize my sum to zero so it
  246. 7:13wasn't as easy as before before i had a
  247. 7:14one-liner sum is zero now i have to have
  248. 7:16a little loop
  249. 7:16to initialize all my sum increment
  250. 7:18accumulators to be zero
  251. 7:21i do the same thing everything's the
  252. 7:22same except that what am i doing
  253. 7:24differently here
  254. 7:26here's this idea each of them is giving
  255. 7:28up a different piece of that array just
  256. 7:30like i was doing my for loop before
  257. 7:32each of them is going to be contributing
  258. 7:33to a different sum of sum of id where id
  259. 7:36is my
  260. 7:37get thread number aha we did this before
  261. 7:39we did this printout we kind of showed
  262. 7:41what that was
  263. 7:42here i'm also printing out this piece of
  264. 7:44it and the id number same as before all
  265. 7:46that's the same code as before
  266. 7:48i am accumulating all to the various
  267. 7:49values of the sum and at the end
  268. 7:51i initialize pi the return value to be
  269. 7:53zero i
  270. 7:54at i keep in sequentially not in
  271. 7:57parallel
  272. 7:58contributing sums contribution into pi
  273. 8:01and then i print what pi is how's my
  274. 8:03number 3.142425 we did pretty well
  275. 8:07i've parallelized this at least into
  276. 8:08four different ways and hopefully this
  277. 8:10is pretty close to being four times as
  278. 8:11fast it's not going to be perfect
  279. 8:12there is some overhead i still have to
  280. 8:14do this adding up everything
  281. 8:16of the sums in sequential order but i'm
  282. 8:18getting close to a four time improvement
  283. 8:20in speed love this did pretty well let's
  284. 8:23increment this to now
  285. 8:24just change one line to say rather than
  286. 8:26uh number steps is ten
  287. 8:27number steps is now with a million and
  288. 8:30how am i doing
  289. 8:31three point one four one five nine two
  290. 8:33six five three five
  291. 8:34eight nine seven not bad
  292. 8:38not bad so we're actually doing fairly
  293. 8:40well this is a pretty good job
  294. 8:42i'm feeling very confident about this i
  295. 8:44love this
  296. 8:46now i gotta take a second back
  297. 8:49how do i paralyze computing sum
  298. 8:52you remember that sum at the end i had
  299. 8:54to stop and remember look
  300. 8:56that was fine when i did this this is
  301. 8:58i'm stalling
  302. 9:00for four threads it's not too bad it's
  303. 9:03only four threads
  304. 9:04um this has nothing to do with its
  305. 9:06number of steps number threads could i
  306. 9:08paralyze
  307. 9:09that threads what if i had a lot of
  308. 9:11threads what if i had
  309. 9:12could i paralyze that parallelize the
  310. 9:15computing the sum
  311. 9:16in some way that would be interesting
  312. 9:19well let's try this one
  313. 9:20let's bring that pi and that pi plus
  314. 9:22equals sum of id
  315. 9:23let's bring that into the loop somehow
  316. 9:25so now i put this bracket around
  317. 9:26parallel omp pair
  318. 9:28parallel pragmatic parallel so here's
  319. 9:29this bracket around that
  320. 9:31and i brought the pi computation into
  321. 9:34that loop
  322. 9:36let's try it i'm so excited now be a
  323. 9:37little bit more efficient just that last
  324. 9:39group of four
  325. 9:40let's bring that in there what do i get
  326. 9:41okay so the summation is now inside the
  327. 9:43parallel section
  328. 9:44so there's not much speed up here right
  329. 9:46you're only speeding up for these four
  330. 9:47guys but what happens when i run this
  331. 9:533.1415926535897932
  332. 9:56uh oh 3.13 wait
  333. 10:00look i've got number of steps is a
  334. 10:01million wait wait i got a million
  335. 10:04i got a thousand threads here look a
  336. 10:05thousand threads so i added more threads
  337. 10:07here a lot more software threads that's
  338. 10:09different so i cranked that up
  339. 10:10i still got my million steps that's
  340. 10:12that's doing fine except do i do that a
  341. 10:14million
  342. 10:15then yeah that's like a hundred thousand
  343. 10:16steps it's a hundred thousand steps it's
  344. 10:18still pretty good
  345. 10:19but look at it behind what happened to
  346. 10:20pi
  347. 10:22that's that's crazy something went wrong
  348. 10:25here and what's even worse
  349. 10:27is the value changes between runs if i
  350. 10:30were to run this
  351. 10:3110 times like you saw before the value
  352. 10:33is going to change between runs
  353. 10:35what is the problem here well the
  354. 10:37problem somehow
  355. 10:38all the other thing i did differently
  356. 10:40was i added more threads that shouldn't
  357. 10:42be the problem
  358. 10:43change of steps down by factor 10 that
  359. 10:44shouldn't be the problem i brought the
  360. 10:46sum
  361. 10:47into the parallel part can you see why
  362. 10:49that's an issue
  363. 10:51what's going on
  364. 10:55this my friends is called a race
  365. 10:58condition
  366. 10:59and this is a big idea when you run in
  367. 11:01parallel where you run with the when you
  368. 11:03run with the parallel people
  369. 11:04having race conditions is something you
  370. 11:06have to really work hard to avoid
  371. 11:08because it's a way to have
  372. 11:10garbage values you're going to it's a
  373. 11:12way to have not just garbage value i
  374. 11:13mean this pie calculator is almost
  375. 11:14useless anymore right i was trying to do
  376. 11:16this so i can get a nice
  377. 11:17very very high resolution value of pi
  378. 11:20i'm not getting that at all and it's
  379. 11:22going to change between runs that you
  380. 11:24can't have that we can't have this is
  381. 11:25again
  382. 11:25pruning that non-determinism and one of
  383. 11:27the things is this race condition is the
  384. 11:29reason for that
  385. 11:31what if more than one thread grabs that
  386. 11:33so more than one thread grabs
  387. 11:35watches pi equals ply plus my sum of id
  388. 11:39so far so good that's so that's no
  389. 11:40problem so what's happening
  390. 11:42and this is at the end by the way that
  391. 11:43look all that main loop here's the main
  392. 11:45loop for for a hundred thousand here's
  393. 11:46the main loop on a thousand
  394. 11:47this is just a little bit here at the
  395. 11:50end where i have to now do this
  396. 11:51calculation
  397. 11:52uh at the end this
  398. 11:55this is problem because what happens at
  399. 11:57the end of all this is
  400. 11:58the day this is the work day this is the
  401. 12:00workday and now i'm at the point where
  402. 12:01all thousand people have to contribute
  403. 12:03their
  404. 12:04computation into the value of pi
  405. 12:08okay
  406. 12:11plus equals plus equal says pi equals pi
  407. 12:14plus
  408. 12:15there which means each what happens if
  409. 12:17two parallel threads
  410. 12:19grab the old value of pi
  411. 12:23they read they read they both grab the
  412. 12:25let's say the pi is pi is three let's
  413. 12:26just say pi
  414. 12:28now it's three okay i'm going to add my
  415. 12:30little fraction to that
  416. 12:33maybe both of us calculated point one
  417. 12:35right 0.05
  418. 12:37so 0.05.05.5 should be 3.1 at the end of
  419. 12:40the day right
  420. 12:40if here's pi at 3 and i calculate 0.05
  421. 12:43and you got 0.05 and we both calculate
  422. 12:45our piece to it
  423. 12:46so that should pi should be 3 plus 0.05
  424. 12:48plus 0.05
  425. 12:49is 3.1 okay and then there's more to get
  426. 12:52the four block etc
  427. 12:54so stay with me we both read three
  428. 12:58you can see this race can you see it
  429. 12:59happening i'm going to slow motion
  430. 13:01we both take r3 we both internally add
  431. 13:04our
  432. 13:05and we both now you write first that now
  433. 13:08three becomes 3.05
  434. 13:10and i write mine pi becomes now
  435. 13:133.05 we both wrote our 3.05 and it's not
  436. 13:16like we able to
  437. 13:17one of them was thrown away the first
  438. 13:18one was thrown away because this one
  439. 13:20was this could have been 3.06 right
  440. 13:22there and then 505 up it wrote over
  441. 13:24there
  442. 13:25trouble trouble trouble trouble trouble
  443. 13:27trouble
  444. 13:31the only way to deal with this is we got
  445. 13:32gotta we gotta figure out so that's the
  446. 13:34problem this is called a race condition
  447. 13:36this is a non-deterministic by the way
  448. 13:37this is through this this is when they
  449. 13:39were both the same what if one is
  450. 13:40bigger than smaller what if this is 0.06
  451. 13:42from 0.05
  452. 13:44here and here whoever writes last is
  453. 13:45going to be able to own that number and
  454. 13:47the other one is lost
  455. 13:48so it's not deterministic and i've lost
  456. 13:50values so this is really trouble
  457. 13:55we've got to figure out how to how to
  458. 13:56prune this so we're going to look at how
  459. 13:58to kind of
  460. 13:59lock the other people out of code that
  461. 14:01really should be sequential now we had a
  462. 14:03sequential at the end
  463. 14:04but we want to be able to do this in a
  464. 14:06clever way
  465. 14:07that we still get a little bit of
  466. 14:08parallelism but we gotta
  467. 14:10have a lock there's got to be a way to
  468. 14:11lock people out of this to be able to do
  469. 14:13this in a clever way
  470. 14:14okay so let's see if we can do this
  471. 14:18i still want to be able to imagine so i
  472. 14:19have a thousand threads i still want to
  473. 14:21be able to paralyze my
  474. 14:23contribution to pi but i want to do it
  475. 14:25in a way that
  476. 14:26makes sure that not two people aren't
  477. 14:28both trying to write to pi at the same
  478. 14:29time
  479. 14:30because if they're independent if a
  480. 14:31thousand people never happen to run it
  481. 14:33and override at the same time i'd be
  482. 14:34fine it'd be great and it'd be parallel
  483. 14:36each one is contributing their pi rather
  484. 14:37than having one serial guy at the end
  485. 14:39so i like that i want to get to that
  486. 14:40place but i need some help from software
  487. 14:42to support and prune that that race
  488. 14:44condition okay
  489. 14:45we'll see the next video

About this transcript

This page contains the full transcript of [CS61C FA20] Lecture 34.3 - Thread-Level Parallelism II: Computing Pi by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 3,111 words across 489 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.