面向 Python 和 C 的高性能代数循环提升与精确有理递推引擎
传统编译器、运行时和 JIT 引擎(如 GCC、Clang、PyPy 或 Numba)将循环视为重复的控制流序列,逐条执行指令:
$$ \text{Runtime Cost} = \mathcal{O}(N) $$
当 $N = 10^6$ 或 $10^9$ 时,顺序执行会消耗数十亿个 CPU 周期。Strilight 通过符号代数提升从根本上重新设计了循环执行方式:
$$ \vec{\mathbf{X}}(N) = \mathbf{A}^N \cdot \vec{\mathbf{X}}0 + \sum{k=0}^{N-1} \mathbf{A}^{N-1-k} \vec{\mathbf{B}} $$
Strilight 并不假装引入了什么深奥的魔法;它本质上是一个开发者体验工具。
在物理建模、科学计算和数值仿真中,工程师经常面临一个令人沮丧的两难困境:
Strilight 解决了这一困境。 你可以用任何自己偏好的直白、自然的语法来书写物理或数学概念。Strilight 会检查你的循环结构,推导出精确的闭式递推公式,并在幕后加速执行——同时保持代码库的完全可读性和简洁性。
浮点运算会引入累积截断误差($1/3 \times 3 \approx 0.9999999999999999$)。Strilight 在有理数域 $\mathbb{Q}$ 上执行仿射归纳和步长分析:
Fraction 表示,保证 100% 位精确的数学一致性。相互依赖的变量(例如,位置依赖于速度、速度依赖于加速度的物理仿真)会被自动提取为变量耦合矩阵($\mathbf{A}$)。Strilight 对 $\mathbf{A}$ 执行二进制幂运算,在不到 2 纳秒内完成数百万次迭代。
@accelerate 装饰器(工作原理)用 @accelerate 装饰任何标准 Python 函数,都会在函数定义时执行一条自动化流水线(每次调用时零运行时分析开销):
for 循环结构,并提取归纳变量。Fraction、math),且不污染模块命名空间。_loop_summary 和 _invariant_contract 附加到编译后的函数对象上,使下游编译器和验证工具能够检查底层的转移矩阵 $\mathbf{A}$。from strilight import accelerate
@accelerate
def compute_simulation(steps: int) -> int:
acc = 0
for i in range(steps):
acc += (i * 3) + 7
return acc
# Executes in O(1) time (~0.001 ms even if steps = 100,000,000)
result = compute_simulation(100_000_000)
#pragma strilight)与 Python 的动态反射不同,Strilight 中的 C 代码变换严格遵循通过 OpenMP 风格 pragma 指令实现的显式开发者契约模型。引擎绝不会隐式修改 C 源代码;仅当由显式开发者契约子句(contract、target、include、model)指示时才会执行变换:
#pragma strilight accelerate:显式授权 Strilight 将标注的 C for 循环提升为等价的闭式数学表达式。#pragma strilight fuse:显式开发者指令,指示 Strilight 将共享相同迭代域的指定相邻循环融合为统一的 $\mathcal{O}(\log N)$ 二元矩阵递推内核。// Example of contract-guided multi-loop fusion via developer directive
int simulate_motion(int n) {
int pos = 0, vel = 10;
#pragma strilight fuse
for (int i = 0; i < n; i++) {
pos += vel;
}
for (int i = 0; i < n; i++) {
vel += 2;
}
return pos;
}
CrossFileResolver)数值模拟经常在单独的头文件或配置模块中定义参数。Strilight 的 CrossFileResolver:
#include / #define 指令。SOLAR_MASS = 4 * PI * PI),而不执行任意运行时代码,也不使用不安全的 eval。table[i % P])提升为预计算前缀和的闭式公式,复杂度为 $\mathcal{O}(1)$。memset 调用或向量切片赋值(arr[:N] = ...)。在力学和天体物理模拟中(例如 $N$ 体系统、轨道力学、粒子运动学),物理实体经常在长时间内沿平滑、无扰动的轨迹运动,而不会发生突然碰撞或方向改变:
@accelerate 装饰器;在 C 中,它只是一个标准 #pragma。你可以随时添加或移除它,而无需改动算法或业务逻辑。现代 AI 编程助手(如 Claude、Gemini、GPT 或 Jules)在处理代数公式和闭式方程时表现出色。Strilight 让开发者和 AI 代理能够直接检查合成代码和数学契约:
from strilight import accelerate
@accelerate
def compute_energy(steps: int) -> int:
total = 0
for i in range(steps):
total += 15
return total
# Execute once to trigger definition-time synthesis
result = compute_energy(100)
# Inspect the underlying mathematical contract:
summary = compute_energy._loop_summary
print("Extracted Induction Formulas:", summary.to_induction_formulas())
print("Invariant Contract:", compute_energy._invariant_contract.to_dict())
你可以直接将 C 源代码传递给 accelerate_c_source,以生成可检查、人类可读的加速 C 内核:
import strilight as sl
c_source = """
long long simulate(void) {
long long total = 0;
#pragma strilight accelerate target(total)
for (int i = 0; i < 1000000; i++) {
total += 42;
}
return total;
}
"""
accelerated_c = sl.accelerate_c_source(c_source)
print(accelerated_c)
# Emits: total += (42LL * 1000000);
我们信奉工程透明度:
target、include、model)可提供确定性的转换保证。在高迭代数值循环上进行评估,比较原生执行与 Strilight 加速:
| 基准测试场景 | 迭代次数($N$) | 原生基线 | Strilight 加速 | 实测加速比 | 精度保真度 |
|---|---|---|---|---|---|
| 耦合 4x4 线性系统(Python) | $1,000,000$ | $75.2\text{ ms}$ | $0.0002\text{ ms}$ | $376,000\times$ | 100% 位精确 |
| 耦合 4x4 线性系统(GCC -O2) | $1,000,000$ | $1.1\text{ ms}$ | $0.00002\text{ ms}$ | $55,000\times$ | 100% 位精确 |
| 循环数组查找求和 | $1,000,000$ | $74.8\text{ ms}$ | $0.0044\text{ ms}$ | $17,000\times$ | 100% 位精确 |
| 行星 N 体天体力学 | $100,000$ | $7.17\text{ ms}$ | $0.051\text{ ms}$ | $140\times$ | 解析轨道一致性 |
flowchart TD
SRC["Source Code (Python / C)"] --> LIFTER["SourceLifter: AST & Pragma Parser"]
LIFTER --> RESOLV["CrossFileResolver: Static Import Resolution"]
RESOLV --> VSA["Algebraic Induction Engine: models.py"]
VSA --> MATRIX["VariableCouplingMatrix: System Transition Matrix A"]
VSA --> QFIELD["Exact Rational Domain over Q: AffineExpr"]
REDUCE --> CODEGEN["CodeGenerator: C / Python Synthesis"]
VSA --> REDUCE["Schur Reduction & Block-Diagonal Decomposition"]
CODEGEN --> OUT["O(1) / O(log N) Executable Kernel"]
Strilight 解决了科学、工程和金融领域的计算瓶颈:
LoopInvariantContract)。pip install strilight
git clone https://github.com/asama7706r-ui/strilight.git
cd strilight
pip install -e .
执行测试套件和可复现的基准测试:
# Run the 60-test unit and induction verification suite:
pytest
# Python recurrence acceleration:
python examples/01_python_recurrence_acceleration.py