Skip to content

Commit dc28563

Browse files
committed
feat: starting to write launch_mapdl_on_cluster_locally
1 parent 15d4657 commit dc28563

File tree

3 files changed

+260
-35
lines changed

3 files changed

+260
-35
lines changed

src/ansys/mapdl/core/launcher/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838

3939

4040
from ansys.mapdl.core.launcher.console import launch_mapdl_console
41+
from ansys.mapdl.core.launcher.grpc import launch_mapdl_grpc
42+
from ansys.mapdl.core.launcher.hpc import launch_mapdl_on_cluster_locally
4143
from ansys.mapdl.core.launcher.launcher import launch_mapdl
4244
from ansys.mapdl.core.launcher.remote import connect_to_mapdl
4345
from ansys.mapdl.core.launcher.tools import (

src/ansys/mapdl/core/launcher/hpc.py

Lines changed: 257 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,11 @@
2929
from ansys.mapdl.core import LOG
3030
from ansys.mapdl.core.errors import MapdlDidNotStart
3131
from ansys.mapdl.core.launcher.grpc import launch_grpc
32-
from ansys.mapdl.core.launcher.local import processing_local_arguments
3332
from ansys.mapdl.core.launcher.tools import (
3433
generate_start_parameters,
3534
get_port,
3635
submitter,
3736
)
38-
from ansys.mapdl.core.licensing import LicenseChecker
3937
from ansys.mapdl.core.mapdl_grpc import MapdlGrpc
4038

4139
LAUNCH_ON_HCP_ERROR_MESSAGE_IP = (
@@ -272,7 +270,7 @@ def get_state_from_scontrol(stdout: str) -> str:
272270
return stdout.split("JobState=")[1].splitlines()[0].strip()
273271

274272

275-
def launch_mapdl_on_cluster(
273+
def launch_mapdl_on_cluster_locally(
276274
nproc: int,
277275
*,
278276
scheduler_options: Union[str, Dict[str, str]] = None,
@@ -291,6 +289,9 @@ def launch_mapdl_on_cluster(
291289
A string or dictionary specifying the job configuration for the
292290
scheduler. For example ``scheduler_options = "-N 10"``.
293291
292+
launch_mapdl_args : Dict[str, Any], optional
293+
Any keyword argument from the :func:`ansys.mapdl.core.launcher.grpc.launch_mapdl_grpc` function.
294+
294295
Returns
295296
-------
296297
MapdlGrpc
@@ -309,10 +310,18 @@ def launch_mapdl_on_cluster(
309310
)
310311
311312
"""
312-
from ansys.mapdl.core.launcher import launch_mapdl
313+
# from ansys.mapdl.core.launcher import launch_mapdl
313314

314315
# Processing the arguments
315316
launch_mapdl_args["launch_on_hpc"] = True
317+
launch_mapdl_args["running_on_hpc"] = True
318+
319+
if launch_mapdl_args.get("license_server_check", False):
320+
raise ValueError(
321+
"The argument 'license_server_check' is not allowed when launching on an HPC platform."
322+
)
323+
324+
launch_mapdl_args["license_server_check"] = False
316325

317326
if launch_mapdl_args.get("mode", "grpc") != "grpc":
318327
raise ValueError(
@@ -327,32 +336,254 @@ def launch_mapdl_on_cluster(
327336
"The 'start_instance' argument must be 'True' when launching on HPC."
328337
)
329338

330-
return launch_mapdl(
339+
if launch_mapdl_args.get("mapdl_output", False):
340+
raise ValueError(
341+
"The 'mapdl_output' argument is not allowed when launching on an HPC platform."
342+
)
343+
344+
return launch_mapdl_grpc_on_hpc(
331345
nproc=nproc,
332346
scheduler_options=scheduler_options,
333347
**launch_mapdl_args,
334348
)
335349

336350

337-
def launch_mapdl_grpc(): # to be fixed
338-
args = processing_local_arguments(locals())
351+
def launch_mapdl_grpc_on_hpc(
352+
*,
353+
exec_file: Optional[str] = None,
354+
run_location: Optional[str] = None,
355+
jobname: str = "file",
356+
nproc: Optional[int] = None,
357+
ram: Optional[Union[int, str]] = None,
358+
mode: Optional[str] = None,
359+
override: bool = False,
360+
loglevel: str = "ERROR",
361+
additional_switches: str = "",
362+
start_timeout: Optional[int] = None,
363+
port: Optional[int] = None,
364+
cleanup_on_exit: bool = True,
365+
start_instance: Optional[bool] = None,
366+
clear_on_connect: bool = True,
367+
log_apdl: Optional[Union[bool, str]] = None,
368+
remove_temp_dir_on_exit: bool = False,
369+
license_server_check: bool = False,
370+
license_type: Optional[bool] = None,
371+
print_com: bool = False,
372+
add_env_vars: Optional[Dict[str, str]] = None,
373+
replace_env_vars: Optional[Dict[str, str]] = None,
374+
version: Optional[Union[int, str]] = None,
375+
running_on_hpc: bool = True,
376+
launch_on_hpc: bool = False,
377+
**kwargs: Dict[str, Any],
378+
) -> MapdlGrpc:
379+
"""Start MAPDL locally with gRPC interface.
380+
381+
Parameters
382+
----------
383+
exec_file : str, optional
384+
The location of the MAPDL executable. Will use the cached
385+
location when left at the default :class:`None` and no environment
386+
variable is set.
387+
388+
The executable path can be also set through the environment variable
389+
:envvar:`PYMAPDL_MAPDL_EXEC`. For example:
390+
391+
.. code:: console
392+
393+
export PYMAPDL_MAPDL_EXEC=/ansys_inc/v211/ansys/bin/mapdl
394+
395+
run_location : str, optional
396+
MAPDL working directory. Defaults to a temporary working
397+
directory. If directory doesn't exist, one is created.
398+
399+
jobname : str, optional
400+
MAPDL jobname. Defaults to ``'file'``.
401+
402+
nproc : int, optional
403+
Number of processors. Defaults to ``2``. If running on an HPC cluster,
404+
this value is adjusted to the number of CPUs allocated to the job,
405+
unless the argument ``running_on_hpc`` is set to ``"false"``.
406+
407+
ram : float, optional
408+
Total size in megabytes of the workspace (memory) used for the initial
409+
allocation. The default is :class:`None`, in which case 2 GB (2048 MB) is
410+
used. To force a fixed size throughout the run, specify a negative
411+
number.
412+
413+
mode : str, optional
414+
Mode to launch MAPDL. Must be one of the following:
415+
416+
- ``'grpc'``
417+
- ``'console'``
418+
419+
The ``'grpc'`` mode is available on ANSYS 2021R1 or newer and
420+
provides the best performance and stability.
421+
The ``'console'`` mode is for legacy use only Linux only prior to 2020R2.
422+
This console mode is pending depreciation.
423+
Visit :ref:`versions_and_interfaces` for more information.
424+
425+
override : bool, optional
426+
Attempts to delete the lock file at the ``run_location``.
427+
Useful when a prior MAPDL session has exited prematurely and
428+
the lock file has not been deleted.
429+
430+
loglevel : str, optional
431+
Sets which messages are printed to the console. ``'INFO'``
432+
prints out all ANSYS messages, ``'WARNING'`` prints only
433+
messages containing ANSYS warnings, and ``'ERROR'`` logs only
434+
error messages.
435+
436+
additional_switches : str, optional
437+
Additional switches for MAPDL, for example ``'aa_r'``, the
438+
academic research license, would be added with:
439+
440+
- ``additional_switches="-aa_r"``
441+
442+
Avoid adding switches like ``-i``, ``-o`` or ``-b`` as these are already
443+
included to start up the MAPDL server. See the notes
444+
section for additional details.
445+
446+
start_timeout : float, optional
447+
Maximum allowable time to connect to the MAPDL server. By default it is
448+
45 seconds, however, it is increased to 90 seconds if running on HPC.
449+
450+
port : int
451+
Port to launch MAPDL gRPC on. Final port will be the first
452+
port available after (or including) this port. Defaults to
453+
``50052``. You can also provide this value through the environment variable
454+
:envvar:`PYMAPDL_PORT`. For instance ``PYMAPDL_PORT=50053``.
455+
However the argument (if specified) has precedence over the environment
456+
variable. If this environment variable is empty, it is as it is not set.
457+
458+
cleanup_on_exit : bool, optional
459+
Exit MAPDL when python exits or the mapdl Python instance is
460+
garbage collected.
461+
462+
start_instance : bool, optional
463+
When :class:`False`, connect to an existing MAPDL instance at ``ip``
464+
and ``port``, which default to ip ``'127.0.0.1'`` at port ``50052``.
465+
Otherwise, launch a local instance of MAPDL. You can also
466+
provide this value through the environment variable
467+
:envvar:`PYMAPDL_START_INSTANCE`.
468+
However the argument (if specified) has precedence over the environment
469+
variable. If this environment variable is empty, it is as it is not set.
470+
471+
clear_on_connect : bool, optional
472+
Defaults to :class:`True`, giving you a fresh environment when
473+
connecting to MAPDL. When if ``start_instance`` is specified
474+
it defaults to :class:`False`.
475+
476+
log_apdl : str, optional
477+
Enables logging every APDL command to the local disk. This
478+
can be used to "record" all the commands that are sent to
479+
MAPDL via PyMAPDL so a script can be run within MAPDL without
480+
PyMAPDL. This argument is the path of the output file (e.g.
481+
``log_apdl='pymapdl_log.txt'``). By default this is disabled.
482+
483+
remove_temp_dir_on_exit : bool, optional
484+
When ``run_location`` is :class:`None`, this launcher creates a new MAPDL
485+
working directory within the user temporary directory, obtainable with
486+
``tempfile.gettempdir()``. When this parameter is
487+
:class:`True`, this directory will be deleted when MAPDL is exited.
488+
Default to :class:`False`.
489+
If you change the working directory, PyMAPDL does not delete the original
490+
working directory nor the new one.
491+
492+
license_server_check : bool, optional
493+
Check if the license server is available if MAPDL fails to
494+
start. Only available on ``mode='grpc'``. Defaults :class:`False`.
495+
496+
license_type : str, optional
497+
Enable license type selection. You can input a string for its
498+
license name (for example ``'meba'`` or ``'ansys'``) or its description
499+
("enterprise solver" or "enterprise" respectively).
500+
You can also use legacy licenses (for example ``'aa_t_a'``) but it will
501+
also raise a warning. If it is not used (:class:`None`), no specific
502+
license will be requested, being up to the license server to provide a
503+
specific license type. Default is :class:`None`.
504+
505+
print_com : bool, optional
506+
Print the command ``/COM`` arguments to the standard output.
507+
Default :class:`False`.
508+
509+
add_env_vars : dict, optional
510+
The provided dictionary will be used to extend the MAPDL process
511+
environment variables. If you want to control all of the environment
512+
variables, use the argument ``replace_env_vars``.
513+
Defaults to :class:`None`.
514+
515+
replace_env_vars : dict, optional
516+
The provided dictionary will be used to replace all the MAPDL process
517+
environment variables. It replace the system environment variables
518+
which otherwise would be used in the process.
519+
To just add some environment variables to the MAPDL
520+
process, use ``add_env_vars``. Defaults to :class:`None`.
521+
522+
version : float, optional
523+
Version of MAPDL to launch. If :class:`None`, the latest version is used.
524+
Versions can be provided as integers (i.e. ``version=222``) or
525+
floats (i.e. ``version=22.2``).
526+
To retrieve the available installed versions, use the function
527+
:meth:`ansys.tools.path.path.get_available_ansys_installations`.
528+
You can also provide this value through the environment variable
529+
:envvar:`PYMAPDL_MAPDL_VERSION`.
530+
For instance ``PYMAPDL_MAPDL_VERSION=22.2``.
531+
However the argument (if specified) has precedence over the environment
532+
variable. If this environment variable is empty, it is as it is not set.
533+
534+
kwargs : dict, Optional
535+
These keyword arguments are interface-specific or for
536+
development purposes. For more information, see Notes.
537+
538+
scheduler_options : :class:`str`, :class:`dict`
539+
Use it to specify options to the scheduler run command. It can be a
540+
string or a dictionary with arguments and its values (both as strings).
541+
For more information visit :ref:`ref_hpc_slurm`.
542+
543+
set_no_abort : :class:`bool`
544+
*(Development use only)*
545+
Sets MAPDL to not abort at the first error within /BATCH mode.
546+
Defaults to :class:`True`.
547+
548+
force_intel : :class:`bool`
549+
*(Development use only)*
550+
Forces the use of Intel message pass interface (MPI) in versions between
551+
Ansys 2021R0 and 2022R2, where because of VPNs issues this MPI is
552+
deactivated by default.
553+
See :ref:`vpn_issues_troubleshooting` for more information.
554+
Defaults to :class:`False`.
555+
556+
Returns
557+
-------
558+
MapdlGrpc
559+
An instance of Mapdl.
560+
"""
561+
args = pack_arguments(locals()) # packs args and kwargs
562+
563+
check_kwargs(args) # check if passing wrong key arguments
564+
565+
pre_check_args(args)
566+
567+
if is_running_on_slurm(args):
568+
LOG.info("On Slurm mode.")
569+
570+
# extracting parameters
571+
get_slurm_options(args, kwargs)
572+
573+
get_start_instance_arg(args)
574+
575+
get_cpus(args)
576+
577+
get_ip(args)
578+
579+
args["port"] = get_port(args["port"], args["start_instance"])
580+
339581
if args.get("mode", "grpc") != "grpc":
340582
raise ValueError("Invalid 'mode'.")
341583
args["port"] = get_port(args["port"], args["start_instance"])
342584

343585
start_parm = generate_start_parameters(args)
344586

345-
# Early exit for debugging.
346-
if args["_debug_no_launch"]:
347-
# Early exit, just for testing
348-
return args # type: ignore
349-
350-
# Check the license server
351-
if args["license_server_check"]:
352-
LOG.debug("Checking license server.")
353-
lic_check = LicenseChecker(timeout=args["start_timeout"])
354-
lic_check.start()
355-
356587
########################################
357588
# Launch MAPDL with gRPC
358589
# ----------------------
@@ -387,27 +618,16 @@ def launch_mapdl_grpc(): # to be fixed
387618

388619
jobid: int = start_parm.get("jobid", "Not found")
389620

390-
if (
391-
args["launch_on_hpc"]
392-
and start_parm.get("finish_job_on_exit", True)
393-
and jobid not in ["Not found", None]
394-
):
621+
if start_parm.get("finish_job_on_exit", True) and jobid not in [
622+
"Not found",
623+
None,
624+
]:
395625

396626
LOG.debug(f"Killing HPC job with id: {jobid}")
397627
kill_job(jobid)
398628

399-
if args["license_server_check"]:
400-
LOG.debug("Checking license server.")
401-
lic_check.check()
402-
403629
raise exception
404630

405-
if args["just_launch"]:
406-
out = [args["ip"], args["port"]]
407-
if hasattr(process, "pid"):
408-
out += [process.pid]
409-
return out
410-
411631
########################################
412632
# Connect to MAPDL using gRPC
413633
# ---------------------------
@@ -425,7 +645,10 @@ def launch_mapdl_grpc(): # to be fixed
425645
)
426646

427647
except Exception as exception:
428-
LOG.error("An error occurred when connecting to MAPDL.")
648+
jobid = start_parm.get("jobid", "'Not found'")
649+
LOG.error(
650+
f"An error occurred when connecting to the MAPDL instance running on job {jobid}."
651+
)
429652
raise exception
430653

431654
return mapdl

src/ansys/mapdl/core/launcher/launcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ def launch_mapdl(
551551
#
552552
if args["start_instance"]:
553553
# ON HPC:
554-
# Assuming that if login node is ubuntu, the computation ones
554+
# Assuming that if login node is ubuntu, the computation nodes
555555
# are also ubuntu.
556556
env_vars = configure_ubuntu(env_vars)
557557

0 commit comments

Comments
 (0)