Jung W. Suh
Copyright
Acquiring Editor: Todd Green
Editorial Project Manager: Lindsay Lawrence
Project Manager: Mohana Natarajan
Designer: Matthew Limbert
Morgan Kaufmann is an imprint of Elsevier
225 Wyman Street, Waltham, MA 02451, USA
First edition 2014
Copyright 2014 Elsevier Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means electronic, mechanical, photocopying, recording or otherwise without the prior written permission of the publisher.
Permissions may be sought directly from Elseviers Science & Technology Rights Department in Oxford, UK: phone (+44) (0) 1865 843830; fax (+44) (0) 1865 853333; email: , and selecting Obtaining permission to use Elsevier material.
Notice
No responsibility is assumed by the publisher for any injury and/or damage to persons or property as a matter of products liability, negligence or otherwise, or from any use or operation of any methods, products, instructions or ideas contained in the materialherein. Because of rapid advances in the medical sciences, in particular, independentverification of diagnoses and drug dosages should be made.
British Library Cataloguing-in-Publication Data
A catalogue record for this book is available from the British Library
Library of Congress Cataloging-in-Publication Data
Application Submitted
ISBN: 978-0-12-408080-5
For information on all MK publications visit our web site at www.mkp.com
Printed and bound in USA
14 15 16 17 18 10 9 8 7 6 5 4 3 2 1
Preface
MATLAB is a widely used simulation tool for rapid prototyping and algorithm development. Many laboratories and research institutions face growing demands to run their MATLAB codes faster for computationally heavy projects after simple simulations. Since MATLAB uses a vector/matrix representation of data, which is suitable for parallel processing, it can benefit a lot from GPU acceleration.
Target Readers and Contents
This book is aimed primarily at the graduate students and researchers in the field of engineering, science, and technology who need huge data processing without losing the many benefits of MATLAB. However, MATLAB users come from various backgrounds and do not necessarily have much programming experience. For those whose backgrounds are not from programming, GPU acceleration for MATLAB may distract their algorithm development and introduce unnecessary hassles, even when setting the environment. This book targets the readers who have some or a lot of experience on MATLAB coding but not enough depth in either C coding or the computer architecture for parallelization. So readers can focus more on their research and work by avoiding non-algorithmic hassles in using GPU and CUDA in MATLAB.
As a primer, the book will start with the basics, walking through the process of setting MATLAB for CUDA (in Windows and Mac OSX), creating c-mex and m -file profiling, then guide the users through the expert-level topics such as third-party CUDA libraries. It also provides many practical ways to modify users MATLAB codes to better utilize the immense computational power of graphics processors.
This book guides the reader to dramatically maximize the MATLAB speed using NVIDIAs Graphics Processing Unit (GPU). NVIDIAs Compute Unified Device Architecture (CUDA) is a parallel computing architecture originally designed for computer games but is getting a reputation in the general science and technology fields for its efficient massive computation power. From this book, the reader can take advantage of the parallel processing power of GPU and abundant CUDA scientific libraries for accelerating MATLAB code with no or less effort and time, and bring readers researches and works to a higher level.
Directions of this Book
GPU Utilization Using c-mex Versus Parallel Computing Toolbox
This book deals with Mathworkss Parallel Computing Toolbox in . For the purpose of general speeding up, GPU-utilization through c-mex proves a better approach and provides more flexibility in current situation.
Tutorial Approach Versus Case Study Approach
As the books title says, we take more of a tutorial approach. MATLAB users may come from many different backgrounds, and web resources are scattered over Mathworks, NVIDIA, and private blogs as fragmented information. The tutorial approach from setting the GPU environment to acquiring critical (but compressed) hardware knowledge for GPU would be beneficial to prospective readers over a wide spectrum. However, this book also has two chapters () that include case examples with working codes.
CUDA Versus OpenCL
When we prepared the proposal of this book, we also considered OpenCL as a topic, because the inclusion of OpenCL would attract a wider range of readers. However, while CUDA is more consistent and stable, because it is solely driven by NVIDIA, the current OpenCL has no unified development environment and is still unstable in some areas, because OpenCL is not governed by one company or institution. For this reason, installing, profiling, and debugging OpenCL are not yet standardized. As a primer, this may distract the focus of this book. More importantly, for some reason Mathworks is very conservative in its support of OpenCL, unlike CUDA. Therefore, we decided not to include OpenCL in this edition of our book. However, we will again consider whether to include OpenCL in future editions if increased needs come from market or Mathworks direction changes.
After reading this book, the reader, in no time, will experience an amazing performance boost in utilizing readers MATLAB codes and be better equipped in research to enjoy the useful open-source resources for CUDA. The features this book covers are available on Windows and Mac.
Accelerating MATLAB without GPU
This chapter deals with basic accelerating methods for MATLAB codes in an intrinsic way, which means simple code optimization without using GPU or C-MEX. This chapter covers vectorization for parallel processing, preallocation for efficient memory management, tips to increase your MATLAB codes, and step-by-step examples that show the code improvements.
Keywords
Vectorization; elementwise operation; vector/matrix operation; memory preallocation; sparse matrix form
1.1 Chapter Objectives
In this chapter, we deal with the basic accelerating methods for MATLAB codes in an intrinsic way a simple code optimization without using GPU or C-MEX. You will learn about the following:
The vectorization for parallel processing.
The preallocation for efficient memory management.
Other useful tips to increase your MATLAB codes.
Examples that show the code improvements step by step.