Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
MoreTypesofSynchronization
11/29/16
Today’sAgenda
• Classicthreadpatterns• Otherparallelprogrammingpatterns
• Moresynchronizationprimitives:• RWlocks• Conditionvariables• Semaphores
• Messagepassing• Exercise
CommonThreadPatterns
• Threadpool(a.k.a.workqueue)
• Producer/Consumer(a.k.a.Boundedbuffer)
• Threadperclientconnection
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone:
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone: Farmoutworktothreadswhenthey’reidle.
ThreadPool /WorkQueue
• Commonwayofstructuringthreadedapps:
ThreadPool
Queueofworktobedone:
Asthreadsfinishworkattheirownrate,theygrabthenextiteminqueue.
Commonfor“embarrassinglyparallel”algorithms.
Worksacrossthenetworktoo!
TheProducer/ConsumerProblem
• Producerproducesdata,placesitinsharedbuffer• Consumerconsumesdata,removesfrombuffer
Producer(s) Consumer(s)3 5 4 92
in
outbuf
Allkindsofreal-worldexamples:printqueue:printerisconsumerCPUqueueofreadyprocesses/threadstorunonCPU
ThreadPerClient
• Considerawebserver:• Clientconnects• Clientasksforapage:
• http://web.cs.swarthmore.edu/~bryce/cs31/f16• Serverlooksthroughfilesystemtofindpath(I/O)• Serversendsbackhtmlforclientbrowser(I/O)
• WebserverdoesthisforMANYclientsatonce
ThreadPerClient• Server“main”thread:• Waitfornewconnections• Uponreceivingone,spawnnewclientthread• Continuewaitingfornewconnections,repeat…
• Clientthreads:• Readclientrequest,findfilesinfilesystem• Sendfilesbacktoclient• Niceproperty: Eachclientisindependent• Niceproperty: WhenathreaddoesI/O,itgetsblockedforawhile.OScanscheduleanotherone.
OtherNoteworthyParallelPatterns
• Singleinstruction,multipledata(SIMD)• Applythesameoperationindependentlytomanypiecesofdata.
• Map-Reduce• Applythesameoperationindependentlytomanypiecesofdata,thencombinetheresults.
Singleinstruction,multipledata
• Applythesameoperationindependentlytomanypiecesofdata.• Thisissocommoningraphicsthatwehavespecializedhardwareforit(graphicscards).• Graphicshardwarecanbeusedfornon-graphicsSIMDtasks.• KnownasGPGPU:generalpurposeprogrammingongraphicsprocessingunits.• Example:matrixmultiplicationformachinelearning.
Map-Reduce
• Mapstep: performsomecomputationoneachpieceofdata.• Reducestep: combinetheresultsofthemappers.
Assigndatatomappers Assigndata
toreducersoutput
Example:findthemost-commonwordsinabook.
SynchronizationMechanisms
• Mutex locks• Guaranteemutuallyexclusive access.
• Barriers• Waitforotherthreadstocatchup.
• Read/writelocks• Conditionvariables• Semaphores
Read/Writelocks• Readers/WritersProblem:• Anobjectissharedamongseveralthreads.• Somethreadsonlyreadtheobject,othersmaywriteit.• Wecansafelyallowmultiplereaders.• Butwritersneedexclusiveaccess.
• pthread_rwlock_t:• pthread_rwlock_init: initializerwlock• pthread_rwlock_rdlock: lockforreading• pthread_rwlock_wrlock: lockforwriting
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.
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);
}
Semaphores:generalizedmutexes
• Semaphore:synchronizationvariable• Hasintegervalue• Listofwaitingthreads
• Workslikeagate• Ifsem >0,gateisopen• Valueequalsnumberofthreadsthatcanenter
• Else,gateisclosed• Possiblywithwaitingthreads
Asemaphorewithinitialvalue1isamutex
criticalsection
sem=1sem=2
sem=3
sem=0
MessagePassing
• OperatingsystemmechanismforIPC• send (destination, message_buffer)• receive (source, message_buffer)
• Datatransfer:intoandoutofkernelmessagebuffers• Synchronization:can’treceiveuntilmessageissent
send (to, buf) receive (from, buf)
kernel
P1 P2
Producer-ConsumerProblem• Asharedfix-sizedbuffer• Twotypesofthreads:
1. Producers:createanitem,addittobuffer.
2. Consumers:removeanitemfrombuffer,andconsumeit.
P0P1…Cm
...
BufferofsizeN
Threads
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
Producer/ConsumerSynchronization?
Producer:• Needstowaitifthereisnospacetoputanewiteminthebuffer(Scheduling)• Needstowaittohavemutuallyexclusiveaccesstosharedstateassociatedwiththebuffer(Atomic):• Sizeofthebuffer(num_items)• Nextspottoinsertinto(in)
Consumer:• Needstowaitifthereis
nothinginthebuffertoconsume(Scheduling)
• Needstowaittohavemutuallyexclusiveaccesstosharedstateassociatedwiththebuffer(Atomic):• Sizeofthebuffer(num_items)• Nextspottoremovefrom(out)
Exercise
Comeupwithapseudo-codesolutiontoproducerandconsumer.• Assumecircularbufferadd/removefunctionsprovided(don’tcheckoverwriteorgarbagereturnvalue)• WhatdoesProducerneedtodotoaddanitem?• WhatdoesConsumerneedtodotoremoveanitem?
QuestionstoAsk:• Wheredoyouneedtoaddsynchronization?
• Whatsortofsynchronization?• Doyouneedanyotherstateinformation?