0

I have a nested For loops as follow:

    // This loop cannot be parallel because results of the next 
    // two loops will be used for next t
    for (int t= 0; t< 6000000; t++)
        // Calculations in the following two loops includes 75% of all time spend for.
        for (int i= 0; i< 1000; i++)
        {
            for (int j= 0; j< 1000; j++)
            {

                if (Vxcal){V.x= ............. // some calculations    }
                if (Vycal){V.y= ............. // some calculations    }
                // Vbar is a two dimensional array
                Vbar = V;
            }
        }

I changed the above code to :

    // This loop cannot be parallel because results of the next 
    // two loops will be used for next t
    for (int t= 0; t< 6000000; t++)
        // Calculations in the following two loops includes 75% of all time spend for.
        Parallel.for (0, 1000, i=>
            {
                Parallel.for (0, 1000, j=>
                    {
                        if (Vxcal){V.x= ............. // some calculations    }
                        if (Vycal){V.y= ............. // some calculations    }
                        // Vbar is a two dimensional array
                        Vbar = V;
                    }
            }

When I run code results are not correct and takes hours instead of 10 mins. My question is: Are these kind of For loops suitable for Parallel? These loops just have some mathematical calculations. How can I make this parallel For loops safe?

I found a keyword "Lock" which can help me for have a safe loop but which part of this loop is unsafe?

Mehdi
  • 211
  • 2
  • 11
  • Where is `V` defined? – Jacob Krall Jul 11 '14 at 03:08
  • 1
    Come to think of it, how does your code even compile? The value `5000000000` is 5 billion. The range of `int` in C# is only 2 billion and change. The statement `t < 5000000000` should give you a compiler error. – Jim Mischel Jul 11 '14 at 03:25
  • @JimMischel I just put some numbers to show there is a lot of calculations. – Mehdi Jul 11 '14 at 03:56
  • @JacobKrall, V is an array that has x and y dimension. Each dimension was calculated separately and put together as V in VBar. My question is, in this situation what should I do? parallel.For don't give me correct answer. – Mehdi Jul 11 '14 at 03:59
  • @Mehdi would you please update your examples to show how `V` and `Vbar` are defined? I strongly suspect that you are overwriting all your calculations with the statement `Vbar = V;`. – Jacob Krall Jul 11 '14 at 14:49

1 Answers1

4

I see you modified your question to put in some other numbers. So your inner loop is now executed 6000000*1000*1000, or 6,000,000,000,000 times. At 4 billion calculations per second (which your computer can't do), that's going to take 1,500 seconds, or 25 minutes. If you get perfect parallelism with Parallel.For, you can cut that down to 8.25 minutes on a 4-core machine. If your calculations are long and complicated, there's no surprise that it takes hours to complete.

Your code is slow because it's doing a lot of work. You need a better algorithm!

Original answer

Consider your nested loops:

for (int t= 0; t< 5000000000; t++)
    // Calculations in the following two loops includes 75% of all time spend for.
    for (int i= 0; i< 5000000000; i++)
    {
        for (int j= 0; j< 5000000000; j++)

The inner loop (your calculation) is being executed 5,000,000,000*5,000,000,000*5,000,000,000 times. 125,000,000,000,000,000,000,000,000,000 is a huge number. Even if your computer could do that loop 4 billion times per second (it can't--not even close), it would take 31,250,000,000,000,000,000 seconds, or about 990 billion years to complete. Using multiple threads on a four-core machine could cut that down to only 250 billion years.

I don't know what you're trying to do, but you'll need a much better algorithm, a computer that's about a 500 billion times faster than the one you have, or several hundred billion processors if you want it to finish in your lifetime.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351