1
$\begingroup$

Here's my minimal example:

xlist = {1, 2, 3, 4};
y1 = ConstantArray[{}, 4];
y2 = ConstantArray[{}, 4];
SetSharedVariable[y1, y2];
f[z_] := Block[{},
   {y1[[Position[xlist, z][[1, 1]]]], 
      y2[[Position[xlist, z][[1, 1]]]]} = 
     Extract[NMaximize[-(q - z)^2 + 2*z, q], {{1}, {2, 1, 2}}];
   ];
ParallelMap[f, xlist];
y1
y2

The f[z] function works fine on its own, but when I try to parallelize, I get the error Part::wrsym : Symbol y1 is Protected (and likewise for y2), even though I've used SetSharedVariable. Any idea how to get this working?

$\endgroup$

1 Answer 1

1
$\begingroup$

I do not really know exactly why it did not work but I found a way to circumvent it. The following seems to produce the desired result:

xlist = {1, 2, 3, 4};
y1 = ConstantArray[{}, 4];
y2 = ConstantArray[{}, 4];
SetSharedVariable[y1,y2];
f[z_] := Block[{bla, i},
   bla = Extract[NMaximize[-(q - z)^2 + 2*z, q], {{1}, {2, 1, 2}}];
   i = Position[xlist, z][[1, 1]];
   y1[[i]] = bla[[1]];
   y2[[i]] = bla[[2]];
   ];
ParallelMap[f, xlist];
$\endgroup$
1
  • $\begingroup$ Thanks, this does work well, although I was hoping to avoid defining an additional variable, bla in this case, because my xlist is going to be much, much larger and I thought doing so my offer a slight speed advantage (not sure if that's actually true). In any case, I would like to know why the error in the original example arises, if only to deepen my understanding of how parallel computing works in Mathematica. $\endgroup$
    – clr66
    Jul 12, 2017 at 17:19

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.