List of all members | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Protected Attributes
casadi::ShellCompiler Class Reference

'shell' plugin for Importer More...

#include <shell_compiler.hpp>

Detailed Description

Author
Joel Andersson
Date
2015

Interface to the JIT compiler SHELL

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


List of available options
IdTypeDescription
cleanupOT_BOOLCleanup temporary files when unloading. Default: true
compilerOT_STRINGCompiler command
compiler_flagsOT_STRINGVECTORAlias for 'compiler_flags'
compiler_output_flagOT_STRINGCompiler flag to denote object output. Default: '-o '
compiler_setupOT_STRINGCompiler setup command. Intended to be fixed. The 'flag' option is the prefered way to set custom flags.
directoryOT_STRINGDirectory to put temporary objects in. Must end with a file separator.
extra_suffixesOT_STRINGVECTORList of suffixes for extra files that the compiler may generate. Default: None
flagsOT_STRINGVECTORCompile flags for the JIT compiler. Default: None
linkerOT_STRINGLinker command
linker_flagsOT_STRINGVECTORLinker flags for the JIT compiler. Default: None
linker_output_flagOT_STRINGLinker flag to denote shared library output. Default: '-o '
linker_setupOT_STRINGLinker setup command. Intended to be fixed. The 'flag' option is the prefered way to set custom flags.
nameOT_STRINGThe file name used to write out compiled objects/libraries. The actual file names used depend on 'temp_suffix' and include extensions. Default: 'tmp_casadi_compiler_shell'
temp_suffixOT_BOOLUse a temporary (seemingly random) filename suffix for file names. This is desired for thread-safety. This behaviour may defeat caching compiler wrappers. Default: true


List of available options
IdTypeDescriptionUsed in
cleanupOT_BOOLCleanup temporary files when unloading. Default: truecasadi::ShellCompiler
compilerOT_STRINGCompiler commandcasadi::ShellCompiler
compiler_flagsOT_STRINGVECTORAlias for 'compiler_flags'casadi::ShellCompiler
compiler_output_flagOT_STRINGCompiler flag to denote object output. Default: '-o 'casadi::ShellCompiler
compiler_setupOT_STRINGCompiler setup command. Intended to be fixed. The 'flag' option is the prefered way to set custom flags.casadi::ShellCompiler
directoryOT_STRINGDirectory to put temporary objects in. Must end with a file separator.casadi::ShellCompiler
extra_suffixesOT_STRINGVECTORList of suffixes for extra files that the compiler may generate. Default: Nonecasadi::ShellCompiler
flagsOT_STRINGVECTORCompile flags for the JIT compiler. Default: Nonecasadi::ShellCompiler
linkerOT_STRINGLinker commandcasadi::ShellCompiler
linker_flagsOT_STRINGVECTORLinker flags for the JIT compiler. Default: Nonecasadi::ShellCompiler
linker_output_flagOT_STRINGLinker flag to denote shared library output. Default: '-o 'casadi::ShellCompiler
linker_setupOT_STRINGLinker setup command. Intended to be fixed. The 'flag' option is the prefered way to set custom flags.casadi::ShellCompiler
nameOT_STRINGThe file name used to write out compiled objects/libraries. The actual file names used depend on 'temp_suffix' and include extensions. Default: 'tmp_casadi_compiler_shell'casadi::ShellCompiler
temp_suffixOT_BOOLUse a temporary (seemingly random) filename suffix for file names. This is desired for thread-safety. This behaviour may defeat caching compiler wrappers. Default: truecasadi::ShellCompiler
verboseOT_BOOLVerbose evaluation – for debuggingcasadi::ImporterInternal

Definition at line 53 of file shell_compiler.hpp.

Inheritance diagram for casadi::ShellCompiler:
Inheritance graph
[legend]
Collaboration diagram for casadi::ShellCompiler:
Collaboration graph
[legend]

Public Types

typedef ImporterInternal *(* Creator) (const std::string &name)
 
using weak_ref_type = WeakRefInternal
 
typedef int(* RegFcn) (Plugin *plugin)
 

