I would like to build up a 2D-Table using ParallelTable
as follows
tab = Flatten[ParallelTable[{i,j,f[i,j]},{i,1,ni},{j,1,nj}],1];
This instruction works perfectly. However, I would like to speed it up. Indeed, the function f[i,j]
is defined as f[i_,j_] := f1[i] f2[i,j]
The particularity here is that f1[i]
is expensive to compute, so that I would like to compute it only one time for each i
.
How would it be possible to tune the ParallelTable
so that f1[i]
is only evaluated when it has never been done before ? A kind of nested ParallelTable
? Using memoization ? (As the construction of the table remains long, it is key for me to necessarily use parallelized evaluations on multiples cores)
f[1]
has only value and I don't care which kernel calculates it and when. I will try your approach. Otherwise, I thought I might proceed manually and define a functionlistf[i_] := v = f1[i]; Table[{v f2[i,j]},{j,1,nj}]
and then useParallelTable[{i,j,listf[i]},{i,1,ni}]
(The list structure is not good, but this is the idea.) However, it might not be as fast as expected. $\endgroup$f1
using the recurrence syntaxf1[i_] := f1[i] = ...
and added the instructionSetSharedFunction[f1]
. Using the instructionPrint[$KernelID]
, I also checked that the evaluations were correctly parallelized. I divided by a factor of 50 my computation time... Should I/you write an answer to my question, or are these comments enough ? $\endgroup$