Message Boards Message Boards

0
|
2115 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

How to ParallelDo ?

Posted 8 years ago

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

POSTED BY: Erdem Uguz
2 Replies
Posted 8 years ago

Thank you. I should have thought about that :)

POSTED BY: Erdem Uguz

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...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract