Evaluate a Function repeatedly with minimal overhead. More...
#include <function.hpp>
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.
>>> 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
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... | |
| casadi::FunctionBuffer::FunctionBuffer | ( | const Function & | f | ) |
Extra doc: https://github.com/casadi/casadi/wiki/L_1ya
| void casadi::FunctionBuffer::_eval | ( | ) |
|
inline |
Definition at line 5311 of file function.hpp.
| int casadi::FunctionBuffer::ret | ( | ) |
| 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.
Extra doc: https://github.com/casadi/casadi/wiki/L_1yb
| 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.
Extra doc: https://github.com/casadi/casadi/wiki/L_1yc
| Dict casadi::FunctionBuffer::stats | ( | ) | const |