Mathematica OOP objects are easily parallelized, because, each instance can run independently. Previously, I showed a parallel computing environment using Raspberry Pi without Mathematica parallel function. In this sample, OOP parallel computation style with prepared parallel function is presented. Two sample codes are different with each other, however, they are written with same OOP style.
Sample class definition is,
pclass[nam_] := Module[{local = 0},
set[nam[x_]] ^:= local = x;
get[nam] ^:= (Pause[0.1]; local^2);
]
Non-parallel run
First step is preparing a list of object name as,
object = Table[Unique[], {10}];
Second step is construction of instances as,
Table[pclass[object[[i]]], {i, 10}]
Then, set local values to each instance as,
Table[set[object[[i]][i]], {i, 10}] // AbsoluteTiming
{0.000072, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
Execution of each instance sending methods. In my condition, execution time is 1.08 sec as,
Map[get[#] &, object] // AbsoluteTiming
{1.079083, {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}}
Parallel run
First step is constructing instances using "ParallelTable" function as,
ParallelTable[pclass[object[[i]]], {i, 10}];
Second step is setting the local variable for each instance as,
ParallelTable[set[object[[i]][i]], {i, 10}]// AbsoluteTiming
{0.028048, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}
Parallel execution of each instance sending a method under the my condition of 2 Mathematica processes, required 0.57 sec.
Parallelize[Map[get[#] &, object]] // AbsoluteTiming
{0.567628, {1, 4, 9, 16, 25, 36, 49, 64, 81, 100}}