VASPml library: Difference between revisions

From VASP Wiki
No edit summary
Line 76: Line 76:
* <code>CPP_OPTIONS</code>: Specific build options, same as in {{VASP}}'s <code>makefile.include</code>. Only options starting with <code>-DVASPML_</code> are relevant. Do not use any of <code>-DMPI -D_OPENMP -DHOST</code> here as this may result in a compile error.
* <code>CPP_OPTIONS</code>: Specific build options, same as in {{VASP}}'s <code>makefile.include</code>. Only options starting with <code>-DVASPML_</code> are relevant. Do not use any of <code>-DMPI -D_OPENMP -DHOST</code> here as this may result in a compile error.


There are more variables present in the template files which usually do not require changes:
Usually, these are all the variables requiring changes for a successful standalone build of the VASPml library. However, there are more variables present in the template files for more fine-grained control and other purposes:


* <code>LD</code>: Specifies the linker to use when building applications linked to <code>libvaspml</code>. Usually the same as the compiler <code>CXX</code>.
* <code>LD</code>: Specifies the linker to use when building applications linked to <code>libvaspml</code>. Usually the same as the compiler <code>CXX</code>.
* <code>LDFLAGS</code>: Flags in addition to <code>CXXFLAGS</code> to pass to the linker. This is where the flags for BLAS and LAPACK are added.
* <code>LDFLAGS</code>: Flags in addition to <code>CXXFLAGS</code> to pass to the linker. This is where the flags for BLAS and LAPACK are added.
* <code>CPP_DEP</code>:
* <code>CPP_DEP</code>: Dependency generator, usually the same as the compiler <code>CXX</code>.
* <code>AR</code>:
* <code>AR</code>: Executable generating archives ("*.a" files) from multiple object files. Used to generate the static library <code>libvaspml.a</code> from the compiled object files.
* <code>ARFLAGS</code>:
* <code>ARFLAGS</code>: Flags passed to the archiver <code>AR</code>.
* <code>BOOST_ROOT</code>:
 
* <code>BOOST_INCLUDE</code>:
Some additional variables are used only in case the internal testsuite of VASPml should be compiled and executed:
* <code>BOOST_LIB</code>:
 
* <code>BOOST_ROOT</code>: If not already defined, you may specify the base path to the Boost.test library here.
* <code>BOOST_INCLUDE</code>: Include directory for Boost.test library (must contain the files <code>boost/test/unit_test.hpp</code>, <code>boost/test/data/test_case.hpp</code> and <code>boost/test/data/monomorphic.hpp</code>).
* <code>BOOST_LIB</code>: Flags required for linking the Boost.test library <code>libboost_unit_test_framework</code>.


[[Category:Machine-learned force fields]]
[[Category:Machine-learned force fields]]

Revision as of 11:19, 18 February 2025

VASPml is a C++ library accompanying VASP, providing functionality related to machine-learned force fields. It is supposed to extend, and eventually replace, the original Fortran machine learning code inside VASP. Currently, it does not yet offer any training capabilities but rather focuses on inference. At this point VASPml is in a beta-testing stage and provides its first application, an interface to the popular molecular dynamics (MD) software LAMMPS. This allows users to combine VASP-generated machine-learned force fields with the large amount of MD-related features provided by LAMMPS, some of which may not be offered in VASP directly.

Warning: As of VASP 6.5.0 the VASPml library is experimental and results should be carefully checked against the standard Fortran code (compile without -Dlibvaspml or set ML_LIB = .FALSE.).

Supported features

If VASP is compiled with the VASPml library and a requested feature is supported by both, the original Fortran code and the C++ VASPml implementation, then the latter code path is used by default. To override this behavior and explicitly avoid the use of the VASPml library set ML_LIB = .FALSE. in the INCAR file.

Restrictions

Since the VASPml library is still under development some features of the original Fortran code are not yet available:

  • No machine learning related file output (e.g. ML_LOGFILE)
Tip: For running the fast prediction-only mode in VASP there is currently no performance gain from the VASPml library. Hence, if file output is important (e.g. when monitoring the spilling factor) we recommend using the original Fortran code (ML_LIB = .FALSE.).

Due to the experimental nature of the VASPml library some bugs may slip through our testing. Until VASPml can be considered stable already identified bugs and workarounds will be collected here:

  • VASPml from VASP 6.5.0 is unable to read in ML_FFs created with VASP 6.5.0. Force fields created from older versions (VASP 6.4.3 and before) work fine (this issue will be fixed in VASP 6.5.1). A simple workaround which circumvents the broken version check requires a minimal source code change: in /path/to/vasp/src/vaspml/src/libvaspml/IoHandlerML_FF.hpp line 169 increase the maximum allowed ML_FF version to 0.2.4:
const SemanticVersion versionMax = SemanticVersion(0, 2, 4);
After complete recompilation of VASP the newer force fields should also be accepted. This bug and workaround also apply to the LAMMPS interface. In case the workaround is applied please also recompile LAMMPS.

Dependencies

The VASPml library depends on the following compilers and external libraries:

  • C++ compiler supporting the C++17 language standard
  • MPI
  • BLAS and the corresponding C interface CBLAS
  • LAPACK and the corresponding C interface LAPACKE

These requirements are usually already covered by the VASP requirements.

Build instructions

The VASPml library is automatically built alongside VASP if -Dlibvaspml is added to the CPP_OPTIONS precompiler option in the makefile.include file. In addition, a few more compiler settings regarding the C++ compiler, include paths and VASPml options may be required. The makefile.include templates provided in VASP's arch directory contain pre-filled blocks corresponding to the VASPml build. Uncomment the VASPml-related lines and fill with values according to your toolchain. For example, when using the GCC toolchain with OpenBLAS the makefile.include section may look like this:

...
# For machine learning library vaspml (experimental)
CPP_OPTIONS += -Dlibvaspml
CPP_OPTIONS += -DVASPML_USE_CBLAS
#CPP_OPTIONS += -DVASPML_DEBUG_LEVEL=3
CXX_ML      = mpic++
CXXFLAGS_ML = -O3 -std=c++17 -pedantic-errors -Wall -Wextra
INCLUDE_ML  = -I$(OPENBLAS_ROOT)/include
...

Apart from the mandatory -Dlibvaspml flag there are the following possible CPP_OPTIONS:

  • -DVASPML_USE_CBLAS: Use CBLAS (C interface for BLAS routines) for linear algebra. This is the default and should always be used.
  • -DVASPML_DEBUG_LEVEL=[0|1|2|3]: If set to 1, 2 or 3 enables various sanity checks during runtime with low, medium and high impact on performance, respectively. Setting it to 0 or omitting the flag disables runtime checks.
Mind: Do not use this flag for production runs as it may decrease performance.
  • -DVASPML_USE_MKL: Use Intel MKL for linear algebra (must be used in combination with -DVASPML_USE_CBLAS).

In addition, VASPml requires to set its own compiler, flags and include path:

  • CXX_ML: This should be a C++17-compatible C++ compiler with MPI support (usually an MPI wrapper corresponding to the selected toolchain, e.g. mpic++, mpicxx, mpicpx or mpinc++).
  • CXXFLAGS_ML: Specifies the flags for the C++ compiler. Typically, here the optimization level (-O3) and the compliance with C++17 is specified.
  • INCLUDE_ML: Include flags for the required dependencies should be added here.
Tip: For some toolchains it is not necessary to explicitly add paths here because the compilers automatically include the correct directories (e.g. Intel oneAPI, NVHPC). In other cases (e.g. GNU compiler with openBLAS) the given path must contain the desired C++ headers of the dependencies:
  • CBLAS: cblas.h
  • LAPACKE: lapacke.h

The VASPml project (source code and related files) is located within the src/vaspml directory relative to the VASP root folder. Upon compilation it is copied to the build/std, build/gam and/or build/ncl build folders, just like all other VASP sources. If the VASPml library was successfully compiled libvaspml.a will be located in build/std/vaspml/lib/ (similarly for the gam and ncl versions). However, it is usually not necessary to check its presence because the VASP build will handle this (and fail if VASPml cannot be built).

Standalone build instructions

Warning: This part of the documentation is incomplete... work in progress!

Although VASPml is typically part of the VASP build process as described above it is also possible to compile it independently. This can be useful, for example, if VASP is deliberately compiled without VASPml but the libvaspml library is still necessary to build LAMMPS with VASPml patch. For a standalone build of VASPml follow these steps:

First, copy the entire vaspml subdirectory to a separate location and move into the new vaspml directory:

cp -r /path/to/vasp/src/vaspml /some/other/location/
cd /some/other/location/vaspml

Inside you will find a similar folder structure as in VASP. For example, the source code is located in src and will be copied and compiled in the build directory. Also, similar to VASP also VASPml requires to enter compiler details and library paths into a file named makefile.include before the standalone build process can be started. The arch folder contains template files for typical compiler toolchains.

Mind: The makefile.include files inside VASPml's arch directory are only required for the standalone build described here. For a regular VASP build they must not be used because then compilers and flags are taken from VASP's makefile.include (for this purpose, the special file makefile.include.vasp is used automatically).

Pick one of the makefile.include files in the arch which is closest to your toolchain and copy it to the base directory, e.g.

cp arch/makefile.include.gnu makefile.include

Next, modify the file according to your actual compiler and library paths. The most important variables here have a corresponding version in VASP's makefile.include, please also review the regular VASP build instructions for details:

  • CXX: C++17-compatible C++ compiler with MPI support, corresponds to CXX_ML in VASP's makefile.include
  • CXXFLAGS: Flags for the C++ compiler, corresponds to CXXFLAGS_ML + VASP_TARGET_CPU in VASP's makefile.include.
  • INCLUDE: Include flags for the required dependencies, corresponds to INCLUDE_ML in VASP's makefile.include.
  • CPP_OPTIONS: Specific build options, same as in VASP's makefile.include. Only options starting with -DVASPML_ are relevant. Do not use any of -DMPI -D_OPENMP -DHOST here as this may result in a compile error.

Usually, these are all the variables requiring changes for a successful standalone build of the VASPml library. However, there are more variables present in the template files for more fine-grained control and other purposes:

  • LD: Specifies the linker to use when building applications linked to libvaspml. Usually the same as the compiler CXX.
  • LDFLAGS: Flags in addition to CXXFLAGS to pass to the linker. This is where the flags for BLAS and LAPACK are added.
  • CPP_DEP: Dependency generator, usually the same as the compiler CXX.
  • AR: Executable generating archives ("*.a" files) from multiple object files. Used to generate the static library libvaspml.a from the compiled object files.
  • ARFLAGS: Flags passed to the archiver AR.

Some additional variables are used only in case the internal testsuite of VASPml should be compiled and executed:

  • BOOST_ROOT: If not already defined, you may specify the base path to the Boost.test library here.
  • BOOST_INCLUDE: Include directory for Boost.test library (must contain the files boost/test/unit_test.hpp, boost/test/data/test_case.hpp and boost/test/data/monomorphic.hpp).
  • BOOST_LIB: Flags required for linking the Boost.test library libboost_unit_test_framework.