views:

166

answers:

2

Hi,

In C# (.NET), I got 2 loops ->

A for ... do:
{
    B for do:
      { something}

something that evaluates all "for"s in B loop
}

I used Parallel.For for the inner loop but the results of those loops varied every time I ran the application. I think it may be a result of Some asynchrounous work, but I am not sure how to be sure about it in VS 2005 Express, moreover I am not sure If I am supposed to do something Like "Wait until Parallel.For is finished, then do the stuff for all of it.

EDIT (due to comment which requests some more specific information):

 for (int i = 0; i < max; i++) //Pre Vsetky retazce ..
       {
           prefix = 0;
           tempsame.Clear();
           bool spracovane = true;
           int dlzkaI = Items[i, 1].Length;

           if (Items[i, 2] != "1") { spracovane = false; } 

           if (spracovane == false)
         //  Parallel.For(0, max, delegate(int j)
         //    {
                  for (int j = 0; j < max; j++) //Pre kazdy dalsi
                  {

                       int dlzkaJ = Items[j, 1].Length;

                       if (dlzkaJ >= dlzkaI)
                       {

                           CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;
                           bool isprefix = myComp.IsPrefix(Items[j, 1], Items[i, 1]);


                           bool issame = false;

                           if (dlzkaJ.Equals(dlzkaI)) issame = true;

                           if (isprefix == true && issame == false)
                           {
                               prefix++;
                           }

                           else if (isprefix == true && issame == true && prefix == 0) 
                           {
                               tempsame.Add(Items[j, 0]);

                           }
                       }
                } 





   if ((prefix==0) && (spracovane==false))
   {
       Items = UpdateUnique(tempsame.ToArray(typeof(string)) as string[], Items);
       unique++;

   }
   }

In short, the 2 loops loop through same array of strings and choose only unique strings -> it means that the string can't be a prefix to any other string.

> Car - unique Car - unique Cafeteria -
> unique Automobile - unique Auto - not
> unique - it's prefix of Automobile
> Auto - not unique

EDIT: No other suggestions?

+1  A: 

If your two looks have interactions with external variables or side effects, then your result may end up non-deterministic. That's one explanation, and you should check these looks for interaction. Seeing how your loops are inset, consider doing Parallel.For on just the outer loop.

One other explanation might be that PFX is not a stable tech yet - it yields lots of bizarre errors, so I would wait for .NET 4/VS 2010 and use it them.

Oh, and if you are doing some math algorithm and want the absolute top performance, I wouldn't use .NET at all - OpenMP is way better, not to mention Threading Building Blocks and the like.

Good luck!

Dmitri Nesteruk
Yes, but I am not going to study another way of coding because of one application / code. Can you check my problem again please? I added specific code.
Skuta
A: 

Without studying the code in detail, there is one obvious potential problem, in that your inner Parallel.For loop increments and inspects the prefix variable which is declared outside the parallel loop.

This means that the parallel iterations may function differently depending on which thread first increments this variable.

jerryjvl