• For devs

How To Use Parallel Programming

Mary Dolan
5 min read

If you’re unfamiliar with the concept of parallel programming, it probably sounds like something out of Star Trek or a creepy Black Mirror episode (the machines were watching us all along). 

Everyone's favorite homicidal computer, HAL, tells dave he's afraid.

However, in the real world, parallel programming is something you can use to save time and quickly accomplish business goals. In this post, I’ll be covering the basics of parallel programming and its pros and cons—no creepy machines necessary. Now, let’s get started.

What Is Parallel Programming?

Parallel programming allows your computer to complete code execution more quickly by breaking up large chunks of data into several pieces. These pieces are then executed simultaneously, making the process faster than executing one long line of code.

Why Use Parallel Programming?

You can use parallel programming when you want to quickly process large amounts of data. It’s easy, and it can help you complete projects much more quickly and efficiently.

Data processing doesn’t have to be difficult, and with the help of parallel programming, you can take your to-do list to the next level.

What Does Parallel Programming Look Like?

For this example, we’ll be using C#. Below is a C# general sequential loop executed across several threads: 

1For each (var service in serviceList)
2{
3 CalculateBill(service);
4}
5Parallel.ForEach(ServiceList, service => {
6 CalculateBill(service)
7});

To understand parallel programming in a business context, imagine that you’re trying to process a large group of payroll data:

1//model
2public class Employees
3{
4public string name {get; set; }
5public double salary {get; set; }
6}
7
8//Instantiate Class
9List<Employees> employeeList = new List<Employees>();
10employeeList.add("Sam", 1000.0);
11employeeList.add("Dean", 1000.0);
12employeeList.add("Bob", 2500.0);

To process this data, you can divide it into smaller pieces and execute it through parallels. By running these parallels, you’re taking data items like the `person.name` and their salary and processing them in different threads.

1//Parallel Simple Example
2Parallel.ForEach(employeeList, person =>
3{
4Debug.WriteLine("Name :" + person.name + ", Salary: $" + person.salary);
5}
6);

Processing these various smaller threads simultaneously is faster than processing one long thread. And, as we all know, faster payroll-related anything is a win for everyone.

Easy and efficient, right? However, before you start parallel programming your heart out, you should be aware of its upsides, as well as a couple of potential downsides.

Pros Of Parallel Programming

The biggest benefit of parallel programming is faster code execution, which saves you time and effort. These rewards are especially evident in large-scale data parallelism, as shown in our example above. Data parallelism is when we have each thread work on the same set of tasks on a subset of values. This means that each thread is performing the same task on different sets of data — the data itself is being parallelized, not the tasks themselves. Fewer tasks equals less time and effort, which equals more time to spend on other details and projects.

Parallel programming is not limited to data parallelism, however. When we take data and spread it across several different tasks, our code should still execute more quickly. By doing so, we’re also increasing a program’s natural resources for work, and thus increasing its capacity. In short, we get things done faster. 

Cons Of Parallel Programming

For all its virtues, speed does have its downsides. Because the code is moving quickly during parallel programming, it might create a few new bugs along the way. And, because the code is executed non-sequentially, if an operation requires a specific order of code for the next statement to process, that operation will fail if parallel programming is applied to it. 

So, when using parallel programming, think of Uncle Ben and remember that with great power comes great responsibility. When used in the right circumstances, it will help you become a coding superhero—and accomplishing your endgame will be easier than ever!

sign up

It's easy to get started. And it's free.

See what you can accomplish with the world’s best email delivery platform.

Sign Up For Free

Last updated on March 19, 2020

  • Related posts
  • Recent posts
  • Top posts
View all

Always be in the know and grab free email resources!

No spam, ever. Only musings and writings from the Mailgun team.

Mailgun is committed to protecting your privacy. Please read ourPrivacy Policybefore providing us with your details.

sign up
It's easy to get started. And it's free.
See what you can accomplish with the world's best email delivery platform.
Sign up for Free