Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sorry I am relatively new to the parallel functionality mathematica.. Here is to code:

Try[] := Block[{temp},temp = Range[400]; Table[temp[[i]] = i^2 + 1, {i, 1, 400}];temp];

If you run it by its own, the result is satisfying. But when you try to evaluate it in parallel:

Data=Range[100];

ParallelDo[Data[[i]] = Try[], {i, 1, 100}]; // Timing

The result is wrong:

What you get is

{0.171275, Null}

and if you try

Data[[10]]

It returns

10

instead of the correct value {2,5,10,......,160001}. Basically Data array is not processed at all in the ParallelDo. If you change it to ParallelTable, ParallelMap, its the same. Did I do any thing wrong in declaring Data=Range[100] or i have to make it shared?

Thanks for your help

share|improve this question

closed as off-topic by RunnyKine, rasher, Kuba, gpap, Sjoerd C. de Vries Apr 15 at 11:28

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – RunnyKine, rasher, gpap
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I think you need SetSharedVariable[Data]. –  xzczd Apr 15 at 5:01
    
@xzczd thanks i will try! –  bboczeng Apr 15 at 5:10
    
@xzczd I think you are right! –  bboczeng Apr 15 at 5:24
    
possible duplicate of Definitions inside ParallelTable? –  xzczd Apr 15 at 6:27
    
If you use SetSharedVariable, it'll mostly negate the benefits of parallelization as it forces constant callbacks to the main kernel. Reformulate your problem in terms of side-effect-less functions such as Table. See here: mathematica.stackexchange.com/a/1771/12 and here: mathematica.stackexchange.com/a/2727/12 –  Szabolcs Apr 15 at 13:53

Browse other questions tagged or ask your own question.