Numtasks grab tottime lock tottime mytime release

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: asks CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 64 Synchroniza3on Mechanisms in Pthreads 1) mutex: “mutual exclusion” – essenDally a lock –  operaDons: •  init, destroy: create and destroy them •  lock, unlock: grab and release the lock •  trylock: aMempt to grab the lock, but don’t block if you can’t CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 65 Synchroniza3on Mechanisms in Pthreads 2) condi@on variables: a “waiDng room” for some condiDon to become true –  operaDons: •  init, destroy: create and destroy them •  wait: wait for a condiDon to become true •  signal/broadcast: signal to one/mulDple thread(s) that it is –  raDonale: avoid spinning on some test in user code •  e.g., “wait for this variable to take on some nonzero value” •  such spinning is typically not a wise use of resources •  instead: let the thread library manage who should wake up when CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 66 Condi3on Variables: Fiddly Details There are some details that complicate condiDon vars: –  mutex argument: must be managed properly –  spurious wakeups: verifying that the condiDon is sDll true once you’ve awoken from a wait() See Ch. 6 of the text and/or this tutorial for details: –  hMps://compuDng.llnl.gov/tutorials/pthreads/#CondiDonVariables CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 67 Using Mutexes to fix RRWW bugs pthread_mutex_t totTimeMutex; pthread_mutex_init(&totTimeMutex, NULL); create tasks … pthread_mutex_lock(&totTimeMutex); totTime += myTime; pthread_mutex_unlock(&totTimeMutex); … join tasks pthread_mutex_destroy(&totTimeMutex); CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 68 Using Mutexes to fix RRWW bugs The result is that there are only two legal orderings of the totTime updates: @me task 1 grabs the mutex first Task 1 mutex lock reg = read totTime reg = reg + myTime totTime = write reg mutex unlock task 2 grabs the mutex first Task 2 mutex lock (…blocks…) reg = read totTime reg = reg + myTime totTime = write reg mutex unlock CSEP 524: Parallel ComputaDon Task 2 Task 1 mutex lock reg = read totTime mutex lock reg = reg + myTime totTime = write reg (…blocks…) mutex unlock reg = read totTime reg = reg + myTime totTime = write reg mutex unlock Winter 2013: Chamberlain 69 Synchroniza3on Mechanisms in Chapel 1) synchroniza@on variables 2) single-­‐assignment variables CSEP 524: Parallel ComputaDon Winter 2013: Chamberlain 70 —  Syntax sync-type: sync type —  SemanDcs —  Stores full/empty state along with normal value —  Defaults to full if iniDalized, empty otherwise —  Default read blocks unDl full, leaves empty —  Default write blocks unDl empty, leaves full —  Examples: CriDcal secDons and futures var lock$: sync bool; var future$: sync real; lock$ = true; critical(); var lockval = lock$; begin future$ = compute(); computeSomethingElse(); useComputedResults(future$); 71 var buff$: [0..#buffersize] sync real; cobegin { producer(); consumer(); } proc producer() { var i = 0; for … { i = (i+1) % buffersize; buff$[i] = …; // blocks until empty, leaves full } } proc consumer() { var i = 0; while … { i= (i+1) % buffersize; …buff$[i]…; // blocks...
View Full Document

This document was uploaded on 04/04/2014.

Ask a homework question - tutors are online