Top Banner
More Types of Synchronization 11/29/16
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:

Jul 14, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

Exercise

Comeupwithapseudo-codesolutiontoproducerandconsumer.• Assumecircularbufferadd/removefunctionsprovided(don’tcheckoverwriteorgarbagereturnvalue)• WhatdoesProducerneedtodotoaddanitem?• WhatdoesConsumerneedtodotoremoveanitem?

QuestionstoAsk:• Wheredoyouneedtoaddsynchronization?

• Whatsortofsynchronization?• Doyouneedanyotherstateinformation?