June 2019
Intermediate to advanced
218 pages
5h 19m
English
Julia includes an inbuilt parallel for loop that can automatically distribute the computation within the loop across all nodes in a cluster. This can sometimes allow code to speed up across machines with little programmer intervention.
In the following code, we generate a million random numbers and add them. The first function computes each step serially:
function serial_add() s=0.0 for i = 1:1000000 s=s+randn() end return s end
Each step in this loop can be computed independently, and thus should be easy to parallelize. The second function here attempts to distribute the steps across the cluster:
function parallel_add() return @distributed (+) for i=1:1000000 randn() end end
We can see that the parallel function ...
Read now
Unlock full access