archimedes.codegen¶
- archimedes.codegen(func: ~typing.Callable | ~archimedes._core._function._compile.FunctionCache, args: ~typing.Sequence[~typing.Any], static_argnums: int | ~typing.Sequence[int] | None = None, static_argnames: str | ~typing.Sequence[str] | None = None, return_names: ~typing.Sequence[str] | None = None, kwargs: dict[str, ~typing.Any] | None = None, float_type: type = <class 'float'>, int_type: type = <class 'int'>, input_descriptions: dict[str, str] | None = None, output_descriptions: dict[str, str] | None = None, output_dir: str | None = None, options: dict[str, ~typing.Any] | None = None) None ¶
Generate C/C++ code from a compiled function.
Creates standalone C or C++ code that implements the computational graph defined by the function. This allows Archimedes models to be deployed on embedded systems, integrated into C/C++ codebases, or compiled to native executables for maximum performance.
- Parameters:
func (Callable | FunctionCache) – The compiled function to generate code for. If not already a FunctionCache, it will be compiled automatically.
args (tuple) – Arguments to the function that specify shapes and dtypes. These can be: - SymbolicArray objects - NumPy arrays with the same shape and dtype as expected inputs - The actual values for static arguments Note: For dynamic arguments, the numeric values are ignored.
static_argnums (tuple, optional) – The indices of the static arguments to the function. Will be ignored if func is already a FunctionCache.
static_argnames (tuple, optional) – The names of the static arguments to the function. Will be ignored if func is already a FunctionCache.
return_names (tuple, optional) – The names of the return values of the function. Ignored if func is already a FunctionCache. For the sake of readability, this argument is required to be provided, either directly or when separately compiling the function to a FunctionCache.
kwargs (dict, optional) – Keyword arguments to pass to the function during specialization.
float_type (type, default=float) – The C type to use for floating point numbers.
int_type (type, default=int) – The C type to use for integers.
input_descriptions (dict[str, str], optional) – Descriptions for the input arguments. Used for generating comments in the code.
output_descriptions (dict[str, str], optional) – Descriptions for the output values. Used for generating comments in the code.
output_dir (str, optional) – Path where the generated code will be written.
options (dict, optional) –
Additional options for code generation. This can include:
verbose: If True, include additional comments in the generated code.
with_mem: If True, generate a simplified C API with memory helpers.
indent: The number of spaces to use for indentation in the generated code.
- Returns:
The function writes the generated code to the specified file(s).
- Return type:
None
Notes
When to use this function: - For deploying models on embedded systems or hardware without Python - For integrating Archimedes algorithms into C/C++ applications - For maximum runtime performance by removing Python interpretation overhead - For creating standalone, portable implementations of your algorithm
This function specializes your computational graph to specific input shapes and types, then uses CasADi’s code generation capabilities to produce C code that implements the same computation. The generated code has no dependencies on Archimedes, CasADi, or Python.
Currently, this function uses CasADi’s code generation directly, so the generated code will contain CASADI_* prefixes and follow CasADi’s conventions. The function will also generate an “interface” API layer with struct definitions for inputs and outputs, along with convenience functions for initialization and function calls.
To store numerical constants in the generated code, either: 1. “Close over” the values in your function definition, or 2. Pass them as hashable static arguments
Examples
>>> import numpy as np >>> import archimedes as arc >>> >>> # Define a simple function >>> @arc.compile ... def rotate(x, theta): ... R = np.array([ ... [np.cos(theta), -np.sin(theta)], ... [np.sin(theta), np.cos(theta)], ... ], like=x) ... return R @ x >>> >>> # Create templates with appropriate shapes and dtypes >>> x_type = np.zeros((2,), dtype=float) >>> theta_type = np.array(0.0, dtype=float) >>> >>> # Generate C code >>> arc.codegen(rotate, (x_type, theta_type))
The above code will generate files including ‘rotate.c’ and ‘rotate.h’ that implement the rotation function in C.
To use numerical constants, declaring arguments as static will fix the value in the generated code:
>>> @arc.compile(static_argnames=("scale",)) ... def scaled_rotation(x, theta, scale=2.0): ... R = np.array([ ... [np.cos(theta), -np.sin(theta)], ... [np.sin(theta), np.cos(theta)], ... ], like=x) ... return scale * (R @ x) >>> >>> arc.codegen(scaled_rotation, (x_type, theta_type, 5.0))
See also
compile
Create a compiled function for use with codegen