Simon Marlow
Parallel and Concurrent
Programming in Haskell
Downloa d f r o m W o w ! e B o o k < w w w.woweb o o k . c o m >
Parallel and Concurrent Programming in Haskell
by Simon Marlow
Copyright © 2013 Simon Marlow. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are
also available for most titles (http://my.safaribooksonline.com). For more information, contact our corporate/
institutional sales department: 800-998-9938 or corporate@oreilly.com.
Editors: Andy Oram and Maria Gulick
Production Editor: Melanie Yarbrough
Copyeditor: Gillian McGarvey
Proofreader: Julie Van Keuren
Indexer: WordCo Indexing Services
Cover Designer: Randy Comer
Interior Designer: David Futato
Illustrator: Rebecca Demarest
July 2013:
First Edition
Revision History for the First Edition:
2013-07-10: First release
See http://oreilly.com/catalog/errata.csp?isbn=9781449335946 for release details.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly
Media, Inc. Parallel and Concurrent Programming in Haskell, the image of a scrawled butterflyfish, and
related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a trade‐
mark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume no
responsibility for errors or omissions, or for damages resulting from the use of the information contained
herein.
ISBN: 978-1-449-33594-6
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
1.
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Terminology: Parallelism and Concurrency 2
Tools and Resources 3
Sample Code 4
Part I. Parallel Haskell
2.
Basic Parallelism: The Eval Monad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Lazy Evaluation and Weak Head Normal Form 9
The Eval Monad, rpar, and rseq 15
Example: Parallelizing a Sudoku Solver 19
Deepseq 29
3.
Evaluation Strategies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Parameterized Strategies 32
A Strategy for Evaluating a List in Parallel 34
Example: The K-Means Problem 35
Parallelizing K-Means 40
Performance and Analysis 42
Visualizing Spark Activity 46
Granularity 47
GC’d Sparks and Speculative Parallelism 48
Parallelizing Lazy Streams with parBuffer 51
Chunking Strategies 54
The Identity Property 55
4. Dataflow Parallelism: The Par Monad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
iii