|
| 1 | +module LinearSolveAutotune |
| 2 | + |
| 3 | +using LinearSolve |
| 4 | +using BenchmarkTools |
| 5 | +using DataFrames |
| 6 | +using PrettyTables |
| 7 | +using Preferences |
| 8 | +using Statistics |
| 9 | +using Random |
| 10 | +using LinearAlgebra |
| 11 | +using Printf |
| 12 | +using Dates |
| 13 | + |
| 14 | +# Optional dependencies for telemetry and plotting |
| 15 | +using GitHub |
| 16 | +using Plots |
| 17 | + |
| 18 | +export autotune_setup |
| 19 | + |
| 20 | +include("algorithms.jl") |
| 21 | +include("gpu_detection.jl") |
| 22 | +include("benchmarking.jl") |
| 23 | +include("plotting.jl") |
| 24 | +include("telemetry.jl") |
| 25 | +include("preferences.jl") |
| 26 | + |
| 27 | +""" |
| 28 | + autotune_setup(; |
| 29 | + large_matrices::Bool = false, |
| 30 | + telemetry::Bool = true, |
| 31 | + make_plot::Bool = true, |
| 32 | + set_preferences::Bool = true, |
| 33 | + samples::Int = 5, |
| 34 | + seconds::Float64 = 0.5) |
| 35 | +
|
| 36 | +Run a comprehensive benchmark of all available LU factorization methods and optionally: |
| 37 | +
|
| 38 | + - Create performance plots |
| 39 | + - Upload results to GitHub telemetry |
| 40 | + - Set Preferences for optimal algorithm selection |
| 41 | + - Support both CPU and GPU algorithms based on hardware detection |
| 42 | +
|
| 43 | +# Arguments |
| 44 | +
|
| 45 | + - `large_matrices::Bool = false`: Include larger matrix sizes for GPU benchmarking |
| 46 | + - `telemetry::Bool = true`: Share results to GitHub issue for community data |
| 47 | + - `make_plot::Bool = true`: Generate performance plots |
| 48 | + - `set_preferences::Bool = true`: Update LinearSolve preferences with optimal algorithms |
| 49 | + - `samples::Int = 5`: Number of benchmark samples per algorithm/size |
| 50 | + - `seconds::Float64 = 0.5`: Maximum time per benchmark |
| 51 | +
|
| 52 | +# Returns |
| 53 | +
|
| 54 | + - `DataFrame`: Detailed benchmark results with performance data |
| 55 | + - `Plot`: Performance visualization (if `make_plot=true`) |
| 56 | +
|
| 57 | +# Examples |
| 58 | +
|
| 59 | +```julia |
| 60 | +using LinearSolve |
| 61 | +using LinearSolveAutotune |
| 62 | +
|
| 63 | +# Basic autotune with default settings |
| 64 | +results = autotune_setup() |
| 65 | +
|
| 66 | +# Custom autotune for GPU systems with larger matrices |
| 67 | +results = autotune_setup(large_matrices = true, samples = 10, seconds = 1.0) |
| 68 | +
|
| 69 | +# Autotune without telemetry sharing |
| 70 | +results = autotune_setup(telemetry = false) |
| 71 | +``` |
| 72 | +""" |
| 73 | +function autotune_setup(; |
| 74 | + large_matrices::Bool = false, |
| 75 | + telemetry::Bool = true, |
| 76 | + make_plot::Bool = true, |
| 77 | + set_preferences::Bool = true, |
| 78 | + samples::Int = 5, |
| 79 | + seconds::Float64 = 0.5) |
| 80 | + @info "Starting LinearSolve.jl autotune setup..." |
| 81 | + @info "Configuration: large_matrices=$large_matrices, telemetry=$telemetry, make_plot=$make_plot, set_preferences=$set_preferences" |
| 82 | + |
| 83 | + # Get system information |
| 84 | + system_info = get_system_info() |
| 85 | + @info "System detected: $(system_info["os"]) $(system_info["arch"]) with $(system_info["num_cores"]) cores" |
| 86 | + |
| 87 | + # Get available algorithms |
| 88 | + cpu_algs, cpu_names = get_available_algorithms() |
| 89 | + @info "Found $(length(cpu_algs)) CPU algorithms: $(join(cpu_names, ", "))" |
| 90 | + |
| 91 | + # Add GPU algorithms if available |
| 92 | + gpu_algs, gpu_names = get_gpu_algorithms() |
| 93 | + if !isempty(gpu_algs) |
| 94 | + @info "Found $(length(gpu_algs)) GPU algorithms: $(join(gpu_names, ", "))" |
| 95 | + end |
| 96 | + |
| 97 | + # Combine all algorithms |
| 98 | + all_algs = vcat(cpu_algs, gpu_algs) |
| 99 | + all_names = vcat(cpu_names, gpu_names) |
| 100 | + |
| 101 | + if isempty(all_algs) |
| 102 | + error("No algorithms found! This shouldn't happen.") |
| 103 | + end |
| 104 | + |
| 105 | + # Get benchmark sizes |
| 106 | + sizes = collect(get_benchmark_sizes(large_matrices)) |
| 107 | + @info "Benchmarking $(length(sizes)) matrix sizes from $(minimum(sizes)) to $(maximum(sizes))" |
| 108 | + |
| 109 | + # Run benchmarks |
| 110 | + @info "Running benchmarks (this may take several minutes)..." |
| 111 | + results_df = benchmark_algorithms(sizes, all_algs, all_names; |
| 112 | + samples = samples, seconds = seconds, large_matrices = large_matrices) |
| 113 | + |
| 114 | + # Display results table |
| 115 | + successful_results = filter(row -> row.success, results_df) |
| 116 | + if nrow(successful_results) > 0 |
| 117 | + @info "Benchmark completed successfully!" |
| 118 | + |
| 119 | + # Create summary table for display |
| 120 | + summary = combine(groupby(successful_results, :algorithm), |
| 121 | + :gflops => mean => :avg_gflops, |
| 122 | + :gflops => maximum => :max_gflops, |
| 123 | + nrow => :num_tests) |
| 124 | + sort!(summary, :avg_gflops, rev = true) |
| 125 | + |
| 126 | + println("\n" * "="^60) |
| 127 | + println("BENCHMARK RESULTS SUMMARY") |
| 128 | + println("="^60) |
| 129 | + pretty_table(summary, |
| 130 | + header = ["Algorithm", "Avg GFLOPs", "Max GFLOPs", "Tests"], |
| 131 | + formatters = ft_printf("%.2f", [2, 3]), |
| 132 | + crop = :none) |
| 133 | + else |
| 134 | + @warn "No successful benchmark results!" |
| 135 | + return results_df, nothing |
| 136 | + end |
| 137 | + |
| 138 | + # Categorize results and find best algorithms per size range |
| 139 | + categories = categorize_results(results_df) |
| 140 | + |
| 141 | + # Set preferences if requested |
| 142 | + if set_preferences && !isempty(categories) |
| 143 | + set_algorithm_preferences(categories) |
| 144 | + end |
| 145 | + |
| 146 | + # Create plot if requested |
| 147 | + plot_obj = nothing |
| 148 | + plot_files = nothing |
| 149 | + if make_plot |
| 150 | + @info "Creating performance plots..." |
| 151 | + plot_obj = create_benchmark_plot(results_df) |
| 152 | + if plot_obj !== nothing |
| 153 | + plot_files = save_benchmark_plot(plot_obj) |
| 154 | + end |
| 155 | + end |
| 156 | + |
| 157 | + # Upload telemetry if requested |
| 158 | + if telemetry && nrow(successful_results) > 0 |
| 159 | + @info "Preparing telemetry data for GitHub..." |
| 160 | + markdown_content = format_results_for_github(results_df, system_info, categories) |
| 161 | + upload_to_github(markdown_content, plot_files) |
| 162 | + end |
| 163 | + |
| 164 | + @info "Autotune setup completed!" |
| 165 | + |
| 166 | + # Return results and plot |
| 167 | + if make_plot && plot_obj !== nothing |
| 168 | + return results_df, plot_obj |
| 169 | + else |
| 170 | + return results_df |
| 171 | + end |
| 172 | +end |
| 173 | + |
| 174 | +end |
0 commit comments