# Set the number of threads Use this when pdemagic should leave some cores of the machine to other work, or when you compare timings on a fixed number of cores. ## Steps 1. Do nothing to use all cores: pdemagic runs its heavy loops on every core of the machine. 2. To use fewer, set the environment variable `RAYON_NUM_THREADS` before Python starts. 3. Run the script as before. The results do not depend on the number of threads. ## Example A script, `my_script.py`, with a solve that is large enough to share its work: ```python import sympy as sp import pdemagic as pm x, y, z = sp.symbols("x y z", real=True) phi = sp.Function("phi")(x, y, z) problem = pm.Problem( sp.Eq(pm.laplacian(phi), -sp.exp(-(x**2 + y**2 + z**2))), domain=pm.FreeSpace(x, y, z), ) solution = pm.solve(problem) print(solution[phi].shape) ``` ```text (64, 64, 64) ``` Run it on two threads: ```bash RAYON_NUM_THREADS=2 python my_script.py ``` ## Notes - That line is for the shells of Linux and macOS. On Windows, set the variable first: `set RAYON_NUM_THREADS=2` in the command prompt, or `$env:RAYON_NUM_THREADS = "2"` in PowerShell. - The variable is read once, when the first solve starts its threads. A script can set it itself before that solve, with `os.environ["RAYON_NUM_THREADS"] = "2"`. Later it has no effect. - The number of threads cannot be set from the plan. - Small problems do not use threads: sharing their work would cost more than it saves. - On a shape in the plane, the sparse factorization and the modes for problems in time use one core so far. - [The planner and its plan](../topics/planner.md) explains what a plan does hold, and [Known limits](../dev/limits.md) lists what bounds the size and the speed of a solve.