How to ParallelDo ?

I want to use parallel do for the following piece of code. This code is just a simple version what I need to do. SetSharedVariable[i]; i = 0; is not working for me. Any ideas ?

rand = {1, 2, 3, 4, 5, 6};
f = x^2;
tot = ConstantArray[0, 6];
ParallelDo[
  Ff = f /. x -> rand[[i]];
  tot[[i]] = Ff;
  , {i, 1, 6, 1}];
totT = Total[tot]

I get 0 as the answer where it should have been 91.

Thank you

Erdem

In your case tot is not shared with the kernels:

rand = {1, 2, 3, 4, 5, 6};
f = x^2;
SetSharedVariable[tot]
tot = ConstantArray[0, 6];
ParallelDo[
  Ff = f /. x -> rand[[i]];
  tot[[i]] = Ff;
  ,
  {i, 1, 6, 1}
  ];
totT = Total[tot]

now it should work…

Thank you. I should have thought about that :slight_smile: