From df08818fef3b4c401152e7568a5eaadfd930ff58 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 11 Jun 2025 02:52:28 -0400 Subject: [PATCH] refactor handling of plugin loading to make plugin a global property --- doc/src/Fortran.rst | 15 +- doc/src/Library_create.rst | 6 + doc/src/plugin.rst | 38 +++- examples/COUPLE/plugin/liblammpsplugin.c | 1 + examples/COUPLE/plugin/liblammpsplugin.h | 1 + fortran/lammps.f90 | 10 +- python/lammps/core.py | 7 +- src/PLUGIN/plugin.cpp | 222 ++++++++++++++++++++--- src/PLUGIN/plugin.h | 2 + src/lammps.cpp | 8 +- src/library.cpp | 39 +++- src/library.h | 1 + src/main.cpp | 1 + src/update.cpp | 7 + tools/swig/lammps.i | 2 + 15 files changed, 305 insertions(+), 55 deletions(-) diff --git a/doc/src/Fortran.rst b/doc/src/Fortran.rst index 0a8434f63d..3d61473068 100644 --- a/doc/src/Fortran.rst +++ b/doc/src/Fortran.rst @@ -69,10 +69,11 @@ statement. Internally, it will call either :cpp:func:`lammps_open_fortran` or :cpp:func:`lammps_open_no_mpi` from the C library API to create the class instance. All arguments are optional and :cpp:func:`lammps_mpi_init` will be called automatically -if it is needed. Similarly, a possible call to -:cpp:func:`lammps_mpi_finalize` is integrated into the :f:func:`close` -function and triggered with the optional logical argument set to -``.TRUE.``. Here is a simple example: +if it is needed. Similarly, optional calls to +:cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_kokkos_finalize`, +:cpp:func:`lammps_python_finalize`, and :cpp:func:`lammps_plugin_finalize` +are integrated into the :f:func:`close` function and triggered with the +optional logical argument set to ``.TRUE.``. Here is a simple example: .. code-block:: fortran @@ -521,8 +522,8 @@ Procedures Bound to the :f:type:`lammps` Derived Type This method will close down the LAMMPS instance through calling :cpp:func:`lammps_close`. If the *finalize* argument is present and has a value of ``.TRUE.``, then this subroutine also calls - :cpp:func:`lammps_kokkos_finalize` and - :cpp:func:`lammps_mpi_finalize`. + :cpp:func:`lammps_kokkos_finalize`, :cpp:func:`lammps_mpi_finalize`, + :cpp:func:`lammps_python_finalize`, and :cpp:func:`lammps_plugin_finalize`. :o finalize: shut down the MPI environment of the LAMMPS library if ``.TRUE.``. @@ -530,6 +531,8 @@ Procedures Bound to the :f:type:`lammps` Derived Type :to: :cpp:func:`lammps_close` :to: :cpp:func:`lammps_mpi_finalize` :to: :cpp:func:`lammps_kokkos_finalize` + :to: :cpp:func:`lammps_python_finalize` + :to: :cpp:func:`lammps_plugin_finalize` -------- diff --git a/doc/src/Library_create.rst b/doc/src/Library_create.rst index 546db9b3be..5566b04e9b 100644 --- a/doc/src/Library_create.rst +++ b/doc/src/Library_create.rst @@ -11,6 +11,7 @@ This section documents the following functions: - :cpp:func:`lammps_mpi_finalize` - :cpp:func:`lammps_kokkos_finalize` - :cpp:func:`lammps_python_finalize` +- :cpp:func:`lammps_plugin_finalize` - :cpp:func:`lammps_error` -------------------- @@ -119,5 +120,10 @@ calling program. ----------------------- +.. doxygenfunction:: lammps_plugin_finalize + :project: progguide + +----------------------- + .. doxygenfunction:: lammps_error :project: progguide diff --git a/doc/src/plugin.rst b/doc/src/plugin.rst index 83a20b3248..a6ce845309 100644 --- a/doc/src/plugin.rst +++ b/doc/src/plugin.rst @@ -10,7 +10,7 @@ Syntax plugin command args -* command = *load* or *unload* or *list* or *clear* +* command = *load* or *unload* or *list* or *clear* or *restore* * args = list of arguments for a particular plugin command .. parsed-literal:: @@ -20,6 +20,7 @@ Syntax *style* = *pair* or *bond* or *angle* or *dihedral* or *improper* or *kspace* or *compute* or *fix* or *region* or *command* or *run* or *min* *list* = print a list of currently loaded plugins *clear* = unload all currently loaded plugins + *restore* = restore all loaded plugins Examples """""""" @@ -31,6 +32,7 @@ Examples plugin unload command hello plugin list plugin clear + plugin restore Description """"""""""" @@ -40,22 +42,46 @@ commands into a LAMMPS binary from so-called dynamic shared object (DSO) files. This enables to add new functionality to an existing LAMMPS binary without having to recompile and link the entire executable. +.. admonition:: Plugins are a global, per-executable property + :class: Hint + + Unlike most settings in LAMMPS, plugins are a per-executable global + property. Loading a plugin means that it is not only available for + the current LAMMPS instance but for all *future* LAMMPS instances. + + After a :doc:`clear ` command, all currently loaded plugins + will be restored and do not need to be loaded again. + + When using the library interface or the Python or Fortran module + to create multiple concurrent LAMMPS instances, all plugins should + be loaded by the first created LAMMPS instance as all future instances + will inherit them. To import plugins that were loaded by a different + LAMMPS instance, use the *restore* command. + + The *load* command will load and initialize all plugins contained in the -plugin DSO with the given filename. A message with information the -plugin style and name and more will be printed. Individual DSO files -may contain multiple plugins. More details about how to write and +plugin DSO with the given filename. A message with information about +the plugin style and name and more will be printed. Individual DSO +files may contain multiple plugins. If a plugin is already loaded +it will be skipped. More details about how to write and compile the plugin DSO is given in programmer's guide part of the manual under :doc:`Developer_plugins`. The *unload* command will remove the given style or the given name from the list of available styles. If the plugin style is currently in use, -that style instance will be deleted. +that style instance will be deleted and replaced by the default setting +for that style. The *list* command will print a list of the loaded plugins and their styles and names. The *clear* command will unload all currently loaded plugins. +.. versionadded:: TBD + +The *restore* command will restore all currently loaded plugins. +This allows to "import" plugins into a different LAMMPS instance. + .. admonition:: Automatic loading of plugins :class: note @@ -79,7 +105,7 @@ If plugins access functions or classes from a package, LAMMPS must have been compiled with that package included. Plugins are dependent on the LAMMPS binary interface (ABI) -and particularly the MPI library used. So they are not guaranteed +and particularly the MPI library used. So they are not guaranteed to work when the plugin was compiled with a different MPI library or different compilation settings or a different LAMMPS version. There are no checks, so if there is a mismatch the plugin object diff --git a/examples/COUPLE/plugin/liblammpsplugin.c b/examples/COUPLE/plugin/liblammpsplugin.c index 5003b3826b..21f330df9e 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.c +++ b/examples/COUPLE/plugin/liblammpsplugin.c @@ -79,6 +79,7 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib) ADDSYM(mpi_finalize); ADDSYM(kokkos_finalize); ADDSYM(python_finalize); + ADDSYM(plugin_finalize); ADDSYM(error); ADDSYM(expand); diff --git a/examples/COUPLE/plugin/liblammpsplugin.h b/examples/COUPLE/plugin/liblammpsplugin.h index 73cef7bc19..637f3db79b 100644 --- a/examples/COUPLE/plugin/liblammpsplugin.h +++ b/examples/COUPLE/plugin/liblammpsplugin.h @@ -134,6 +134,7 @@ struct _liblammpsplugin { void (*mpi_finalize)(); void (*kokkos_finalize)(); void (*python_finalize)(); + void (*plugin_finalize)(); void (*error)(void *, int, const char *); char *(*expand)(void *, const char *); diff --git a/fortran/lammps.f90 b/fortran/lammps.f90 index 4baff4d03a..ed9e7b2b6b 100644 --- a/fortran/lammps.f90 +++ b/fortran/lammps.f90 @@ -408,6 +408,12 @@ MODULE LIBLAMMPS SUBROUTINE lammps_kokkos_finalize() BIND(C) END SUBROUTINE lammps_kokkos_finalize + SUBROUTINE lammps_python_finalize() BIND(C) + END SUBROUTINE lammps_python_finalize + + SUBROUTINE lammps_plugin_finalize() BIND(C) + END SUBROUTINE lammps_plugin_finalize + SUBROUTINE lammps_error(handle, error_type, error_text) BIND(C) IMPORT :: c_ptr, c_int IMPLICIT NONE @@ -1135,7 +1141,7 @@ CONTAINS SIZE_IMAGEINT = lmp_extract_setting(lmp_open, 'imageint') END FUNCTION lmp_open - ! Combined Fortran wrapper around lammps_close() and lammps_mpi_finalize() + ! Combined Fortran wrapper around lammps_close() and lammps_*_finalize() SUBROUTINE lmp_close(self, finalize) CLASS(lammps), INTENT(IN) :: self LOGICAL, INTENT(IN), OPTIONAL :: finalize @@ -1146,6 +1152,8 @@ CONTAINS IF (finalize) THEN CALL lammps_kokkos_finalize() CALL lammps_mpi_finalize() + CALL lammps_python_finalize() + CALL lammps_plugin_finalize() END IF END IF END SUBROUTINE lmp_close diff --git a/python/lammps/core.py b/python/lammps/core.py index d808fc713f..cab3171cd3 100644 --- a/python/lammps/core.py +++ b/python/lammps/core.py @@ -653,8 +653,9 @@ class lammps(object): def finalize(self): """Shut down the MPI communication and Kokkos environment (if active) through the - library interface by calling :cpp:func:`lammps_mpi_finalize` and - :cpp:func:`lammps_kokkos_finalize`. + library interface by calling :cpp:func:`lammps_mpi_finalize`, + :cpp:func:`lammps_kokkos_finalize`, :cpp:func:`lammps_python_finalize`, and + :cpp:func:`lammps_plugin_finalize` You cannot create or use any LAMMPS instances after this function is called unless LAMMPS was compiled without MPI and without Kokkos support. @@ -662,6 +663,8 @@ class lammps(object): self.close() self.lib.lammps_kokkos_finalize() self.lib.lammps_mpi_finalize() + self.lib.lammps_python_finalize() + self.lib.lammps_plugin_finalize() # ------------------------------------------------------------------------- diff --git a/src/PLUGIN/plugin.cpp b/src/PLUGIN/plugin.cpp index 0a52e027f3..7326563003 100644 --- a/src/PLUGIN/plugin.cpp +++ b/src/PLUGIN/plugin.cpp @@ -58,6 +58,9 @@ void Plugin::command(int narg, char **arg) } else if (cmd == "clear") { plugin_clear(lmp); + } else if (cmd == "restore") { + plugin_restore(lmp, false); + } else if (cmd == "list") { if (comm->me == 0) { int num = plugin_get_num_plugins(); @@ -68,15 +71,16 @@ void Plugin::command(int narg, char **arg) } } } else - error->all(FLERR, "Illegal plugin command"); + error->all(FLERR, "Unknown plugin command {}", cmd); } // auto-load DSOs from designated folder(s) void plugin_auto_load(LAMMPS *lmp) { #if defined(LMP_PLUGIN) + bool oldverbose = verbose; + verbose = false; for (const auto &plugin_dir : platform::list_pathenv("LAMMPS_PLUGIN_PATH")) { - verbose = false; int count = 0; for (const auto &file : platform::list_directory(plugin_dir)) { if (utils::strmatch(file, "\\plugin.so$")) @@ -84,6 +88,7 @@ void plugin_auto_load(LAMMPS *lmp) } if (lmp->comm->me == 0) utils::logmesg(lmp, "Loaded {} plugins from {}\n", count, plugin_dir); } + verbose = oldverbose; #endif } @@ -262,7 +267,8 @@ void plugin_register(lammpsplugin_t *plugin, void *ptr) auto minimize_map = lmp->update->minimize_map; if (minimize_map->find(plugin->name) != minimize_map->end()) { if (lmp->comm->me == 0) - lmp->error->warning(FLERR, "Overriding built-in run style {} from plugin", plugin->name); + lmp->error->warning(FLERR, "Overriding built-in minimize style {} from plugin", + plugin->name); } (*minimize_map)[plugin->name] = (Update::MinimizeCreator) plugin->creator.v1; @@ -319,9 +325,6 @@ void plugin_unload(const char *style, const char *name, LAMMPS *lmp) std::string pstyle = style; if (pstyle == "pair") { - auto found = lmp->force->pair_map->find(name); - if (found != lmp->force->pair_map->end()) lmp->force->pair_map->erase(found); - // must delete pair style instance if in use if (lmp->force->pair_style) { @@ -332,82 +335,90 @@ void plugin_unload(const char *style, const char *name, LAMMPS *lmp) } } - } else if (pstyle == "bond") { + auto found = lmp->force->pair_map->find(name); + if (found != lmp->force->pair_map->end()) lmp->force->pair_map->erase(found); - auto found = lmp->force->bond_map->find(name); - if (found != lmp->force->bond_map->end()) lmp->force->bond_map->erase(found); + } else if (pstyle == "bond") { // must delete bond style instance if in use if ((lmp->force->bond_style != nullptr) && (lmp->force->bond_match(name) != nullptr)) lmp->force->create_bond("none", 0); - } else if (pstyle == "angle") { + auto found = lmp->force->bond_map->find(name); + if (found != lmp->force->bond_map->end()) lmp->force->bond_map->erase(found); - auto found = lmp->force->angle_map->find(name); - if (found != lmp->force->angle_map->end()) lmp->force->angle_map->erase(found); + } else if (pstyle == "angle") { // must delete angle style instance if in use if ((lmp->force->angle_style != nullptr) && (lmp->force->angle_match(name) != nullptr)) lmp->force->create_angle("none", 0); - } else if (pstyle == "dihedral") { + auto found = lmp->force->angle_map->find(name); + if (found != lmp->force->angle_map->end()) lmp->force->angle_map->erase(found); - auto found = lmp->force->dihedral_map->find(name); - if (found != lmp->force->dihedral_map->end()) lmp->force->dihedral_map->erase(found); + } else if (pstyle == "dihedral") { // must delete dihedral style instance if in use if ((lmp->force->dihedral_style) && (lmp->force->dihedral_match(name) != nullptr)) lmp->force->create_dihedral("none", 0); - } else if (pstyle == "improper") { + auto found = lmp->force->dihedral_map->find(name); + if (found != lmp->force->dihedral_map->end()) lmp->force->dihedral_map->erase(found); - auto found = lmp->force->improper_map->find(name); - if (found != lmp->force->improper_map->end()) lmp->force->improper_map->erase(found); + } else if (pstyle == "improper") { // must delete improper style instance if in use if ((lmp->force->improper_style != nullptr) && (lmp->force->improper_match(name) != nullptr)) lmp->force->create_improper("none", 0); + auto found = lmp->force->improper_map->find(name); + if (found != lmp->force->improper_map->end()) lmp->force->improper_map->erase(found); + } else if (pstyle == "kspace") { + // must delete kspace style instance if in use + + if ((lmp->force->kspace_style != nullptr) && (lmp->force->kspace_match(name, 1) != nullptr)) + lmp->force->create_kspace("none", 0); + auto kspace_map = lmp->force->kspace_map; auto found = kspace_map->find(name); if (found != kspace_map->end()) kspace_map->erase(name); } else if (pstyle == "compute") { - auto compute_map = lmp->modify->compute_map; - auto found = compute_map->find(name); - if (found != compute_map->end()) compute_map->erase(name); - // must delete all compute instances using this compute style for (auto &icompute : lmp->modify->get_compute_by_style(name)) lmp->modify->delete_compute(icompute->id); - } else if (pstyle == "fix") { + auto compute_map = lmp->modify->compute_map; + auto found = compute_map->find(name); + if (found != compute_map->end()) compute_map->erase(name); - auto fix_map = lmp->modify->fix_map; - auto found = fix_map->find(name); - if (found != fix_map->end()) fix_map->erase(name); + } else if (pstyle == "fix") { // must delete all fix instances using this fix style for (auto &ifix : lmp->modify->get_fix_by_style(name)) lmp->modify->delete_fix(ifix->id); + auto fix_map = lmp->modify->fix_map; + auto found = fix_map->find(name); + if (found != fix_map->end()) fix_map->erase(name); + } else if (pstyle == "region") { + for (auto &iregion : lmp->domain->get_region_by_style(name)) + lmp->domain->delete_region(iregion); + auto region_map = lmp->domain->region_map; auto found = region_map->find(name); if (found != region_map->end()) region_map->erase(name); - for (auto &iregion : lmp->domain->get_region_by_style(name)) - lmp->domain->delete_region(iregion); - } else if (pstyle == "command") { auto command_map = lmp->input->command_map; @@ -416,12 +427,24 @@ void plugin_unload(const char *style, const char *name, LAMMPS *lmp) } else if (pstyle == "run") { + // must restore default run style if plugin style is in use + + if (strcmp(name, lmp->update->integrate_style) == 0) { + char *str = (char *) "verlet"; + lmp->update->create_integrate(1, &str, 1); + } auto integrate_map = lmp->update->integrate_map; auto found = integrate_map->find(name); if (found != integrate_map->end()) integrate_map->erase(name); } else if (pstyle == "min") { + // must restore default minimize style if plugin style is in use + + if (strcmp(name, lmp->update->minimize_style) == 0) { + char *str = (char *) "cg"; + lmp->update->create_minimize(1, &str, 1); + } auto minimize_map = lmp->update->minimize_map; auto found = minimize_map->find(name); if (found != minimize_map->end()) minimize_map->erase(name); @@ -434,18 +457,157 @@ void plugin_unload(const char *style, const char *name, LAMMPS *lmp) #endif } +/* -------------------------------------------------------------------- + restore previously loaded plugins + -------------------------------------------------------------------- */ + +void plugin_restore(LAMMPS *lmp, bool warnflag) +{ + for (auto &plugin : pluginlist) { + if (lmp->comm->me == 0) + utils::logmesg(lmp, "Restoring plugin: {} by {}\n", plugin.info, plugin.author); + + std::string pstyle = plugin.style; + if (pstyle == "pair") { + auto pair_map = lmp->force->pair_map; + if (pair_map->find(plugin.name) != pair_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in pair style {} from plugin", plugin.name); + } + (*pair_map)[plugin.name] = (Force::PairCreator) plugin.creator.v1; + + } else if (pstyle == "bond") { + auto bond_map = lmp->force->bond_map; + if (bond_map->find(plugin.name) != bond_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in bond style {} from plugin", plugin.name); + } + (*bond_map)[plugin.name] = (Force::BondCreator) plugin.creator.v1; + + } else if (pstyle == "angle") { + auto angle_map = lmp->force->angle_map; + if (angle_map->find(plugin.name) != angle_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in angle style {} from plugin", plugin.name); + } + (*angle_map)[plugin.name] = (Force::AngleCreator) plugin.creator.v1; + + } else if (pstyle == "dihedral") { + auto dihedral_map = lmp->force->dihedral_map; + if (dihedral_map->find(plugin.name) != dihedral_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in dihedral style {} from plugin", + plugin.name); + } + (*dihedral_map)[plugin.name] = (Force::DihedralCreator) plugin.creator.v1; + + } else if (pstyle == "improper") { + auto improper_map = lmp->force->improper_map; + if (improper_map->find(plugin.name) != improper_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in improper style {} from plugin", + plugin.name); + } + (*improper_map)[plugin.name] = (Force::ImproperCreator) plugin.creator.v1; + + } else if (pstyle == "kspace") { + auto kspace_map = lmp->force->kspace_map; + if (kspace_map->find(plugin.name) != kspace_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in kspace style {} from plugin", + plugin.name); + } + (*kspace_map)[plugin.name] = (Force::KSpaceCreator) plugin.creator.v1; + + } else if (pstyle == "compute") { + auto compute_map = lmp->modify->compute_map; + if (compute_map->find(plugin.name) != compute_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in compute style {} from plugin", + plugin.name); + } + (*compute_map)[plugin.name] = (Modify::ComputeCreator) plugin.creator.v2; + + } else if (pstyle == "fix") { + auto fix_map = lmp->modify->fix_map; + if (fix_map->find(plugin.name) != fix_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in fix style {} from plugin", plugin.name); + } + (*fix_map)[plugin.name] = (Modify::FixCreator) plugin.creator.v2; + + } else if (pstyle == "region") { + auto region_map = lmp->domain->region_map; + if (region_map->find(plugin.name) != region_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in region style {} from plugin", + plugin.name); + } + (*region_map)[plugin.name] = (Domain::RegionCreator) plugin.creator.v2; + + } else if (pstyle == "command") { + auto command_map = lmp->input->command_map; + if (command_map->find(plugin.name) != command_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in command style {} from plugin", + plugin.name); + } + (*command_map)[plugin.name] = (Input::CommandCreator) plugin.creator.v1; + + } else if (pstyle == "run") { + auto integrate_map = lmp->update->integrate_map; + if (integrate_map->find(plugin.name) != integrate_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in run style {} from plugin", plugin.name); + } + (*integrate_map)[plugin.name] = (Update::IntegrateCreator) plugin.creator.v2; + + } else if (pstyle == "min") { + auto minimize_map = lmp->update->minimize_map; + if (minimize_map->find(plugin.name) != minimize_map->end()) { + if (warnflag && (lmp->comm->me == 0)) + lmp->error->warning(FLERR, "Overriding built-in minimize style {} from plugin", + plugin.name); + } + (*minimize_map)[plugin.name] = (Update::MinimizeCreator) plugin.creator.v1; + } + } +} + /* -------------------------------------------------------------------- unload all loaded plugins -------------------------------------------------------------------- */ void plugin_clear(LAMMPS *lmp) { + bool oldverbose = verbose; verbose = true; while (pluginlist.size() > 0) { auto p = pluginlist.begin(); plugin_unload(p->style, p->name, lmp); } - verbose = true; + verbose = oldverbose; +} + +/* -------------------------------------------------------------------- + unload all shared objects + -------------------------------------------------------------------- */ + +void plugin_finalize() +{ +#if defined(LMP_PLUGIN) + while (pluginlist.size() > 0) { + auto p = pluginlist.begin(); + + void *handle = p->handle; + plugin_erase(p->style, p->name); + + // if reference count is down to zero, close DSO handle. + + --dso_refcounter[handle]; + if (dso_refcounter[handle] == 0) { platform::dlclose(handle); } + } +#endif } /* -------------------------------------------------------------------- diff --git a/src/PLUGIN/plugin.h b/src/PLUGIN/plugin.h index 4d8d6c29f8..838f32581c 100644 --- a/src/PLUGIN/plugin.h +++ b/src/PLUGIN/plugin.h @@ -34,10 +34,12 @@ class Plugin : public Command { void plugin_auto_load(LAMMPS *); int plugin_load(const char *, LAMMPS *); void plugin_register(lammpsplugin_t *, void *); +void plugin_restore(LAMMPS *, bool); void plugin_unload(const char *, const char *, LAMMPS *); void plugin_erase(const char *, const char *); void plugin_clear(LAMMPS *); +void plugin_finalize(); int plugin_get_num_plugins(); int plugin_find(const char *, const char *); diff --git a/src/lammps.cpp b/src/lammps.cpp index c891cea14d..98839e33fb 100644 --- a/src/lammps.cpp +++ b/src/lammps.cpp @@ -883,8 +883,9 @@ void LAMMPS::create() python = new Python(this); - // auto-load plugins + // restore and auto-load plugins #if defined(LMP_PLUGIN) + plugin_restore(this, true); plugin_auto_load(this); #endif } @@ -991,11 +992,6 @@ void LAMMPS::init() void LAMMPS::destroy() { - // must wipe out all plugins first, if configured -#if defined(LMP_PLUGIN) - plugin_clear(this); -#endif - delete update; update = nullptr; diff --git a/src/library.cpp b/src/library.cpp index a8acbade52..aacea70269 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -357,7 +357,8 @@ completed and then MPI is cleanly shut down. After calling this function no more MPI calls may be made. *See also* - :cpp:func:`lammps_kokkos_finalize`, :cpp:func:`lammps_python_finalize` + :cpp:func:`lammps_kokkos_finalize`, :cpp:func:`lammps_python_finalize`, + :cpp:func:`lammps_plugin_finalize` \endverbatim */ void lammps_mpi_finalize() @@ -389,7 +390,8 @@ closed (to release associated resources). After calling this function no Kokkos functionality may be used. *See also* - :cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_python_finalize` + :cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_python_finalize`, + :cpp:func:`lammps_plugin_finalize` \endverbatim */ void lammps_kokkos_finalize() @@ -399,6 +401,35 @@ void lammps_kokkos_finalize() /* ---------------------------------------------------------------------- */ +/** Unload all plugins and release the corresponding DSO handles + * +\verbatim embed:rst + +.. versionadded:: TBD + +This function clears the list of all loaded plugins and closes the +corresponding DSO handles and releases the imported executable code. + +However, this is **not** done when a LAMMPS instance is deleted because +plugins and their shared objects are global properties. + +This function can be called to explicitly clear out all loaded plugins +in case it is safe to do so. + +*See also* + :cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_kokkos_finalize`, + :cpp:func:`lammps_python_finalize` +\endverbatim */ + +void lammps_plugin_finalize() +{ +#if defined(LMP_PLUGIN) + plugin_finalize(); +#endif +} + +/* ---------------------------------------------------------------------- */ + /** Clear the embedded Python environment * \verbatim embed:rst @@ -425,7 +456,8 @@ This function can be called to explicitly clear the Python environment in case it is safe to do so. *See also* - :cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_kokkos_finalize` + :cpp:func:`lammps_mpi_finalize`, :cpp:func:`lammps_kokkos_finalize`, + :cpp:func:`lammps_plugin_finalize` \endverbatim */ void lammps_python_finalize() @@ -433,7 +465,6 @@ void lammps_python_finalize() Python::finalize(); } - /* ---------------------------------------------------------------------- */ /** Call a LAMMPS Error class function diff --git a/src/library.h b/src/library.h index a4ca396628..08b619eb78 100644 --- a/src/library.h +++ b/src/library.h @@ -140,6 +140,7 @@ void lammps_close(void *handle); void lammps_mpi_init(); void lammps_mpi_finalize(); void lammps_kokkos_finalize(); +void lammps_plugin_finalize(); void lammps_python_finalize(); void lammps_error(void *handle, int error_type, const char *error_text); diff --git a/src/main.cpp b/src/main.cpp index 951d828b51..65f5a9658d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ static void finalize() { lammps_kokkos_finalize(); lammps_python_finalize(); + lammps_plugin_finalize(); } /* ---------------------------------------------------------------------- diff --git a/src/update.cpp b/src/update.cpp index 4140a31731..950ac59b40 100644 --- a/src/update.cpp +++ b/src/update.cpp @@ -99,6 +99,13 @@ Update::Update(LAMMPS *lmp) : Update::~Update() { + // restore default styles to avoid segfaults from plugins + char *str = (char *) "verlet"; + create_integrate(1, &str, 1); + + str = (char *) "cg"; + create_minimize(1, &str, 1); + delete[] unit_style; delete[] integrate_style; diff --git a/tools/swig/lammps.i b/tools/swig/lammps.i index 283b5d8b4e..6761ce7acf 100644 --- a/tools/swig/lammps.i +++ b/tools/swig/lammps.i @@ -116,6 +116,7 @@ extern void lammps_mpi_init(); extern void lammps_mpi_finalize(); extern void lammps_kokkos_finalize(); extern void lammps_python_finalize(); +extern void lammps_plugin_finalize(); extern void lammps_error(void *handle, int error_type, const char *error_text); extern char *lammps_expand(void *handle, const char *line); @@ -315,6 +316,7 @@ extern void lammps_mpi_init(); extern void lammps_mpi_finalize(); extern void lammps_kokkos_finalize(); extern void lammps_python_finalize(); +extern void lammps_plugin_finalize(); extern void lammps_error(void *handle, int error_type, const char *error_text); extern char *lammps_expand(void *handle, const char *line);