simpler interfaces
This commit is contained in:
@ -22,21 +22,23 @@ static Pair *morse2ompcreator(LAMMPS *lmp)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static lammpsplugin_t plugin;
|
static lammpsplugin_t plugin;
|
||||||
extern "C" void lammpsplugin_init(void *lmp, void *handle, lammpsplugin_regfunc register_plugin)
|
extern "C" void lammpsplugin_init(void *lmp, void *handle, void *regfunc)
|
||||||
{
|
{
|
||||||
memset(&plugin,0,sizeof(lammpsplugin_t));
|
// register plain morse2 pair style
|
||||||
plugin.version = LAMMPS_VERSION;
|
lammpsplugin_regfunc register_plugin = (lammpsplugin_regfunc) regfunc;
|
||||||
plugin.style = "pair";
|
memset(&plugin,0,sizeof(lammpsplugin_t));
|
||||||
plugin.name = "morse2";
|
plugin.version = LAMMPS_VERSION;
|
||||||
plugin.info = "Morse2 variant pair style v1.0";
|
plugin.style = "pair";
|
||||||
plugin.author = "Axel Kohlmeyer (akohlmey@gmail.com)";
|
plugin.name = "morse2";
|
||||||
plugin.creator = (lammpsplugin_factory *) &morse2creator;
|
plugin.info = "Morse2 variant pair style v1.0";
|
||||||
plugin.handle = handle;
|
plugin.author = "Axel Kohlmeyer (akohlmey@gmail.com)";
|
||||||
register_plugin(&plugin,lmp);
|
plugin.creator = (lammpsplugin_factory *) &morse2creator;
|
||||||
|
plugin.handle = handle;
|
||||||
|
register_plugin(&plugin,lmp);
|
||||||
|
|
||||||
plugin.style = "pair";
|
// also register morse2/omp pair style. only need to update changed fields
|
||||||
plugin.name = "morse2/omp";
|
plugin.name = "morse2/omp";
|
||||||
plugin.info = "Morse2 variant pair style for OpenMP v1.0";
|
plugin.info = "Morse2 variant pair style for OpenMP v1.0";
|
||||||
plugin.creator = (lammpsplugin_factory *) &morse2ompcreator;
|
plugin.creator = (lammpsplugin_factory *) &morse2ompcreator;
|
||||||
register_plugin(&plugin,lmp);
|
register_plugin(&plugin,lmp);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -31,12 +31,12 @@ extern "C" {
|
|||||||
} lammpsplugin_t;
|
} lammpsplugin_t;
|
||||||
|
|
||||||
typedef void (*lammpsplugin_regfunc)(lammpsplugin_t *, void *);
|
typedef void (*lammpsplugin_regfunc)(lammpsplugin_t *, void *);
|
||||||
typedef void (*lammpsplugin_initfunc)(void *, void *, lammpsplugin_regfunc);
|
typedef void (*lammpsplugin_initfunc)(void *, void *, void *);
|
||||||
|
|
||||||
// prototype for initializer function required
|
// prototype for initializer function required
|
||||||
// to load a plugin; uses C bindings
|
// to load a plugin; uses C bindings
|
||||||
|
|
||||||
void lammpsplugin_init(void *, void *, lammpsplugin_regfunc);
|
void lammpsplugin_init(void *, void *, void *);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -18,6 +18,7 @@
|
|||||||
#include "lammps.h"
|
#include "lammps.h"
|
||||||
#include "modify.h"
|
#include "modify.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -28,14 +29,20 @@
|
|||||||
|
|
||||||
namespace LAMMPS_NS
|
namespace LAMMPS_NS
|
||||||
{
|
{
|
||||||
|
// store list of plugin information data for loaded styles
|
||||||
static std::vector<lammpsplugin_t> pluginlist;
|
static std::vector<lammpsplugin_t> pluginlist;
|
||||||
|
// map of dso handles
|
||||||
|
static std::map<void *, int> dso_refcounter;
|
||||||
|
|
||||||
void plugin_load(const char *file, void *ptr)
|
// load DSO and call included registration function
|
||||||
|
void plugin_load(const char *file, LAMMPS *lmp)
|
||||||
{
|
{
|
||||||
LAMMPS *lmp = (LAMMPS *)ptr;
|
|
||||||
#if defined(WIN32)
|
#if defined(WIN32)
|
||||||
utils::logmesg(lmp,"Loading of plugins not supported on Windows yet\n");
|
utils::logmesg(lmp,"Loading of plugins not supported on Windows yet\n");
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
// open DSO from given path
|
||||||
|
|
||||||
void *dso = dlopen(file,RTLD_NOW|RTLD_GLOBAL);
|
void *dso = dlopen(file,RTLD_NOW|RTLD_GLOBAL);
|
||||||
if (dso == nullptr) {
|
if (dso == nullptr) {
|
||||||
utils::logmesg(lmp,fmt::format("Loading of plugin from file {} failed: "
|
utils::logmesg(lmp,fmt::format("Loading of plugin from file {} failed: "
|
||||||
@ -43,17 +50,23 @@ namespace LAMMPS_NS
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// look up lammpsplugin_init() function in DSO. must have C bindings.
|
||||||
|
|
||||||
void *initfunc = dlsym(dso,"lammpsplugin_init");
|
void *initfunc = dlsym(dso,"lammpsplugin_init");
|
||||||
if (initfunc == nullptr) {
|
if (initfunc == nullptr) {
|
||||||
dlclose(dso);
|
dlclose(dso);
|
||||||
utils::logmesg(lmp,fmt::format("Symbol lookup failure in file {}\n",file));
|
utils::logmesg(lmp,fmt::format("Symbol lookup failure in file {}\n",file));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
((lammpsplugin_initfunc)(initfunc))(ptr,dso,&plugin_register);
|
|
||||||
// dlclose(dso);
|
// call initializer function loaded from DSO and pass pointer to LAMMPS instance,
|
||||||
|
// the DSO handle (for reference counting) and plugin registration function pointer
|
||||||
|
|
||||||
|
((lammpsplugin_initfunc)(initfunc))((void *)lmp, dso, (void *)&plugin_register);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// register new style from plugin with LAMMPS
|
||||||
void plugin_register(lammpsplugin_t *plugin, void *ptr)
|
void plugin_register(lammpsplugin_t *plugin, void *ptr)
|
||||||
{
|
{
|
||||||
LAMMPS *lmp = (LAMMPS *)ptr;
|
LAMMPS *lmp = (LAMMPS *)ptr;
|
||||||
@ -99,10 +112,10 @@ namespace LAMMPS_NS
|
|||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void plugin_unload(const char *style, const char *name, void *ptr)
|
// remove plugin from given style table
|
||||||
|
void plugin_unload(const char *style, const char *name, LAMMPS *lmp)
|
||||||
{
|
{
|
||||||
LAMMPS *lmp = (LAMMPS *)ptr;
|
|
||||||
std::string pstyle = style;
|
std::string pstyle = style;
|
||||||
if (pstyle == "pair") {
|
if (pstyle == "pair") {
|
||||||
auto found = lmp->force->pair_map->find(name);
|
auto found = lmp->force->pair_map->find(name);
|
||||||
|
|||||||
@ -18,12 +18,13 @@
|
|||||||
|
|
||||||
namespace LAMMPS_NS
|
namespace LAMMPS_NS
|
||||||
{
|
{
|
||||||
void plugin_load(const char *, void *);
|
class LAMMPS;
|
||||||
|
void plugin_load(const char *, LAMMPS *);
|
||||||
void plugin_register(lammpsplugin_t *, void *);
|
void plugin_register(lammpsplugin_t *, void *);
|
||||||
int plugin_get_num_plugins();
|
int plugin_get_num_plugins();
|
||||||
const lammpsplugin_t *plugin_info(int);
|
const lammpsplugin_t *plugin_info(int);
|
||||||
int plugin_find(const char *, const char *);
|
int plugin_find(const char *, const char *);
|
||||||
void plugin_unload(const char *, const char *, void *);
|
void plugin_unload(const char *, const char *, LAMMPS *);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user