Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Random Forest with Parallel Computing in R Programming
Next article icon

foreach parallel computing using external packages

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Parallel computing is a method of breaking down large computational tasks into smaller ones that can be executed simultaneously on multiple processors or cores, leading to faster results. The foreach loop is a popular construct in R programming, which allows users to iterate over a list or vector of elements. In parallel computing, foreach can be used to execute code in parallel across multiple processors or cores, leading to significant speedups in performance. In this article, we will discuss the concepts related to foreach parallel computing and the steps needed to use it, along with some good examples.


CONCEPTS:
The concept behind parallel computing is to break down a large computational task into smaller sub-tasks and execute them simultaneously across multiple processors or cores. In R programming, parallel computing can be achieved using the parallel package, which provides support for multiple types of parallelism, including fork-based, socket-based, and cluster-based parallelism.

The foreach loop is another popular construct in R programming that allows users to iterate over a list or vector of elements. The foreach package provides support for parallel computing using foreach loops, which allows users to execute code in parallel across multiple processors or cores.

Steps:

To use foreach parallel computing in R, the following steps are needed:

  • Install the required packages: The foreach and doParallel packages are required to use foreach parallel computing in R. They can be installed using the following commands:
     
R
install.packages("foreach") install.packages("doParallel") 
  • Load the packages: Once the packages are installed, they need to be loaded into the R environment using the following commands:
R
library(foreach) library(doParallel) 
  • Create a cluster: Before executing code in parallel, a cluster needs to be created to specify the number of processors or cores to use. This can be done using the following command:
     
R
cl <- makeCluster(4) # Create a cluster with 4 cores 
  • Register the cluster: The cluster needs to be registered with the doParallel package using the following command:
     
R
registerDoParallel(cl) 
  • Execute code in parallel: Finally, the code can be executed in parallel using the foreach loop. The following code demonstrates how to calculate the sum of squares of a vector in parallel:
     

     

R
vec <- c(1:1000) result <- foreach(i = 1:length(vec), .combine = "+") %dopar% {   vec[i]^2 } 

          In this example, the %dopar% operator is used to indicate that the code should be executed in parallel. The combine argument specifies the method to combine the results of each iteration, which in this case is the sum.

EXAMPLES:
The foreach package can be used in various scenarios where there is a need to iterate over a list or vector of elements and execute code in parallel. Some good examples include:

  • Parallelizing a for loop : In R, the for loop is a popular construct used to iterate over a sequence of values. However, when dealing with large datasets, a for loop can take a significant amount of time to execute. By using the foreach package, we can parallelize the for loop and execute the iterations in parallel. The following code demonstrates how to do this:
     
R
library(foreach) library(doParallel)  # Create a cluster with 4 cores cl <- makeCluster(4) registerDoParallel(cl)  # Create a vector of values vec <- 1:100  # Parallelize the for loop result <- foreach(i = 1:length(vec), .combine = c) %dopar% {   vec[i] * 2 }  # Print the result print(result) 

  OUTPUT :
 

output


      In this example, we create a cluster with 4 cores, register the cluster with the doParallel package, and then use foreach to parallelize the for loop. The code        calculates the product of each value in the vector vec by 2, and stores the results in the variable result.

  • Squaring numbers in parallel :-Suppose we have a vector of numbers and we want to square each number in parallel. Here's how we can do it with foreach:
R
library(foreach) library(doParallel)  # Create a cluster with 4 cores cl <- makeCluster(4) registerDoParallel(cl)  # Define a vector of numbers vec <- 1:10  # Parallelize the for loop result <- foreach(i = 1:length(vec), .combine = c) %dopar% {   vec[i]^2 }  # Stop the cluster stopCluster(cl)  # Print the result print(result) 


 The 1:100 notation produces an integer vector from 1 to 100, which is a vector of values.The for loop that comes after it is parallelized using the  foreach() method. The loop loops through the values in the vector, multiplying each value by two as it goes. The outcomes of the parallel  calculations are combined into a single vector using the.combine = c argument.The foreach() loop uses the %dopar% operator to indicate that  the loop should run concurrently on all available cores.The result variable holds the output of the concurrent computation.The computation's  findings are shown using the print() function.

OUTPUT :
 
 

output

          This code creates a cluster with 4 cores, defines a vector of numbers, and then parallelizes the for loop to square each number using the %dopar% operator. The .combine = c argument tells foreach to combine the results into a single vector. Finally, the code stops the cluster and prints the result.

  • Finding the maximum of a list of matrices:
    Suppose we have a list of matrices and we want to find the maximum value across all the matrices in parallel. Here's how we can do it with foreach:
     
R
library(foreach) library(doParallel)  # Create a cluster with 4 cores cl <- makeCluster(4) registerDoParallel(cl)  # Define a list of matrices lst <- list(matrix(1:9, ncol = 3), matrix(10:18, ncol = 3), matrix(19:27, ncol = 3))  # Parallelize the for loop result <- foreach(mat = lst, .combine = max) %dopar% {   max(mat) }  # Stop the cluster stopCluster(cl)  print(lst) # Print the result print(result) 


OUTPUT :

