Skip to content

Commit e7d01ed

Browse files
Add MKL preference management to autotune system (#706)
* Add MKL preference management to autotune system - Analyze if MKL algorithms (MKLLUFactorization) perform best in any category - Write LoadMKL_JLL preference based on benchmark results - Set to false if MKL is never best to avoid loading unnecessary dependencies - Set to true if MKL wins in any category to ensure availability - Add MKL preference display in show_current_preferences - Include MKL preference clearing in clear_algorithm_preferences This optimization reduces startup time and memory usage when MKL is not beneficial for the user's workload. * Delete lib/LinearSolveAutotune/test/test_mkl_preference.jl * Force enable MKL during benchmarking to ensure availability - Set LoadMKL_JLL=true before loading LinearSolve in autotune module - This ensures MKL algorithms are available for benchmarking - Track the original preference to inform user of temporary change - Final preference is still set based on benchmark results - Added documentation explaining this behavior
1 parent 2ec08b4 commit e7d01ed

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

lib/LinearSolveAutotune/src/LinearSolveAutotune.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
module LinearSolveAutotune
22

3+
# Ensure MKL is available for benchmarking by setting the preference before loading LinearSolve
4+
using Preferences
5+
using MKL_jll
6+
7+
# Set MKL preference to true for benchmarking if MKL is available
8+
# We need to use UUID instead of the module since LinearSolve isn't loaded yet
9+
const LINEARSOLVE_UUID = Base.UUID("7ed4a6bd-45f5-4d41-b270-4a48e9bafcae")
10+
if MKL_jll.is_available()
11+
# Force load MKL for benchmarking to ensure we can test MKL algorithms
12+
# The autotune results will determine the final preference setting
13+
current_pref = Preferences.load_preference(LINEARSOLVE_UUID, "LoadMKL_JLL", nothing)
14+
if current_pref !== true
15+
Preferences.set_preferences!(LINEARSOLVE_UUID, "LoadMKL_JLL" => true; force = true)
16+
@info "Temporarily setting LoadMKL_JLL=true for benchmarking (was $(current_pref))"
17+
end
18+
end
19+
320
using LinearSolve
421
using BenchmarkTools
522
using DataFrames
623
using PrettyTables
7-
using Preferences
824
using Statistics
925
using Random
1026
using LinearAlgebra
@@ -18,7 +34,6 @@ using CPUSummary
1834
using RecursiveFactorization
1935
using blis_jll
2036
using LAPACK_jll
21-
using MKL_jll
2237
using CUDA
2338
using Metal
2439

@@ -143,6 +158,12 @@ Run a comprehensive benchmark of all available LU factorization methods and opti
143158
- Set Preferences for optimal algorithm selection
144159
- Support both CPU and GPU algorithms based on hardware detection
145160
- Test algorithm compatibility with different element types
161+
- Automatically manage MKL loading preference based on performance results
162+
163+
!!! note "MKL Preference Management"
164+
During benchmarking, MKL is temporarily enabled (if available) to test MKL algorithms.
165+
After benchmarking, the LoadMKL_JLL preference is set based on whether MKL algorithms
166+
performed best in any category. This optimizes startup time and memory usage.
146167
147168
# Arguments
148169

lib/LinearSolveAutotune/src/preferences.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,21 @@ function set_algorithm_preferences(categories::Dict{String, String})
2323

2424
# Extract benchmarked results by element type and size
2525
benchmarked = Dict{String, Dict{String, String}}()
26+
mkl_is_best_somewhere = false # Track if MKL wins any category
27+
2628
for (key, algorithm) in categories
2729
if contains(key, "_")
2830
eltype, size_range = split(key, "_", limit=2)
2931
if !haskey(benchmarked, eltype)
3032
benchmarked[eltype] = Dict{String, String}()
3133
end
3234
benchmarked[eltype][size_range] = algorithm
35+
36+
# Check if MKL algorithm is best for this category
37+
if contains(algorithm, "MKL")
38+
mkl_is_best_somewhere = true
39+
@info "MKL algorithm ($algorithm) is best for $eltype at size $size_range"
40+
end
3341
end
3442
end
3543

@@ -118,6 +126,18 @@ function set_algorithm_preferences(categories::Dict{String, String})
118126
end
119127
end
120128

129+
# Set MKL preference based on whether it was best for any category
130+
# If MKL wasn't best anywhere, disable it to avoid loading unnecessary dependencies
131+
# Note: During benchmarking, MKL is temporarily enabled to test MKL algorithms
132+
# This final preference setting determines whether MKL loads in normal usage
133+
Preferences.set_preferences!(LinearSolve, "LoadMKL_JLL" => mkl_is_best_somewhere; force = true)
134+
135+
if mkl_is_best_somewhere
136+
@info "MKL was best in at least one category - setting LoadMKL_JLL preference to true"
137+
else
138+
@info "MKL was not best in any category - setting LoadMKL_JLL preference to false to avoid loading unnecessary dependencies"
139+
end
140+
121141
# Set a timestamp for when these preferences were created
122142
Preferences.set_preferences!(LinearSolve, "autotune_timestamp" => string(Dates.now()); force = true)
123143

@@ -178,6 +198,10 @@ function clear_algorithm_preferences()
178198
Preferences.delete_preferences!(LinearSolve, "autotune_timestamp"; force = true)
179199
end
180200

201+
# Clear MKL preference
202+
Preferences.delete_preferences!(LinearSolve, "LoadMKL_JLL"; force = true)
203+
@info "Cleared MKL preference"
204+
181205
@info "Preferences cleared from LinearSolve.jl."
182206
end
183207

@@ -214,6 +238,12 @@ function show_current_preferences()
214238
end
215239
end
216240

241+
# Show MKL preference
242+
mkl_pref = Preferences.load_preference(LinearSolve, "LoadMKL_JLL", nothing)
243+
if mkl_pref !== nothing
244+
println("\nMKL Usage: $(mkl_pref ? "Enabled" : "Disabled")")
245+
end
246+
217247
timestamp = Preferences.load_preference(LinearSolve, "autotune_timestamp", "unknown")
218248
println("\nLast updated: $timestamp")
219249
end

0 commit comments

Comments
 (0)