List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions
casadi::PluginInterface< Derived > Class Template Referenceabstract

Interface for accessing input and output data structures. More...

#include <plugin_interface.hpp>

Detailed Description

template<class Derived>
class casadi::PluginInterface< Derived >

Author
Joel Andersson
Date
2013

Extra doc: https://github.com/casadi/casadi/wiki/L_rp

Definition at line 54 of file plugin_interface.hpp.

Classes

struct  Plugin
 Fields. More...
 

Public Types

typedef int(* RegFcn) (Plugin *plugin)
 

Public Member Functions

virtual const char * plugin_name () const =0
 
void serialize_type (SerializingStream &s) const
 Serialize type information. More...
 

Static Public Member Functions

static bool has_plugin (const std::string &pname, bool verbose=false)
 Check if a plugin is available or can be loaded. More...
 
static const Optionsplugin_options (const std::string &pname)
 Get the plugin options. More...
 
static Deserialize plugin_deserialize (const std::string &pname)
 Get the plugin deserialize_map. More...
 
static Plugin pluginFromRegFcn (RegFcn regfcn)
 Instantiate a Plugin struct from a factory function. More...
 
static Plugin load_plugin (const std::string &pname, bool register_plugin=true, bool needs_lock=true)
 Load a plugin dynamically. More...
 
static handle_t load_library (const std::string &libname, std::string &resultpath, bool global)
 Load a library dynamically. More...
 
static void registerPlugin (const Plugin &plugin, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static void registerPlugin (RegFcn regfcn, bool needs_lock=true)
 Register an integrator in the factory. More...
 
static PlugingetPlugin (const std::string &pname)
 Load and get the creator function. More...
 
template<class Problem >
static Derived * instantiate (const std::string &fname, const std::string &pname, Problem problem)
 
static ProtoFunctiondeserialize (DeserializingStream &s)
 Deserialize with type disambiguation. More...
 

Member Typedef Documentation

◆ RegFcn

template<class Derived >
typedef int(* casadi::PluginInterface< Derived >::RegFcn) (Plugin *plugin)

Definition at line 73 of file plugin_interface.hpp.

Member Function Documentation

◆ deserialize()

template<class Derived >
static ProtoFunction* casadi::PluginInterface< Derived >::deserialize ( DeserializingStream s)
inlinestatic

Extra doc: https://github.com/casadi/casadi/wiki/L_rr

Definition at line 121 of file plugin_interface.hpp.

121  {
122  std::string class_name, plugin_name;
123  s.unpack("PluginInterface::plugin_name", plugin_name);
125  return deserialize(s);
126  }
static Deserialize plugin_deserialize(const std::string &pname)
Get the plugin deserialize_map.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
virtual const char * plugin_name() const =0
ProtoFunction *(* Deserialize)(DeserializingStream &)

References casadi::PluginInterface< Derived >::plugin_deserialize(), casadi::PluginInterface< Derived >::plugin_name(), and casadi::DeserializingStream::unpack().

Referenced by casadi::Conic::deserialize(), casadi::Integrator::deserialize(), casadi::Interpolant::deserialize(), casadi::LinsolInternal::deserialize(), casadi::Nlpsol::deserialize(), and casadi::Rootfinder::deserialize().

◆ getPlugin()

template<class Derived >
PluginInterface< Derived >::Plugin & casadi::PluginInterface< Derived >::getPlugin ( const std::string &  pname)
static

Definition at line 291 of file plugin_interface.hpp.

291  {
292 
293 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
294  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
295 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
296 
297  // Check if the solver has been loaded
298  auto it=Derived::solvers_.find(pname);
299 
300  // Load the solver if needed
301  if (it==Derived::solvers_.end()) {
302  load_plugin(pname, true, false);
303  it=Derived::solvers_.find(pname);
304  }
305  casadi_assert_dev(it!=Derived::solvers_.end());
306  return it->second;
307  }
static Plugin load_plugin(const std::string &pname, bool register_plugin=true, bool needs_lock=true)
Load a plugin dynamically.

◆ has_plugin()

template<class Derived >
bool casadi::PluginInterface< Derived >::has_plugin ( const std::string &  pname,
bool  verbose = false 
)
static

Definition at line 131 of file plugin_interface.hpp.

131  {
132 
133 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
134  std::lock_guard<std::mutex> lock(Derived::mutex_solvers_);
135 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
136 
137  // Quick return if available
138  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
139  return true;
140  }
141 
142  // Try loading the plugin
143  try {
144  (void)load_plugin(pname, false, false);
145  return true;
146  } catch (CasadiException& ex) {
147  if (verbose) {
148  casadi_warning(ex.what());
149  }
150  return false;
151  }
152  }
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros

