none
How to use Parallel.ForEach loop with partition-local variables for my code RRS feed

  • Question

  • see my code. which has 3 Parallel.ForEach. i want when first inner Parallel.ForEach will execute to populate brokerRowWeightageRowNumber   then at end it will pass row number to block where i will populate ConsensusRowWeightageRowNumber and after that last nested Parallel.ForEach will execute which will populate QcRowWeightageRowNumber

    how to achieve for my scenario using Parallel.ForEach loop with partition-local variables. thanks

    Parallel.ForEach(OrderWiseLineItem, data =>
                        {
                            // string Li = data1.LineItem;
                            string section = data.Section;
                            string Li = data.Lineitem;
    
                            if (!String.IsNullOrEmpty(Li) && !String.IsNullOrEmpty(section))
                            {
                                Parallel.ForEach(DistinctBroker, broker =>
                                {
                                        // for broker row .... weightage 1 (no color)
    
                                        Interlocked.Increment(ref rowNumber);
                                        brokerRowWeightageRowNumber = new WeightageRowNumber();
                                        brokerRowWeightageRowNumber.Section = section;
                                        brokerRowWeightageRowNumber.Lineitem = Li;
                                        brokerRowWeightageRowNumber.Broker = broker;
                                        brokerRowWeightageRowNumber.RowNumber = rowNumber;
                                        brokerRowWeightageRowNumber.Weightage = (int)RowWeightage.BrokerRow;
    
                                        lock (_lock)
                        		    {
                                            WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                        }
                                }
    
                                    Interlocked.Increment(ref rowNumber);
                                    ConsensusRowWeightageRowNumber = new WeightageRowNumber();
                                    ConsensusRowWeightageRowNumber.Section = section;
                                    ConsensusRowWeightageRowNumber.Lineitem = Li;
                                    ConsensusRowWeightageRowNumber.Broker = "";
                                    ConsensusRowWeightageRowNumber.RowNumber = rowNumber;
                                    ConsensusRowWeightageRowNumber.Weightage = (int)RowWeightage.ConsenSusRow;
    
                                    lock (_lock)
                                    {
                                        WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
                                    }
    
    
                                if (qcTrueDistin.Any(x => x.TabName.Equals(section) && x.StandardLineItem.Equals(Li)))
                                {
                                    // for QC Check row .... weightage 3, if any  (yellow color)
                                    Parallel.ForEach(DistinctBroker, broker =>
                                    {
    
    				    Interlocked.Increment(ref rowNumber);
    				    QcRowWeightageRowNumber = new WeightageRowNumber();
    
    				    QcRowWeightageRowNumber.Section = section;
    				    QcRowWeightageRowNumber.Lineitem = Li;
    				    QcRowWeightageRowNumber.Broker = broker;
    				    QcRowWeightageRowNumber.RowNumber = rowNumber;
    				    QcRowWeightageRowNumber.Weightage = (int)RowWeightage.QcRow;
    				    lock (_lock)
    				    {
    				    WeightageRowNumberall.Add(QcRowWeightageRowNumber);
    				    }
    
                                    }
                                }
                            }
    
                        }


    Saturday, January 26, 2019 6:02 PM

All replies

  • actually this way i did it first which is working

                              Parallel.ForEach(DistinctBroker, broker =>,,
                              {
                                  // for broker row .... weightage 1 (no color)
    
                                  //Interlocked.Increment(ref rowNumber);
                                  brokerRowWeightageRowNumber = new WeightageRowNumber();
                                  brokerRowWeightageRowNumber.Section = section;
                                  brokerRowWeightageRowNumber.Lineitem = Li;
                                  brokerRowWeightageRowNumber.Broker = broker;
                                  brokerRowWeightageRowNumber.RowNumber = Interlocked.Increment(ref rowNumber);
                                  brokerRowWeightageRowNumber.Weightage = 1;
    
                                  lock (_lock)
                                  {
                                      WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                  }
                              });
    

    now i want to use partition so i change my code this way but now my code not compiling. here is code

                              Parallel.ForEach(DistinctBroker, broker,
                                  (j, loop, subtotal) =>
                              {
                                  brokerRowWeightageRowNumber = new WeightageRowNumber();
                                  brokerRowWeightageRowNumber.Section = section;
                                  brokerRowWeightageRowNumber.Lineitem = Li;
                                  brokerRowWeightageRowNumber.Broker = broker;
                                  brokerRowWeightageRowNumber.RowNumber = Interlocked.Increment(ref rowNumber);
                                  brokerRowWeightageRowNumber.Weightage = 1;
    
                                  lock (_lock)
                                  {
                                      WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
                                  }
                                  return brokerRowWeightageRowNumber.RowNumber;
    
                              },
                              
                              (finalResult) =>
    				  ConsensusRowWeightageRowNumber = new WeightageRowNumber();
    				  ConsensusRowWeightageRowNumber.Section = section;
    				  ConsensusRowWeightageRowNumber.Lineitem = Li;
    				  ConsensusRowWeightageRowNumber.Broker = "";
    				  ConsensusRowWeightageRowNumber.RowNumber = Interlocked.Increment(ref finalResult); 
    				  ConsensusRowWeightageRowNumber.Weightage = 2;
    
    				  lock (_lock)
    				  {
    				      WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
    				  }                              
                              );
    

    please see my second set of code and show me how to restructure to use partition for parallel foreach to iterate my list.

    please help me with rectified version of my code which i can test. thanks



    Sunday, January 27, 2019 6:37 PM
  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, dur the code is not completed, I could not test the code.

    It is possible to use nested parallel.foreach. It is similar to parallel.for, please check the link below.

    https://blogs.msdn.microsoft.com/pfxteam/2012/03/14/is-it-ok-to-use-nested-parallel-for-loops/

    What do you want to get with nested parallel.foreach? In the link, the results is orderless. The blog shows that the nested Parallel.ForEach() shows a degradation in performance.

    https://www.guyellisrocks.com/2010/10/nested-parallelforeach.html

    If you want to use parallel.foreach with normal foreach loop, you could try the way in the stackOverFlow.

    https://stackoverflow.com/questions/3281604/nested-parallel-foreach-loops-on-the-same-list

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Friday, February 1, 2019 7:37 AM
    Monday, January 28, 2019 7:17 AM
    Moderator