Go Parallel 2

This is a computer translation of the original content. It is provided for general information only and should not be relied upon as complete or accurate.

This is the second part of Go Parallel series (part 1) about parallel programming with Go language. As I promised we will look at parallel divide-and-conquer decomposition and parallel pipelines.

Guess what problem we will parallelize. Right! Fibonacci number computation. There are more efficient ways to calculate Fibonacci numbers, but we will use the simple recursive formula. Because, well, it’s simple.

I hope it’s pretty self-explanatory. However, there is an interesting thing about this program. It uses data-driven recursion (recursion depth depends on user input), and usually it’s a bad idea because it can cause stack overflow. But Go uses dynamically sized stacks, that is, stack grows and shrinks are necessary. This allows to express such inherently recursive algorithms in the natural form without fear of stack overflows.

Note how we use the channel to join subtasks and collect results at the same time. “<-res + <-res” may look a bit unusual at first. Channel receive operation is an expression, so it can be used inside of more complex expressions.

Let’s move on to pipelines. Let’s say we want to implement a “classical” pipeline with serial input stage, parallel transformation stage and serial output stage. For example, we want to read image files from disk, resize them and write the resulting images back to disk. A usual structure for such processing looks as follows:

Note that for efficient pipelining channels must be sufficiently buffered to avoid unnecessary idling of goroutines. Once again, Go allows to express this complex interaction of concurrent activities as the straight-line code that you can read from top to bottom.

Here you can see how this pattern is applied to simplified drug design problem: we match candidate drug molecules (ligands) to a target protein to find good candidates for use as drugs.

In the next part we will look at sync and sync/atomic packages and how they can help with common parallel programming tasks. Stay tuned!

1 comment

So, I do have a few reservations regarding this code and that's probably because I am new to Go & Parallel programming in general.

I tried passing 50 as a parameter in the function and then ran the parallel code to see how that would work and that made my laptop get stuck for a while. I am not sure what's the reason exactly. Do you think that maybe its a condition of stack overflow?