0
$\begingroup$

I want to modify a vector in a Table function, such as

vec = {0, 0, 0, 0};
Table[vec[[i]] = i, {i, 1, 4}];
vec
// output {1,2,3,4}

however, when I use ParallelTable function to speed up the progress, it fails to work.

vec = {0, 0, 0, 0};
ParallelTable[vec[[i]] = i, {i, 1, 4}];
vec
// output {0,0,0,0}

How can I fix it?

$\endgroup$
3
  • $\begingroup$ This can't be fixed without a significant performance degradation (SetSharedVariable) because Mathematica uses distributed memory parallelism. Restructure your algorithms so no global data needs to be written. $\endgroup$
    – Szabolcs
    Nov 25, 2018 at 19:26
  • $\begingroup$ @Szabolcs Can you point to a more elaborated discussion of these points? Thanks. $\endgroup$
    – Alan
    Nov 26, 2018 at 0:16
  • 1
    $\begingroup$ @Alan mathematica.stackexchange.com/a/138911/12 $\endgroup$
    – Szabolcs
    Nov 26, 2018 at 7:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.