Dr. Dobb's Journal - October 2007 - (Page 10) Alia Vox by James Reinders Rules for Parallel Programming for Multicore James offers eight key rules for multicore programming based on parallel programming experiences of his own and others. PROGRAMMING FOR MULTICORE processors poses new challenges. Here are eight rules for multicore programming to help you be successful: 1. Think parallel. Approach all problems looking for the parallelism. Understand where parallelism is, and organize your thinking to express it. Decide on the best parallel approach before other design or implementation decisions. Learn to “Think Parallel.” 2. Program using abstraction. Focus on writing code to express parallelism, but avoid writing code to manage threads or processor cores. Libraries, OpenMP, and Intel Threading Building Blocks are all examples of using abstractions. Do not use raw native threads (pthreads, Windows threads, Boost threads, and the like). Threads and MPI are the assembly languages for parallelism. They offer maximum flexibility, but require too much time to write, debug, and maintain. Your programming should be at a high-enough level that your code is about your problem, not about thread or core management. 3. Program in tasks (chores), not threads (cores). Leave the mapping of tasks to threads or processor cores as a distinctly separate operation in your program, preferably an abstraction you are using that handles thread/core management for you. Create an abundance of tasks in your program, or a task that can be spread across processor cores automatically (such as an OpenMP loop). By creating tasks, you are free to create as many as you can without worrying about oversubscription. 4. Design with the option to turn concurrency off. To make debugging simpler, create programs that can run without concurrency. This way, when debugging, you can run programs first with—then without—concurrency, and see if both runs fail or not. Debugging common issues is simpler when the program is not running concurrently because it is more familiar and better supported by today’s tools. Knowing that something fails only when run concurrently hints at the type of bug you are tracking down. If you ignore this rule and can’t force your program to run in only one thread, you’ll spend too much time debugging. Since you want to have the capability to run in a single thread specifically for debugging, it doesn’t need to be efficient. You just need to avoid creating parallel programs that require concurrency to work correctly, such as many producer-consumer models. MPI programs often violate this rule, which is part of the reason MPI programs can be problematic to implement and debug. Avoid using locks. Simply say “no” to locks. Locks slow programs, reduce their scalability, and are the source of bugs in parallel programs. Make implicit synchronization the solution for your program. When you still need explicit synchronization, use atomic operations. Use locks only as a last resort. Work hard to design the need for locks completely out of your program. Use tools and libraries designed to help with concurrency. Don’t “tough it out” with old tools. Be critical of tool support with regards to how it presents and interacts with parallelism. Most tools are not yet ready for parallelism. Look for threadsafe libraries—ideally ones that are designed to utilize parallelism themselves. Use scalable memory allocators. Threaded programs need to use scalable memory allocators. Period. There are a number of solutions and I’d guess that all of them are better than malloc(). Using scalable memory allocators speeds up applications by eliminating global bottlenecks, reusing memory within threads to better utilize caches, and partitioning properly to avoid cache line sharing. Design to scale through increased workloads. The amount of work your program needs to handle increases over time. Plan for that. Designed with scaling in mind, your program will handle more work as the number of processor cores increase. Every year, we ask our computers to do more and more. Your designs should favor using increases in parallelism to give you advantages in handling bigger workloads in the future. 5. James is part of Intel’s Software Development Products team, and author of Intel Threading Building Blocks: Outfitting C++ for Multi-core Processor Parallelism. He can be reached at james.r.reinders@intel.com. 6. 7. 8. I wrote these rules with explicit mention of threading everywhere. Only rule #7 is specifically related to threading. Threading is not the only way to get value out of multicore. Running multiple programs or multiple processes is often used, especially in server applications. These rules will work well for you to get the most out of multicore. Some will grow in importance over the next 10 years, as the number of processor cores rises and we see an increase in the diversity of the cores themselves. The coming of heterogeneous processors and NUMA, for instance, makes rule #3 more and more important. You should understand all eight and take all eight to heart. I look forward to any comments you may have about these rules or parallelism in general. DDJ 10 Dr. Dobb’s Journal l www.ddj.com l October 2007 http://www.ddj.com
Table of Contents Feed for the Digital Edition of Dr. Dobb's Journal - October 2007 Cover Contents Hmmmm Alia Vox Developer Diaries Developer’s Notebook AI: It’s OK Again! Conversations Visual Cryptography and Bit-Plane Complexity Segmentation Inside the Windows Vista Disk Encryption Algorithm Memory-Aware Components Software and the Core Description Process Logging In C++ Effective Concurrency The Agile Edge Swaine’s Flames Dr. Dobb's Journal - October 2007 Dr. Dobb's Journal - October 2007 - Cover (Page Cover1) Dr. Dobb's Journal - October 2007 - Cover (Page Cover2) Dr. Dobb's Journal - October 2007 - Cover (Page 1) Dr. Dobb's Journal - October 2007 - Cover (Page 2) Dr. Dobb's Journal - October 2007 - Cover (Page 3) Dr. Dobb's Journal - October 2007 - Contents (Page 4) Dr. Dobb's Journal - October 2007 - Contents (Page 5) Dr. Dobb's Journal - October 2007 - Hmmmm (Page 6) Dr. Dobb's Journal - October 2007 - Hmmmm (Page 7) Dr. Dobb's Journal - October 2007 - Hmmmm (Page 8) Dr. Dobb's Journal - October 2007 - Hmmmm (Page 9) Dr. Dobb's Journal - October 2007 - Alia Vox (Page 10) Dr. Dobb's Journal - October 2007 - Alia Vox (Page 11) Dr. Dobb's Journal - October 2007 - Developer Diaries (Page 12) Dr. Dobb's Journal - October 2007 - Developer Diaries (Page 13) Dr. Dobb's Journal - October 2007 - Developer’s Notebook (Page 14) Dr. Dobb's Journal - October 2007 - Developer’s Notebook (Page 15) Dr. Dobb's Journal - October 2007 - AI: It’s OK Again! (Page 16) Dr. Dobb's Journal - October 2007 - AI: It’s OK Again! (Page 17) Dr. Dobb's Journal - October 2007 - AI: It’s OK Again! (Page 18) Dr. Dobb's Journal - October 2007 - AI: It’s OK Again! (Page 19) Dr. Dobb's Journal - October 2007 - Conversations (Page 20) Dr. Dobb's Journal - October 2007 - Conversations (Page 21) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 22) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 23) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 24) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 25) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 26) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 27) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 28) Dr. Dobb's Journal - October 2007 - Visual Cryptography and Bit-Plane Complexity Segmentation (Page 29) Dr. Dobb's Journal - October 2007 - Inside the Windows Vista Disk Encryption Algorithm (Page 30) Dr. Dobb's Journal - October 2007 - Inside the Windows Vista Disk Encryption Algorithm (Page 31) Dr. Dobb's Journal - October 2007 - Inside the Windows Vista Disk Encryption Algorithm (Page 32) Dr. Dobb's Journal - October 2007 - Inside the Windows Vista Disk Encryption Algorithm (Page 33) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 34) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 35) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 36) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 37) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 38) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 39) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 40) Dr. Dobb's Journal - October 2007 - Memory-Aware Components (Page 41) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 42) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 43) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 44) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 45) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 46) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 47) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 48) Dr. Dobb's Journal - October 2007 - Software and the Core Description Process (Page 49) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 50) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 51) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 52) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 53) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 54) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 55) Dr. Dobb's Journal - October 2007 - Logging In C++ (Page 56) Dr. Dobb's Journal - October 2007 - Effective Concurrency (Page 57) Dr. Dobb's Journal - October 2007 - Effective Concurrency (Page 58) Dr. Dobb's Journal - October 2007 - Effective Concurrency (Page 59) Dr. Dobb's Journal - October 2007 - The Agile Edge (Page 60) Dr. Dobb's Journal - October 2007 - The Agile Edge (Page 61) Dr. Dobb's Journal - October 2007 - The Agile Edge (Page 62) Dr. Dobb's Journal - October 2007 - The Agile Edge (Page 63) Dr. Dobb's Journal - October 2007 - Swaine’s Flames (Page 64) Dr. Dobb's Journal - October 2007 - Swaine’s Flames (Page Cover3) Dr. Dobb's Journal - October 2007 - Swaine’s Flames (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.