Nested Parallel Queries

Are there any problems with running PLINQ subqueries?

For instance:

//Contains roughly 7000+ elements mycollections.AsParallel().ForAll(x => { //contains 12 elements anothercollection.AsParallel().ForAll(y => { //download some data from the web and parse it }); }); 
+4
source share
1 answer

There are no fundamental problems with using nested queries, so you can do it, and PLINQ will try to do everything possible to parallelize the code as efficiently as possible. However, this can happen, and you should definitely take some measurements if you want to get the best performance.

The best option depends on the number of items in both collections and the time required to complete the processing.

  • If the outer set is small enough, you will also need an inner loop, so that you create enough potential for parallelization (generate enough tasks for PLINQ so that it can execute)

  • If the outer collection contains a large number of elements, then the inner loop is probably not needed, because one outer loop is enough to provide PLINQ with enough space for parallelization. In fact, the inner loop can only be the addition of overhead (although, in my experience, this only happens with a very large number of elements)

In addition, if you are going to parallelize only one cycle, it should be external. Thus, all the work can be divided at once, and you will incur the overhead of parallelization only once.

+6
source

All Articles