Public Member Functions

 ShellCompiler (const std::string &name)
 Constructor. More...
 
 ~ShellCompiler () override
 Destructor. More...
 
void init (const Dict &opts) override
 Initialize. More...
 
const char * plugin_name () const override
 Get name of plugin. More...
 
std::string class_name () const override
 Get type name. More...
 
signal_t get_function (const std::string &symname) override
 Get a function pointer for numerical evaluation. More...
 
std::string library () const override
 Get library name. More...
 
void disp (std::ostream &stream, bool more) const override
 Print. More...
 
void construct (const Dict &opts)
 Construct. More...
 
virtual void finalize ()
 
bool has_function (const std::string &symname) const
 Get a function pointer for numerical evaluation. More...
 
bool has_meta (const std::string &cmd, casadi_int ind=-1) const
 Does an entry exist? More...
 
std::string get_meta (const std::string &cmd, casadi_int ind=-1) const
 Get entry as a text. More...
 
void read_meta (std::istream &file, casadi_int &offset)
 Get meta information. More...
 
void read_external (const std::string &sym, bool inlined, std::istream &file, casadi_int &offset)
 Get an external function declaration. More...
 
bool inlined (const std::string &symname) const
 
std::string body (const std::string &symname) const
 Get the function body, if inlined. More...
 
virtual bool can_have_meta () const
 Can meta information be read? More...
 
std::string to_text (const std::string &cmd, casadi_int ind=-1) const
 Get entry as a text. More...
 
void serialize (SerializingStream &s) const
 
virtual void serialize_type (SerializingStream &s) const
 
virtual void serialize_body (SerializingStream &s) const
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 

Static Public Member Functions

static ImporterInternalcreator (const std::string &name)
 Create a new JIT function. More...
 
static std::string shortname ()
 Short name. More...
 
static std::string indexed (const std::string &cmd, casadi_int ind)
 
static ImporterInternaldeserialize (DeserializingStream &s)
 
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 Plugin & getPlugin (const std::string &pname)
 Load and get the creator function. More...
 
static ImporterInternalinstantiate (const std::string &fname, const std::string &pname, Problem problem)
 

Public Attributes

std::string name_
 C filename. More...
 
std::map< std::string, std::pair< casadi_int, std::string > > meta_
 Meta data. More...
 
std::map< std::string, std::pair< bool, std::string > > external_
 External functions. More...
 
bool verbose_
 Verbose – for debugging purposes. More...
 

Static Public Attributes

static const std::string meta_doc
 A documentation string. More...
 
static std::map< std::string, Plugin > solvers_
 Collection of solvers. More...
 
static const std::string infix_ = "importer"
 Infix. More...
 

Protected Member Functions

void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Protected Attributes

std::string base_name_
 
std::string bin_name_
 Temporary file. More...
 
std::string obj_name_
 Temporary file. More...
 
std::vector< std::string > extra_suffixes_
 Extra files. More...
 
bool cleanup_
 Cleanup temporary files when unloading. More...
 
handle_t handle_
 
static const Options options_
 Options. More...
 
const Optionsget_options () const override
 Options. More...
 

Member Typedef Documentation

◆ Creator

typedef ImporterInternal*(* casadi::ImporterInternal::Creator) (const std::string &name)
inherited

Definition at line 66 of file importer_internal.hpp.

◆ RegFcn

typedef int(* casadi::PluginInterface< ImporterInternal >::RegFcn) (Plugin *plugin)
inherited

Definition at line 73 of file plugin_interface.hpp.

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ ShellCompiler()

casadi::ShellCompiler::ShellCompiler ( const std::string &  name)
explicit

Definition at line 57 of file shell_compiler.cpp.

57  :
58  ImporterInternal(name) {
59  handle_ = nullptr;
60  }
ImporterInternal(const std::string &name)
Constructor.

References handle_.

◆ ~ShellCompiler()

casadi::ShellCompiler::~ShellCompiler ( )
override

Definition at line 62 of file shell_compiler.cpp.

62  {
63  if (handle_) close_shared_library(handle_);
64 
65  if (cleanup_) {
66  if (remove(bin_name_.c_str())) casadi_warning("Failed to remove " + bin_name_);
67  if (remove(obj_name_.c_str())) casadi_warning("Failed to remove " + obj_name_);
68  for (const std::string& s : extra_suffixes_) {
69  std::string name = base_name_+s;
70  remove(name.c_str());
71  }
72  }
73  }
std::string bin_name_
Temporary file.
std::string obj_name_
Temporary file.
std::vector< std::string > extra_suffixes_
Extra files.
bool cleanup_
Cleanup temporary files when unloading.
bool remove(const std::string &path)
Definition: ghc.cpp:47

References base_name_, bin_name_, cleanup_, extra_suffixes_, handle_, obj_name_, and casadi::remove().

Member Function Documentation

◆ body()

std::string casadi::ImporterInternal::body ( const std::string &  symname) const
inherited

Definition at line 299 of file importer_internal.cpp.

299  {
300  auto it = external_.find(symname);
301  casadi_assert_dev(it!=external_.end() && it->second.first);
302  return it->second.second;
303  }
std::map< std::string, std::pair< bool, std::string > > external_
External functions.

References casadi::ImporterInternal::external_.

◆ can_have_meta()

virtual bool casadi::ImporterInternal::can_have_meta ( ) const
inlinevirtualinherited

Reimplemented in casadi::DllLibrary.

Definition at line 142 of file importer_internal.hpp.

142 { return true;}

Referenced by casadi::ImporterInternal::init().

◆ class_name()

std::string casadi::ShellCompiler::class_name ( ) const
inlineoverridevirtual

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

Reimplemented from casadi::ImporterInternal.

Definition at line 83 of file shell_compiler.hpp.

83 { return "ShellCompiler";}

◆ construct()

void casadi::ImporterInternal::construct ( const Dict opts)
inherited

Prepares the function for evaluation

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

Definition at line 57 of file importer_internal.cpp.

57  {
58  // Sanitize dictionary is needed
59  if (!Options::is_sane(opts)) {
60  // Call recursively
61  return construct(Options::sanitize(opts));
62  }
63 
64  // Make sure all options exist
65  get_options().check(opts);
66 
67  // Initialize object
68  init(opts);
69 
70  // Revisit class hierarchy in reverse order
71  finalize();
72  }
virtual void init(const Dict &opts)
Initialize.
void construct(const Dict &opts)
Construct.
virtual const Options & get_options() const
Options.
static bool is_sane(const Dict &opts)
Is the dictionary sane.
Definition: options.cpp:169
static Dict sanitize(const Dict &opts, bool top_level=true)
Sanitize a options dictionary.
Definition: options.cpp:173
void check(const Dict &opts) const
Check if options exist.
Definition: options.cpp:240

References casadi::Options::check(), casadi::ImporterInternal::finalize(), casadi::ImporterInternal::get_options(), casadi::ImporterInternal::init(), casadi::Options::is_sane(), and casadi::Options::sanitize().

Referenced by casadi::Importer::create().

◆ creator()

static ImporterInternal* casadi::ShellCompiler::creator ( const std::string &  name)
inlinestatic

Definition at line 60 of file shell_compiler.hpp.

60  {
61  return new ShellCompiler(name);
62  }
ShellCompiler(const std::string &name)
Constructor.

Referenced by casadi::casadi_register_importer_shell().

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

162  {
163  // Note: i != this because of something something multiple inheritance
164  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
165  }
std::string str(const T &v)
String representation, any type.

◆ deserialize()

ImporterInternal * casadi::ImporterInternal::deserialize ( DeserializingStream s)
staticinherited

Definition at line 332 of file importer_internal.cpp.

332  {
333  std::string class_name;
334  s.unpack("ImporterInternal::type", class_name);
335  if (class_name=="DllLibrary") {
336  return DllLibrary::deserialize(s);
337  } else {
338  casadi_error("Cannot deserialize type '" + class_name + "'");
339  }
340  }
static ImporterInternal * deserialize(DeserializingStream &s)
std::string class_name() const override
Get type name.

References casadi::ImporterInternal::class_name(), casadi::DllLibrary::deserialize(), and casadi::DeserializingStream::unpack().

Referenced by casadi::Importer::deserialize().

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::ImporterInternal::disp ( std::ostream &  stream,
bool  more 
) const
overridevirtualinherited

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

Implements casadi::SharedObjectInternal.

Definition at line 38 of file importer_internal.cpp.

38  {
39  }

◆ finalize()

virtual void casadi::ImporterInternal::finalize ( )
inlinevirtualinherited

Reimplemented in casadi::DllLibrary.

Definition at line 88 of file importer_internal.hpp.

88 {}

Referenced by casadi::ImporterInternal::construct().

◆ get_function()

signal_t casadi::ShellCompiler::get_function ( const std::string &  symname)
overridevirtual

Reimplemented from casadi::ImporterInternal.

Definition at line 262 of file shell_compiler.cpp.

262  {
263 #ifdef _WIN32
264  return (signal_t)GetProcAddress(handle_, TEXT(symname.c_str()));
265 #else // _WIN32
266  signal_t fcnPtr = reinterpret_cast<signal_t>(dlsym(handle_, symname.c_str()));
267  if (dlerror()) {
268  fcnPtr=nullptr;
269  dlerror(); // Reset error flags
270  }
271  return fcnPtr;
272 #endif // _WIN32
273  }
void(* signal_t)(void)
Function pointer types for the C API.

References handle_.

◆ get_meta()

std::string casadi::ImporterInternal::get_meta ( const std::string &  cmd,
casadi_int  ind = -1 
) const
inherited

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

Definition at line 283 of file importer_internal.cpp.

283  {
284  if (ind>=0) return get_meta(indexed(cmd, ind));
285  casadi_assert(has_meta(cmd), "No such command: " + cmd);
286  return meta_.at(cmd).second;
287  }
std::map< std::string, std::pair< casadi_int, std::string > > meta_
Meta data.
std::string get_meta(const std::string &cmd, casadi_int ind=-1) const
Get entry as a text.
bool has_meta(const std::string &cmd, casadi_int ind=-1) const
Does an entry exist?
static std::string indexed(const std::string &cmd, casadi_int ind)

References casadi::ImporterInternal::has_meta(), casadi::ImporterInternal::indexed(), and casadi::ImporterInternal::meta_.

◆ get_options()

const Options& casadi::ShellCompiler::get_options ( ) const
inlineoverridevirtual

Reimplemented from casadi::ImporterInternal.

Definition at line 70 of file shell_compiler.hpp.

70 { return options_;}
static const Options options_
Options.

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

186  {
187  return static_cast<const Internal*>(this)->count;
188  }

◆ getPlugin()

PluginInterface< ImporterInternal >::Plugin & casadi::PluginInterface< ImporterInternal >::getPlugin ( const std::string &  pname)
staticinherited

Definition at line 102 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_function()

bool casadi::ImporterInternal::has_function ( const std::string &  symname) const
inherited

Definition at line 214 of file importer_internal.cpp.

214  {
215  // Check if in meta information
216  if (external_.find(symname)!=external_.end()) return true;
217 
218  // Convert to a dummy function pointer
219  return const_cast<ImporterInternal*>(this)->get_function(symname)!=nullptr;
220  }
virtual signal_t get_function(const std::string &symname)
Get a function pointer for numerical evaluation.

References casadi::ImporterInternal::external_, and casadi::ImporterInternal::get_function().

◆ has_meta()

bool casadi::ImporterInternal::has_meta ( const std::string &  cmd,
casadi_int  ind = -1 
) const
inherited

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

Definition at line 289 of file importer_internal.cpp.

289  {
290  if (ind>=0) return has_meta(indexed(cmd, ind));
291  return meta_.find(cmd) != meta_.end();
292  }

References casadi::ImporterInternal::indexed(), and casadi::ImporterInternal::meta_.

Referenced by casadi::ImporterInternal::get_meta().

