# Alternative for SharedVariable

I have the following code:

Answer = Table[Undefined, {1048576}];

ParallelDo[(

eq = getEx[i];
(* Some calculation here with eq *)

), {i, 1048576}];



What getEx[i] does is to return me with an expression that I need to solve.

I need to get all 1M differents equations and save it´s answers, they are all independet of each other, so I could use parallel programming to speed thing a lot.

My problem is that with a SharedVariable, each kernel always goes to the main one for evaluation, so this code is almost slower then using a simple Do[] instead of ParallelDo[]

I would like to know how could I make this run better? Since I am just writing into a SharedVariable but never reading from that during the parallel programing.

Thanks to all!

• Thanks, I will have a look on that. I just started 2 days ago working with mathemathica. – user5343 Apr 12 '18 at 11:08

Why not just use ParallelTable and dispense with the shared variable altogether? Also, make sure that the definition of getEx gets distributed: if it lives in Global that shouldn't be a problem, but if it lives in a package it might.

LaunchKernels[];
DistributeDefinitions[getEx];
solutions = ParallelTable[
Solve[getEx[i]][[1]]
{i, 1048576}
];
CloseKernels[];

• Thanks!! I will try that – user5343 Apr 12 '18 at 11:14
• It worked perfectly!, Thanks! My simulation was taking something around 3h, now I got that in 10 minuts – user5343 Apr 12 '18 at 12:02
• You're welcome. For future reference: in Mathematica you generally don't want to use Do loops to assemble lists. That's what Table and functions like Map (and their Parallel brothers) are for. – Sjoerd Smit Apr 12 '18 at 13:11