parallel_and_concurrent_programming_in_haskell.pdf

(18378 KB) Pobierz
Parallel and Concurrent
Programming in Haskell
Simon Marlow
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
July 2013:
First Edition
Indexer:
WordCo Indexing Services
Cover Designer:
Randy Comer
Interior Designer:
David Futato
Illustrator:
Rebecca Demarest
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
Tools and Resources
Sample Code
2
3
4
Part I.
Parallel Haskell
9
15
19
29
32
34
35
40
42
46
47
48
51
54
55
2. Basic Parallelism: The Eval Monad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Lazy Evaluation and Weak Head Normal Form
The Eval Monad, rpar, and rseq
Example: Parallelizing a Sudoku Solver
Deepseq
Parameterized Strategies
A Strategy for Evaluating a List in Parallel
Example: The K-Means Problem
Parallelizing K-Means
Performance and Analysis
Visualizing Spark Activity
Granularity
GC’d Sparks and Speculative Parallelism
Parallelizing Lazy Streams with parBuffer
Chunking Strategies
The Identity Property
3. Evaluation Strategies. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
4. Dataflow Parallelism: The Par Monad. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
iii
Zgłoś jeśli naruszono regulamin