www.it-ebooks.info OpenCL Parallel Programming Development Cookbook Accelerate your applications and understand high-performance computing with over 50 OpenCL recipes Raymond Tay BIRMINGHAM - MUMBAI www.it-ebooks.info OpenCL Parallel Programming Development Cookbook Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: August 2013 Production Reference: 1210813 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84969-452-0 www.packtpub.com Cover Image by Suresh Mogre ([email protected]) www.it-ebooks.info Credits Author Project Coordinator Raymond Tay Shiksha Chaturvedi Reviewers Proofreader Nitesh Bhatia Faye Coulman Darryl Gove Lesley Harrison Seyed Hadi Hosseini Paul Hindle Kyle Lutz Viraj Paropkari Indexer Tejal R. Soni Acquisition Editors Saleem Ahmed Graphics Sheetal Aute Erol Staveley Ronak Druv Valentina D'silva Lead Technical Editor Ankita Shashi Disha Haria Abhinash Sahu Technical Editors Veena Pagare Production Coordinator Krishnaveni Nair Melwyn D'sa Ruchita Bhansali Shali Sashidharan Cover Work Melwyn D'sa www.it-ebooks.info About the Author Raymond Tay has been a software developer for the past decade and his favorite programming languages include Scala, Haskell, C, and C++. He started playing with GPGPU technology since 2008, first with the CUDA toolkit by NVIDIA and OpenCL toolkit by AMD, and then Intel. In 2009, he decided to submit a GPGPU project on which he was working to the editorial committee working on the "GPU Computing Gems" to be published by Morgan Kauffmann. And though his work didn't make it to the final published work, he was very happy to have been short-listed for candidacy. Since then, he's worked on projects that use GPGPU technology and techniques in CUDA and OpenCL. He's also passionate about functional programming paradigms and their applications in cloud computing which has led him investigating on various paths to accelerate applications in the cloud through the use of GPGPU technology and the functional programming paradigm. He is a strong believer of continuous learning and hopes to be able to continue to do so for as long as he possibly can. This book could not have been possible without the support of foremost, my wife and my family, as I spent numerous weekends and evenings away from them so that I could get this book done and I would make it up to them soon. Packt Publishing for giving me the opportunity to be able to work on this project and I've received much help from the editorial team and lastly to the reviewing team, and I would also like to thank Darryl Gove – The senior principal software engineer at Oracle and Oleg Strikov – the CPU Architect at NVIDIA, who had rendered much help for getting this stuff right with their sublime and gentle intellect, and lastly to my manager, Sau Sheong, who inspired me to start this. Thanks guys. www.it-ebooks.info About the Reviewers Nitesh Bhatia is a tech geek with a background in information and communication technology (ICT) with an emphasis on computing and design research. He worked with Infosys Design as a user experience designer, and is currently a doctoral scholar at the Indian Institute of Science, Bangalore. His research interests include visual computing, digital human modeling, and applied ergonomics. He delights in exploring different programming languages, computing platforms, embedded systems and so on. He is a founder of several social media startups. In his leisure time, he is an avid photographer and an art enthusiast, maintaining a compendium of his creative works through his blog Dangling-Thoughts (http://www. dangling-thoughts.com). Darryl Gove is a senior principal software engineer in the Oracle Solaris Studio team, working on optimizing applications and benchmarks for current and future processors. He is also the author of the books, Multicore Application Programming, Solaris Application Programming, and The Developer's Edge. He writes his blog at http://www.darrylgove.com. Seyed Hadi Hosseini is a software developer and network specialist, who started his career at the age of 16 by earning certifications such as MCSE, CCNA, and Security+. He decided to pursue his career in Open Source Technology, and for this Perl programming was the starting point. He concentrated on web technologies and software development for almost 10 years. He is also an instructor of open source courses. Currently, Hadi is certified by the Linux Professional Institute, Novell, and CompTIA as a Linux specialist (LPI, LINUX+, NCLA and DCTS). High Performance Computing is one of his main research areas. His first published scientific paper was awarded as the best article in the fourth Iranian Bioinformatics Conference held in 2012. In this article, he developed a super-fast processing algorithm for SSR in Genome and proteome datasets, by using OpenCL as the GPGPU programming framework in C language, and benefiting from the massive computing capability of GPUs. Special thanks to my family and grandma for their invaluable support. I would also like to express my sincere appreciation to my wife, without her support and patience, this work would not have been done easily. www.it-ebooks.info Kyle Lutz is a software engineer and is a part of the Scientific Computing team at Kitware, Inc, New York. He holds a bachelor's degree in Biological Sciences from the University of California at Santa Barbara. He has several years of experience writing scientific simulation, analysis, and visualization software in C++ and OpenCL. He is also the lead developer of the Boost.Compute library – a C++ GPU/parallel-computing library based on OpenCL. Viraj Paropkari has done his graduation in computer science from University of Pune, India, in 2004, and MS in computer science from Georgia Institute of Technology, USA, in 2008. He is currently a senior software engineer at Advanced Micro Devices (AMD), working on performance optimization of applications on CPUs, GPUs using OpenCL. He also works on exploring new challenges in big data and High Performance Computing (HPC) applications running on large scale distributed systems. Previously, he was systems engineer at National Energy Research Scientific Computing Center (NERSC) for two years, where he worked on one of the world's largest supercomputers running and optimizing scientific applications. Before that, he was a visiting scholar in Parallel Programming Lab (PPL) at Computer Science Department of University of Illinois, Urbana-Champaign, and also a visiting research scholar at Oak Ridge National Laboratory, one of the premier research labs in U.S.A. He also worked on developing software for mission critical flight simulators at Indian Institute of technology, Bombay, India, and Tata institute of Fundamental Research (TIFR), India. He was the main contributor of the team that was awarded the HPC Innovation Excellence Award to speed up the CFD code and achieve the first ever simulation of a realistic fuel-spray related application. The ability to simulate this problem helps reduce design cycles to at least 66 percent and provides new insights into the physics that can provide sprays with enhanced properties. I'd like to thank my parents, who have been inspiration to me and also thank my beloved wife, Anuya, who encouraged me in spite of all the time it took me away from her. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Using OpenCL 7 Introduction 7 Querying OpenCL platforms 14 Querying OpenCL devices on your platform 18 Querying OpenCL device extensions 22 Querying OpenCL contexts 25 Querying an OpenCL program 29 Creating OpenCL kernels 35 Creating command queues and enqueuing OpenCL kernels 38 Chapter 2: Understanding OpenCL Data Transfer and Partitioning 43 Introduction 43 Creating OpenCL buffer objects 44 Retrieving information about OpenCL buffer objects 50 Creating OpenCL sub-buffer objects 54 Retrieving information about OpenCL sub-buffer objects 58 Understanding events and event synchronization 61 Copying data between memory objects 64 Using work items to partition data 71 Chapter 3: Understanding OpenCL Data Types 79 Introduction 79 Initializing the OpenCL scalar data types 80 Initializing the OpenCL vector data types 82 Using OpenCL scalar types 85 Understanding OpenCL vector types 88 Vector and scalar address spaces 100 Configuring your OpenCL projects to enable the double data type 103 www.it-ebooks.info
Description: