Refactor: Run SymPy expression simplification before dependency resolution
All checks were successful
CI Test Suite / run-tests (push) Successful in 1m13s
Deploy Documentation / build-and-deploy (push) Successful in 56s

This commit is contained in:
ignis 2026-06-04 12:55:58 +00:00
parent 070dbf9dd7
commit 5f4efb740c
2 changed files with 26 additions and 12 deletions

View file

@ -7,6 +7,7 @@ from post import (
CompilationContext, CompilationContext,
ParserStage, ParserStage,
DerivativeExpansionStage, DerivativeExpansionStage,
SympySimplificationStage,
DependencyResolutionStage, DependencyResolutionStage,
SympyOptimizationStage, SympyOptimizationStage,
FortranProgramWriter, FortranProgramWriter,
@ -74,11 +75,11 @@ class VersionInfo(object):
def compile_terms(terms_raw): def compile_terms(terms_raw):
"""Parses DSL terms spec and runs it through the four compiler stages. """Parses DSL terms spec and runs it through the five compiler stages.
This function initializes a new `CompilationContext` and executes the compiler pipeline This function initializes a new `CompilationContext` and executes the compiler pipeline
sequentially: parsing, derivative/fluctuation expansion, data dependency resolution, sequentially: parsing, derivative/fluctuation expansion, SymPy expression simplification,
and SymPy optimization (including buffer pooling). data dependency resolution, and array buffer pooling.
Args: Args:
terms_raw (str): The raw string contents of the DSL terms specification input file. terms_raw (str): The raw string contents of the DSL terms specification input file.
@ -89,6 +90,7 @@ def compile_terms(terms_raw):
ctx = CompilationContext() ctx = CompilationContext()
ParserStage().execute(terms_raw, ctx) ParserStage().execute(terms_raw, ctx)
DerivativeExpansionStage().execute(ctx) DerivativeExpansionStage().execute(ctx)
SympySimplificationStage().execute(ctx)
DependencyResolutionStage().execute(ctx) DependencyResolutionStage().execute(ctx)
SympyOptimizationStage().execute(ctx) SympyOptimizationStage().execute(ctx)
return ctx return ctx

View file

@ -2497,21 +2497,19 @@ class DependencyResolutionStage(object):
return order return order
class SympyOptimizationStage(object): class SympySimplificationStage(object):
"""Compiler pipeline Stage 4: Optimizes mathematical equations and shares memory buffers. """Compiler pipeline Stage 3: Expands, simplifies equations and prunes dependencies.
This stage: This stage:
1. Invokes the SympyOptimizer to apply algebraic simplification and CSE. 1. Invokes the SympyOptimizer to substitute and simplify equations.
2. Updates variable dependencies to reflect optimized equations. 2. Updates variable dependencies in CompilationContext to reflect optimized equations.
3. Runs liveness window analysis to map multiple non-overlapping temporary variables
to a limited set of shared XYZ buffers (array pooling) to prevent RAM exhaustion.
""" """
def execute(self, ctx): def execute(self, ctx):
"""Executes Stage 4 mathematical optimization and array buffer sharing. """Executes Stage 3 mathematical expression expansion and dependency pruning.
Args: Args:
ctx (CompilationContext): Active compilation context. ctx (CompilationContext): Active compilation context to update.
""" """
# 1. SymPy 수식 최적화 엔진 초기화 # 1. SymPy 수식 최적화 엔진 초기화
opt = SympyOptimizer.get_instance(ctx.derived) opt = SympyOptimizer.get_instance(ctx.derived)
@ -2531,9 +2529,23 @@ class SympyOptimizationStage(object):
updated_dependency[name] = dep_set updated_dependency[name] = dep_set
ctx.dependency = updated_dependency ctx.dependency = updated_dependency
class SympyOptimizationStage(object):
"""Compiler pipeline Stage 5: Performs liveness analysis and memory buffer sharing.
This stage runs liveness window analysis to map multiple non-overlapping temporary
variables to a limited set of shared XYZ buffers (array pooling) to prevent RAM exhaustion.
"""
def execute(self, ctx):
"""Executes Stage 5 array buffer sharing based on topologically sorted lists.
Args:
ctx (CompilationContext): Active compilation context.
"""
self.array_name = "xyzbuffer{}" self.array_name = "xyzbuffer{}"
# 3. Pass 1 및 Pass 2 연산 순서 배열들에 대해 각각 버퍼 공유 매핑(Pooling) 수행 # 1. Pass 1 및 Pass 2 연산 순서 배열들에 대해 각각 버퍼 공유 매핑(Pooling) 수행
narr1, alloc1 = (self.allocate_arr(ctx, ctx.pass1)) narr1, alloc1 = (self.allocate_arr(ctx, ctx.pass1))
narr2, alloc2 = (self.allocate_arr(ctx, ctx.pass2)) narr2, alloc2 = (self.allocate_arr(ctx, ctx.pass2))