Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TPL Parallel.For with long running tasks

I am wanting to use the Task Parallel Library (TPL) in F# to execute many (>1000) long running tasks. Here is my current code:

Parallel.For(1, numberOfSets, fun j ->
    //Long running task here
    )

When I start this it appears that .NET initiates all of the tasks at once and bounces between them constantly. What would be better is if it stayed on a task until it is done before moving to the next one. This would minimize the context switching.

Is there a way to provide a hint to the scheduler? I know that it is possible to provide hints but I cannot find clear examples or is the scheduler already smart about this and it's just my perception that there are too many context switches occuring. Thanks for the help!

like image 383
Matthew Crews Avatar asked Nov 24 '12 18:11

Matthew Crews


1 Answers

We had a similar problem - using C# instead than F#, but the libraries are the same. The solution was to limit the degree of parallelism:

ParallelOptions parallelOptions = new ParallelOptions();
parallelOptions.MaxDegreeOfParallelism = 16;
Parallel.For(0, n, parallelOptions, i => {
   . . . 
});

16 worked well for our tasks - you should experiment to see which value is better in your case.

like image 188
MiMo Avatar answered Oct 29 '22 12:10

MiMo