So after testing some things I am completely reformulating my problem. First an example
as = Association[{2 -> 7, 3 -> 8, 4 -> 9, 5 -> 10, 6 -> 11}]
<|2 -> 7, 3 -> 8, 4 -> 9, 5 -> 10, 6 -> 11|>
calcF[x_, y_, Max_] := Sum[Lookup[as, x + y, 0], {i, 1, Max}]
doo[Max_] := Table[calcF[x, y, Max], {x, 1, 5}, {y, 1, 5}]
AbsoluteTiming[doo[50000]]
{0.539171, {{350000, 400000, 450000, 500000, 550000}, {400000, 450000,
500000, 550000, 0}, {450000, 500000, 550000, 0, 0}, {500000,
550000, 0, 0, 0}, {550000, 0, 0, 0, 0}}}
doo[Max_] := ParallelTable[calcF[x, y, Max], {x, 1, 5}, {y, 1, 5}]
AbsoluteTiming[doo[50000]]
{2.45135, {{350000, 400000, 450000, 500000, 550000}, {400000, 450000,
500000, 550000, 0}, {450000, 500000, 550000, 0, 0}, {500000,
550000, 0, 0, 0}, {550000, 0, 0, 0, 0}}}
ls = {0, 7, 8, 9, 10, 11, 0, 0, 0, 0}
{0, 7, 8, 9, 10, 11, 0, 0, 0, 0}
malcF[x_, y_, Max_] := Sum[ls[[x + y]], {i, 1, Max}]
foo[Max_] := Table[malcF[x, y, Max], {x, 1, 5}, {y, 1, 5}]
AbsoluteTiming[foo[50000]]
{0.07679, {{350000, 400000, 450000, 500000, 550000}, {400000, 450000,
500000, 550000, 0}, {450000, 500000, 550000, 0, 0}, {500000,
550000, 0, 0, 0}, {550000, 0, 0, 0, 0}}}
foo[Max_] := ParallelTable[malcF[x, y, Max], {x, 1, 5}, {y, 1, 5}]
AbsoluteTiming[foo[50000]]
{0.032803, {{350000, 400000, 450000, 500000, 550000}, {400000, 450000,
500000, 550000, 0}, {450000, 500000, 550000, 0, 0}, {500000,
550000, 0, 0, 0}, {550000, 0, 0, 0, 0}}}
So, from this it seems like ParallelTable
has some issue with an Association
, as it performs slower than Table
. Is this behaviour expected? Is it a bug and can I get over it somehow?
Forgot to mention, in case it matters - my laptop has 4 cores that are hyper-threaded, but I have stuck with the default Mathematica setting to launch 4 kernels for parallel computations.
The reason I want to use an Association
is because in my actual calculation ls
will be a massive 5-dimensional Table
with hundreds of millions of elements (thus taking all my RAM), but 95% of them will be zeroes. Therefore, I want to replace it with an Association
that will have only non-zero terms and will thus be much smaller, hence allowing me to perform the calculation.
alternativeSum
$\endgroup$Association
doesn't work withParallelTable
generally it should be possible to demonstrate it in only a few lines of code. If not there may be a contributing factor that also needs to be discovered. $\endgroup$