Skip to content

Commit

Permalink
Merge pull request homalg-project#1704 from zickgraf/master
Browse files Browse the repository at this point in the history
Cache precompiled functions without irreversible optimizations for future use
  • Loading branch information
zickgraf authored Oct 24, 2024
2 parents 09385b8 + 5dd0e7a commit 666c23b
Show file tree
Hide file tree
Showing 25 changed files with 4,417 additions and 565 deletions.
2 changes: 1 addition & 1 deletion CAP/PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SetPackageInfo( rec(

PackageName := "CAP",
Subtitle := "Categories, Algorithms, Programming",
Version := "2024.10-06",
Version := "2024.10-07",
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
License := "GPL-2.0-or-later",

Expand Down
2 changes: 2 additions & 0 deletions CAP/gap/CAP.gi
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ InstallGlobalFunction( "CreateCapCategoryWithDataTypes", FunctionWithNamedArgume

obj!.input_sanity_check_functions := rec( );

obj!.cached_precompiled_functions := rec( );

# convenience for Julia lists
if IsPackageMarkedForLoading( "JuliaInterface", ">= 0.2" ) then

Expand Down
7 changes: 7 additions & 0 deletions CAP/gap/InstallAdds.gi
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ InstallMethod( AddCapOperation,

Add( category!.added_functions.( function_name ), func_to_install );

# if a function for which a cached precompiled function exists is overwritten, drop the cached precompiled function
if IsBound( category!.cached_precompiled_functions.( function_name ) ) then

Unbind( category!.cached_precompiled_functions.( function_name ) );

fi;

replaced_filter_list := CAP_INTERNAL_REPLACED_STRINGS_WITH_FILTERS( record.filter_list, category );

## Nr arguments sanity check
Expand Down
2 changes: 1 addition & 1 deletion CompilerForCAP/PackageInfo.g
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SetPackageInfo( rec(

PackageName := "CompilerForCAP",
Subtitle := "Speed up and verify categorical algorithms",
Version := "2024.10-05",
Version := "2024.10-06",
Date := (function ( ) if IsBound( GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE ) then return GAPInfo.SystemEnvironment.GAP_PKG_RELEASE_DATE; else return Concatenation( ~.Version{[ 1 .. 4 ]}, "-", ~.Version{[ 6, 7 ]}, "-01" ); fi; end)( ),
License := "GPL-2.0-or-later",

Expand Down
8 changes: 4 additions & 4 deletions CompilerForCAP/examples/LinearAlgebraForCAP.g
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ Display( ENHANCED_SYNTAX_TREE_CODE( CAP_JIT_INTERNAL_POST_PROCESSED_TREE(
vec
) ) );
#! function ( cat_1, alpha_1 )
#! local morphism_attr_1_1;
#! morphism_attr_1_1 := SyzygiesOfRows( AsHomalgMatrix( alpha_1 ) );
#! local deduped_1_1;
#! deduped_1_1 := SyzygiesOfRows( AsHomalgMatrix( alpha_1 ) );
#! return
#! AsCapCategoryMorphism( cat_1,
#! AsCapCategoryObject( cat_1, NumberRows( morphism_attr_1_1 ) ),
#! morphism_attr_1_1, Source( alpha_1 ) );
#! AsCapCategoryObject( cat_1, NumberRows( deduped_1_1 ) ), deduped_1_1,
#! Source( alpha_1 ) );
#! end

#! @EndExample
10 changes: 9 additions & 1 deletion CompilerForCAP/gap/CompileCAPOperation.gi
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ InstallGlobalFunction( "CapJitCompiledCAPOperationAsEnhancedSyntaxTree", functio

if not IsBound( cat!.compiled_functions_trees.(operation_name) ) then

function_to_compile := cat!.operations.(operation_name).func;
if IsBound( cat!.cached_precompiled_functions.(operation_name) ) then

function_to_compile := cat!.cached_precompiled_functions.(operation_name);

else

function_to_compile := cat!.operations.(operation_name).func;

fi;

if IsOperation( function_to_compile ) or IsKernelFunction( function_to_compile ) then

Expand Down
60 changes: 38 additions & 22 deletions CompilerForCAP/gap/CompilerForCAP.gi
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,20 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_COMPILED_ENHANCED_SYNTAX_TREE, function

end );

InstallGlobalFunction( CAP_JIT_INTERNAL_POST_PROCESSED_TREE, function ( tree, category )
local changed, pre_func, domains, additional_arguments_func;
InstallGlobalFunction( CAP_JIT_INTERNAL_POST_PROCESSED_TREE, function ( tree, category, args... )
local apply_irreversible_optimizations, changed, pre_func, domains, additional_arguments_func;

Assert( 0, Length( args ) in [ 0, 1 ] );

if Length( args ) = 0 then

apply_irreversible_optimizations := true;

else

apply_irreversible_optimizations := args[1];

fi;

if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 1 then

Expand Down Expand Up @@ -509,7 +521,7 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_POST_PROCESSED_TREE, function ( tree, ca

fi;

tree := CapJitAppliedCompilerHints( tree, category );
tree := CapJitAppliedCompilerHints( tree, category, apply_irreversible_optimizations );

if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 2 then

Expand All @@ -526,27 +538,31 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_POST_PROCESSED_TREE, function ( tree, ca
# do not hoist/deduplicate expressions in proof assistant mode
if not CAP_JIT_PROOF_ASSISTANT_MODE_ENABLED then

# CapJitExtractedExpensiveOperationsFromLoops

if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 2 then
if apply_irreversible_optimizations then

# COVERAGE_IGNORE_BLOCK_START
Print( "#### Continue post-processing of\n" );
Print( "# ", tree.name, "\n" );
Print( "# apply CapJitExtractedExpensiveOperationsFromLoops\n" );
# COVERAGE_IGNORE_BLOCK_END
# CapJitExtractedExpensiveOperationsFromLoops

fi;

tree := CapJitExtractedExpensiveOperationsFromLoops( tree );

if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 2 then
if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 2 then

# COVERAGE_IGNORE_BLOCK_START
Print( "#### Continue post-processing of\n" );
Print( "# ", tree.name, "\n" );
Print( "# apply CapJitExtractedExpensiveOperationsFromLoops\n" );
# COVERAGE_IGNORE_BLOCK_END

fi;

# COVERAGE_IGNORE_BLOCK_START
Print( "# result:\n" );
Display( CapJitPrettyPrintFunction( ENHANCED_SYNTAX_TREE_CODE( tree ) ) );
Print( "\n" );
# COVERAGE_IGNORE_BLOCK_END
tree := CapJitExtractedExpensiveOperationsFromLoops( tree );

if CAP_JIT_INTERNAL_DEBUG_LEVEL >= 2 then

# COVERAGE_IGNORE_BLOCK_START
Print( "# result:\n" );
Display( CapJitPrettyPrintFunction( ENHANCED_SYNTAX_TREE_CODE( tree ) ) );
Print( "\n" );
# COVERAGE_IGNORE_BLOCK_END

fi;

fi;

Expand Down Expand Up @@ -651,7 +667,7 @@ InstallGlobalFunction( CAP_JIT_INTERNAL_POST_PROCESSED_TREE, function ( tree, ca
# However, in this case we might not want to apply the simplification.
# Note: In the case of a single enclosing domain which is equal to the domain, this would more or less correspond to the traditional concept of loop fusion.
# That is why we call it "generalized loop fusion".
while true do
while true and apply_irreversible_optimizations do

changed := false;

Expand Down
5 changes: 3 additions & 2 deletions CompilerForCAP/gap/CompilerHints.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#! @Section Compilation steps

#! @Description
#! Applies all compiler hints (see <Ref Sect="Section_CompilerHints" />) to <A>tree</A>.
#! Applies compiler hints (see <Ref Sect="Section_CompilerHints" />) to <A>tree</A>.
#! Depending on <A>apply_irreversible_optimizations</A>, only reversible optimizations or all optimizations are applied.
#! @Returns a record
#! @Arguments tree, category
#! @Arguments tree, category, apply_irreversible_optimizations
DeclareGlobalFunction( "CapJitAppliedCompilerHints" );

#! @Description
Expand Down
Loading

0 comments on commit 666c23b

Please sign in to comment.