Multithreading for Visual Effects
Total Page:16
File Type:pdf, Size:1020Kb
Multithreading for Visual Effects Multithreading for Visual Effects Martin Watt • Erwin Coumans • George ElKoura • Ronald Henderson Manuel Kraemer • Jeff Lait • James Reinders Boca Raton London New York CRC Press is an imprint of the Taylor & Francis Group, an informa business CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2015 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Version Date: 20140618 International Standard Book Number-13: 978-1-4822-4357-4 (eBook - PDF) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the valid- ity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or uti- lized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopy- ing, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com Multithreading applications is hard, but for today's performance|critical codes, an absolute necessity. This book shows how the latest parallel-programming tech- nology can simplify the daunting challenge of producing fast and reliable software for multicore processors. Although the instructive case studies are drawn from visual effects applications, the authors cover the gamut of issues that developers face when parallelizing legacy applications from any domain. Charles Leiserson, MIT Computer Science and Artificial Intelligence Laboratory Multithreading graphics algorithms is a new and exciting area of research. It is crucial to computer graphics. This book will prove invaluable to researchers and practitioners alike. Indeed, it will have a strong impact on movie visual effects and games. Jos Stam, Senior Principal Research Scientist, Autodesk, Inc. Visual effects programming is undergoing a renaissance as high-end video game effects technology approaches the state-of-the-art defined by blockbuster Holly- wood movies, empowered by the capabilities of multi-Teraflop GPU hardware. A wealth of graphics algorithms are now graduating into the realm of real-time rendering, yet today's programmers face a formidable challenge in structuring these algorithms to take full advantage of today's multicore CPU architectures and deliver on their potential. This book, the collaborative result of many industry luminaries, wonderfully bridges the gap between the theory of multithreading and the practice of multi- threading in advanced graphical applications. Join them on this journey to bring real-time visual effects technology to the next level! Tim Sweeney, CEO and Founder of Epic Games Contents Preface xiii Acknowledgments xv Authors xvii List of Figures xix List of Tables xxiii 1 Introduction and Overview 1 James Reinders 1.1 Introduction . .1 1.2 Overview of Case Studies . .2 1.3 Motivation . .3 1.3.1 Quickly Increasing Clock Speeds Ended by 2005 . .3 1.3.2 The Move to Multicore . .4 1.3.3 SIMD Is Parallelism Too . .6 1.3.4 Highly Threaded Hardware . .7 1.4 Program in Tasks, Not Threads . .7 1.5 Value of Abstraction . .8 1.6 Scaling and Vectorization . .9 1.7 Advancing Programming Languages for Parallel Programming . 10 1.7.1 Abstraction . 10 1.7.2 Parallel Programming Needs . 10 1.7.3 Relaxed Sequential Semantics . 11 1.8 Parallel Programming in C and C++ . 11 1.8.1 Brief Survey of Key Parallelism Options . 12 1.8.1.1 TBB . 12 1.8.1.2 Cilk Plus . 12 1.8.1.3 OpenMP . 12 1.8.1.4 OpenCL . 13 1.8.1.5 GPU Specific Models . 13 1.8.2 More on TBB: Intel Threading Building Blocks . 13 1.8.2.1 Parallel for: parallel for(...) . 14 1.8.2.2 Parallel Reductions: parallel reduce . 15 1.8.2.3 Parallel Invocation of Functions: parallel invoke . 16 1.8.2.4 Learning More about TBB . 16 1.9 Data Movement and Layout . 16 1.10 Summary . 17 1.11 Additional Reading . 18 vii viii Contents 2 Houdini: Multithreading Existing Software 19 Jeff Lait 2.1 What Is Houdini? . 19 2.2 Rewrite or Refactor . 21 2.2.1 Cleaning Statics . 22 2.2.2 Threading the Simple Cases . 27 2.3 Patterns . 30 2.3.1 Always Be Reentrant . 30 2.3.2 Never Lock . 31 2.3.3 Atomics Are Slow . 31 2.3.4 Never Blindly Thread . 32 2.3.5 Command Line Control . 33 2.3.6 Constant Memory versus Number of Cores . 33 2.3.7 Memory Allocation . 34 2.4 Copy on Write . 34 2.4.1 Const Correctness . 35 2.4.2 Reader/Writer Locks . 35 2.4.3 Ownership Is Important . 36 2.4.4 Sole Ownership Is a Writer Lock . 37 2.4.5 Failure Modes of This System . 38 2.5 Dependencies . 40 2.5.1 Task Locks . 41 2.5.2 Mantra . 43 2.6 OpenCL . 44 3 The Presto Execution System: Designing for Multithreading 47 George ElKoura 3.1 Introduction . 48 3.1.1 A Note about Interactivity . 48 3.2 Presto . 49 3.2.1 Presto Objects . 50 3.2.2 Rigging in Presto . 51 3.2.3 Animation in Presto . 52 3.3 Presto's Execution System . 52 3.3.1 Phases of Execution . 53 3.3.1.1 Compilation . 53 3.3.1.2 Scheduling . 54 3.3.1.3 Evaluation . 54 3.3.2 Engine Architecture . 54 3.3.2.1 Network . 55 3.3.2.2 Schedulers . 55 3.3.2.3 Data Managers . 56 3.3.2.4 Executors . 56 3.3.2.5 Engine Architecture and Multithreading . 56 3.4 User Extensions . 57 3.4.1 Dependencies Declared a Priori .................... 57 3.4.2 Client Callbacks Are Static Functions . 57 3.4.3 Presto Singletons Are Protected . 58 3.4.4 Iterators . 58 3.4.5 And Then There's Python . 58 3.4.5.1 Global Interpreter Lock . 58 Contents ix 3.4.5.2 Performance . 59 3.5 Memory Access Patterns . 59 3.6 Flexibility to Experiment . 60 3.6.1 Modular Design . 60 3.6.2 Targeting Other Platforms . 60 3.7 Multithreading Strategies . 61 3.7.1 Per-Node Multithreading . 61 3.7.2 Per-Branch Multithreading . 62 3.7.3 Per-Model Multithreading . 62 3.7.4 Per-Frame Multithreading . 64 3.8 Background Execution . 64 3.8.1 User Interaction . 65 3.8.2 Frame Scheduling . 65 3.8.3 Interruption . 66 3.8.4 Constant Data . 67 3.8.5 Problematic Data Structures . 67 3.9 Other Multithreading Strategies . 69 3.9.1 Strip Mining . 69 3.9.2 Predictive Computations . 70 3.10 Debugging and Profiling Tools . 70 3.11 Summary . 71 4 LibEE: Parallel Evaluation of Character Rigs 73 Martin Watt 4.1 Introduction . ..