References casadi::find(), and casadi::CasadiException::what().

◆ instantiate()

template<class Derived >
template<class Problem >
Derived * casadi::PluginInterface< Derived >::instantiate ( const std::string &  fname,
const std::string &  pname,
Problem  problem 
)
static

Definition at line 311 of file plugin_interface.hpp.

313  {
314 
315  // Assert the plugin exists (needed for adaptors)
316  if (!has_plugin(pname, true)) {
317  casadi_error("Plugin '" + pname + "' is not found.");
318  }
319  return getPlugin(pname).creator(fname, problem);
320  }
static bool has_plugin(const std::string &pname, bool verbose=false)
Check if a plugin is available or can be loaded.
static Plugin & getPlugin(const std::string &pname)
Load and get the creator function.

◆ load_library()

template<class Derived >
handle_t casadi::PluginInterface< Derived >::load_library ( const std::string &  libname,
std::string &  resultpath,
bool  global 
)
static

Definition at line 183 of file plugin_interface.hpp.

184  {
185 
186 #ifndef WITH_DL
187  casadi_error("WITH_DL option needed for dynamic loading");
188 #else // WITH_DL
189 
190  // Get the name of the shared library
191  std::string lib = SHARED_LIBRARY_PREFIX + libname + SHARED_LIBRARY_SUFFIX;
192 
193  // Build up search paths;
194  std::vector<std::string> search_paths = get_search_paths();
195  return open_shared_library(lib, search_paths, resultpath,
196  "PluginInterface::load_plugin", global);
197 
198 #endif // WITH_DL
199  }
std::vector< std::string > get_search_paths()
Definition: casadi_os.cpp:49

References casadi::get_search_paths().

◆ load_plugin()

template<class Derived >
PluginInterface< Derived >::Plugin casadi::PluginInterface< Derived >::load_plugin ( const std::string &  pname,
bool  register_plugin = true,
bool  needs_lock = true 
)
static

Definition at line 203 of file plugin_interface.hpp.

204  {
205 
206 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
207  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
208 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
209 
210  // Issue warning and quick return if already loaded
211  if (Derived::solvers_.find(pname) != Derived::solvers_.end()) {
212  casadi_warning("PluginInterface: Solver " + pname + " is already in use. Ignored.");
213  return Plugin();
214  }
215 
216  // Logger singletons are lazily instantiated on first uout()/uerr() calls
217  // This instantation may lead to a data race with potential instatiations in plugin
218  // To be safe, trigger instantatin before any plugin loading
219  uout();
220  uerr();
221 
222 #ifndef WITH_DL
223  casadi_error("WITH_DL option needed for dynamic loading");
224 #else // WITH_DL
225  // Retrieve the registration function
226  RegFcn reg;
227 
228  // Load the dll
229  std::string regName = "casadi_register_" + Derived::infix_ + "_" + pname;
230 
231  std::string searchpath;
232  handle_t handle = load_library("casadi_" + Derived::infix_ + "_" + pname, searchpath,
233  false);
234 
235 #ifdef _WIN32
236 
237 #if __GNUC__
238 #pragma GCC diagnostic push
239 #pragma GCC diagnostic ignored "-Wcast-function-type"
240 #endif
241  reg = reinterpret_cast<RegFcn>(GetProcAddress(handle, TEXT(regName.c_str())));
242 #if __GNUC__
243 #pragma GCC diagnostic pop
244 #endif
245 
246 #else // _WIN32
247  // Reset error
248  dlerror();
249 
250  // Load creator
251  reg = reinterpret_cast<RegFcn>(dlsym(handle, regName.c_str()));
252 #endif // _WIN32
253  casadi_assert(reg!=nullptr,
254  "PluginInterface::load_plugin: no \"" + regName + "\" found in " + searchpath + ".");
255 
256  // Create a temporary struct
257  Plugin plugin = pluginFromRegFcn(reg);
258  // Register the plugin
259  if (register_plugin) {
260  registerPlugin(plugin, false);
261  }
262 
263  return plugin;
264 
265 #endif // WITH_DL
266  }
static handle_t load_library(const std::string &libname, std::string &resultpath, bool global)
Load a library dynamically.
int(* RegFcn)(Plugin *plugin)
static Plugin pluginFromRegFcn(RegFcn regfcn)
Instantiate a Plugin struct from a factory function.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
std::ostream & uerr()
std::ostream & uout()

References casadi::find(), casadi::uerr(), and casadi::uout().

◆ plugin_deserialize()

template<class Derived >
Deserialize casadi::PluginInterface< Derived >::plugin_deserialize ( const std::string &  pname)
static

Definition at line 162 of file plugin_interface.hpp.

