Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Multiple pylammps objects don't work with plugins #3880

Open
njzjz opened this issue Aug 4, 2023 · 1 comment
Open

[BUG] Multiple pylammps objects don't work with plugins #3880

njzjz opened this issue Aug 4, 2023 · 1 comment
Assignees
Labels

Comments

@njzjz
Copy link
Contributor

njzjz commented Aug 4, 2023

Summary

When two pylammps objects are initialized and each of them executes plugin load, the second one still gets Exception: ERROR: Unrecognized pair style error.

The stable version in 2022 doesn't have this problem.

LAMMPS Version and Platform

stable_2Aug2023, linux

Expected Behavior

Each of the multiple pylammps objects can load plugins.

Actual Behavior

If the first pylammps object is not closed, the second one does not actually load the plugin though the message says it does.

Steps to Reproduce

from lammps import PyLammps

lammps = PyLammps()
lammps.plugin("load some_plugin.so")
lammps2 = PyLammps()
lammps2.plugin("load some_plugin.so")
lammps2.pair_style("some_pair")
# error!

Got Exception: ERROR: Unrecognized pair style.

However, it works when the first pylammps is closed before the second one is initialized:

from lammps import PyLammps

lammps = PyLammps()
lammps.plugin("load some_plugin.so")
lammps.close()
lammps2 = PyLammps()
lammps2.plugin("load some_plugin.so")
lammps2.pair_style("some_pair")
# no error!

Using LAMMPS_PLUGIN_PATH will observe the same behavior.

Further Information, Files, and Links

@njzjz njzjz added the bug label Aug 4, 2023
@akohlmey
Copy link
Member

akohlmey commented Aug 4, 2023

This is not easy to resolve. The plugin interface is not set up for this kind of use case.
When importing a plugin, it is added to the global address space. The data structure that knows about styles and how to create them, however, is a per-class instance data structure. so when you create the second LAMMPS instance and try to load the same plugin, LAMMPS will refuse, because the handle for the plugin is already open and the code loaded. However, the factory function has not been registered with that LAMMPS instance, hence the error.

Plugins have to be managed in a static global data structure (see the file src/PLUGIN/plugin.cpp). There are two possible strategies:

  • add reference counting to the global data structure so that the same plugin can be loaded multiple times but always the same factory function is registered and then the plugin is only unloaded after all references are gone
  • move the map holds the factory function pointer and connects them to the style name also to a static global data structure.
    In the first case, each plugin must be loaded with each instance, but the second and further loads are "fake". In the second case, each plugin can only be loaded once, but is accessible from all LAMMPS instances.

This is independent from whether you use PyLammps, the plain python module or the LAMMPS executable.

@akohlmey akohlmey self-assigned this Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: In Progress
Development

No branches or pull requests

2 participants