I am evaluating a code which ends with Table having ParallelEvaluate
of a function XXXX[phi, theta, si]
. For a grid of 225 points, a normal 2 processor laptop is taking 7 h as compared to 8.30 h by a high end Xeon 4 processor computer. CPU and memory usage for laptop and computer are about 66% vs 99% and 700MB vs 900 MB respectively. Will be thankful for any suggestion on how to improve the evaluation speed on computer. Thanks
1 Answer
Without knowing the exact function (I assume it's something fairly long, possibly involving integrals or differential equations), I can only make the following suggestions:
It looks like you're using exact numbers. If this is necessary for your application, then there's probably not a lot you can do, but exact numbers usually slow things down substantially. If you can, use Real
numbers (just place a dot after the numbers like {phi, 0., Pi/4., Pi/56.}
. If you need more precision than that but don't necessarily require the infinite precision of exact numbers, you can also do this: {phi, 0`50, Pi/4`50, Pi/56`50}
. This will give you 50 digits of precision to work with which should make your final answer pretty close to the exact answer.
The other thing I would try is:
XX1 = ParallelTable[
{XXXX[phi, theta, si]], phi, theta, si},
{phi, 0, Pi/4, Pi/56},
{theta, 0, ArcCot[Cos[phi]], ArcCot[Cos[phi]]/14},
{si, 0 Pi, 0 Pi, 0}
]
I think that ParallelTable
is a better way to handle this than ParallelEvaluate
. On a trial function, I see about a 100x speedup. ParallelEvaluate
is simply evaluating your exact same function 4 times at each data point rather than splitting the task into multiple threads.
If you can, combine both things for the best speedup.
I hope this helps a bit! There are some people on here that are amazing at optimizing, perhaps they will be able to improve the speed even more. If it's possible, I would recommend posting your XXXX
function unless it's insanely long.
-
$\begingroup$ Thanks @LukasLang ! How do you type grave accents without it interpreting them as the inline code markers? I tried backslashes before them, but that didn’t help. $\endgroup$ Jan 18, 2019 at 9:10
-
1$\begingroup$ You have to increase the amount of enclosing accents: ``` `` Code
with
accents`` ```. If you need double accents, you enclose the code with three, and so on (edit: for some reason, it doesn't work in the comment section - but you can edit your answer to see how it's done) $\endgroup$ Jan 18, 2019 at 9:24 -
-
$\begingroup$ Thanks both of you. three points 1. I do not necessary need to use exact values of (theta, phi) if it can speed up, can use ".". 2. I tried to use ParalleleTable first, but in contrast to your experience, it took 30h/48h for 4/2 processor computer as compared to 8h/7h for ParallelEvaluate. 4 - 8 times slower. 3. How can I combine both...you mean ParallelTable[ParallelEvaluate[. ?? $\endgroup$ Jan 18, 2019 at 11:03
-
1$\begingroup$ @user49535 As MassDefect already pointed out, using
ParallelEvaluate
here does not make sense at all. It enforces that the same value is computed on each of your CPU cores which is why you won't gain any speedup. It really depends on your actual functionXXXX
whetherParallelTable
can help at all. If it is a pure function thenParallelTable
should help.But ifXXXX
has side effects (like modifying data that has to be used by another thread) then it is hard to parallelize the execution. In a nutshell, we cannot give any further suggestions without knowingXXXX
. $\endgroup$ Jan 18, 2019 at 11:49
Sort
andSortBy
commands); instead of concise function calls with purely numerical input and output, you use replace rules;... $\endgroup$XXXX[0, 0, 0]
once takes 135 s on my machine. I guess this can be executed 100--1000 times faster with proper refactoring of your code (and probably by usingCompile
here and there). $\endgroup$DSC[0, 0, 0, 1]
by itself, I get output that's over 12 million bytes because the code is unable to multiply the numbers by your f values since they're one of the last things to be defined. If possible, I would try to store the f values as actual numbers in a matrix. It looks to me like the output of DSC is actually supposed to be a matrix with 36 rows and 3 columns, where the second 2 columns are just indices, so it should be on the order of 1000 bytes. $\endgroup$dsc/.R[m-1]
$\endgroup$