Welcome, new employee, to most innovative startup on the planet! I’m so glad to have bestowed upon you the greatest achievement of your life: being chosen to work here.
Now that you’re an official team/family member, you get to enter our Startup Stealth Tent and learn what you’ll actually work on. Our plan is to disrupt the ancient field of palm reading with a mobile app. This engineering marvel will let a user position their phone so that the camera faces their palm and takes a picture, then predicts the future. I call it: iFacePalm.
The real value of our paradigm-annihilating app will lie as much in its accuracy as its ease of use, so we’ll need to develop an accurate palm-based human life prediction model. And to do that, we’re going to need to process a poop-ton of data, comparing people’s palm stats with their entire life history to reveal the connections that without a doubt exist.
Processing poop-tons of data is exactly what parallel programming is good for, and to get you onboarded I’ve written this ebook to explain it. In truth, though, it’s not just important for our app; parallel programming has become an essential tool for modern hackers thanks to fundamental changes in computer hardware. (Not that you’ll ever need to work on anything else once the money deluge sweeps us away.)
In Clojure for the Brave and True I wrote a bit about concurrent and parallel programming, or “the sacred art of structuring your application to safely manage multiple, simultaneously executing tasks.” That book focused on state-management hassles and how Clojure can help you deal with them, and it took only brief forays into the world of parallelism.
This book focuses on performance and parallelization, and it will show you how to structure your application so that it will get better performance by automatically dividing work among all CPU cores. In the pages (screens? not-yet-scrolled-to portion?) ahead, you’ll learn about Clojure’s core.reducers library, a great option for doing parallel computation. Whereas clojure.core provides only pmap to parallelize mapping, I’ll show you how to use reducers to parallelize take, filter, and more.
My larger goal is for you to understand the reducers library completely; if I only show you how to use reducers, I’ll have failed you as an author and as an iconoclastic visionary. The world of parallel programming is fascinating, and this article will take you on a thorough tour of it so you’ll understand how the reducers library fits into the broader computing landscape. To that end, this book is divided into three parts: Practice, Theory, and Implementation.
In part one, Practice, I’ll start you off with a tutorial that will give you a good understanding of how to use the library. You’ll learn about what the functions do, when to use them, and unexpected behavior to watch out for. I’ll also compare reducer performance to serial counterparts.
This will give you enough to hang your hat on; it will give you a good concrete reference point to make sense of the more abstract discussion that will follow. Plus, it will inspire the kind of pulse-pounding edge-of-your-seat suspense that you haven’t felt since Lost went off the air as your brain scrambles to answer the question But how do reducers do that? (Probably.) And if you somehow manage to stop reading after the tutorial, you’ll still have learned enough to improve your code.
In part two, Theory, you’ll jump into the more conceptual portion by learning all about parallel performance. You’ll learn more about why it matters, and some general performance strategies that can be applied to any language, not just Clojure. You’ll dig deep into data parallism, where you’ll learn about the work-span model, one of the theoretical models used to reason about parallel performance. It’s not all theory, though; you’ll also learn about the practical approaches to writing parallel programs. I’ll discuss how you can achieve the balance between minimizing overhead and load balancing using thread management, granularity, parallel slack, tiling, and fusion. You’ll learn about the executors in Java’s java.util.concurrent package, which Clojure uses extensively.
All of this fun prep work will have you primed to understand the fork/join framework. You’ll learn how fork/join implements many of the techniques mentioned above and adds a couple more to the mix, including recursive decomposition and work stealing.
In Part three, Implementation, I’ll reach out my hand to you like Aladdin from Disney’s Aladdin and ask, “Do you trust me?”, then take you on a magic carpet ride through the surprisingly succinct reducers codebase. It’s always worthwhile to read Clojure’s source, and the reducers library is particularly delightful because it uses functional programming and Clojure’s protocols in ways that, personally, I find exhilirating. (Actually, the first time I worked my way through the code it my head hurt, but that’s pretty much the same as exhilirating, right? Hopefully, with me as your guide you’ll get all of the joy and none of the headache.)
As a foretaste: the reducers library relies on functions that return functions that return functions. It’s like Leonardo DiCaprio’s character in Inception had a baby with that Yo Dawg meme guy, and it was raised by John McCarthy using a parenting manual written by Alonzo Church. In short, I give you my word as your new boss and as a purveyor of fine ideas that part three will help you understand how to use Clojure and functional programming to write elegant code.
After going through the implementation code, your brain, which is a parallel processing machine, will fully understand Clojure’s parallel processing library. You will then enjoy a moment of smug assurance that your brain is able to appreciate this fact, while Clojure’s reducers library cannot. And then you will experience the onset of existential angst as, unbidden by quote-unqoute “you”, your brain spontaneously excretes the thought, But for how long?
Luckily, you’ll have the tools to start building our fortune-telling app, so you’ll actually be able to know how much time you’ll have to enjoy the bajillionteen bucks we’ll make selling this Nobel-worthy consumer application before our silicon successors take the helm of starship Earth and we go the way of Australopithecus.
Sounds super fun! Let’s get started!