Parallel and Concurrent Programming in Haskell
Simon Marlow
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Download from Wow! eBook
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
As one of the developers of the Glasgow Haskell Compiler (GHC) for almost15 years, I have seen Haskell grow from a niche research languageinto a rich and thriving ecosystem. I spent a lot ofthat time working on GHCs support for parallelism and concurrency.One of the first things I did to GHC in 1997 was to rewrite itsruntime system, and a key decision we made at that time was to buildconcurrency right into the core of the system rather than making it anoptional extra or an add-on library. I like to think this decision was founded uponshrewd foresight, but in reality it had as much to do with thefact that we found a way to reduce the overhead of concurrency to nearzero (previously it had been on the order of 2%; weve always beenperformance-obsessed). Nevertheless, having concurrency benon-optional meant that it was always a first-class part of theimplementation, and Im sure that this decision was instrumental inbringing about GHCs solid and lightning-fast concurrencysupport.
Haskell has a long tradition of being associated with parallelism.To name just a few of the projects, there was the pH variant of Haskell derived from the Id language, which wasdesigned for parallelism, the GUM system for running parallel Haskellprograms on multiple machines in a cluster, and the GRiP system: acomplete computer architecture designed for running parallelfunctional programs. All ofthese happened well before the current multicore revolution, and theproblem was that this was the time when Moores law was still givingus ever-faster computers. Parallelism was difficult to achieve, anddidnt seem worth the effort when ordinary computers were gettingexponentially faster.
Around 2004, we decided to build a parallel implementation of the GHCruntime system for running on shared memory multiprocessors, somethingthat had not been done before. This was just before the multicorerevolution. Multiprocessor machines were fairly common, but multicoreswere still around the corner. Again, Id like to think the decision totackle parallelism at this point was enlightened foresight, but it hadmore to do with the fact that building a shared-memory parallelimplementation was an interesting research problem and sounded likefun. Haskells purity was essentialit meant that we could avoidsome of the overheads of locking in the runtime system and garbagecollector, which in turn meant that we could reduce the overhead ofusing parallelism to a low-single-digit percentage. Nevertheless, ittook more research, a rewrite of the scheduler, and a new parallelgarbage collector before the implementation was really usable and ableto speed up a wide range of programs. The paper I presented at the International Conference on Functional Programming (ICFP) in 2009 marked the turning point from an interesting prototype into ausable tool.
All of this research and implementation was great fun, but good-quality resources for teaching programmers how to use parallelism andconcurrency in Haskell were conspicuously absent. Over the lastcouple of years, I was fortunate to have had the opportunity to teach twosummer school courses on parallel and concurrent programming inHaskell: one at the Central European Functional Programming (CEFP) 2011 summer school in Budapest, and the other the CEA/EDF/INRIA 2012Summer School at Cadarache in the south of France. In preparing thematerials for these courses, I had an excuse to write some in-depthtutorial matter for the first time, and to start collecting goodillustrative examples. After the 2012 summer school I had about100 pages of tutorial, and thanks to prodding from one or two people(see the Acknowledgments), I decided to turn it into a book. At thetime, I thought I was about 50% done, but in fact it was probablycloser to 25%. Theres a lot to say! I hope you enjoy the results.
Audience
You will need a working knowledge of Haskell, which is not covered inthis book. For that, a good place to start is an introductory booksuch as Real World Haskell (OReilly), Programming in Haskell (Cambridge University Press), Learn You a Haskell for Great Good! (No Starch Press), or Haskell: The Craft of Functional Programming (Addison-Wesley).
How to Read This Book
The main goal of the book is to get you programming competently withParallel and Concurrent Haskell. However, as you probably know bynow, learning about programming is not something you can do by readinga book alone. This is why the book is deliberately practical: Thereare lots of examples that you can run, play with, and extend.Some of the chapters have suggestions for exercises you can tryout to get familiar with the topics covered in that chapter, and Istrongly recommend that you either try a few of these, or code up someof your own ideas.
cover frameworks thatwere developed in the last few years.
.
While the two parts are mostly independent from each other, the chapters should be read sequentially within each part. This isnt a referencebook; it contains running examples and themes that are developed acrossmultiple chapters.
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic Used for emphasis, new terms, URLs, Unix commands and utilities, and file and directory names. Constant width
Indicates variables, functions, types, parameters, objects, and other programming constructs.
Tip
This icon signifies a tip, suggestion, or a general note.
Caution
This icon indicates a trap or pitfall to watch out for, typicallysomething that isnt immediately obvious.
Code samples look like this:
timetable1.hs
search
::
(
partial
->
Maybe
solution
)
--
->
(
partial
->
[
partial
]
)
->
partial
->
[
solution
]
The heading gives the filename of the source file containing the codesnippet, which may be found in the sample code; see forhow to obtain the sample code. When there are multiple snippetsquoted from the same file, usually only the first will have thefilename heading.
There will often be commentary referring to individual lines inthe code snippet, which look like this.
Commands that you type into the shell look like this:
$ ./loggerhellobyelogger: stop
The $
character is the prompt, the command follows it, and the restof the lines are the output generated by the command.