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.