4,392 Posts served
10,722 Conversations started
- Academic

- Android

- Art, Music, & Animation

- Embedded Computing

- Events

- Game Development

- Graphics & Media

- Intel SW Partner Program

- Intel® AppUp Developer Program

- Manageability & Security

- Mobility

- Open Source

- Parallel Programming

- Performance and Optimization

- Power Efficiency

- Site News & Announcements

- Software Tools

- Association for Computing Machinery TechNews (ACM)
- Go Parallel! (Dr. Dobbs)
- HPCwire (Tabor Communications, Inc.)
- insideHPC (John West)
- Joe Duffy's Weblog (Microsoft)
- Microsoft Parallel Programming Development Center (Microsoft Germany)
- MultiCoreInfo.com
- scalability.org (Scalable Informatics)
- Software Dev Blog (Intel Germany)
- Soft Talk Blog (Intel United Kingdom)
- The Moth (Microsoft)
Specifying the desired degree of parallelism in .Net 4.0 TPL Beta 1
By Gastón C. Hillar (14 posts) on May 6, 2009 at 7:20 am
Sometimes, you don’t want to use all the available cores in a parallel loop. Why? Because you have better plans for the remaining available cores. Thus, you want to specify the concurrency level of a parallel loop. Luckily, Task Parallel Library Beta 1 will allow you to do this using the new ParallelOptions class.
This is an advanced option. It is recommended for developers who understand what they are doing. I’m not recommending this practice for its use in all the parallel loops. Some applications don’t need this kind of configurations.
The previous TaskManager instance was not successful in some cases. Therefore, the TPL team added the new ParallelOptions class, which adds many interesting properties.
First, you have to create a new instance of ParallelOptions. Then, in order to limit the concurrency level of parallel loops, you just have to set the desired number of logical cores to use to the MaxDegreeOfParallelism property. This property has a default value of -1 which means attempting to use all the available logical cores.
The following loop will use all the available logical cores:
var myOptions = new ParallelOptions { MaxDegreeOfParallelism = -1 };
Parallel.For(0, 60000, myOptions, i=>
{
// Code to run
});
The following loop will use no more than two logical cores, no matter the total number of available logical cores:
var myOptions = new ParallelOptions { MaxDegreeOfParallelism = 2 };
Parallel.For(0, 60000, myOptions, i=>
{
// Code to run
});
If you know the number of logical cores, you can use a relative value. For example, if myNumberOfCores stores the number of logical cores, the following loop will leave one core free:
var myOptions = new ParallelOptions { MaxDegreeOfParallelism = myNumberOfCores – 1 };
Parallel.For(0, 60000, myOptions, i=>
{
// Code to run
});
Of course, you must take into account that the additional threads created at run-time and the operating system scheduler will influence the final results.
If you have 8 logical cores, you can run two parallel loop asynchronously, each using 4 cores. It depends on your needs.
Again, this is an advanced topic. You must know what you’re doing when using these scheduling options. However, it’s good news that the old TaskManager introduced in previous TPL CTPs (Community Technology Previews), which was pretty inaccurate, can be replaced by the new ParallelOptions.
Categories: Parallel Programming
Tags: .Net 4.0, C# 4.0, CSharp, parallel extensions, Task Parallel Library
For more complete information about compiler optimizations, see our Optimization Notice.
Comments (2)
| May 11, 2009 12:40 PM PDT
Gastón C. Hillar
|
Hi Miroslav, I'm posting updated info about Parallel Extensions, .Net 4.0 and other stuff. Microsoft TechEd 2009 Kicks Off today. I recommend you to check Doug Holland's posts about it. He is there and he is blogging about the most important issues. He is also waiting for Visual Studio 2010 Beta 1. You can obtain RSS feeds updates from his profile: http://software.intel.com/en-us/blogs/author/doug-holland/ Cheers, Gastón |
Trackbacks (1)
- Desafíos en la Era de Múltiples Núcleos – Parte 3 by Techdoer Times
June 18, 2010 5:19 PM PDT



Miroslav
Good to see your posts here. I've been reading them. Nice update to the info provided in your book.
Do you have any news about Visual Studio 2010 Beta 1?