Replace Python numerical kernels with native C execution
This commit is contained in:
1 parent
48da6be21c
commit
3b38f73fe0
227 files changed
+16801
-75499
No files matched your search
+15
-59
@@ -39,68 +39,19 @@ def simulation_warmup_enabled() -> bool:
|
||||
)
|
||||
|
||||
|
||||
def _run_numerical_warmup() -> None:
|
||||
"""Exercise only in-memory SciPy paths used by real simulations."""
|
||||
|
||||
import numpy as np
|
||||
from scipy.integrate import BDF, DOP853, LSODA, RK23, RK45, Radau, solve_ivp
|
||||
from scipy.optimize import brentq, least_squares
|
||||
from scipy.optimize._numdiff import group_columns
|
||||
from scipy.sparse import csc_matrix, csr_matrix
|
||||
|
||||
# Importing these classes is intentional even though the micro solve below
|
||||
# uses BDF: the stepwise solver selects them dynamically at runtime.
|
||||
solver_types = (BDF, DOP853, LSODA, RK23, RK45, Radau)
|
||||
if len(solver_types) != 6:
|
||||
raise RuntimeError("SciPy solver warm-up did not load every supported method.")
|
||||
def _run_native_warmup() -> None:
|
||||
"""Check the native toolchain and XML schema without running SciPy solvers.
|
||||
|
||||
sparsity = csc_matrix(np.array([[1.0]], dtype=float))
|
||||
groups = group_columns(sparsity)
|
||||
if groups.shape != (1,):
|
||||
raise RuntimeError("SciPy Jacobian grouping warm-up returned an invalid shape.")
|
||||
|
||||
integration = solve_ivp(
|
||||
lambda _time, state: -state,
|
||||
(0.0, 1.0e-4),
|
||||
np.array([1.0], dtype=float),
|
||||
method="BDF",
|
||||
t_eval=np.array([0.0, 1.0e-4], dtype=float),
|
||||
jac_sparsity=sparsity,
|
||||
rtol=1.0e-6,
|
||||
atol=1.0e-9,
|
||||
)
|
||||
if not integration.success or not np.isfinite(integration.y).all():
|
||||
raise RuntimeError("SciPy integration warm-up did not complete successfully.")
|
||||
|
||||
algebraic_sparsity = csr_matrix(np.eye(2, dtype=bool))
|
||||
algebraic = least_squares(
|
||||
lambda state: np.array(
|
||||
[state[0] - 1.0, state[1] - 2.0],
|
||||
dtype=float,
|
||||
),
|
||||
np.array([0.5, 0.5], dtype=float),
|
||||
bounds=(
|
||||
np.array([0.0, 0.0], dtype=float),
|
||||
np.array([3.0, 3.0], dtype=float),
|
||||
),
|
||||
jac_sparsity=algebraic_sparsity,
|
||||
tr_solver="lsmr",
|
||||
)
|
||||
if (
|
||||
not algebraic.success
|
||||
or not np.isfinite(algebraic.x).all()
|
||||
or not np.allclose(algebraic.x, np.array([1.0, 2.0]), atol=1.0e-8)
|
||||
):
|
||||
raise RuntimeError("SciPy algebraic warm-up did not complete successfully.")
|
||||
|
||||
root = brentq(lambda value: value - 0.5, 0.0, 1.0)
|
||||
if abs(root - 0.5) > 1.0e-12:
|
||||
raise RuntimeError("SciPy scalar root warm-up returned an invalid result.")
|
||||
|
||||
# Compile the cached v3 XSD through the same public validation path. The
|
||||
# intentionally incomplete document is never accepted or persisted.
|
||||
The model-specific executable is generated when a model is submitted.
|
||||
"""
|
||||
from app.simulation.native_codegen.build import toolchain
|
||||
from app.system_xml import validate_system_xml_document
|
||||
|
||||
if os.name != "nt":
|
||||
raise RuntimeError("Native v1 currently supports Windows x64 build packaging only.")
|
||||
toolchain()
|
||||
validate_system_xml_document(b"<System/>")
|
||||
|
||||
|
||||
@@ -114,6 +65,10 @@ def warm_up_simulation_runtime() -> SimulationWarmupReport:
|
||||
|
||||
global _WARMUP_REPORT
|
||||
|
||||
from app.simulation.backends import numeric_engine_name
|
||||
|
||||
engine = numeric_engine_name()
|
||||
|
||||
with _WARMUP_LOCK:
|
||||
if _WARMUP_REPORT is not None:
|
||||
return _WARMUP_REPORT
|
||||
@@ -126,7 +81,7 @@ def warm_up_simulation_runtime() -> SimulationWarmupReport:
|
||||
|
||||
started = perf_counter()
|
||||
try:
|
||||
_run_numerical_warmup()
|
||||
_run_native_warmup()
|
||||
except MemoryError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
@@ -142,7 +97,8 @@ def warm_up_simulation_runtime() -> SimulationWarmupReport:
|
||||
duration_ms=(perf_counter() - started) * 1000.0,
|
||||
)
|
||||
LOGGER.info(
|
||||
"Simulation runtime warm-up completed in %.1f ms.",
|
||||
"Simulation runtime warm-up (%s) completed in %.1f ms.",
|
||||
engine,
|
||||
_WARMUP_REPORT.duration_ms,
|
||||
)
|
||||
return _WARMUP_REPORT
|
||||
|
||||
Reference in new issue
Block a user