162  {
163  Deserialize m = getPlugin(pname).deserialize;
164  casadi_assert(m, "Plugin \"" + pname + "\" does not support deserialize");
165  return m;
166  }

Referenced by casadi::PluginInterface< Derived >::deserialize().

◆ plugin_name()

template<class Derived >
virtual const char* casadi::PluginInterface< Derived >::plugin_name ( ) const
pure virtual

Implemented in casadi::LinsolInternal, casadi::SymbolicQr, casadi::Sqpmethod, casadi::ShellCompiler, casadi::Scpgen, casadi::RungeKutta, casadi::Qrsqp, casadi::Qrqp, casadi::QpToNlp, casadi::Newton, casadi::Lsqr, casadi::LinsolTridiag, casadi::LinsolQr, casadi::LinsolLdl, casadi::LinearInterpolant, casadi::Ipqp, casadi::ImplicitToNlp, casadi::Feasiblesqpmethod, casadi::FastNewton, casadi::Collocation, casadi::BSplineInterpolant, casadi::WorhpInterface, casadi::TinyXmlInterface, casadi::SuperscsInterface, casadi::KinsolInterface, casadi::IdasInterface, casadi::CvodesInterface, casadi::SqicInterface, casadi::SnoptInterface, casadi::SlicotExpm, casadi::SlicotDple, casadi::SLEQPInterface, casadi::QpoasesInterface, casadi::ProxqpInterface, casadi::OsqpInterface, casadi::OoqpInterface, casadi::MumpsInterface, casadi::MadnlpInterface, casadi::LapackQr, casadi::LapackLu, casadi::KnitroInterface, casadi::IpoptInterface, casadi::Ma27Interface, casadi::HpmpcInterface, casadi::HpipmInterface, casadi::HighsInterface, casadi::GurobiInterface, casadi::FatropInterface, casadi::FatropConicInterface, casadi::DaqpInterface, casadi::CsparseInterface, casadi::CSparseCholeskyInterface, casadi::CplexInterface, casadi::ClpInterface, casadi::ClarabelInterface, casadi::ClangCompiler, casadi::CbcInterface, casadi::BonminInterface, casadi::Blocksqp, casadi::AmplInterface, casadi::AlpaqaInterface, and casadi::ImporterInternal.

Referenced by casadi::PluginInterface< Derived >::deserialize(), and casadi::PluginInterface< Derived >::serialize_type().

◆ plugin_options()

template<class Derived >
const Options & casadi::PluginInterface< Derived >::plugin_options ( const std::string &  pname)
static

Definition at line 155 of file plugin_interface.hpp.

155  {
156  const Options *op = getPlugin(pname).options;
157  casadi_assert(op!=nullptr, "Plugin \"" + pname + "\" does not support options");
158  return *op;
159  }

◆ pluginFromRegFcn()

template<class Derived >
PluginInterface< Derived >::Plugin casadi::PluginInterface< Derived >::pluginFromRegFcn ( RegFcn  regfcn)
static

Definition at line 170 of file plugin_interface.hpp.

170  {
171  // Create a temporary struct
172  Plugin plugin;
173 
174  // Set the fields
175  int flag = regfcn(&plugin);
176  casadi_assert(flag==0, "Registration of plugin failed.");
177 
178  return plugin;
179  }

◆ registerPlugin() [1/2]

template<class Derived >
void casadi::PluginInterface< Derived >::registerPlugin ( const Plugin plugin,
bool  needs_lock = true 
)
static

Definition at line 274 of file plugin_interface.hpp.

274  {
275 
276 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
277  casadi::conditional_lock_guard<std::mutex> lock(Derived::mutex_solvers_, needs_lock);
278 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
279 
280  // Check if the solver name is in use
281  typename std::map<std::string, Plugin>::iterator it=Derived::solvers_.find(plugin.name);
282  casadi_assert(it==Derived::solvers_.end(),
283  "Solver " + str(plugin.name) + " is already in use");
284 
285  // Add to list of solvers
286  Derived::solvers_[plugin.name] = plugin;
287  }
std::string str(const T &v)
String representation, any type.

References casadi::PluginInterface< Derived >::Plugin::name, and casadi::str().

◆ registerPlugin() [2/2]

template<class Derived >
void casadi::PluginInterface< Derived >::registerPlugin ( RegFcn  regfcn,
bool  needs_lock = true 
)
static

Definition at line 269 of file plugin_interface.hpp.

269  {
270  registerPlugin(pluginFromRegFcn(regfcn), needs_lock);
271  }

◆ serialize_type()

template<class Derived >
void casadi::PluginInterface< Derived >::serialize_type ( SerializingStream s) const
inline

The documentation for this class was generated from the following file: