Writing better code – Parallelize

Today, I am going to share a secret recipe for writing beautiful and efficient code that I learned, while creating simulation models for nanoveda.

Nanoveda is using advanced nanoscale simulations to design next generation cancer therapeutics.

The secret recipe is: parallelizing code.

Most modern PCs have a multicore processor inside it. We seldom code to exploit all the cores. The greatest advantage of coding for parallel processing is: getting things done faster. This is possible by utilizing all the available resources in your computer. The coding principles for parallel computing described here are applicable to cloud computing also. While using cloud computing infrastructure, we often buy virtual machines with multiple processing cores and seldom use all the processing power we paid for, to run the codes. Coding for parallel processing allows efficient use of available computing resources.

Here, I am using an example in R to demonstrate how to write a program for parallel processing.

some.function = function(r) {
    return(pi * r^2)
}
require(doParallel)
registerDoParallel(cores=2)
require(foreach)
x <- foreach(a=1:10) %dopar% { 
some.function(a)
} 
x

The code above uses 2 cores to run the function: ‘some.function’. This was achieved using doParallel and foreach packages.

To utilize all the available cores for parallel processing, modify the doParallel parameters to:

require(doParallel)
n <- detectCores
registerDoParallel(cores=n)

Parallel processing has another huge advantage. Very lengthy and complex codes can be executed with limited resources without overwhelming the system. Parallel processing is one of the key steps in code optimization. Parallelized code facilitate efficient running of massive simulation models and machine learning codes. To take advantage of hardware solutions such as Xeon Phi require this skill to code for multiple processing cores.

Nanoveda has a crowdfunding campaign to support the awesome work to create next generation cancer therapy. Donation, or not, share our crowdfunding campaign and help spread the word.