Skip to content

Commit

Permalink
feat: adding 'pymapdl_nproc' to non-slurm runs (#3487)
Browse files Browse the repository at this point in the history
* feat: adding 'pymapdl_proc' to non-slurm run. Adding tests too.

* chore: adding changelog file 3487.miscellaneous.md [dependabot-skip]

---------

Co-authored-by: pyansys-ci-bot <[email protected]>
  • Loading branch information
germa89 and pyansys-ci-bot authored Oct 17, 2024
1 parent 71db67d commit a0fb521
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/changelog.d/3487.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
feat: adding 'pymapdl_nproc' to non-slurm runs
18 changes: 11 additions & 7 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2330,20 +2330,24 @@ def get_cpus(args: Dict[str, Any]):

# Bypassing number of processors checks because VDI/VNC might have
# different number of processors than the cluster compute nodes.
# Also the CPUs are set in `get_slurm_options`
if args["ON_SLURM"]:
return

# Setting number of processors
machine_cores = psutil.cpu_count(logical=False)

# Some machines only have 1 core
min_cpus = machine_cores if machine_cores < 2 else 2

if not args["nproc"]:
# Some machines only have 1 core
args["nproc"] = machine_cores if machine_cores < 2 else 2
else:
if machine_cores < int(args["nproc"]):
raise NotEnoughResources(
f"The machine has {machine_cores} cores. PyMAPDL is asking for {args['nproc']} cores."
)
# Check the env var `PYMAPDL_NPROC`
args["nproc"] = int(os.environ.get("PYMAPDL_NPROC", min_cpus))

if machine_cores < int(args["nproc"]):
raise NotEnoughResources(
f"The machine has {machine_cores} cores. PyMAPDL is asking for {args['nproc']} cores."
)


def remove_err_files(run_location, jobname):
Expand Down
33 changes: 33 additions & 0 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
force_smp_in_student,
generate_mapdl_launch_command,
generate_start_parameters,
get_cpus,
get_exec_file,
get_run_location,
get_slurm_options,
Expand Down Expand Up @@ -1112,3 +1113,35 @@ def test_launch_grpc(tmpdir):
assert isinstance(kwags["stdin"], type(subprocess.DEVNULL))
assert isinstance(kwags["stdout"], type(subprocess.PIPE))
assert isinstance(kwags["stderr"], type(subprocess.PIPE))


@patch("psutil.cpu_count", lambda *args, **kwags: 5)
@pytest.mark.parametrize("arg", [None, 3, 10])
@pytest.mark.parametrize("env", [None, 3, 10])
def test_get_cpus(monkeypatch, arg, env):
if env:
monkeypatch.setenv("PYMAPDL_NPROC", env)

context = NullContext()
cores_machine = psutil.cpu_count(logical=False) # it is patched

if (arg and arg > cores_machine) or (arg is None and env and env > cores_machine):
context = pytest.raises(NotEnoughResources)

args = {"nproc": arg, "ON_SLURM": False}
with context:
get_cpus(args)

if arg:
assert args["nproc"] == arg
elif env:
assert args["nproc"] == env
else:
assert args["nproc"] == 2


@patch("psutil.cpu_count", lambda *args, **kwags: 1)
def test_get_cpus_min():
args = {"nproc": None, "ON_SLURM": False}
get_cpus(args)
assert args["nproc"] == 1

0 comments on commit a0fb521

Please sign in to comment.