# Limit the number of cores used

Sometimes, when Mathematica thinks it is needed, it automatically threads a computation on several cores, it can happen for NIntegrate or NDSolve for example.

I am going to use a shared workstation, so I cannot risk that my program uses all the cores: is there a way to limit the use of cores in this case? Something similar to LaunchKernels used for manual parallelization.

EDIT I have tried to change the default properties in the menu Evaluation->Parallel Kernel Configurations

The default was When needed, I have changed it to Manual, but Mathematica still tries to thread the computation over all the cores of the machine. Any idea?

If needed I am running version 12.3 for Linux

• It would be nice if you provide an MWE so we could test our solutions. Feb 14 at 9:14

There are SystemOptions["ParallelOptions"]:

SystemOptions["ParallelOptions"]

{"ParallelOptions" -> {"AbortPause" -> 2., "BusyWait" -> 0.01, "MathLinkTimeout" -> 15.,
"RelaunchFailedKernels" -> True,
"VectorParallelLengthThresholds" -> {8192, 8192, 8192, 8192, 8192},
"VectorVendorLengthThresholds" -> {128, 32, 32}}}


You can try changing the options:

SetSystemOptions["ParallelOptions" -> {"MKLThreadNumber" -> 1, "ParallelThreadNumber" -> 1}]


This works for the current Kernel session only.

Let say you want to use only 2 kernels:

LaunchKernels[2]


To check how many kernels are in use currently, run

$KernelCount  Controlling physical cores instead of kernels/threads is an OS-side problem. You can check how many physical cores are available in the machine via $ProcessorCount, which is based on what OS is telling Mathematica.