1
+ """
2
+ Example demonstrating the new CudaOffloadLUFactorization and CudaOffloadQRFactorization algorithms.
3
+
4
+ This example shows how to use the new GPU offloading algorithms for solving linear systems
5
+ with different numerical properties.
6
+ """
7
+
8
+ using LinearSolve
9
+ using LinearAlgebra
10
+ using Random
11
+
12
+ # Set random seed for reproducibility
13
+ Random. seed! (123 )
14
+
15
+ println (" CUDA Offload Factorization Examples" )
16
+ println (" =" ^ 40 )
17
+
18
+ # Create a well-conditioned problem
19
+ println (" \n 1. Well-conditioned problem (condition number ≈ 10)" )
20
+ A_good = rand (100 , 100 )
21
+ A_good = A_good + 10 I # Make it well-conditioned
22
+ b_good = rand (100 )
23
+ prob_good = LinearProblem (A_good, b_good)
24
+
25
+ println (" Matrix size: $(size (A_good)) " )
26
+ println (" Condition number: $(cond (A_good)) " )
27
+
28
+ # Try to use CUDA if available
29
+ try
30
+ using CUDA
31
+
32
+ # Solve with LU (faster for well-conditioned)
33
+ println (" \n Solving with CudaOffloadLUFactorization..." )
34
+ sol_lu = solve (prob_good, CudaOffloadLUFactorization ())
35
+ println (" Solution norm: $(norm (sol_lu. u)) " )
36
+ println (" Residual norm: $(norm (A_good * sol_lu. u - b_good)) " )
37
+
38
+ # Solve with QR (more stable)
39
+ println (" \n Solving with CudaOffloadQRFactorization..." )
40
+ sol_qr = solve (prob_good, CudaOffloadQRFactorization ())
41
+ println (" Solution norm: $(norm (sol_qr. u)) " )
42
+ println (" Residual norm: $(norm (A_good * sol_qr. u - b_good)) " )
43
+
44
+ catch e
45
+ println (" \n Note: CUDA.jl is not loaded. To use GPU offloading:" )
46
+ println (" 1. Install CUDA.jl: using Pkg; Pkg.add(\" CUDA\" )" )
47
+ println (" 2. Add 'using CUDA' before running this example" )
48
+ println (" 3. Ensure you have a CUDA-compatible NVIDIA GPU" )
49
+ end
50
+
51
+ # Create an ill-conditioned problem
52
+ println (" \n 2. Ill-conditioned problem (condition number ≈ 1e6)" )
53
+ A_bad = rand (50 , 50 )
54
+ # Make it ill-conditioned
55
+ U, S, V = svd (A_bad)
56
+ S[end ] = S[1 ] / 1e6 # Create large condition number
57
+ A_bad = U * Diagonal (S) * V'
58
+ b_bad = rand (50 )
59
+ prob_bad = LinearProblem (A_bad, b_bad)
60
+
61
+ println (" Matrix size: $(size (A_bad)) " )
62
+ println (" Condition number: $(cond (A_bad)) " )
63
+
64
+ try
65
+ using CUDA
66
+
67
+ # For ill-conditioned problems, QR is typically more stable
68
+ println (" \n Solving with CudaOffloadQRFactorization (recommended for ill-conditioned)..." )
69
+ sol_qr_bad = solve (prob_bad, CudaOffloadQRFactorization ())
70
+ println (" Solution norm: $(norm (sol_qr_bad. u)) " )
71
+ println (" Residual norm: $(norm (A_bad * sol_qr_bad. u - b_bad)) " )
72
+
73
+ println (" \n Solving with CudaOffloadLUFactorization (may be less stable)..." )
74
+ sol_lu_bad = solve (prob_bad, CudaOffloadLUFactorization ())
75
+ println (" Solution norm: $(norm (sol_lu_bad. u)) " )
76
+ println (" Residual norm: $(norm (A_bad * sol_lu_bad. u - b_bad)) " )
77
+
78
+ catch e
79
+ println (" \n Skipping GPU tests (CUDA not available)" )
80
+ end
81
+
82
+ # Demonstrate the deprecation warning
83
+ println (" \n 3. Testing deprecated CudaOffloadFactorization" )
84
+ try
85
+ using CUDA
86
+ println (" Creating deprecated CudaOffloadFactorization..." )
87
+ alg = CudaOffloadFactorization () # This will show a deprecation warning
88
+ println (" The deprecated algorithm still works but shows a warning above" )
89
+ catch e
90
+ println (" Skipping deprecation test (CUDA not available)" )
91
+ end
92
+
93
+ println (" \n " * " =" ^ 40 )
94
+ println (" Summary:" )
95
+ println (" - Use CudaOffloadLUFactorization for well-conditioned problems (faster)" )
96
+ println (" - Use CudaOffloadQRFactorization for ill-conditioned problems (more stable)" )
97
+ println (" - The old CudaOffloadFactorization is deprecated" )
0 commit comments