-1

I am wanting to use a Parallel For Loop in C# to run a function and write the results to a variable.

This is the current for loop I am using:

string[][,] PatternTables;                    
for (i = 0; i < BOMs.Length; i++)
            {                  
                PatternTables[i] = BOMAnalysis(Pattern, PatternMatch, BOMs, HeaderIndex);
           }

also if there is a way to make it so the treads pause before writing the variable until the previous thread completes would be nice as well to keep things ordered but is no necessary.

Yes I am running the same thing multiple times right now because I have not done a parallel loop before and I want to ensure consistency before adding variation. How do I rewrite this for loop as a parallel for loop so I can get a consistent result?

This is the statement where I seem to be getting most of my errors in:

if (scrMatch)
    {
        Array.Resize(ref PatternMatch[k][0], PatternMatch[k][0].Length + 1);
        Array.Resize(ref PatternMatch[k][1], PatternMatch[k][1].Length + 1);
        Array.Resize(ref PatternMatch[k][2], PatternMatch[k][2].Length + 1);
        Array.Resize(ref PatternMatch[k][3], PatternMatch[k][3].Length + 1);
        Array.Resize(ref PatternMatch[k][4], PatternMatch[k][4].Length + 1);
        Array.Resize(ref PatternMatch[k][5], PatternMatch[k][5].Length + 1);
        Array.Resize(ref PatternMatch[k][6], PatternMatch[k][6].Length + 1);

        int Row = j + 1;

        PatternMatch[k][0][PatternMatch[k][0].Length - 1] = Row.ToString();
        PatternMatch[k][1][PatternMatch[k][1].Length - 1] = BOMs[i][j, HeaderIndex[4, i]];
        PatternMatch[k][2][PatternMatch[k][2].Length - 1] = BOMs[i][j, HeaderIndex[2, i]];
        PatternMatch[k][3][PatternMatch[k][3].Length - 1] = BOMs[i][j, HeaderIndex[6, i]];
        PatternMatch[k][4][PatternMatch[k][4].Length - 1] = BOMs[i][j, HeaderIndex[3, i]];
        PatternMatch[k][5][PatternMatch[k][5].Length - 1] = BOMs[i][j, HeaderIndex[0, i]];
        PatternMatch[k][6][PatternMatch[k][6].Length - 1] = BOMs[i][j, HeaderIndex[1, i]];


    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JMPWalker
  • 1
  • 3
  • In all of the iterations of your loop, `BOMAnalysis` is supplied the same parameters and will do the same thing every time. Why not just call `BOMAnalysis` once and assign the same value to all elements in the array? (If `BOMAnalysis` isn't [idempotent](https://stackoverflow.com/questions/1077412/what-is-an-idempotent-operation), we need to know-- a parallel solution may not work) – John Wu Oct 19 '17 at 18:19
  • 1
    Not sure what the question is - if you don't want parallel write to the table, why using Parallel For? Can you clarify the question and explain the exact problem you're trying to solve? – Amittai Shapira Oct 19 '17 at 18:31

2 Answers2

1

Take a look at the Parallel.For() Method.
Your code with the Parallel.For()-Method should look something like this:

        string[][,] PatternTables;
        Parallel.For(0, PatternTables.Length,
            index =>
            {
                PatternTables[index] = BOMAnalysis(Pattern, PatternMatch, BOMs, HeaderIndex);
            });

Parameters:

  1. Start index
  2. Last index
  3. The delegate (the code that gets executed every iteration)
    Take a look at this: https://msdn.microsoft.com/de-de/library/dd460713(v=vs.110).aspx
CoLiNaDE
  • 338
  • 2
  • 9
  • This doesn't meet the OP's requirements of not doing any work in parallel and performing all of the operations in order. – Servy Oct 19 '17 at 18:23
  • Thanks for the response CoLiNaDE. I am getting a some loops run without error and others have a varying range of errors when dealing with my array sizes sating objects don't exist and also im running the same thing multiple times and i get one result with 1717 matches and another with 1638 and another with 1613 and so on. im lost on what to do. – JMPWalker Oct 20 '17 at 15:34
  • I just ran it and i got "IndexOutofRangeException was unhanded by user" Pointing at the on the end bracket of an if statement. I edited my post to add the statement where the error usually occurs. – JMPWalker Oct 20 '17 at 19:41
  • I have been running it and my errors that occur outside of that if statement come from what seem like on of the array.resize has been skipped. witch means i wind up with one of my seven arrays being shorter than the others – JMPWalker Oct 20 '17 at 19:53
0

To make it thread safe you would have to use a Lock.

Dim locker as new Object();
Parallel.For(0, BMOs.length, index =>{

   lock(locker){
     //thread safe code
   }

 });

If the whole function that you have in the for loop needs to be locked then the regular loop you are using now will work fine. This is because the lock will only allow one thread to access that function at a time, defeating the purpose of the parallel loop.

Dan
  • 23
  • 5