◆ has_plugin()

bool casadi::PluginInterface< ImporterInternal >::has_plugin ( const std::string &  pname,
bool  verbose = false 
)
staticinherited

Definition at line 76 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

◆ indexed()

static std::string casadi::ImporterInternal::indexed ( const std::string &  cmd,
casadi_int  ind 
)
inlinestaticinherited

Convert indexed command

Definition at line 150 of file importer_internal.hpp.

150  {
151  std::stringstream ss;
152  ss << cmd << "[" << ind << "]";
153  return ss.str();
154  }

Referenced by casadi::ImporterInternal::get_meta(), and casadi::ImporterInternal::has_meta().

◆ init()

void casadi::ShellCompiler::init ( const Dict opts)
overridevirtual

Reimplemented from casadi::ImporterInternal.

Definition at line 131 of file shell_compiler.cpp.

131  {
132  // Base class
134 
135  // Default options
136 
137  cleanup_ = true;
138  bool temp_suffix = true;
139  std::string bare_name = "tmp_casadi_compiler_shell";
140  std::string directory = "";
141 
142  std::vector<std::string> compiler_flags;
143  std::vector<std::string> linker_flags;
144  std::string suffix = OBJECT_FILE_SUFFIX;
145 
146 #ifdef _WIN32
147  std::string compiler = "cl.exe";
148  std::string linker = "link.exe";
149  std::string compiler_setup = "/c";
150  std::string linker_setup = "/DLL";
151  std::string compiler_output_flag = "/Fo";
152  std::string linker_output_flag = "/out:";
153  extra_suffixes_ = {".exp", ".lib"};
154 #else
155  std::string compiler = "gcc";
156  std::string linker = "gcc";
157  std::string compiler_setup = "-fPIC -c";
158  std::string linker_setup = "-shared";
159  std::string compiler_output_flag = "-o ";
160  std::string linker_output_flag = "-o ";
161 #endif
162 
163  // Read options
164  for (auto&& op : opts) {
165  if (op.first=="compiler") {
166  compiler = op.second.to_string();
167  } else if (op.first=="linker") {
168  linker = op.second.to_string();
169  } else if (op.first=="directory") {
170  directory = op.second.to_string();
171  } else if (op.first=="compiler_setup") {
172  compiler_setup = op.second.to_string();
173  } else if (op.first=="cleanup") {
174  cleanup_ = op.second;
175  } else if (op.first=="linker_setup") {
176  linker_setup = op.second.to_string();
177  } else if (op.first=="compiler_flags" || op.first=="flags") {
178  compiler_flags = op.second;
179  } else if (op.first=="linker_flags") {
180  linker_flags = op.second;
181  } else if (op.first=="compiler_output_flag") {
182  compiler_output_flag = op.second.to_string();
183  } else if (op.first=="linker_output_flag") {
184  linker_output_flag = op.second.to_string();
185  } else if (op.first=="extra_suffixes") {
186  extra_suffixes_ = op.second.to_string_vector();
187  } else if (op.first=="name") {
188  bare_name = op.second.to_string();
189  } else if (op.first=="temp_suffix") {
190  temp_suffix = op.second;
191  }
192  }
193 
194  // Name of temporary file
195  if (temp_suffix) {
196  obj_name_ = temporary_file(directory + bare_name, suffix);
197  } else {
198  obj_name_ = directory + bare_name + suffix;
199  }
200  base_name_ = std::string(obj_name_.begin(), obj_name_.begin()+obj_name_.size()-suffix.size());
201  bin_name_ = base_name_+SHARED_LIBRARY_SUFFIX;
202 
203 #ifndef _WIN32
204  // Have relative paths start with ./
205  if (obj_name_.at(0)!='/') {
206  obj_name_ = "./" + obj_name_;
207  }
208 
209  if (bin_name_.at(0)!='/') {
210  bin_name_ = "./" + bin_name_;
211  }
212 #endif // _WIN32
213 
214  // Construct the compiler command
215  std::stringstream cccmd;
216  cccmd << compiler;
217  for (auto i=compiler_flags.begin(); i!=compiler_flags.end(); ++i) {
218  cccmd << " " << *i;
219  }
220  cccmd << " " << compiler_setup;
221 
222  // C/C++ source file
223  cccmd << " " << name_;
224 
225  // Temporary object file
226  cccmd << " " + compiler_output_flag << obj_name_;
227 
228  // Compile into an object
229  if (verbose_) casadi_message("calling \"" + cccmd.str() + "\"");
230  if (system(cccmd.str().c_str())) {
231  casadi_error("Compilation failed. Tried \"" + cccmd.str() + "\"");
232  }
233 
234  // Link step
235  std::stringstream ldcmd;
236  ldcmd << linker;
237 
238  // Temporary file
239  ldcmd << " " << obj_name_ << " " + linker_output_flag + bin_name_;
240 
241  // Add flags
242  for (auto i=linker_flags.begin(); i!=linker_flags.end(); ++i) {
243  ldcmd << " " << *i;
244  }
245  ldcmd << " " << linker_setup;
246 
247  // Compile into a shared library
248  if (verbose_) casadi_message("calling \"" + ldcmd.str() + "\"");
249  if (system(ldcmd.str().c_str())) {
250  casadi_error("Linking failed. Tried \"" + ldcmd.str() + "\"");
251  }
252 
253  std::vector<std::string> search_paths = get_search_paths();
254  handle_ = open_shared_library(bin_name_, search_paths, "ShellCompiler::init");
255 
256  }
bool verbose_
Verbose – for debugging purposes.
std::string name_
C filename.
std::string temporary_file(const std::string &prefix, const std::string &suffix)
std::vector< std::string > get_search_paths()
Definition: casadi_os.cpp:49

