List of all members | Public Member Functions
casadi::FunctionBuffer Class Reference

Evaluate a Function repeatedly with minimal overhead. More...

#include <function.hpp>

Detailed Description

A FunctionBuffer holds the work vectors and a memory object of a Function and evaluates it directly on caller-owned contiguous double buffers, skipping argument conversion and allocation. Bind each input with set_arg and each output with set_res (nonzeros in column-major order, see nz_from_in), then evaluate; the results appear in the bound output buffers and ret() gives the return code (0 on success). The buffers must stay alive and unchanged in size while the FunctionBuffer is used.

Function.buffer() returns a pair (FunctionBuffer, callable): the callable runs one evaluation; bind numpy arrays with memoryview.

>>> x = SX.sym("x"); y = SX.sym("y", 2)
>>> f = Function("f", [x, y], [x*y], ["x", "y"], ["r"])
>>> fb, evaluate = f.buffer()
>>> a = np.array([2.0]); b = np.array([3.0, 4.0]); r = np.zeros(2)
>>> fb.set_arg(0, memoryview(a)); fb.set_arg(1, memoryview(b))
>>> fb.set_res(0, memoryview(r))
>>> evaluate()
>>> r, fb.ret()
(array([6., 8.]), 0)
>>> a[0] = 5; evaluate(); print(r)
[15. 20.]
>>> try:
...   fb.set_arg(1, memoryview(a))
... except RuntimeError as e:
...   print("Buffer is not large enough" in str(e))
True

See also
Function::nz_from_in, Function::checkout, Function::sz_w

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

Definition at line 5263 of file function.hpp.

Public Member Functions

 FunctionBuffer (const Function &f)
 Create a buffer for f, allocating its work vectors and a memory object. More...
 
 ~FunctionBuffer ()
 
 FunctionBuffer (const FunctionBuffer &f)
 
FunctionBuffer & operator= (const FunctionBuffer &f)
 
void set_arg (casadi_int i, const double *a, casadi_int size)
 Bind a contiguous buffer holding the nonzeros of input i. More...
 
void set_res (casadi_int i, double *a, casadi_int size)
 Bind a contiguous buffer that receives the nonzeros of output i. More...
 
int ret ()
 Get the return code of the last evaluation (0 on success) More...
 
void _eval ()
 
void * _self ()
 
Dict stats () const
 Get the statistics of the memory object used by this buffer, see Function::stats. More...
 

Constructor & Destructor Documentation

◆ FunctionBuffer() [1/2]

casadi::FunctionBuffer::FunctionBuffer ( const Function &  f)

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

Definition at line 2150 of file function.cpp.

