By default, ParallelTable
will use all the processors available in a PC. Is there a way to set a limit to the number of processors used by ParallelTable
? For example, suppose I am running a Mathematica Script which has a ParallelTable
statement on a PC with 8 cores. I want to limit the number of cores used by the script to 4. How can I do this?
2 Answers
Before evaluating ParallelTable
just launch however many kernels you want to use:
LaunchKernels[4]
By default ParallelTable
and the other Parallel*
functions call LaunchKernels[]
which will launch whatever you have configured(default is essentially LaunchKernels[Min[$ProcessorCount, $MaxLicenseSubprocesses]]
).
To demonstrate evaluation in parallel computing on different number of kernels
1)Default list of kernels run for parallel computing
`$ConfiguredKernels`
(*("<<`1` local kernels>>", 6]}*)
2) Run ParallelTable
on all kernels:
LaunchKernels[];
ParallelTable[Pause[1]; f[i], {i, 6}] // AbsoluteTiming
(*{1.009058, {f[1], f[2], f[3], f[4], f[5], f[6]}}*)
This result demonstrates how Pause[1]
is distributed on 6 kernels: Total 6 seconds of pause are executed on 6 Kernels in parallel resulting in 1 seconds of execution
3) Run ParallelTable
on only two kernels:
CloseKernels[];
LaunchKernels[2]
ParallelTable[Pause[1]; f[i], {i, 6}] // AbsoluteTiming
(*{KernelObject[37, "local"], KernelObject[38, "local"]} *)
(*{3.004172, {f[1], f[2], f[3], f[4], f[5], f[6]}}*)
This result demonstrates how Pause[1]
is distributed on 2 kernels: Total 6 seconds of pause are executed on 2 Kernels in parallel resulting in 3 seconds of execution
Unprotect[$ProcessorCount]; $ProcessorCount = 4;
I would presumeLaunchKernels
is better for most use cases. $\endgroup$