References base_name_, bin_name_, cleanup_, extra_suffixes_, casadi::get_search_paths(), handle_, casadi::ImporterInternal::init(), casadi::ImporterInternal::name_, obj_name_, casadi::temporary_file(), and casadi::ImporterInternal::verbose_.

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ inlined()

bool casadi::ImporterInternal::inlined ( const std::string &  symname) const
inherited

Definition at line 294 of file importer_internal.cpp.

294  {
295  auto it = external_.find(symname);
296  return it!=external_.end() && it->second.first;
297  }

References casadi::ImporterInternal::external_.

Referenced by casadi::ImporterInternal::init(), and casadi::ImporterInternal::read_external().

◆ instantiate()

ImporterInternal * casadi::PluginInterface< ImporterInternal >::instantiate ( const std::string &  fname,
const std::string &  pname,
Problem  problem 
)
staticinherited

Definition at line 106 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.

◆ library()

std::string casadi::ShellCompiler::library ( ) const
overridevirtual

Reimplemented from casadi::ImporterInternal.

Definition at line 258 of file shell_compiler.cpp.

258  {
259  return bin_name_;
260  }

References bin_name_.

◆ load_library()

handle_t casadi::PluginInterface< ImporterInternal >::load_library ( const std::string &  libname,
std::string &  resultpath,
bool  global 
)
staticinherited

Definition at line 92 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  }

◆ load_plugin()

PluginInterface< ImporterInternal >::Plugin casadi::PluginInterface< ImporterInternal >::load_plugin ( const std::string &  pname,
bool  register_plugin = true,
bool  needs_lock = true 
)
staticinherited

Definition at line 88 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.
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()

◆ plugin_deserialize()

Deserialize casadi::PluginInterface< ImporterInternal >::plugin_deserialize ( const std::string &  pname)
staticinherited

Definition at line 82 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  }
ProtoFunction *(* Deserialize)(DeserializingStream &)

◆ plugin_name()

const char* casadi::ShellCompiler::plugin_name ( ) const
inlineoverridevirtual

Reimplemented from casadi::ImporterInternal.

Definition at line 80 of file shell_compiler.hpp.

80 { return "shell";}

◆ plugin_options()

const Options & casadi::PluginInterface< ImporterInternal >::plugin_options ( const std::string &  pname)
staticinherited