output

 

 

This code creates a cluster with 4 cores, defines a list of matrices, and then parallelizes the for loop to find the maximum value across all the matrices using the %dopar% operator. The .combine = max argument tells foreach to combine the results using the max function. Finally, the code stops the cluster and prints the result.
 

  •  Parallel Matrix Multiplication:
    This example demonstrates how to use foreach to parallelize matrix multiplication:
     
R
library(foreach) library(doParallel)  # Create a cluster with 4 cores cl <- makeCluster(4) registerDoParallel(cl)  # Define two matrices A <- matrix(rnorm(10000), 5, 5) B <- matrix(rnorm(10000), 5, 5)  # Parallelize the matrix multiplication result <- foreach(i = 1:5, .combine = "cbind") %:%   foreach(j = 1:5, .combine = "c") %dopar% {     sum(A[i,] * B[,j])   }  # Stop the cluster stopCluster(cl)  # Print the result print(A) print(B) print(result) 


 


OUTPUT:
 

output

In this example, we create a cluster with 4 cores using makeCluster and register it for use with foreach. We then define two matrices A and B, and parallelize the matrix multiplication using foreach. The %:% operator indicates that the loops should be executed in parallel, and the .combine parameter specifies that the results should be combined using cbind and c to construct the resulting matrix. Finally, we stop the cluster and print the result
 

CONCLUSION:


In conclusion, foreach and doParallel are powerful R packages that enable users to parallelize their code and speed up computation on multi-core processors. By splitting a task into smaller chunks and distributing those chunks across multiple cores, users can dramatically reduce the time it takes to run computationally intensive code.

While parallel computing can be a powerful tool, it is important to keep in mind that it is not always the best solution for every problem. In some cases, parallelizing code can actually slow down computation due to overhead associated with distributing and combining results. Additionally, not all algorithms can be parallelized effectively, so it is important to carefully consider the nature of the problem and the structure of the code before attempting to parallelize.

Overall, foreach and doParallel are valuable tools to have in your R toolkit when working with computationally intensive code, and can help to significantly reduce the time it takes to perform complex simulations and data analysis.
 


Next Article
Random Forest with Parallel Computing in R Programming

D

dm75323
Improve
Article Tags :
  • R Language

Similar Reads

  • Parallel Algorithm Models in Parallel Computing
    Parallel Computing is defined as the process of distributing a larger task into a small number of independent tasks and then solving them using multiple processing elements simultaneously. Parallel computing is more efficient than the serial approach as it requires less computation time.   Parallel
    7 min read
  • Azure Batch for Large-scale Parallel Computing
    Azure Batch, as one of Microsoft Azure’s solutions, is a job scheduling service dedicated to parallel and high-performance computing applications. It helps you distribute computational workloads to other cloud resources and lets you do a computation in parallel on a large number of Virtual Machines
    8 min read
  • Dask: Empowering Machine Learning with Scalable Parallel Computing
    Traditional tools like NumPy, pandas, and scikit-learn are powerful but often fall short when dealing with data that exceeds memory capacity or requires extensive computational resources. This is where Dask, an open-source parallel computing library in Python, comes into play. Dask extends the capab
    5 min read
  • Random Forest with Parallel Computing in R Programming
    Random Forest in R Programming is basically a bagging technique. From the name we can clearly interpret that this algorithm basically creates the forest with a lot of trees. It is a supervised classification algorithm. In a general scenario, if we have a greater number of trees in a forest it gives
    4 min read
  • Loop Level Parallelism in Computer Architecture
    Since the beginning of multiprocessors, programmers have faced the challenge of how to take advantage of the power of process available. Sometimes parallelism is available but it is present in a form that is too complicated for the programmer to think about. In addition, there exists a large sequent
    3 min read
  • What is Parallel File System in Cloud Computing?
    Cloud computing is a popular choice among IT professionals and companies in the digital marketing industry. It allows users to access shared resources through the Internet with little to no up-front investment. Companies that offer cloud computing services typically charge clients a flat fee per mon
    3 min read
  • Parallel processing using "parallel" in R
    Parallel processing allows your application to do more tasks in less time. These assist in solving significant issues. In this article, we are going to look at how we can do parallel processing using the parallel library in R Programming Language. Using parallel library The parallel is a base packag
    3 min read
  • Difference Between Implicit Parallelism and Explicit Parallelism in Parallel Computing
    Implicit Parallelism is defined as a parallelism technique where parallelism is automatically exploited by the compiler or interpreter. The objective of implicit parallelism is the parallel execution of code in the runtime environment. In implicit parallelism, parallelism is being carried out withou
    6 min read
  • Nix - The Purely Functional Package Manager for Linux
    Nix is a purely functional package manager for Linux, that serves to provide a purely functional approach to any system's software package management. Due to its functional and declarative approach, it is lauded for its capacity to:  Support the installation of multiple versions of a given package.E
    6 min read
  • Efficient way to install and load R packages
    The most common method of installing and loading packages is using the install.packages() and library() function respectively. Let us see a brief about these functions - Install.packages() is used to install a required package in the R programming language. Syntax: install.packages("package_name") l
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences