diff --git a/code/code_gen/code_gen.py b/code/code_gen/code_gen.py index 0662779..351724c 100644 --- a/code/code_gen/code_gen.py +++ b/code/code_gen/code_gen.py @@ -7,6 +7,7 @@ from post import ( CompilationContext, ParserStage, DerivativeExpansionStage, + SympySimplificationStage, DependencyResolutionStage, SympyOptimizationStage, FortranProgramWriter, @@ -74,11 +75,11 @@ class VersionInfo(object): 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 - sequentially: parsing, derivative/fluctuation expansion, data dependency resolution, - and SymPy optimization (including buffer pooling). + sequentially: parsing, derivative/fluctuation expansion, SymPy expression simplification, + data dependency resolution, and array buffer pooling. Args: 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() ParserStage().execute(terms_raw, ctx) DerivativeExpansionStage().execute(ctx) + SympySimplificationStage().execute(ctx) DependencyResolutionStage().execute(ctx) SympyOptimizationStage().execute(ctx) return ctx diff --git a/code/code_gen/post.py b/code/code_gen/post.py index 11db601..143ebbd 100644 --- a/code/code_gen/post.py +++ b/code/code_gen/post.py @@ -2497,21 +2497,19 @@ class DependencyResolutionStage(object): return order -class SympyOptimizationStage(object): - """Compiler pipeline Stage 4: Optimizes mathematical equations and shares memory buffers. +class SympySimplificationStage(object): + """Compiler pipeline Stage 3: Expands, simplifies equations and prunes dependencies. This stage: - 1. Invokes the SympyOptimizer to apply algebraic simplification and CSE. - 2. Updates variable dependencies 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. + 1. Invokes the SympyOptimizer to substitute and simplify equations. + 2. Updates variable dependencies in CompilationContext to reflect optimized equations. """ def execute(self, ctx): - """Executes Stage 4 mathematical optimization and array buffer sharing. + """Executes Stage 3 mathematical expression expansion and dependency pruning. Args: - ctx (CompilationContext): Active compilation context. + ctx (CompilationContext): Active compilation context to update. """ # 1. SymPy 수식 최적화 엔진 초기화 opt = SympyOptimizer.get_instance(ctx.derived) @@ -2531,9 +2529,23 @@ class SympyOptimizationStage(object): updated_dependency[name] = dep_set 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{}" - # 3. Pass 1 및 Pass 2 연산 순서 배열들에 대해 각각 버퍼 공유 매핑(Pooling) 수행 + # 1. Pass 1 및 Pass 2 연산 순서 배열들에 대해 각각 버퍼 공유 매핑(Pooling) 수행 narr1, alloc1 = (self.allocate_arr(ctx, ctx.pass1)) narr2, alloc2 = (self.allocate_arr(ctx, ctx.pass2))