diff --git a/lib/LinearSolveAutotune/src/LinearSolveAutotune.jl b/lib/LinearSolveAutotune/src/LinearSolveAutotune.jl index c3146e6ba..6c6bdf0d0 100644 --- a/lib/LinearSolveAutotune/src/LinearSolveAutotune.jl +++ b/lib/LinearSolveAutotune/src/LinearSolveAutotune.jl @@ -1,10 +1,26 @@ module LinearSolveAutotune +# Ensure MKL is available for benchmarking by setting the preference before loading LinearSolve +using Preferences +using MKL_jll + +# Set MKL preference to true for benchmarking if MKL is available +# We need to use UUID instead of the module since LinearSolve isn't loaded yet +const LINEARSOLVE_UUID = Base.UUID("7ed4a6bd-45f5-4d41-b270-4a48e9bafcae") +if MKL_jll.is_available() + # Force load MKL for benchmarking to ensure we can test MKL algorithms + # The autotune results will determine the final preference setting + current_pref = Preferences.load_preference(LINEARSOLVE_UUID, "LoadMKL_JLL", nothing) + if current_pref !== true + Preferences.set_preferences!(LINEARSOLVE_UUID, "LoadMKL_JLL" => true; force = true) + @info "Temporarily setting LoadMKL_JLL=true for benchmarking (was $(current_pref))" + end +end + using LinearSolve using BenchmarkTools using DataFrames using PrettyTables -using Preferences using Statistics using Random using LinearAlgebra @@ -18,7 +34,6 @@ using CPUSummary using RecursiveFactorization using blis_jll using LAPACK_jll -using MKL_jll using CUDA using Metal @@ -143,6 +158,12 @@ Run a comprehensive benchmark of all available LU factorization methods and opti - Set Preferences for optimal algorithm selection - Support both CPU and GPU algorithms based on hardware detection - Test algorithm compatibility with different element types + - Automatically manage MKL loading preference based on performance results + +!!! note "MKL Preference Management" + During benchmarking, MKL is temporarily enabled (if available) to test MKL algorithms. + After benchmarking, the LoadMKL_JLL preference is set based on whether MKL algorithms + performed best in any category. This optimizes startup time and memory usage. # Arguments diff --git a/lib/LinearSolveAutotune/src/preferences.jl b/lib/LinearSolveAutotune/src/preferences.jl index 6db7fbecc..efc171bbb 100644 --- a/lib/LinearSolveAutotune/src/preferences.jl +++ b/lib/LinearSolveAutotune/src/preferences.jl @@ -23,6 +23,8 @@ function set_algorithm_preferences(categories::Dict{String, String}) # Extract benchmarked results by element type and size benchmarked = Dict{String, Dict{String, String}}() + mkl_is_best_somewhere = false # Track if MKL wins any category + for (key, algorithm) in categories if contains(key, "_") eltype, size_range = split(key, "_", limit=2) @@ -30,6 +32,12 @@ function set_algorithm_preferences(categories::Dict{String, String}) benchmarked[eltype] = Dict{String, String}() end benchmarked[eltype][size_range] = algorithm + + # Check if MKL algorithm is best for this category + if contains(algorithm, "MKL") + mkl_is_best_somewhere = true + @info "MKL algorithm ($algorithm) is best for $eltype at size $size_range" + end end end @@ -118,6 +126,18 @@ function set_algorithm_preferences(categories::Dict{String, String}) end end + # Set MKL preference based on whether it was best for any category + # If MKL wasn't best anywhere, disable it to avoid loading unnecessary dependencies + # Note: During benchmarking, MKL is temporarily enabled to test MKL algorithms + # This final preference setting determines whether MKL loads in normal usage + Preferences.set_preferences!(LinearSolve, "LoadMKL_JLL" => mkl_is_best_somewhere; force = true) + + if mkl_is_best_somewhere + @info "MKL was best in at least one category - setting LoadMKL_JLL preference to true" + else + @info "MKL was not best in any category - setting LoadMKL_JLL preference to false to avoid loading unnecessary dependencies" + end + # Set a timestamp for when these preferences were created Preferences.set_preferences!(LinearSolve, "autotune_timestamp" => string(Dates.now()); force = true) @@ -178,6 +198,10 @@ function clear_algorithm_preferences() Preferences.delete_preferences!(LinearSolve, "autotune_timestamp"; force = true) end + # Clear MKL preference + Preferences.delete_preferences!(LinearSolve, "LoadMKL_JLL"; force = true) + @info "Cleared MKL preference" + @info "Preferences cleared from LinearSolve.jl." end @@ -214,6 +238,12 @@ function show_current_preferences() end end + # Show MKL preference + mkl_pref = Preferences.load_preference(LinearSolve, "LoadMKL_JLL", nothing) + if mkl_pref !== nothing + println("\nMKL Usage: $(mkl_pref ? "Enabled" : "Disabled")") + end + timestamp = Preferences.load_preference(LinearSolve, "autotune_timestamp", "unknown") println("\nLast updated: $timestamp") end \ No newline at end of file