# ParallelDo and distributing function definition

Consider this code:

f[i_] := Sin[i]
Do[array1[i] = f[i], {i, 1, 5}]
Table[array1[i], {i, 1, 5}]


Returns:

{Sin[1], Sin[2], Sin[3], Sin[4], Sin[5]}


Now, I want to parallelize my code:

ParallelDo[array2[i] = f[i], {i, 1, 5}]
Table[array2[i], {i, 1, 5}]


Returns:

{array2[1], array2[2], array2[3], array2[4], array2[5]}


The parallelized version is unevaluated.

How can I resolve the issue? I used SetSharedFunction[f] but it did not work.

• Once you use SetSharedFunction for the most expensive part of the code in Do, so have not only negated all benefits of parallelization, but made the code (considerably) slower. SetSharedFunction forces the function to be evaluated on the main kernel, i.e. almost the same as a non-parallel Do except that in this case instead of evaluating something directly on the main kernel, the main kernel is asking the subkernel to ask the main kernel to evaluate a piece of code. This introduces considerable overhead. – Szabolcs Nov 5 '14 at 17:27
• @Szabolcs Actually, I need to use parallelization to get speed up. What do you suggest to overcome this problem? – MOON Nov 5 '14 at 17:45
• Formulate the problem in terms of functions which don't have side effects. Specifically: do not modify the same data structure form different threads. Then it's trivially parallelizable without having to think about synchronization between the threads. Also keep in mind that not every algorithm is parallelizable. Do no blindly replace Do with ParallelDo, but think about what the subkernels would need to do precisely (and how they need to communicate) to achieve your goal, i.e. design it as a parallel algorithm from the start. – Szabolcs Nov 5 '14 at 18:39

\$Version


"9.0 for Microsoft Windows (32-bit) (January 24, 2013)"

It works well. Have you tried like this?

SetSharedFunction[array2]

f[i_] := Sin[i]
ParallelDo[array2[i] = f[i], {i, 1, 5}]
Table[array2[i], {i, 1, 5}]


{Sin[1], Sin[2], Sin[3], Sin[4], Sin[5]}