Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions lib/LinearSolveAutotune/src/LinearSolveAutotune.jl
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,7 +34,6 @@ using CPUSummary
using RecursiveFactorization
using blis_jll
using LAPACK_jll
using MKL_jll
using CUDA
using Metal

Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions lib/LinearSolveAutotune/src/preferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ 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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

for (key, algorithm) in categories
if contains(key, "_")
eltype, size_range = split(key, "_", limit=2)
if !haskey(benchmarked, eltype)
benchmarked[eltype] = Dict{String, String}()
end
benchmarked[eltype][size_range] = algorithm

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

# 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

Expand Down Expand Up @@ -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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

# Set a timestamp for when these preferences were created
Preferences.set_preferences!(LinearSolve, "autotune_timestamp" => string(Dates.now()); force = true)

Expand Down Expand Up @@ -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"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

@info "Preferences cleared from LinearSolve.jl."
end

Expand Down Expand Up @@ -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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change

timestamp = Preferences.load_preference(LinearSolve, "autotune_timestamp", "unknown")
println("\nLast updated: $timestamp")
end
Loading