0

I have a CFD (Computational fluid dynamics ) in c# which takes too much time to calculate results. For improving my code, I started to learn TPL and using parallel codes. For the loops that order is not important I can use TPL and for the loops with order the PLINQ is the only way. Am I correct?

As the first step, I changed For loops to Parallel.For and interestingly found out that run time increased !

Sample of my code:

for (int i = 0; i < nx; i++)
{
    for (int j = 0; j < ny; j++)
    {
        if (!Cells[i, j, 0].IsVirtual)
        {
            // calculate x velocity
            // calculate y velocity

        }
     }
 }

With parallel tasks:

Parallel.for (0,nx, i =>
{
    for (int j = 0; j < ny; j++)
    {
        if (!Cells[i, j, 0].IsVirtual)
        {
            // calculate x velocity
            // calculate y velocity
        }
    }
});

How can I speed up my code? Each my outputs takes 10 min which is very long time and I ned at least 5000 outputs.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • 1
    Providing actual code and not just a skeleton structure is required to determine why it's performing badly. It could be because the overhead of threads is more than your computations (http://stackoverflow.com/questions/10763184/how-does-sequential-loop-run-faster-than-parallel-loop-in-c). It could be because your using too many global variables (http://stackoverflow.com/questions/10846550/disappointing-performance-with-parallel-for). It could be because you've set your thread pool size too low. Or some other reason that only deeper knowledge of what you're doing could uncover. – Sybeus Jul 04 '14 at 03:12
  • What CPU you are using? Parallel computing requires hardware support. – Johnny Jul 04 '14 at 03:25
  • I have a pc with core i7 cpu. If TPL cannot help me to increase the calculation speed, what is my other solution ? – user3803849 Jul 04 '14 at 03:28
  • possible duplicate of [Nested Parallel.ForEach Loops on the same list?](http://stackoverflow.com/questions/3281604/nested-parallel-foreach-loops-on-the-same-list) – Arseni Mourzenko Jul 04 '14 at 03:43
  • Also worth noting .net 4 has awful support for TPL. The overhead makes it worthless. – Aron Jul 04 '14 at 03:46
  • What is the best way to increase my calculation speed? I need to decrease the run time, otherwise I should wait for 6 weeks till getting some data ! – user3803849 Jul 04 '14 at 03:48
  • Can anyone help me how use PLINQ for these For loops? – user3803849 Jul 04 '14 at 04:31
  • Check [`[task-parallel-library]`](http://stackoverflow.com/tags/task-parallel-library/info) tag wiki, particularly ["When Should I Use Parallel.ForEach? When Should I Use PLINQ?"](http://download.microsoft.com/download/B/C/F/BCFD4868-1354-45E3-B71B-B851CD78733D/WhenToUseParallelForEachOrPLINQ.pdf). – noseratio Jul 04 '14 at 07:33
  • If you give us the calculations we might help you more, we do not have enough information about the calculations being done and their dependencies. You have not given us enough to be sure that the problem even is the loops. – flindeberg Jul 04 '14 at 08:06

2 Answers2

3

For small loops the overhead of managing the threads is probably impacting the overall execution time. You would probably see different results if each iteration took longer to execute.

TGH
  • 38,769
  • 12
  • 102
  • 135
0

Over large datasets (e.g. at least 500,000 cells) you might be hitting problems with cache invalidation because you're iterating through memory inefficiently.

You might see a performance increase if you change it to this (regardless of whether or not you use TPL) (note how I flipped the iteration from i,j to j,i):

for (int j = 0; j < ny; j++)
{
    for (int i = 0; i < nx; i++)
    {
        if (!Cells[i, j, 0].IsVirtual)
        {
            // calculate x velocity
            // calculate y velocity

        }
     }
 }

See here for an explanation: Why does the order of the loops affect performance when iterating over a 2D array?

Community
  • 1
  • 1
Dai
  • 141,631
  • 28
  • 261
  • 374
  • You mean I do not have any way to increase my code speed? I have more than 1 million cells and the calculation for this part of code takes so much time. Regarding other trivial For loops, I applied TPL but it wasn't effective and the main loops are important for time saving. – user3803849 Jul 04 '14 at 04:10
  • @user3803849 No, I didn't say that – Dai Jul 04 '14 at 04:11