Definition at line 79 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()

PluginInterface< ImporterInternal >::Plugin casadi::PluginInterface< ImporterInternal >::pluginFromRegFcn ( RegFcn  regfcn)
staticinherited

Definition at line 85 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  }

◆ read_external()

void casadi::ImporterInternal::read_external ( const std::string &  sym,
bool  inlined,
std::istream &  file,
casadi_int &  offset 
)
inherited

Definition at line 179 of file importer_internal.cpp.

180  {
181  // New entry
182  std::stringstream ss;
183 
184  // Are we still in the function declaration
185  bool in_declaration = true;
186 
187  // Loop over the lines
188  std::string line;
189  while (getline(file, line)) {
190  offset++;
191 
192  // Skip line if still in declaration
193  if (in_declaration) {
194  size_t stop = line.find('{');
195  if (stop != std::string::npos) in_declaration = false;
196  continue;
197  }
198 
199  // End of declaration found?
200  if (line.find("/*CASADIEXTERNAL") != std::string::npos) {
201  auto new_el = external_.insert(std::make_pair(sym, std::make_pair(inlined, ss.str())));
202  casadi_assert(new_el.second, "Duplicate symbol: \"" + sym + "\"");
203  return;
204  }
205 
206  // Add to entry
207  if (inlined) {
208  ss << line << std::endl;
209  }
210  }
211  casadi_error("End-of-file reached while searching for \"/*CASADIEXTERNAL\"");
212  }
bool inlined(const std::string &symname) const

References casadi::ImporterInternal::external_, and casadi::ImporterInternal::inlined().

Referenced by casadi::ImporterInternal::init().

◆ read_meta()

void casadi::ImporterInternal::read_meta ( std::istream &  file,
casadi_int &  offset 
)
inherited

Definition at line 132 of file importer_internal.cpp.

132  {
133  // Loop over the lines
134  std::string line;
135  while (getline(file, line)) {
136  offset++;
137 
138  // End of meta found?
139  if (line.find("*/") != std::string::npos) return;
140 
141  // If comment or empty line, skip
142  if (line.empty() || line.at(0)=='#') continue;
143 
144  // Get command string
145  casadi_assert(line.at(0)==':',
146  "Syntax error: " + line + " is not a command string");
147  std::string cmd = line.substr(1, line.find(' ')-1);
148 
149  // New entry
150  std::stringstream ss;
151 
152  // Collect the meta data
153  line = line.substr(cmd.size()+2);
154  while (true) {
155  // Find the backslash, if any
156  size_t stop = line.find('\\');
157 
158  // Add to entry
159  ss << line.substr(0, stop);
160 
161  // Break if not multiline
162  if (stop == std::string::npos) break;
163 
164  // Read another line
165  ss << std::endl;
166  if (!getline(file, line)) {
167  casadi_error("Failed to read \"" + cmd + "\"");
168  }
169  offset++;
170  }
171 
172  // Insert new element in map
173  auto new_el = meta_.insert(std::make_pair(cmd, std::make_pair(offset, ss.str())));
174  casadi_assert(new_el.second, "Duplicate entry: \"" + cmd + "\"");
175  }
176  casadi_error("End-of-file reached while searching for \"*/\"");
177  }

References casadi::ImporterInternal::meta_.

Referenced by casadi::ImporterInternal::init().

◆ registerPlugin() [1/2]

void casadi::PluginInterface< ImporterInternal >::registerPlugin ( const Plugin &  plugin,
bool  needs_lock = true 
)
staticinherited

Definition at line 96 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  }

◆ registerPlugin() [2/2]

void casadi::PluginInterface< ImporterInternal >::registerPlugin ( RegFcn  regfcn,
bool  needs_lock = true 
)
staticinherited

Definition at line 99 of file plugin_interface.hpp.

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

◆ serialize()

void casadi::ImporterInternal::serialize ( SerializingStream s) const
inherited

Definition at line 309 of file importer_internal.cpp.

309  {
310  serialize_type(s);
311  serialize_body(s);
312  }
virtual void serialize_body(SerializingStream &s) const
virtual void serialize_type(SerializingStream &s) const