2150  : f_(f) {
2151  w_.resize(f_.sz_w());
2152  iw_.resize(f_.sz_iw());
2153  arg_.resize(f_.sz_arg());
2154  res_.resize(f_.sz_res());
2155  if (f_->checkout_) {
2156  mem_ = f_->checkout_();
2157  } else {
2158  mem_ = f_.checkout();
2159  mem_internal_ = f_.memory(mem_);
2160  }
2161  f_node_ = f.operator->();
2162  }
casadi_checkout_t checkout_
Checkout redirected to a C function.
size_t sz_res() const
Get the length of the output pointer array needed to evaluate (at least.
Definition: function.cpp:1237
casadi_int checkout() const
Reserve a memory object for evaluation and return its index.
Definition: function.cpp:1970
size_t sz_iw() const
Get the length of the integer work vector needed to evaluate.
Definition: function.cpp:1239
void * memory(int ind) const
Get memory object.
Definition: function.cpp:1978
size_t sz_w() const
Get the length of the real work vector needed to evaluate.
Definition: function.cpp:1241
size_t sz_arg() const
Get the length of the input pointer array needed to evaluate (at least n_in)
Definition: function.cpp:1235

References casadi::Function::checkout(), casadi::FunctionInternal::checkout_, casadi::Function::memory(), casadi::Function::sz_arg(), casadi::Function::sz_iw(), casadi::Function::sz_res(), and casadi::Function::sz_w().

◆ ~FunctionBuffer()

casadi::FunctionBuffer::~FunctionBuffer ( )

Definition at line 2164 of file function.cpp.

2164  {
2165  if (f_->release_) {
2166  f_->release_(mem_);
2167  } else {
2168  f_.release(mem_);
2169  }
2170  }
casadi_release_t release_
Release redirected to a C function.
void release(int mem) const
Give back a memory object obtained with checkout.
Definition: function.cpp:1974

References casadi::Function::release(), and casadi::FunctionInternal::release_.

◆ FunctionBuffer() [2/2]

casadi::FunctionBuffer::FunctionBuffer ( const FunctionBuffer &  f)

Definition at line 2172 of file function.cpp.

2173  : f_(f.f_), w_(f.w_), iw_(f.iw_), arg_(f.arg_), res_(f.res_), f_node_(f.f_node_) {
2174  if (f_->checkout_) {
2175  mem_ = f_->checkout_();
2176  } else {
2177  mem_ = f_.checkout();
2178  mem_internal_ = f_.memory(mem_);
2179  }
2180  }

References casadi::Function::checkout(), casadi::FunctionInternal::checkout_, and casadi::Function::memory().

Member Function Documentation

◆ _eval()

void casadi::FunctionBuffer::_eval ( )

Definition at line 2216 of file function.cpp.

2216  {
2217  if (f_node_->eval_) {
2218  ret_ = f_node_->eval_(get_ptr(arg_), get_ptr(res_), get_ptr(iw_), get_ptr(w_), mem_);
2219  } else {
2220  ret_ = f_node_->eval(get_ptr(arg_), get_ptr(res_), get_ptr(iw_), get_ptr(w_), mem_internal_);
2221  }
2222  }
virtual int eval(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const
Evaluate numerically.
eval_t eval_
Numerical evaluation redirected to a C function.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.

References casadi::FunctionInternal::eval(), casadi::FunctionInternal::eval_, and casadi::get_ptr().

◆ _self()

void* casadi::FunctionBuffer::_self ( )
inline

Definition at line 5311 of file function.hpp.

5311 { return this; }

◆ operator=()

FunctionBuffer & casadi::FunctionBuffer::operator= ( const FunctionBuffer &  f)

Definition at line 2182 of file function.cpp.

2182  {
2183  if (this == &f) return *this;
2184 
2185  if (f_->release_) {
2186  f_->release_(mem_);
2187  } else {
2188  f_.release(mem_);
2189  }
2190 
2191  f_ = f.f_;
2192  w_ = f.w_; iw_ = f.iw_; arg_ = f.arg_; res_ = f.res_; f_node_ = f.f_node_;
2193  // Checkout fresh memory
2194  if (f_->checkout_) {
2195  mem_ = f_->checkout_();
2196  } else {
2197  mem_ = f_.checkout();
2198  mem_internal_ = f_.memory(mem_);
2199  }
2200 
2201  return *this;
2202  }

References casadi::Function::checkout(), casadi::FunctionInternal::checkout_, casadi::Function::memory(), casadi::Function::release(), and casadi::FunctionInternal::release_.

◆ ret()

int casadi::FunctionBuffer::ret ( )

Definition at line 2223 of file function.cpp.

2223  {
2224  return ret_;
2225  }

◆ set_arg()

void casadi::FunctionBuffer::set_arg ( casadi_int  i,
const double *  a,
casadi_int  size 
)

size is the buffer size in bytes; it must be at least nnz_in(i)*sizeof(double) or an error is raised. The values are read in column-major (Fortran) order of the input sparsity.

Pass a memoryview of a numpy array: set_arg(0, memoryview(a)).

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

Definition at line 2204 of file function.cpp.

2204  {
2205  casadi_assert(size>=f_.nnz_in(i)*sizeof(double),
2206  "Buffer is not large enough. Needed " + str(f_.nnz_in(i)*sizeof(double)) +
2207  " bytes, got " + str(size) + ".");
2208  arg_.at(i) = a;
2209  }
casadi_int nnz_in() const
Get the number of structural nonzeros of one input, or of all inputs.
Definition: function.cpp:1003
std::string str(const T &v)
String representation, any type.

References casadi::Function::nnz_in(), and casadi::str().

◆ set_res()

void casadi::FunctionBuffer::set_res ( casadi_int  i,
double *  a,
casadi_int  size 
)

size is the buffer size in bytes, at least nnz_out(i)*sizeof(double). Values are written in column-major order of the output sparsity.

Pass a memoryview of a numpy array: set_res(0, memoryview(r)).

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

Definition at line 2210 of file function.cpp.

2210  {
2211  casadi_assert(size>=f_.nnz_out(i)*sizeof(double),
2212  "Buffer is not large enough. Needed " + str(f_.nnz_out(i)*sizeof(double)) +
2213  " bytes, got " + str(size) + ".");
2214  res_.at(i) = a;
2215  }
casadi_int nnz_out() const
Get the number of structural nonzeros of one output, or of all outputs.
Definition: function.cpp:1007

References casadi::Function::nnz_out(), and casadi::str().

◆ stats()

Dict casadi::FunctionBuffer::stats ( ) const

Definition at line 2231 of file function.cpp.

2231  {
2232  return f_.stats(mem_);
2233  }
Dict stats(int mem=0) const
Get the statistics recorded by the last numerical evaluation.
Definition: function.cpp:1080

References casadi::Function::stats().


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