www.it-ebooks.info www.it-ebooks.info Parallel Programming with Microsoft ® Visual Studio ® 2010 Step by Step Donis Marshall www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, California 95472 Copyright © 2011 by Donis Marshall All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. ISBN: 978-0-7356-4060-3 1 2 3 4 5 6 7 8 9 QG 6 5 4 3 2 1 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/ IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are ctitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, O’Reilly Media, Inc., Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions and Developmental Editors: Russell Jones and Devon Musgrave Production Editor: Holly Bauer Editorial Production: Online Training Solutions, Inc. Technical Reviewer: Ashish Ghoda Copyeditor: Kathy Krause, Online Training Solutions, Inc. Proofreader: Jaime Odell, Online Training Solutions, Inc. Indexer: Fred Brown Cover Design: Twist Creative • Seattle Cover Composition: Karen Montgomery Illustrator: Jeanne Craver, Online Training Solutions, Inc. www.it-ebooks.info This book is dedicated to my mother, who is extremely proud that I am a published author. She even gives my books to friends at her church—even though none of them are programmers. But that does not matter. Thanks, Mom! www.it-ebooks.info www.it-ebooks.info v Contents at a Glance 1 Introduction to Parallel Programming 1 2 Task Parallelism 19 3 Data Parallelism 59 4 PLINQ 89 5 Concurrent Collections 117 6 Customization 147 7 Reports and Debugging 181 www.it-ebooks.info www.it-ebooks.info vii Table of Contents Foreword xi Introduction xiii 1 Introduction to Parallel Programming 1 Multicore Computing 2 Multiple Instruction Streams/Multiple Data Streams 3 Multithreading 4 Synchronization 5 Speedup 6 Amdahl’s Law 7 Gustafson’s Law 8 Software Patterns 9 The Finding Concurrency Pattern 11 The Algorithm Structure Pattern 14 The Supporting Structures Pattern 15 Summary 16 Quick Reference 17 2 Task Parallelism 19 Introduction to Parallel Tasks 19 Threads 21 The Task Class 22 Using Function Delegates 28 Unhandled Exceptions in Tasks 30 Sort Examples 36 Bubble Sort 36 Insertion Sort 37 Pivot Sort 38 Using the Barrier Class 38 Refactoring the Pivot Sort 42 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you To participate in a brief online survey, please visit: microsoftcom/learning/booksurvey www.it-ebooks.info viii Table of Contents Cancellation 43 Task Relationships 46 Continuation Tasks 46 Parent and Child Tasks 52 The Work-Stealing Queue 54 Summary 56 Quick Reference 57 3 Data Parallelism 59 Unrolling Sequential Loops into Parallel Tasks 60 Evaluating Performance Considerations 63 The Parallel For Loop 64 Interrupting a Loop 67 Handling Exceptions 72 Dealing with Dependencies 74 Reduction 74 Using the MapReduce Pattern 80 A Word Count Example 84 Summary 86 Quick Reference 87 4 PLINQ 89 Introduction to LINQ 90 PLINQ 94 PLINQ Operators and Methods 99 The ForAll Operator 99 ParallelExecutionMode 100 WithMergeOptions 101 AsSequential 102 AsOrdered 103 WithDegreeOfParallelism 104 Handling Exceptions 105 Cancellation 107 Reduction 108 Using MapReduce with PLINQ 112 Summary 115 Quick Reference 116 www.it-ebooks.info [...]... looking to decompose your application into parallel tasks that execute over separate processor cores, then Visual Studio 2010 and the TPL are the tools you need Parallel Programming with Microsoft Visual Studio 2010 Step by Step provides an organized walkthrough of using Visual Studio 2010 to create parallel applications It discusses the TPL and parallel programming concepts in considerable detail;... help maintain and debug parallel applications Both the www.it-ebooks.info 1 2 Parallel Programming with Microsoft Visual Studio 2010 Step by Step Parallel Tasks and Parallel Stacks windows help you interpret an application from the context of a parallel execution and tasks For performance tuning, the Visual Studio Profiler and Concurrency Visualizer work together to analyze a parallel application and... best use them Parallel Programming with Microsoft Visual Studio 2010 Step by Step ensures that programmers worldwide can effectively add parallel programming to their design portfolios Tracy Monteith www.it-ebooks.info www.it-ebooks.info Introduction Parallel programming truly redefines the programming model for multicore architecture, which has become commonplace For this reason, parallel programming. .. xv Finding Your Best Starting Point in This Book The different sections of Parallel Programming with Microsoft Visual Studio 2010 Step by Step cover a wide range of technologies and concepts associated with parallel programming in the NET Framework Depending on your needs and your current level of familiarity with parallel programming in the NET Framework 4, you might want to focus on specific areas... between linear, sequential programming to a design that leverages parallel processing requires new approaches In order to produce fast, secure, reliable, world-ready software, programmers need new tools to supplement their current approach To that end, Parallel Programming with Microsoft Visual Studio 2010 Step by Step was written Donis Marshall has put together his expertise with a narrative format that... you in maintaining and debugging parallel applications by using Visual Studio Beyond the explanatory content, most chapters include step- by -step examples and downloadable sample projects that you can explore for yourself Who Should Read This Book This book exists to help Microsoft Visual Basic and Microsoft Visual C# developers understand the core concepts of parallel programming and related technologies... 2010) or Michael Halvorson’s Microsoft Visual Basic 2010 Step by Step (Microsoft Press, 2010) With a heavy focus on concurrent programming concepts, this book also assumes that you have a basic understanding of threads and thread synchronization concepts To go beyond this book and expand your knowledge of threading, consider reading Jeffrey Richter’s CLR via C# (Microsoft Press, 2010) Organization of This... scope Parallel programming is one solution In this way, Moore’s Law will continue into the indefinite future Microsoft recognizes the vital role of parallel programming for the future That is the reason parallel programming was promoted from an extension to a core component of the common language runtime (CLR) New features have been added to the Microsoft NET Framework 4 and Microsoft Visual Studio 2010. .. related to parallel programming ■ Chapter 1, “Introduction to Parallel Programming, ” introduces the fundamental concepts of parallel programming ■ Chapter 2, “Task Parallelism,” focuses on creating parallel iterations and refactoring sequential loops into parallel tasks ■ Chapter 3, “Data Parallelism,” focuses on creating parallel tasks from separate operations ■ Chapter 4, “PLINQ,” is an overview of parallel. .. object-oriented programming concepts Although the TPL is available to most, if not all, NET Framework 4 language platforms, this book includes examples only in C# However, the examples should be portable to Visual Basic NET with minimal changes If you have not yet picked up either of these languages, consider reading John Sharp’s Microsoft Visual C# 2010 Step by Step (Microsoft Press, 2010) or Michael . into parallel tasks that execute over separate processor cores, then Visual Studio 2010 and the TPL are the tools you need. Parallel Programming with Microsoft Visual Studio 2010 Step by Step. www.it-ebooks.info www.it-ebooks.info Parallel Programming with Microsoft ® Visual Studio ® 2010 Step by Step Donis Marshall www.it-ebooks.info Published with the authorization of Microsoft Corporation by: O’Reilly. Book The different sections of Parallel Programming with Microsoft Visual Studio 2010 Step by Step cover a wide range of technologies and concepts associated with parallel programming in the .NET