10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

F.5. CHAPTER 5: PARTITIONING AND SYNCHRONIZATION DESIGN 291P1cited. <strong>Do</strong> not forget to compare to a sequential implementation!P5P2Quick Quiz 5.5:Move all the elements to the queue that becameempty? In what possible universe is this braindeadsolution in any way optimal???P4P3Answer:<strong>It</strong> is optimal in the case where data flow switchesdirection only rarely. <strong>It</strong> would of course be an extremelypoor choice if the double-ended queue wasbeing emptied from both ends concurrently. Thisof course raises the question as to what possibleuniverse emptying from both ends concurrentlywould be a reasonable thing to do...FigureF.3: DiningPhilosophersProblem, FullyPartitionedData flows up and down this stack. “Horizontal parallelism”processes packets from different networkconnections in parallel, while “vertical parallelism”handles different protocol-processing steps for agiven packet in parallel.“Vertical parallelism” is also called “pipelining”.Quick Quiz 5.6:Why can’t the compound parallel double-endedqueue implementation be symmetric?Answer:The need to avoid deadlock by imposing a lockhierarchy forces the asymmetry, just as it does in thefork-numbering solution to the Dining PhilosophersProblem.Quick Quiz 5.3:In this compound double-ended queue implementation,what should be done if the queue has becomenon-empty while releasing and reacquiring the lock?Answer:In this case, simply dequeue an item from thenow-nonempty queue, release both locks, andreturn.Quick Quiz 5.4:<strong>Is</strong> the hashed double-ended queue a good solution?Why or why not?Answer:The best way to answer this is to run lockhdeq.con a number of different multiprocessor systems,and you are encouraged to do so in the strongestpossible terms. One reason for concern is that eachoperation on this implementation must acquire notone but two locks.The first well-designed performance study will beQuick Quiz 5.7:Why is it necessary to retry the right-dequeueoperation on line 29 of Figure 5.11?Answer:This retry is necessary because some other threadmight have enqueued an element between the timethat this thread dropped the lock and the time thatit reacquired the lock.Quick Quiz 5.8:Surely the left-hand lock must sometimes beavailable!!! <strong>So</strong> why is it necessary that line 26 ofFigure 5.11 unconditionally release the right-handlock?Answer:<strong>It</strong> would be possible to use spin_trylock() toattempt to acquire the left-hand lock when it wasavailable. However, the failure case would still needto drop the right-hand lock and then re-acquirethe two locks in order. Making this transformation(and determining whether or not it is worthwhile)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!