Parallel Programming with C#
Hi friend, there is an important approach of coding "Parallel programming". Now a days many workstation have multiple cores (CPU). What will happen if we have multiple core station but our approach of coding is serial program? The output is our program will still use one thread of a single core at a time. So, when times have changed and processors have greatly evolved we also changed our programming model and then after introduce the new programming model called "Parallel Programming Model.".
The idea of parallelism is not new, but previously the muti-core processor is available only for few institution and lab. We have to thankful the new invention then after mostly all types of device have multiple cores.
But unfortunately still the developers are not as aware of the features of the "Parallel programming". To take advantage of the hardware of today and tomorrow, we can parallelize our code to distribute work across multiple processors. Visual Studio 2010 and the .NET Framework 4 enhance support for parallel programming by providing a new runtime, new class library types, and new diagnostic tools. These features simplify parallel development so that we can write efficient, fine-grained, and scalable parallel code in a natural idiom without having to work directly with threads or the thread pool.
The Parallel Programming model of .NET 4.0 is composed of the following:
- The Task Parallel Library (TPL): this is the base of the Task-based programming discussed in the previous section. It consists of:
a.) Task class: the unit of work you will code against instead of the previous thread model.
b.) Parallel class: a static class that exposes a task-based version of some parallel-nature problems. More specifically, it contains the following methods:
- Parallel LINQ (PLINQ): built on top of the TPL and exposes the familiar LINQ as Parallel extensions.
Take the look at the following illustration provides a high-level overview of the parallel programming architecture in the .NET Framework 4.
.Net Framework provide us different terms of technology to implement "Parallel programming" in our C#. We will start in sequence to understand the all terms and technology.
The main target of "Parallel programming" is nothing more than just a performance play. This is the key benefit of "Parallel programming". But still I want to say that "Going Parallel" is not cure for everything we should aware of this concept before using. We should be able to make the proper and experienced decision whether to stay sequential or move into "Parallel programming".
Today we will start discussion on first terms of parallel programming is "Task Parallel Library". The TPL is the preferred way to write multithreaded and parallel code. However, not all code is suitable for parallelization; for example, if a loop performs only a small amount of work on each iteration, or it doesn't run for many iterations, then the overhead of parallelization can cause the code to run more slowly. Furthermore, parallelization like any multithreaded code adds complexity to our program execution. Although the TPL simplifies multithreaded scenarios, we recommend that we should have a basic understanding of threading concepts, for example, locks, deadlocks, and race conditions, so that you can use the TPL effectively.
Parallel vs. Concurrent
Well, both concepts are related but not the same: in its simplest definition, Concurrency is about executing multiple un-related tasks in a concurrent mode. What these un-related tasks share are system resources, but they do not share a common problem to solve; meaning, they are logically independent. Now, think of these tasks as threads. And, since these threads share system resources, you face the globally common problems of concurrent programming such as deadlocks and data races. This makes concurrent programming extremely difficult and debugging complicated.
What is Parallelism? Parallelism is taking a certain task and dividing it into a set of related tasks to be executed concurrently. Sweet! So again, thinking of these tasks as threads, Parallel Programming still has the same problems of concurrent programming (deadlocks, data races, etc...), and introduces new challenges, most notably, sharing and partitioning data across these related threads. This makes Parallel Programming even more difficult and debugging even more complicated :(
For example we have a list and need to perform operation on that. Here we have to take decision on parallel programming base on the list data. If data would be not large then we should go with sequence programming and use normal looping in our C#. Because there is always some overhead involved since the TPL engine needs to manage all the features. So if we have just a small amount of work to do, running it concurrently may not outperform the sequential version. But if we have large amount of work to do with large data then we should go with parallel programming.
Here we have an important question "How to write a Simple Parallel Program?"
Look at the following code:
- var sync = new object();
- var myNewList = new List<SomeObject>();
- Parallel.ForEach(myListOfSomethings, a =>
- // Some other code...
- var someObj = new SomeObject();
- // More other code...
- // Even more code...
In the above code we can see there is parallel ForEach is used to implement the "Parallel Programming" and also use "locking" block to implement thread safe.
Now look the following code example:
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace ParallelExample
- class Program
- static void Main(string args)
- Stopwatch watch;
- watch = new Stopwatch();
- //Serial implementation
- for (int i = 0; i < 100; i++)
- //Perform Opration
- Console.WriteLine("My Serial Time: " + watch.Elapsed.Seconds.ToString());
- //Parallel implementation
- watch = new Stopwatch();
- System.Threading.Tasks.Parallel.For(0, 100, i =>
- //Perform operation with i below
- Console.WriteLine("My Parallel Time: " + watch.Elapsed.Seconds.ToString());
In the above code we can find the difference in execution time of performing a unit of work (simulated by a Sleep operation) serially versus in parallel. Now, this is a huge improvement! Imagine all the work you usually do inside For loops (or ForEach loops) and think of the time enhancement you now can get by utilizing the new Parallel.For and Parallel.Foreach methods.
But again we have to take care and handle the problems like synchronization and dead-locks with parallel programming. Now after the above discussion please do your own stuff to understand the basic design concept of "Parallel Programming" then after we will discuss further terms of "Parallel Programming".
Happy reading !!!