Parallel Programming using R

The foreach package in R basically provides a new looping construct which is used to execute a series of statements repeatedly. This looping construct perform the same work that is done by other looping constructs such as for and while. But, the main advantage of the foreach package is that it can execute the loop in parallel across multiple cores of a system.

To execute a set of statements in parallel using foreach package, we need to use doParallel package which is a parallel backend for the foreach package. We need to register the doParallel to be used by foreach using registerDoParallel(cores=n)function. The argument to that function specifies how many cores that has to be used forperforming the operation.

Time series data was taken in order to comparison between foreach package and normal R code(sequential). Timeseries data consists of 586 rows and 42 columns where each row represents time series data for one location(time series object) and and column represents monthwise data. Chart below represents time taken by foreach package with different cores and without using foreach package i.e. sequential.

parallel programming using R Image1

From above result we can see that using 4 cores in foreach package is 3.77 times faster than R code which runs sequentially for time series data.

Here are the results when rolling forecast was done using foreach package.

parallel programming using R Image2

From above result we can see that using 4 cores in foreach package is 3.73 times faster than R code which runs sequentially.

Above analysis was done on Desktop machine with 4 cores and 8GB of RAM.

Leave a Reply

Your email address will not be published. Required fields are marked *