Skip to content

Commit

Permalink
Checking number of processors before launching. (#2616)
Browse files Browse the repository at this point in the history
  • Loading branch information
germa89 authored Jan 5, 2024
1 parent 4c2779a commit b691f66
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 64 deletions.
36 changes: 2 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -537,24 +537,8 @@ jobs:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}

- name: "Getting files change filters"
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
workflows:
- '.github/workflows/**'
- name: "Setup Python with cache"
- name: "Setup Python"
uses: actions/setup-python@v5
if: steps.changes.outputs.workflows != 'true'
with:
cache: 'pip'
python-version: ${{ env.MAIN_PYTHON_VERSION }}

- name: "Setup Python without cache"
uses: actions/setup-python@v5
if: steps.changes.outputs.workflows == 'true'
with:
python-version: ${{ env.MAIN_PYTHON_VERSION }}

Expand Down Expand Up @@ -680,24 +664,8 @@ jobs:
sudo apt-get update
sudo apt-get install -y libgomp1
- name: "Getting files change filters"
uses: dorny/paths-filter@v2
id: changes
with:
filters: |
workflows:
- '.github/workflows/**'
- name: "Setup Python with cache"
- name: "Setup Python"
uses: actions/setup-python@v5
if: steps.changes.outputs.workflows != 'true'
with:
cache: 'pip'
python-version: ${{ env.MAIN_PYTHON_VERSION }}

- name: "Setup Python without cache"
uses: actions/setup-python@v5
if: steps.changes.outputs.workflows == 'true'
with:
python-version: ${{ env.MAIN_PYTHON_VERSION }}

Expand Down
11 changes: 11 additions & 0 deletions src/ansys/mapdl/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ def __init__(self, msg="MAPDL has exited"):
RuntimeError.__init__(self, msg)


class NotEnoughResources(MapdlExitedError):
"""Raised when MAPDL has exited"""

def __init__(
self,
msg="MAPDL has exited because there is not enough resources ({resource})",
resource="CPUs",
):
MapdlExitedError.__init__(self, msg.format(resource=resource))


class LockFileException(RuntimeError):
"""Error message when the lockfile has not been removed"""

Expand Down
81 changes: 51 additions & 30 deletions src/ansys/mapdl/core/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
LockFileException,
MapdlDidNotStart,
MapdlRuntimeError,
NotEnoughResources,
PortAlreadyInUse,
PortAlreadyInUseByAnMAPDLInstance,
VersionError,
Expand Down Expand Up @@ -248,9 +249,17 @@ def is_ansys_process(proc: psutil.Process) -> bool:
def get_process_at_port(port) -> Optional[psutil.Process]:
"""Get the process (psutil.Process) running at the given port"""
for proc in psutil.process_iter():
for conns in proc.connections(kind="inet"):
try:
connections = proc.connections(
kind="inet"
) # just to check if we can access the
except psutil.AccessDenied:
continue

for conns in connections:
if conns.laddr.port == port:
return proc

return None


Expand Down Expand Up @@ -510,10 +519,11 @@ def launch_grpc(
else:
if port_in_use(port):
proc = get_process_at_port(port)
if is_ansys_process(proc):
raise PortAlreadyInUseByAnMAPDLInstance
else:
raise PortAlreadyInUse
if proc:
if is_ansys_process(proc):
raise PortAlreadyInUseByAnMAPDLInstance
else:
raise PortAlreadyInUse

pymapdl._LOCAL_PORTS.append(port)

Expand Down Expand Up @@ -1030,31 +1040,31 @@ def _force_smp_student_version(add_sw, exec_path):


def launch_mapdl(
exec_file=None,
run_location=None,
jobname="file",
nproc=2,
ram=None,
mode=None,
override=False,
loglevel="ERROR",
additional_switches="",
start_timeout=45,
port=None,
cleanup_on_exit=True,
start_instance=None,
ip=None,
clear_on_connect=True,
log_apdl=None,
remove_temp_files=None,
remove_temp_dir_on_exit=False,
verbose_mapdl=None,
license_server_check=True,
license_type=None,
print_com=False,
add_env_vars=None,
replace_env_vars=None,
version=None,
exec_file: Optional[str] = None,
run_location: Optional[str] = None,
jobname: str = "file",
nproc: Optional[int] = None,
ram: Optional[Union[int, str]] = None,
mode: Optional[str] = None,
override: bool = False,
loglevel: str = "ERROR",
additional_switches: str = "",
start_timeout: int = 45,
port: Optional[int] = None,
cleanup_on_exit: bool = True,
start_instance: Optional[bool] = None,
ip: Optional[str] = None,
clear_on_connect: bool = True,
log_apdl: Optional[Union[bool, str]] = None,
remove_temp_files: Optional[bool] = None,
remove_temp_dir_on_exit: bool = False,
verbose_mapdl: Optional[bool] = None,
license_server_check: bool = True,
license_type: Optional[bool] = None,
print_com: bool = False,
add_env_vars: Optional[Dict[str, str]] = None,
replace_env_vars: Optional[Dict[str, str]] = None,
version: Optional[Union[int, str]] = None,
**kwargs,
) -> Union[MapdlGrpc, "MapdlConsole"]:
"""Start MAPDL locally.
Expand Down Expand Up @@ -1668,6 +1678,17 @@ def launch_mapdl(
additional_switches = _check_license_argument(license_type, additional_switches)
LOG.debug(f"Using additional switches {additional_switches}.")

# Setting number of processors
machine_cores = psutil.cpu_count(logical=False)
if not nproc:
if machine_cores < 2: # default required cores
nproc = machine_cores # to avoid starting issues
else:
nproc = 2
else:
if machine_cores < int(nproc):
raise NotEnoughResources

start_parm.update(
{
"exec_file": exec_file,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import tempfile
from time import sleep

import psutil
import pytest

from ansys.mapdl import core as pymapdl
from ansys.mapdl.core.errors import (
LicenseServerConnectionError,
MapdlDidNotStart,
NotEnoughResources,
PortAlreadyInUseByAnMAPDLInstance,
)
from ansys.mapdl.core.launcher import (
Expand Down Expand Up @@ -519,3 +521,10 @@ def test_launched(mapdl):
def test_launching_on_busy_port(mapdl):
with pytest.raises(PortAlreadyInUseByAnMAPDLInstance):
launch_mapdl(port=mapdl.port)


@requires("local")
def test_cpu_checks():
machine_cores = psutil.cpu_count(logical=False)
with pytest.raises(NotEnoughResources):
launch_mapdl(nproc=machine_cores + 2)

0 comments on commit b691f66

Please sign in to comment.