How to prevent data over lapping when using parallel.foreach

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
see my code first where i have nested parallel.foreach loop

object _lock = new object();
int rowNumber = 0;

Parallel.ForEach(OrderWiseLineItem,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
data =>
{
// string Li = data1.LineItem;
string section = data.Section;
string Li = data.Lineitem;

if (!String.IsNullOrEmpty(section) && !String.IsNullOrEmpty(Li))
{
Parallel.ForEach<string, int>(
DistinctBroker,
new ParallelOptions { MaxDegreeOfParallelism = 10 },
() => rowNumber,
(broker, state, localrownumber) =>
{
lock (_lock)
{
// for broker row .... weightage 1 (no color)
localrownumber = Interlocked.Increment(ref rowNumber);
brokerRowWeightageRowNumber = new WeightageRowNumber();
brokerRowWeightageRowNumber.Section = section;
brokerRowWeightageRowNumber.Lineitem = Li;
brokerRowWeightageRowNumber.Broker = broker;
brokerRowWeightageRowNumber.RowNumber = localrownumber;
brokerRowWeightageRowNumber.Weightage = 1;

WeightageRowNumberall.Add(brokerRowWeightageRowNumber);
}

return rowNumber;
},
(incrementedRowNumber) =>
{
lock (_lock)
{
// for broker row .... weightage 2 (red color)
ConsensusRowWeightageRowNumber = new WeightageRowNumber();
ConsensusRowWeightageRowNumber.Section = section;
ConsensusRowWeightageRowNumber.Lineitem = Li;
ConsensusRowWeightageRowNumber.Broker = "";
ConsensusRowWeightageRowNumber.RowNumber = Interlocked.Increment(ref rowNumber);
ConsensusRowWeightageRowNumber.Weightage = 2;
WeightageRowNumberall.Add(ConsensusRowWeightageRowNumber);
}

});
}
});

MessageBox.Show("done");

WeightageRowNumberall.ForEach(x =>
{
textBox1.Text += (x.Section + " " + x.Lineitem + " " + x.Broker + " " + " Weitage " + x.Weightage + " Rno " + x.RowNumber) + Environment.NewLine;
});

public class WeightageRowNumber
{

public WeightageRowNumber()
{
this.Broker = string.Empty;
this.Section = string.Empty;
this.Lineitem = string.Empty;
this.RowNumber = -1;
this.Weightage = 0;
this.Id = "-1";
}

public string Broker { get; set; }
public string Section { get; set; }
public string Lineitem { get; set; }
public int RowNumber { get; set; }
public int Weightage { get; set; }
public string Id { get; set; }

}

when MaxDegreeOfParallelism = 10 then data is getting overlapping but when MaxDegreeOfParallelism = 1 then right output is coming perfect. please guide me how to prevent data over lapping when MaxDegreeOfParallelism = 10.

please rectify my code as a result data will not overlap with MaxDegreeOfParallelism = 10

when MaxDegreeOfParallelism = 10

Consensus Model Net Revenue 3A-P1 Weitage 1 Rno 1
Consensus Model Net Revenue 4E Weitage 1 Rno 2
Consensus Model Operating Income 3A-P1 Weitage 1 Rno 3
Consensus Model Operating Income 4E Weitage 1 Rno 4
Consensus Model Selling and Administrative 3A-P1 Weitage 1 Rno 5
Consensus Model Selling and Administrative 4E Weitage 1 Rno 6
Consensus Model Net Revenue Weitage 2 Rno 7

there is two broker 3A-P1 & 4E. above output print data section, line item, broker wise & weitage wise. if you see my code then understand i print data section, line item, broker wise & weitage wise whose weitage is 1 and at end one same data without broker with weitage 2. but above output print data whose weitage is 1 and very last print data with weitage 2 which is wrong.

when MaxDegreeOfParallelism = 1

then right output is this

Consensus Model Net Revenue 3A-P1 Weitage 1 Rno 1
Consensus Model Net Revenue 4E Weitage 1 Rno 2
Consensus Model Net Revenue Weitage 2 Rno 3
Consensus Model Cost of Revenue 3A-P1 Weitage 1 Rno 4
Consensus Model Cost of Revenue 4E Weitage 1 Rno 5
Consensus Model Cost of Revenue Weitage 2 Rno 6
Consensus Model Cost of Revenue-GAAP 3A-P1 Weitage 1 Rno 7
Consensus Model Cost of Revenue-GAAP 4E Weitage 1 Rno 8
Consensus Model Cost of Revenue-GAAP Weitage 2 Rno 9
Consensus Model Gross Profit 3A-P1 Weitage 1 Rno 10
Consensus Model Gross Profit 4E Weitage 1 Rno 11
Consensus Model Gross Profit Weitage 2 Rno 12

see in the above output same section, same line item and same weitage 1 and at end same data without broker with weitage 2

example - Consensus Model is section, Gross Profit is line item, 4E & 3A-P1 broker

right output print fashion would be section , line item weitage, row number. one sample added

Section Line Item Broker Weitage Row Number
Consensus Mode Taxes/(Benefit) 4E 1 1
Consensus Mode Taxes/(Benefit) 3A-P1 1 2
Consensus Mode Taxes/(Benefit) 2 3
Consensus Model Net Revenue 4E 1 4
Consensus Model Net Revenue 3A-P1 1 5
Consensus Model Net Revenue 2 6




please show me right direction. thanks

Continue reading...
 
Back
Top