diff --git a/doc/src/Library_config.rst b/doc/src/Library_config.rst index a5b87e23a7..04b12f35de 100644 --- a/doc/src/Library_config.rst +++ b/doc/src/Library_config.rst @@ -15,6 +15,8 @@ This section documents the following functions: - :cpp:func:`lammps_config_package_count` - :cpp:func:`lammps_config_package_name` - :cpp:func:`lammps_config_accelerator` +- :cpp:func:`lammps_has_gpu_device` +- :cpp:func:`lammps_gpu_device_info` - :cpp:func:`lammps_has_style` - :cpp:func:`lammps_style_count` - :cpp:func:`lammps_style_name` @@ -132,6 +134,16 @@ approach. ----------------------- +.. doxygenfunction:: lammps_has_gpu_device + :project: progguide + +----------------------- + +.. doxygenfunction:: lammps_get_gpu_device_info + :project: progguide + +----------------------- + .. doxygenfunction:: lammps_has_style :project: progguide diff --git a/python/lammps/core.py b/python/lammps/core.py index 8610e85811..6761f9502c 100644 --- a/python/lammps/core.py +++ b/python/lammps/core.py @@ -281,6 +281,7 @@ class lammps(object): self.lib.lammps_version.argtypes = [c_void_p] self.lib.lammps_get_os_info.argtypes = [c_char_p, c_int] + self.lib.lammps_get_gpu_device_info.argtypes = [c_char_p, c_int] self.lib.lammps_get_mpi_comm.argtypes = [c_void_p] @@ -490,7 +491,7 @@ class lammps(object): sb = create_string_buffer(512) self.lib.lammps_get_os_info(sb,512) - return sb + return sb.value.decode() # ------------------------------------------------------------------------- @@ -1552,6 +1553,37 @@ class lammps(object): # ------------------------------------------------------------------------- + @property + def has_gpu_device(self): + """ Availability of GPU package compatible device + + This is a wrapper around the :cpp:func:`lammps_has_gpu_device` + function of the C library interface. + + :return: True if a GPU package compatible device is present, otherwise False + :rtype: bool + """ + return self.lib.lammps_has_gpu_device() != 0 + + # ------------------------------------------------------------------------- + + def get_gpu_device_info(self): + """Return a string with detailed information about any devices that are + usable by the GPU package. + + This is a wrapper around the :cpp:func:`lammps_get_gpu_device_info` + function of the C-library interface. + + :return: GPU device info string + :rtype: string + """ + + sb = create_string_buffer(8192) + self.lib.lammps_get_gpu_device_info(sb,8192) + return sb.value.decode() + + # ------------------------------------------------------------------------- + @property def installed_packages(self): """ List of the names of enabled packages in the LAMMPS shared library diff --git a/src/library.cpp b/src/library.cpp index 8677b30079..2e2f7a3d0d 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -4111,8 +4111,10 @@ int lammps_version(void *handle) The :cpp:func:`lammps_get_os_info` function can be used to retrieve detailed information about the hosting operating system and compiler/runtime. + A suitable buffer for a C-style string has to be provided and its length. -If the assembled text will be truncated to not overflow this buffer. +The assembled text will be truncated to not overflow this buffer. The +string is typically a few hundred bytes long. .. versionadded:: 9Oct2020 @@ -4339,6 +4341,60 @@ int lammps_config_accelerator(const char *package, return Info::has_accelerator_feature(package,category,setting) ? 1 : 0; } +/** Check for presence of a viable GPU package device + * +\verbatim embed:rst + +The :cpp:func:`lammps_has_gpu_device` function checks at runtime if +an accelerator device is present that can be used with the +:doc:`GPU package `. If at least one suitable device is +present the function will return 1, otherwise 0. + +More detailed information about the available device or devices can +be obtained by calling the +:cpp:func:`lammps_get_gpu_device_info` function. + +.. versionadded:: 14May2021 + +\endverbatim + * + * \return 1 if viable device is available, 0 if not. */ + +int lammps_has_gpu_device() +{ + return Info::has_gpu_device() ? 1: 0; +} + +/** Get GPU package device information + * +\verbatim embed:rst + +The :cpp:func:`lammps_get_gpu_device_info` function can be used to retrieve +detailed information about any accelerator devices that are viable for use +with the :doc:`GPU package `. It will produce a string that is +equivalent to the output of the ``nvc_get_device`` or ``ocl_get_device`` or +``hip_get_device`` tools that are compiled alongside LAMMPS if the GPU +package is enabled. + +A suitable buffer for a C-style string has to be provided and its length. +The assembled text will be truncated to not overflow this buffer. This +string can be several kilobytes long, if multiple devices are present. + +.. versionadded:: 14May2021 + +\endverbatim + * + * \param buffer string buffer to copy the information to + * \param buf_size size of the provided string buffer */ + +void lammps_get_gpu_device_info(char *buffer, int buf_size) +{ + if (buf_size <= 0) return; + buffer[0] = buffer[buf_size-1] = '\0'; + std::string devinfo = Info::get_gpu_device_info(); + strncpy(buffer, devinfo.c_str(), buf_size-1); +} + /* ---------------------------------------------------------------------- */ /** Check if a specific style has been included in LAMMPS diff --git a/src/library.h b/src/library.h index 4cfaf2ce44..a199c86652 100644 --- a/src/library.h +++ b/src/library.h @@ -196,6 +196,8 @@ int lammps_config_package_count(); int lammps_config_package_name(int, char *, int); int lammps_config_accelerator(const char *, const char *, const char *); +int lammps_has_gpu_device(); +void lammps_get_gpu_device_info(char *buffer, int buf_size); int lammps_has_style(void *, const char *, const char *); int lammps_style_count(void *, const char *); diff --git a/tools/swig/lammps.i b/tools/swig/lammps.i index 986fd5cad3..ec37120c07 100644 --- a/tools/swig/lammps.i +++ b/tools/swig/lammps.i @@ -116,6 +116,8 @@ extern int lammps_config_has_package(const char *); extern int lammps_config_package_count(); extern int lammps_config_package_name(int, char *, int); extern int lammps_config_accelerator(const char *, const char *, const char *); +extern int lammps_has_gpu_device(); +extern void lammps_get_gpu_device_info(char *buffer, int buf_size); extern int lammps_has_style(void *, const char *, const char *); extern int lammps_style_count(void *, const char *); extern int lammps_style_name(void *, const char *, int, char *buffer, int buf_size); @@ -236,6 +238,8 @@ extern int lammps_config_has_package(const char *); extern int lammps_config_package_count(); extern int lammps_config_package_name(int, char *, int); extern int lammps_config_accelerator(const char *, const char *, const char *); +extern int lammps_has_gpu_device(); +extern void lammps_get_gpu_device_info(char *buffer, int buf_size); extern int lammps_has_style(void *, const char *, const char *); extern int lammps_style_count(void *, const char *); extern int lammps_style_name(void *, const char *, int, char *buffer, int buf_size);