English | 中文 | Русский | Français
2,555 Posts served
8,264 Conversations started
In a recent post, Robert Chesebrough (Intel) talked about less focus on threads and more focus on tasks. I agree with him. I do believe that decomposing the job to be done into many tasks is the key to a successfully parallelized algorithm.
Once you have the most important tasks, you can re-design the algorithm taking into account that you must exploit parallel architectures. Of course, you must understand how threads work and how modern multi-core microprocessors work. Then, you can use tools like a Gantt chart (yes, a Gantt chart) to find the critical sections (those areas where parallelization is extremely difficult or nearly impossible).
This time, I’m going to show a simple example taking into one of the new features offered by future .Net 4.0 Parallel Extension. If you have an application that must perform many tasks and you discover that you have more than 2 cores available, it would be very convenient to launch them in parallel.
However, which is the simplest way to launch many completely independent tasks in parallel? In C# 4.0, you’ll be able to use the new Parallel.Invoke method combined with lambda expressions:
Parallel.Invoke(
() => ConvertMeshes(myMeshes),
() => ConvertMaterials(myMaterials),
() => ConvertLights(myLights)
() => ConvertCameras(myCameras)
);
Taking into account the default options (you can even change many options taking into account your needs), if you run this code in a quad-core CPU with four logical cores, this is what will happen:
ConvertMeshes(myMeshes) will run in Core #0.
ConvertMaterials(myMaterials) will run in Core #1.
ConvertLights(myLights) will run in Core #2.
ConvertCameras(myCameras) will run in Core #3.
The current thread will be blocked until the four methods return from their execution in parallel. However, you could also run these four tasks concurrently and asynchronously. I’m trying to keep things simple.
Is the code above easy to understand? Yes, it is very easy to understand it. It is easy to maintain. It is easy to optimize it.
If you want to take full advantage of the future Parallel Extensions in .Net 4.0, it is highly recommended to learn about lambda expressions. They were introduced in C# 3.0 to shorten the code and to make it more functional.
Combining lambda expressions with the new task oriented approach that is going to be introduced in Parallel Extensions in .Net 4.0, exploiting multi-core microprocessors will be indeed easier for most C# developers.
The aforementioned example is very simple. I tried to keep things simple, this was my idea in this post.
However, please, do not forget to learn about threads. You’ll need that knowledge in the new parallel age.
| April 28, 2009 11:33 AM PDT
Gastón C. Hillar
|
Hi Vitaly, There were some redesigns related to Parallel Extensions that will be available with C# 4.0 and .Net 4.0 (Visual Studio 2010 Beta 1). I'm waiting for VS 2010 Beta 1 to check my code and to see how the new changes work. I always post about things that I've checked myself. However, these links will help you: The changes for Parallel Extensions Beta 1: http://blogs.msdn.com/pfxteam/archive/2009/03/27/9514938.aspx In this post, Doug Holland included many interesting features of future C# 4.0: http://software.intel.com/en-us/blogs/2009/03/30/the-c-progr.....ersion-40/ Cheers, Gastón |

Vitaly Dilmukhametov
180
Status Points:
130
You are right - Parallel Extension for .NET is very easy to understand. Any .net developer, who understood basic C# 3.0 features and know something about ThreadPool, can start to use Parallel Fx for .NET quickly
What about new syntax in C# 4.0 for parallel constructions such as cycles, etc? We should use extension methods and lambdas like in October 2008 VS2010 CTP, or may be new version of C# has language support?