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...
 
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()

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

Member Function Documentation

◆ _eval()

void casadi::FunctionBuffer::_eval ( )

◆ _self()

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

Definition at line 5311 of file function.hpp.

◆ ret()

int casadi::FunctionBuffer::ret ( )

◆ 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

◆ 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

◆ stats()

Dict casadi::FunctionBuffer::stats ( ) const

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