References casadi::ImporterInternal::serialize_body(), and casadi::ImporterInternal::serialize_type().

◆ serialize_body()

void casadi::ImporterInternal::serialize_body ( SerializingStream s) const
virtualinherited

Definition at line 318 of file importer_internal.cpp.

318  {
319  s.version("ImporterInternal", 1);
320  s.pack("ImporterInternal::name", name_);
321  s.pack("ImporterInternal::meta", meta_);
322  s.pack("ImporterInternal::external", external_);
323  }

References casadi::ImporterInternal::external_, casadi::ImporterInternal::meta_, casadi::ImporterInternal::name_, casadi::SerializingStream::pack(), and casadi::SerializingStream::version().

Referenced by casadi::ImporterInternal::serialize().

◆ serialize_type()

void casadi::ImporterInternal::serialize_type ( SerializingStream s) const
virtualinherited

Definition at line 314 of file importer_internal.cpp.

314  {
315  s.pack("ImporterInternal::type", class_name());
316  }

References casadi::ImporterInternal::class_name(), and casadi::SerializingStream::pack().

Referenced by casadi::ImporterInternal::serialize().

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ shortname()

static std::string casadi::ImporterInternal::shortname ( )
inlinestaticinherited

Definition at line 104 of file importer_internal.hpp.

104 { return "importer";}

◆ to_text()

std::string casadi::ImporterInternal::to_text ( const std::string &  cmd,
casadi_int  ind = -1 
) const
inherited

◆ weak()

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

Definition at line 67 of file generic_shared_internal.hpp.

191  {
192  if (weak_ref_==nullptr) {
193  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
194  }
195  return weak_ref_;
196  }

Member Data Documentation

◆ base_name_

std::string casadi::ShellCompiler::base_name_
protected

Definition at line 92 of file shell_compiler.hpp.

Referenced by init(), and ~ShellCompiler().

◆ bin_name_

std::string casadi::ShellCompiler::bin_name_
protected

Definition at line 95 of file shell_compiler.hpp.

Referenced by init(), library(), and ~ShellCompiler().

◆ cleanup_

bool casadi::ShellCompiler::cleanup_
protected

Definition at line 104 of file shell_compiler.hpp.

Referenced by init(), and ~ShellCompiler().

◆ external_

std::map<std::string, std::pair<bool, std::string> > casadi::ImporterInternal::external_
inherited

◆ extra_suffixes_

std::vector<std::string> casadi::ShellCompiler::extra_suffixes_
protected

Definition at line 101 of file shell_compiler.hpp.

Referenced by init(), and ~ShellCompiler().

◆ handle_

handle_t casadi::ShellCompiler::handle_
protected

Definition at line 107 of file shell_compiler.hpp.

Referenced by get_function(), init(), ShellCompiler(), and ~ShellCompiler().

◆ infix_

const std::string casadi::ImporterInternal::infix_ = "importer"
staticinherited

Definition at line 101 of file importer_internal.hpp.

◆ meta_

std::map<std::string, std::pair<casadi_int, std::string> > casadi::ImporterInternal::meta_
inherited

◆ meta_doc

const std::string casadi::ShellCompiler::meta_doc
static

Definition at line 77 of file shell_compiler.hpp.

Referenced by casadi::casadi_register_importer_shell().

◆ name_

std::string casadi::ImporterInternal::name_
inherited

◆ obj_name_

std::string casadi::ShellCompiler::obj_name_
protected

Definition at line 98 of file shell_compiler.hpp.

Referenced by init(), and ~ShellCompiler().

◆ options_

const Options casadi::ShellCompiler::options_
static

Definition at line 69 of file shell_compiler.hpp.

Referenced by casadi::casadi_register_importer_shell().

◆ solvers_

std::map< std::string, ImporterInternal::Plugin > casadi::ImporterInternal::solvers_
staticinherited

Definition at line 94 of file importer_internal.hpp.

◆ verbose_

bool casadi::ImporterInternal::verbose_
inherited

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