next iteration: rename functions/files, split header, store dso handle

This commit is contained in:
Axel Kohlmeyer
2021-03-11 07:26:57 -05:00
parent 592490be4e
commit 8e1ccb6123
5 changed files with 59 additions and 43 deletions

View File

@ -22,19 +22,21 @@ static Pair *morse2ompcreator(LAMMPS *lmp)
}
static lammpsplugin_t plugin;
void lammpsplugin_init(void *lmp)
extern "C" void lammpsplugin_init(void *lmp, void *handle, lammpsplugin_regfunc register_plugin)
{
memset(&plugin,0,sizeof(lammpsplugin_t));
plugin.version = LAMMPS_VERSION;
plugin.style = "pair";
plugin.name = "morse2";
plugin.info = "Morse2 variant pair style v1.0 by Axel Kohlmeyer";
plugin.info = "Morse2 variant pair style v1.0";
plugin.author = "Axel Kohlmeyer (akohlmey@gmail.com)";
plugin.creator = (lammpsplugin_factory *) &morse2creator;
lammpsplugin_register(&plugin, (LAMMPS *)lmp);
plugin.handle = handle;
register_plugin(&plugin,lmp);
plugin.style = "pair";
plugin.name = "morse2/omp";
plugin.info = "Morse2 variant pair style for OpenMP v1.0 by Axel Kohlmeyer";
plugin.info = "Morse2 variant pair style for OpenMP v1.0";
plugin.creator = (lammpsplugin_factory *) &morse2ompcreator;
lammpsplugin_register(&plugin, (LAMMPS *)lmp);
register_plugin(&plugin,lmp);
}

View File

