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.

Chapter 13Ease of Use“Creating a perfect API is like committing theperfect crime. There are at least fifty things thatcan go wrong, and if you are a genius, you might beable to anticipate twenty-five of them.”13.1 Rusty Scale for API Design1. <strong>It</strong> is impossible to get wrong. dwim()2. The compiler or linker won’t let you get itwrong.3. The compiler or linker will warn you if you getit wrong.4. The simplest use is the correct one.5. The name tells you how to use it.6. <strong>Do</strong> it right or it will always break at runtime.7. Follow common convention and you will get itright. malloc()8. Read the documentation and you will get itright.9. Read the implementation and you will get itright.10. Read the right mailing-list archive and you willget it right.11. Read the right mailing-list archive and you willget it wrong.12. Read the implementation and you will get itwrong. The non-CONFIG_PREEMPT implementationof rcu_read_lock().13. Read the documentation and you will get itwrong. DEC Alpha wmb instruction.14. Follow common convention and you will get itwrong. printf() (failing to check for error return).15. <strong>Do</strong> it right and it will break at runtime.16. The name tells you how not to use it.17. The obvious use is wrong. smp_mb().18. The compiler or linker will warn you if you getit right.19. The compiler or linker won’t let you get it right.20. <strong>It</strong> is impossible to get right. gets().13.2 Shaving the MandelbrotSetThe set of useful programs resembles the Mandelbrotset (shown in Figure 13.1) in that it does nothave a clear-cut smooth boundary — if it did, thehalting problem would be solvable. But we needAP<strong>Is</strong> that real people can use, not ones that requireaPh.D.dissertationbecompletedforeachandeverypotential use. <strong>So</strong>, we “shave the Mandelbrot set”, 1restricting the use of the API to an easily describedsubset of the full set of potential uses.Such shaving may seem counterproductive. Afterall, if an algorithm works, why shouldn’t it be used?Toseewhyatleastsomeshavingisabsolutelynecessary,consider a locking design that avoids deadlock,but in perhaps the worst possible way. This designusesacirculardoublylinkedlist,whichcontainsone element for each thread in the system along witha header element. When a new thread is spawned,theparentthreadmustinsertanewelementintothislist, which requires some sort of synchronization.One way to protect the list is to use a global lock.However, this might be a bottleneck if threads were1 Due to Josh Triplett.

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

Saved successfully!

Ooh no, something went wrong!