User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper 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:

  1. 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:
  2. 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:

  1. var sync = new object();
  2. var myNewList = new List<SomeObject>();
  3. Parallel.ForEach(myListOfSomethings, a =>
  4. {
  5. // Some other code...
  6. var someObj = new SomeObject();
  7. // More other code...
  8. lock(sync)
  9. {
  10. myNewList.Add(someObj);
  11. }
  12. // Even more code...
  13. });

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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace ParallelExample
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. Stopwatch watch;
  15. watch = new Stopwatch();
  16. watch.Start();
  17. //Serial implementation
  18. for (int i = 0; i < 100; i++)
  19. {
  20. Thread.Sleep(1000);
  21. //Perform Opration
  22. }
  23. watch.Stop();
  24. Console.WriteLine("My Serial Time: " + watch.Elapsed.Seconds.ToString());
  25. //Parallel implementation
  26. watch = new Stopwatch();
  27. watch.Start();
  28. System.Threading.Tasks.Parallel.For(0, 100, i =>
  29. {
  30. Thread.Sleep(1000);
  31. //Perform operation with i below
  32. }
  33. );
  34. watch.Stop();
  35. Console.WriteLine("My Parallel Time: " + watch.Elapsed.Seconds.ToString());
  36. Console.ReadLine();
  37. }
  38. }
  39. }

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 !!!

Abhishek Kumar

crazydeveloper Home Page 28 August 2016

Become a Fan