Is parallel programming hard, and, if so, what can you do about it?
Oh parallelism, the one thing I used to worry about more, and probably should (as far as using GPUs goes)... but also one I don&#x27;t really sweat so much. Most of the reason, for me, was finding Elixir&#x2F;Erlang AND their virtual machine BEAM.<p>Until Elixir and BEAM for me, parallel programming had been difficult because the competing mental models in other languages pretty much make for a textbook fantasy.<p>I&#x27;m sure some folks just hit rage land, &quot;Pardon me, you contemptuously ignorant and vile buffoon, what did you say?&quot; but if you&#x27;d be so kind as to let me qualify it...<p>Parallelism on computers has more or less evolved from things being asynchronous which gave us the <i>effect</i> of parallelism (yay, for concurrency) to real parallelism with the proliferation of multi-core computers. This jump from emulated parallelism to real parallelism hasn&#x27;t really been too thoughtfully designed into most languages, and for those starting out, depending on your language and runtime&#x2F;vm, you might be in for quite an odyssey. We live in a world where almost every language you use has NOT been designed with parallelism as a first class problem.<p>I&#x27;m going to try to give an abridged version of my odyssey, Please note I love anything I seem hate on in this tirade.<p>First was Ruby, pure and simple, it has (system) processes (forking), threads, fibers, and libraries implementing actors and event-loops :naive-heart-eyes: but _none_ of them offer real parallelism inside the runtime itself. When one writes code with one of those mental models it falls apart at the metal (hey, GIL) and eventually you&#x27;re left scratching your head, disillusioned with the world, and realize &quot;why am I even doing this if my work is CPU bound? It won&#x27;t matter.&quot;<p>Next up, I knew the JVM had &quot;REAL THREADS!&quot; I needed to get experience with another language and thought &quot;oh hell, yeah, baby, I&#x27;m gonna rock the shit outta these 1.. 2... 4 CORES&quot; watch out CPU bound tasks. Then came reality creeping in like Santa Claus in July... &quot;Creating threads is expensive? Oh. No, shit?&quot; and &quot;oh wow, so like, I now have between 1 and N threads, that I must to schedule, and now I lock my system somehow.&quot; I was told apply locks until the burning goes away, but it never does..<p>And in that a drunken haze of threading hell one night I saw what I thought was a beacon of hope, a JVM based Actor pattern named Akka. &quot;I get the actor model, how could this go wrong?&quot; So. Many. Ways. First and foremost is choking this new fangled &quot;thread pool,&quot; Akka had given me with my CPU bound tasks. How does one then gloriously obliterate CPU bound tasks? Oh shit... One goes right back to where they started... in a separate thread. Or one can do as I did, and GALAXY BRAIN the solution: create a separate pool for my CPU bound tasks. This also introduces a new gang of friends &quot;back pressure,&quot; &quot;priority,&quot; and my favorite &quot;contention.&quot;<p>Eventually, I kind of accepted fate, knowing that some mental model even if totally contrived was better than no mental model-- right? Still I believed there had to be something better, and I kept reading about how awesome Erlang was for this sort of thing [1] at the time and Elixir had just come out.<p>Peanut butter meet pickles. This shit was good.<p>Erlang the language, and it&#x27;s runtime&#x2F;vm BEAM, were designed to work together to solve a problem space: asynchronous, fault tolerant, and distributed systems. At that point of realization, I blacked out from astonishment that people could write asynchronous code in a language and run-time <i>designed</i> to do so. Turns out its mental model was for me, the end all be all, because they (lang&#x2F;vm) were, and this my big point, DESIGNED TOGETHER TO SOLVE THE PROBLEM. On BEAM a &quot;process&quot; can be pre-empted (queue choir of angels) and thus _everything_ is non-blocking. The actor pattern was finally able to hit the metal in my head and my beard gained substantial majesty.<p>I write Elixir nearly all day long right now, and while I love it, I wish there were other options (I should probably check out Pony again) for me to use, but honestly, I don&#x27;t know of any. I&#x27;m beyond thankful for Erlang and BEAM since it really helped out my software development game and understanding how parallelism works.<p>P.S. Anyone know if is there a language designed for running on GPUs designed to make it easier to facilitate programming even if they&#x27;re slightly slower to execute, e.g. the Erlang&#x2F;OTP&#x2F;Beam of GPUs?<p>[1] BEAM is actually not great a CPU bound tasks, but that&#x27;s a different story.