Shared Memory Application Programming
Concepts and strategies in multicore application programming
First Edition
Victor Alessandrini
Copyright
Morgan Kaufmann is an imprint of Elsevier
225 Wyman Street, Waltham, MA 02451, USA
Copyright 2016 Elsevier Inc. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from the publisher. Details on how to seek permission, further information about the Publishers permissions policies and our arrangements with organizations such as the Copyright Clearance Center and the Copyright Licensing Agency, can be found at our website: www.elsevier.com/permissions.
This book and the individual contributions contained in it are protected under copyright by the Publisher (other than as may be noted herein).
Notices
Knowledge and best practice in this field are constantly changing. As new research and experience broaden our understanding, changes in research methods, professional practices, or medical treatment may become necessary.
Practitioners and researchers must always rely on their own experience and knowledge in evaluating and using any information, methods, compounds, or experiments described herein. In using such information or methods they should be mindful of their own safety and the safety of others, including parties for whom they have a professional responsibility.
To the fullest extent of the law, neither the Publisher nor the authors, contributors, or editors, assume any liability 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 material herein.
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
A catalog record for this book is available from the Library of Congress
For information on all MK publications visit our website at http://store.elsevier.com/
ISBN: 978-0-12-803761-4
Preface
This book proposes a pedagogical introduction to shared memory application programming. It grew out of several years of user training at IDRIS supercomputing center in France, and, more recently, at the PATC training program of the PRACE European infrastructure. It is designed to guide readers with a reasonable background in C-C++ programming, in becoming acquainted with current multicore programming environments, and in developing useful insights about threads. This book is therefore placed at an intermediate level; basic experience in multithreaded programming is of course welcomed, but not mandatory.
Multithreaded programming is today a core technology, at the basis of any software development project in any branch of applied computer science. There are, naturally, a number of excellent presentations of the subject. We tried to provide in this book a basic overview of the multithreading landscape, so that readers can start mastering threads, benefiting from the abundant references that push these subjects much further in several directions. It seems therefore appropriate to expose in some detail the pedagogical motivations and strategies adopted here.
Pedagogical Objectives
Software engineering practices have experienced a profound evolution in the last 25 years, since shared memory programming first showed up. From simple, monolithic applications designed to solve a specific problem, dealing with a simple parallel context, we moved toward software packages dealing with complex, multicomponent modules, sometimes running in distributed computational environments. Another major evolution in software engineering occurred in the mid-2000s when working processor frequenciesand the peak performance that comes with itceased to double every 18 months, because a power dissipation wall was hit. Today, the only way to increase performance is to increase the number of processors, and for this reason multithreading, which was initially a question of choice and opportunity, has become a mandatory software technology. Threads are definitely a part of the life of any software developer concerned with issues of efficient network connectivity and I/O, interoperability, computational performance and scalability, or efficient interactive access to an application.
In this diversified and rapidly evolving landscape, a rather broad view of the utilities and the strategic options proposed by the different multithreading libraries and environments seems to be an important asset for software developers. An overview of different libraries and programming environments operating on Unix-Linux or Windows platforms is proposed, underlining their often complementary capabilities, as well as the fact that there is sometimes benefit in taking advantage of their interoperability. The first chapters of this book develop an overview of the native multithreading libraries like Pthreads (for Unix-Linux systems) or Windows threads, as well as the recent C++11 threads standard, with the purpose of grasping the fundamental issues on thread management and synchronization discussed in the following chapters. A quick introduction to the most basic OpenMP thread management interfaces is also introduced at this stage. In the second part of the book, a detailed discussion of the two high-level, standard programming environmentsOpenMP and Intel Threading Building Blocks (TBB)is given.
OpenMPwhich played a fundamental role in disseminating shared memory programmingis today a broadly adopted and well-established standard. Nevertheless, we believe that a more general presentation of the basic concepts of shared memory programming, showing the way they are declined in the various programming environments, is useful for learning to think creatively about threads, not about programming languages. True enough, OpenMP can cope with practically any parallel context in an application. But the implementations of the basic multithreading concepts by different libraries often incorporate additional features that enable, in some specific cases, a better match to the application requirements. Some libraries propose specific extended utilities. A few cases will be given in which C++11 or TBB utilities are profitably used to boost application performance. This does not mean, however, that software developers need to abandon their well-rooted habits and best practices, because most of the time different libraries coexist peacefully and inter-operate inside an application. Examples are proposed of utilities developed using native or basic libraries, but exploited, for example, in an OpenMP environment.
This book focuses on parallel and concurrent patterns and issues that commonly occur in real applications, by discussing useful programming idioms, potential pitfalls, and preferred best practices that are very often independent of the underlying programming environment. In any subject, however, complex, there is always a limited number of important concepts and ideas to be solidly grasped. Once they are mastered, the rest is information that can be easily integrated when needed. Being able to think creatively about threads is more important than knowing every possible detail about specific libraries. In training users, our purpose has always been to enhance their insights on threads and to broaden their understanding of the strategic design choices open to them. For this purpose, a substantial number of examples are proposed in each chapter, with commented sources. All, without exception, are applications of varying degrees of complexity that can be compiled, run, and modified if needed. Many examples involve input parameters that can be changed to better understand the code behavior. A special effort has been made to propose simple but realistic examples, dealing with common issues in application programming.