none
Can Parallel.ForEach() nests to another Parallel.ForEach? RRS feed

  • Question

  • Dear All,

    I am working on some performance improvements work,

    in an existing function, I have already use the Parallel :

    void func1(masterItemList)

    {

    Parallel.ForEach(masterItemList,
    (subList) =>
    {
    do something on each subList...
    });

    }

    and now, I created another function:

    void func2()

    {

         List<Item> [] totalItemListArray = new List<Item>[100];

    // assume initialize the elements in totalItemListArray here

         Parallel.ForEach(totalItemListArray ,
        (subItemList) =>
        {
    func1(subItemList);
         });

    }

    Is this practicable? or is it recommended to use the Parallel like this? who could give me some suggestion about this?

    Thanks in advance.

    Ping

    Friday, August 31, 2018 8:05 AM

Answers

  • The problem with parallel is that you're eventually going to run out of available threads and you're going to block. Throwing more parallel work at a process won't necessarily speed it up. Once you hit the magic limit (generally of the processor count) it is still a blocking call. Doing parallel within parallel will get you there really quick. On a quad core processor with hyperthreading you have 8 logical threads that can run at once. If you parallel your outer loop by 4 then you're inner loop is only going to get 2 before you ultimately start blocking. I'm ignoring here the entire concern around scheduling and whatnot by the way. My point is that at any one time you are only doing 8 parallel operations. Everything else is blocking.

    So I'd ultimately say that parallel in parallel is allowed but doesn't make much sense. Either better partition your original group and parallel that or don't. Personally I also tend to use tasks over parallel anyway. The code is cleaner to me and the scheduler can figure what can and cannot be parallelized. But this is a very opinionated question you've asked so others will likely have other feelings.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, August 31, 2018 3:04 PM
    Moderator

All replies

  • The problem with parallel is that you're eventually going to run out of available threads and you're going to block. Throwing more parallel work at a process won't necessarily speed it up. Once you hit the magic limit (generally of the processor count) it is still a blocking call. Doing parallel within parallel will get you there really quick. On a quad core processor with hyperthreading you have 8 logical threads that can run at once. If you parallel your outer loop by 4 then you're inner loop is only going to get 2 before you ultimately start blocking. I'm ignoring here the entire concern around scheduling and whatnot by the way. My point is that at any one time you are only doing 8 parallel operations. Everything else is blocking.

    So I'd ultimately say that parallel in parallel is allowed but doesn't make much sense. Either better partition your original group and parallel that or don't. Personally I also tend to use tasks over parallel anyway. The code is cleaner to me and the scheduler can figure what can and cannot be parallelized. But this is a very opinionated question you've asked so others will likely have other feelings.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, August 31, 2018 3:04 PM
    Moderator
  • Thank your so much for the explanation.

    Monday, September 3, 2018 9:20 AM