Reputation: 14736
How does TPL handle multiple Parallel invocations?
Example: I want to process two unrelated collections in parallel. (assume that processing for one item in each collection take very long time)
Currently I do it like this:
Parallel.Invoke(() => Parallel.ForEach(collection1,...),
() => Parallel.ForEach(collection2,...))
This works but I am curious how the TPL scheduler will deal with the 3 separate invokes.
Should I do it differently?
Upvotes: 2
Views: 738
Reputation: 224
2 important things first:
If you have long running iterations in a Parallel.For / Parallel.ForEach workload (or any type of blocking in the loop delegate), you should always specify the concurrency level using ParallelOptions.MaxDegreeOfParallelism. This is because the parallel loop implementations were optimized for finer grained workloads, and they may end up injecting extra worker threads when they encounter long running iterations.
By launching simultaneous parallel loops you are already forcing the 2 loops to compete for CPU resources. So even if you didn't have long running iterations, it would be wise to load balance them explicitly by limiting each loop's MaxDOP (practically I'd go with Environment.ProcessorCount / 2 for each)
To answer your original questions:
...3 separate invokes...
Yes, TPL will handle 3 or more separate Parallel.Invokes just fine. It's even optimized not to waste threads in a nested parallelism scenario like this one.
Should I do it differently?
At the very least you should use MaxDOP as I explained above. But you may want to pick a different DOP strategy based on these:
Upvotes: 2