I am interested in running the same function that does some monte carlo evaluations with different values of the arguments on mulitple kernels in a parallel fashion. I also want to ensure that entire function runs on the same kernel, without the computations within the function being distributed across kernels. For example, suppose I have a function (deliberately simplified)
f[a_, b_] := Module[{}, RandomReal[{a, b}]]
In[1]:= LaunchKernels[]
Out[1]= {KernelObject[1, "local"], KernelObject[2, "local"],
KernelObject[3, "local"], KernelObject[4, "local"],
KernelObject[5, "local"], KernelObject[6, "local"],
KernelObject[7, "local"]}
SeedRandom[795132, Method -> "ParallelGenerator"];
m1 = 1; m2 = 2; m3 = 3; m4 = 4; m5 = 5; m6 = 6; m7 = 7; m8 = 8;
DistributeDefinitions[f, m1, m2, m3, m4, m5, m6, m7, m8];
I now want to run f[m1, m2], f[m3, m4], f[m5, m6], f[m7, m8] f[m9, m10] on five different kernels with no information transfer across these kernels, i.e, with a separate stream of random numbers across the different kernels.
How can one do this within Mathematica?
With Mathematica's comprehensive symbolic and numerical functions, built-in application area support, and graphical interface building functions, users can write hybrid algorithms that use the CPU and GPU depending on the efficiency of each algorithm.
With parallel programming, a developer writes code with specialized software to make it easy for them to run their program across on multiple nodes or processors. A simple example of where parallel programming could be used to speed up processing is recoloring an image.
Overview. The main way to run Mathematica is interactively with notebook functionality. It can also be run text-based in the terminal, and (not recommended) can be run in batch mode with some work. This guide will focus on setting up the standard notebook functionality, using many cores to enable parallelism.
Parallel computing refers to the process of breaking down larger problems into smaller, independent, often similar parts that can be executed simultaneously by multiple processors communicating via shared memory, the results of which are combined upon completion as part of an overall algorithm.
Perhaps you can seed individual kernels with $KernelID
and $ProcessID
?
ParallelEvaluate[
Print[$KernelID $ProcessID];
SeedRandom[$KernelID $ProcessID]
]
And this should go to five different kernels (the FinestGrained option takes every evaluation to a new kernel):
ParallelTable[$KernelID -> f[2 i - 1, 2 i], {i, 5}, Method -> "FinestGrained"]
When i
(max 5) is greater than the number of kernels (8), this is going to run into issues though, i.e. f[13,14]
may use the same seed as f[2,3]
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With