@ -29,13 +29,13 @@
#include "group.h"
#include "improper.h"
#include "kspace.h"
#include "lammpsplugin.h"
#include "memory.h"
#include "min.h"
#include "modify.h"
#include "neighbor.h"
#include "output.h"
#include "pair.h"
#include "plugin.h"
#include "special.h"
#include "style_command.h"
#include "thermo.h"
@ -1106,16 +1106,16 @@ void Input::plugin()
if (cmd == "load") {
if (narg < 2) error->all(FLERR,"Illegal plugin load command");
for (int i=1; i < narg; ++i)
lammpsplugin_load(arg[i],lmp);
plugin_load(arg[i],lmp);
} else if (cmd == "unload") {
if (narg != 3) error->all(FLERR,"Illegal plugin unload command");
lammpsplugin_unload(arg[1],arg[2],lmp);
plugin_unload(arg[1],arg[2],lmp);
} else if (cmd == "list") {
if (comm->me == 0) {
int num = lammpsplugin_get_num_plugins();
int num = plugin_get_num_plugins();
utils::logmesg(lmp,"Currently loaded plugins\n");
for (int i=0; i < num; ++i) {
auto entry = lammpsplugin_info(i);
auto entry = plugin_info(i);
utils::logmesg(lmp,fmt::format("{:4}: {} style plugin {}\n",
i+1,entry->style,entry->name));
}

View File

@ -14,41 +14,29 @@
#ifndef LMP_LAMMPSPLUGIN_H
#define LMP_LAMMPSPLUGIN_H
// C style API and data structures required for dynamic loading
// C style API and data structure required for dynamic loading
extern "C" {
typedef void *(lammpsplugin_factory)(void *);
typedef void (*lammpsplugin_initfunc)(void *);
typedef struct {
const char *version;
const char *style;
const char *name;
const char *info;
const char *author;
lammpsplugin_factory *creator;
void *handle;
} lammpsplugin_t;
typedef struct {
const char *style;
const char *name;
const void *handle;
} lammpsplugin_entry_t;
typedef void (*lammpsplugin_regfunc)(lammpsplugin_t *, void *);
typedef void (*lammpsplugin_initfunc)(void *, void *, lammpsplugin_regfunc);
// prototype for initializer function required
// to load a plugin; uses C bindings
void lammpsplugin_init(void *);
}
namespace LAMMPS_NS
{
extern void lammpsplugin_load(const char *, void *);
extern void lammpsplugin_register(lammpsplugin_t *, void *);
extern int lammpsplugin_get_num_plugins();
extern const lammpsplugin_entry_t *lammpsplugin_info(int);
extern int lammpsplugin_find(const char *, const char *);
extern void lammpsplugin_unload(const char *, const char *, void *);
void lammpsplugin_init(void *, void *, lammpsplugin_regfunc);
}
#endif

View File

@ -11,7 +11,7 @@
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#include "lammpsplugin.h"
#include "plugin.h"
#include "error.h"
#include "force.h"
@ -28,9 +28,9 @@
namespace LAMMPS_NS
{
static std::vector<lammpsplugin_entry_t> pluginlist;
static std::vector<lammpsplugin_t> pluginlist;
void lammpsplugin_load(const char *file, void *ptr)
void plugin_load(const char *file, void *ptr)
{
LAMMPS *lmp = (LAMMPS *)ptr;
#if defined(WIN32)
@ -46,15 +46,15 @@ namespace LAMMPS_NS
void *initfunc = dlsym(dso,"lammpsplugin_init");
if (initfunc == nullptr) {
dlclose(dso);
utils::logmesg(lmp,fmt::format("Symbol lookup failure in file {}",file));
utils::logmesg(lmp,fmt::format("Symbol lookup failure in file {}\n",file));
return;
}
((lammpsplugin_initfunc)(initfunc))(ptr);
((lammpsplugin_initfunc)(initfunc))(ptr,dso,&plugin_register);
// dlclose(dso);
#endif
}
void lammpsplugin_register(lammpsplugin_t *plugin, void *ptr)
void plugin_register(lammpsplugin_t *plugin, void *ptr)
{
LAMMPS *lmp = (LAMMPS *)ptr;
@ -64,11 +64,8 @@ namespace LAMMPS_NS
"version {} loaded into LAMMPS "
"version {}\n", plugin->info,
plugin->version, lmp->version));
lammpsplugin_entry_t entry;
entry.style = plugin->style;
entry.name = plugin->name;
entry.handle = nullptr;
pluginlist.push_back(entry);
pluginlist.push_back(*plugin);
std::string pstyle = plugin->style;
if (pstyle == "pair") {
@ -81,19 +78,19 @@ namespace LAMMPS_NS
}
}
int lammpsplugin_get_num_plugins()
int plugin_get_num_plugins()
{
return pluginlist.size();
}
const lammpsplugin_entry_t *lammpsplugin_info(int idx)
const lammpsplugin_t *plugin_info(int idx)
{
if ((idx < 0) || idx >= pluginlist.size()) return nullptr;
return &pluginlist[idx];
}
int lammpsplugin_find(const char *style, const char *name)
int plugin_find(const char *style, const char *name)
{
for (int i=0; i < pluginlist.size(); ++i) {
if ((strcmp(style,pluginlist[i].style) == 0)
@ -103,7 +100,7 @@ namespace LAMMPS_NS
return -1;
}
void lammpsplugin_unload(const char *style, const char *name, void *ptr)
void plugin_unload(const char *style, const char *name, void *ptr)
{
LAMMPS *lmp = (LAMMPS *)ptr;
std::string pstyle = style;

29
src/plugin.h Normal file
View File

@ -0,0 +1,29 @@
/* -*- c++ -*- ----------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
http://lammps.sandia.gov, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LMP_PLUGIN_H
#define LMP_PLUGIN_H
#include "lammpsplugin.h"
namespace LAMMPS_NS
{
void plugin_load(const char *, void *);
void plugin_register(lammpsplugin_t *, void *);
int plugin_get_num_plugins();
const lammpsplugin_t *plugin_info(int);
int plugin_find(const char *, const char *);
void plugin_unload(const char *, const char *, void *);
}
#endif