more types of synchronization - swarthmore collegebryce/cs31/f16/slides/... · 2016-12-06 · •...
TRANSCRIPT
![Page 1: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/1.jpg)
MoreTypesofSynchronization
11/29/16
![Page 2: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/2.jpg)
Today’sAgenda
• Classicthreadpatterns• Otherparallelprogrammingpatterns
• Moresynchronizationprimitives:• RWlocks• Conditionvariables• Semaphores
• Messagepassing• Exercise
![Page 3: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/3.jpg)
CommonThreadPatterns
• Threadpool(a.k.a.workqueue)
• Producer/Consumer(a.k.a.Boundedbuffer)
• Threadperclientconnection
![Page 4: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/4.jpg)
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
![Page 5: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/5.jpg)
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone:
![Page 6: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/6.jpg)
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone: Farmoutworktothreadswhenthey’reidle.
![Page 7: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/7.jpg)
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone:
Asthreadsfinishworkattheirownrate,theygrabthenextiteminqueue.
Commonfor“embarrassinglyparallel”algorithms.
Worksacrossthenetworktoo!
![Page 8: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/8.jpg)
TheProducer/ConsumerProblem
• Producerproducesdata,placesitinsharedbuffer• Consumerconsumesdata,removesfrombuffer
Producer(s) Consumer(s)3 5 4 92
in
outbuf
Allkindsofreal-worldexamples:printqueue:printerisconsumerCPUqueueofreadyprocesses/threadstorunonCPU
![Page 9: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/9.jpg)
ThreadPerClient
• Considerawebserver:• Clientconnects• Clientasksforapage:
• http://web.cs.swarthmore.edu/~bryce/cs31/f16• Serverlooksthroughfilesystemtofindpath(I/O)• Serversendsbackhtmlforclientbrowser(I/O)
• WebserverdoesthisforMANYclientsatonce
![Page 10: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/10.jpg)
ThreadPerClient• Server“main”thread:• Waitfornewconnections• Uponreceivingone,spawnnewclientthread• Continuewaitingfornewconnections,repeat…
• Clientthreads:• Readclientrequest,findfilesinfilesystem• Sendfilesbacktoclient• Niceproperty: Eachclientisindependent• Niceproperty: WhenathreaddoesI/O,itgetsblockedforawhile.OScanscheduleanotherone.
![Page 11: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/11.jpg)
OtherNoteworthyParallelPatterns
• Singleinstruction,multipledata(SIMD)• Applythesameoperationindependentlytomanypiecesofdata.
• Map-Reduce• Applythesameoperationindependentlytomanypiecesofdata,thencombinetheresults.
![Page 12: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/12.jpg)
Singleinstruction,multipledata
• Applythesameoperationindependentlytomanypiecesofdata.• Thisissocommoningraphicsthatwehavespecializedhardwareforit(graphicscards).• Graphicshardwarecanbeusedfornon-graphicsSIMDtasks.• KnownasGPGPU:generalpurposeprogrammingongraphicsprocessingunits.• Example:matrixmultiplicationformachinelearning.
![Page 13: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/13.jpg)
Map-Reduce
• Mapstep: performsomecomputationoneachpieceofdata.• Reducestep: combinetheresultsofthemappers.
Assigndatatomappers Assigndata
toreducersoutput
Example:findthemost-commonwordsinabook.
![Page 14: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/14.jpg)
SynchronizationMechanisms
• Mutex locks• Guaranteemutuallyexclusive access.
• Barriers• Waitforotherthreadstocatchup.
• Read/writelocks• Conditionvariables• Semaphores
![Page 15: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/15.jpg)
Read/Writelocks• Readers/WritersProblem:• Anobjectissharedamongseveralthreads.• Somethreadsonlyreadtheobject,othersmaywriteit.• Wecansafelyallowmultiplereaders.• Butwritersneedexclusiveaccess.
• pthread_rwlock_t:• pthread_rwlock_init: initializerwlock• pthread_rwlock_rdlock: lockforreading• pthread_rwlock_wrlock: lockforwriting
![Page 16: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/16.jpg)
ConditionVariablesWaitforaconditiontobetrue.
• Inthepthreads library:• pthread_cond_init: InitializeCV• pthread_cond_wait: WaitonCV• pthread_cond_signal: Wakeuponewaiter• pthread_cond_broadcast: Wakeupallwaiters
• Conditionvariableisassociatedwithamutex:1. Lockmutex,realizeconditionsaren’treadyyet.2. Temporarilygiveupmutex untilCVsignaled.3. Reacquiremutex andwakeupwhenready.
![Page 17: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/17.jpg)
UsingConditionVariables
while (TRUE) {//independent code
lock(m);while (conditions bad)
wait(cond, m);
//proceed knowing that conditions are now good
signal (other_cond); // Let other thread knowunlock(m);
}
![Page 18: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/18.jpg)
Semaphores:generalizedmutexes
• Semaphore:synchronizationvariable• Hasintegervalue• Listofwaitingthreads
• Workslikeagate• Ifsem >0,gateisopen• Valueequalsnumberofthreadsthatcanenter
• Else,gateisclosed• Possiblywithwaitingthreads
Asemaphorewithinitialvalue1isamutex
criticalsection
sem=1sem=2
sem=3
sem=0
![Page 19: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/19.jpg)
MessagePassing
• OperatingsystemmechanismforIPC• send (destination, message_buffer)• receive (source, message_buffer)
• Datatransfer:intoandoutofkernelmessagebuffers• Synchronization:can’treceiveuntilmessageissent
send (to, buf) receive (from, buf)
kernel
P1 P2
![Page 20: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/20.jpg)
Producer-ConsumerProblem• Asharedfix-sizedbuffer• Twotypesofthreads:
1. Producers:createanitem,addittobuffer.
2. Consumers:removeanitemfrombuffer,andconsumeit.
P0P1…Cm
...
BufferofsizeN
Threads
![Page 21: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/21.jpg)
Producer/ConsumerSynchronization?
Producer: Consumer:
CircularQueueBuffer:addtooneend(in),removefromother(out)
int buf[N];int in, out;int num_items;
9 11 3 7buf:
AssumeProducers&Consumersforeverproduce&consumeQ:WhereisSynchronizationNeededinProducer&Consumer?
add/remove
out: in:num_items:1 5 4
![Page 22: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/22.jpg)
Producer/ConsumerSynchronization?
Producer:• Needstowaitifthereisnospacetoputanewiteminthebuffer(Scheduling)• Needstowaittohavemutuallyexclusiveaccesstosharedstateassociatedwiththebuffer(Atomic):• Sizeofthebuffer(num_items)• Nextspottoinsertinto(in)
Consumer:• Needstowaitifthereis
nothinginthebuffertoconsume(Scheduling)
• Needstowaittohavemutuallyexclusiveaccesstosharedstateassociatedwiththebuffer(Atomic):• Sizeofthebuffer(num_items)• Nextspottoremovefrom(out)
![Page 23: More Types of Synchronization - Swarthmore Collegebryce/cs31/f16/slides/... · 2016-12-06 · • pthread_cond_init: Initialize CV • pthread_cond_wait: Wait on CV • pthread_cond_signal:](https://reader034.vdocuments.site/reader034/viewer/2022050107/5f451de852efcd191d6e6e5e/html5/thumbnails/23.jpg)
Exercise
Comeupwithapseudo-codesolutiontoproducerandconsumer.• Assumecircularbufferadd/removefunctionsprovided(don’tcheckoverwriteorgarbagereturnvalue)• WhatdoesProducerneedtodotoaddanitem?• WhatdoesConsumerneedtodotoremoveanitem?
QuestionstoAsk:• Wheredoyouneedtoaddsynchronization?
• Whatsortofsynchronization?• Doyouneedanyotherstateinformation?