1

I Honestly don't understand when to use WithDegreeOfParallelism and how it can improve performance?

Not sure in what context limiting the number of maximum number of tasks that are executing the query

TheWommies
  • 4,922
  • 11
  • 61
  • 79

2 Answers2

4

Second link when searching for it gives the Introduction to PLINQ page, which gives two examples:

This is useful when you want to make sure that other processes running on the computer receive a certain amount of CPU time.

In cases where a query is performing a significant amount of non-compute-bound work such as File I/O, it might be beneficial to specify a degree of parallelism greater than the number of cores on the machine.

If neither of these are compelling to you, or match how you're using PLINQ, and you can't think of any other cases (e.g. if they're going to be competing to use some other resource which has a finite number of instances available) then I'd suggest leaving it out.

Community
  • 1
  • 1
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 1
    It's quite interesting that people would advice to use *more* threads with IO. It's of coz depends on the hardware, but to my limited knowledge it's best to use *fewer* threads for IO to reduce time of context switches and the associated IO work required to support multithreading. – oleksii Jan 19 '12 at 11:21
  • Sorry but I still don't understand, why would you want to have more threads when doing File/IO. How does changing the number of CPU's being used alter how much CPU time a process gets – TheWommies Jan 20 '12 at 01:05
  • @AllenHo - it doesn't affect how much CPU time a process gets - but it does allow more IO ops to be "in flight" simultaneously - each of the threads involved isn't *trying* to use the CPU. – Damien_The_Unbeliever Jan 20 '12 at 05:43
  • @Damien_The_Unbeliever inflight --- on which core ? All of them ? Each ? Doesnt plinq is dividing to thread on each core ? Can you explain ? – Royi Namir Aug 17 '12 at 14:57
  • @RoyiNamir - When we talk about IO ops being "in flight", we mean that the IO hardware is performing the operation, and that we're waiting for a signal back from the hardware that the operation has completed. So they're not in flight on *any* core - that's the point - they don't need a core, they're using other hardware inside the machine. – Damien_The_Unbeliever Aug 17 '12 at 16:04
  • @Damien_The_Unbeliever http://stackoverflow.com/questions/12020568/plinq-cores-and-withdegreeofparallelism – Royi Namir Aug 18 '12 at 17:24
0

You can use this method in parallel loops. For example I used paralellizm in process of records in DataGridView.

int tasks = Environment.ProcessorCount;
DataGrid.Rows.AsParallel().OfType<DataGridViewRow>().WithDegreeOfParallelism(tasks).ForAll(i =>
{
  // some code
});

By default DegreeOfParallelism is equal to number of processor cores. This parameter defines how many concurrent tasks (i.e. threads) will be created at processing. When I use default degree (4 cores) my application "hangs" a little so I decided to decrease degree of parallelism to 2. In any case you should select the value of this parameter as you want (or give user the option to force him make a selection ;) )

ALT
  • 1,202
  • 11
  • 7