Module: Ignis::Solver::SVD
- Defined in:
- lib/nvruby/solver/svd.rb
Overview
Singular Value Decomposition (SVD) operations using cuSOLVER Computes A = U * Σ * V^T decomposition
Constant Summary collapse
- JOB_ALL =
Job types for SVD computation
"A".ord
- JOB_SLIM =
Compute all m columns of U / all n rows of V^T
"S".ord
- JOB_OVERWRITE =
Compute min(m,n) columns of U / rows of V^T
"O".ord
- JOB_NONE =
Overwrite A with U or V^T
"N".ord
Class Method Summary collapse
-
.cond(matrix) ⇒ Float
Compute condition number using SVD.
-
.gesvd(matrix, full_matrices: false, compute_uv: true) ⇒ Hash
Compute SVD of a matrix.
-
.rank(matrix, tol: nil) ⇒ Integer
Compute matrix rank using SVD.
-
.singular_values(matrix) ⇒ NvArray
Compute only singular values (faster than full SVD).
Class Method Details
.cond(matrix) ⇒ Float
Compute condition number using SVD
127 128 129 130 131 132 133 134 |
# File 'lib/nvruby/solver/svd.rb', line 127 def cond(matrix) s = singular_values(matrix) s_host = s.to_a return Float::INFINITY if s_host.empty? || s_host.last.zero? s_host.first / s_host.last end |
.gesvd(matrix, full_matrices: false, compute_uv: true) ⇒ Hash
Compute SVD of a matrix
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/nvruby/solver/svd.rb', line 23 def gesvd(matrix, full_matrices: false, compute_uv: true) CuSolverBindings.ensure_loaded! validate_matrix!(matrix) m, n = matrix.shape lda = m min_mn = [m, n].min # Determine job type if compute_uv jobu = full_matrices ? JOB_ALL : JOB_SLIM jobvt = full_matrices ? JOB_ALL : JOB_SLIM else jobu = JOB_NONE jobvt = JOB_NONE end # Allocate output arrays s = allocate_singular_values(min_mn, matrix.dtype) u = allocate_u_matrix(m, min_mn, full_matrices, compute_uv, matrix.dtype) vt = allocate_vt_matrix(n, min_mn, full_matrices, compute_uv, matrix.dtype) # Dimensions ldu = compute_uv ? (full_matrices ? m : m) : 1 ldvt = compute_uv ? (full_matrices ? n : min_mn) : 1 # Get workspace size lwork_ptr = FFI::MemoryPointer.new(:int) get_gesvd_buffer_size(m, n, lwork_ptr, matrix.dtype) lwork = lwork_ptr.read_int # Allocate workspace workspace = CUDA::Memory.new(lwork * dtype_size(matrix.dtype)) rwork = CUDA::Memory.new(min_mn * dtype_size(real_dtype(matrix.dtype))) info = CUDA::Memory.new(4) # Copy matrix to avoid overwriting original work_matrix = matrix.dup # Perform SVD perform_gesvd( jobu, jobvt, m, n, work_matrix.device_ptr, lda, s, u, ldu, vt, ldvt, workspace, lwork, rwork, info, matrix.dtype ) # Check info info_value = read_device_int(info) if info_value < 0 raise CuSolverError.new("SVD: parameter #{-info_value} had an illegal value", cusolver_code: CuSolverBindings::CUSOLVER_STATUS_INVALID_VALUE) elsif info_value > 0 raise CuSolverError.new("SVD: #{info_value} superdiagonals did not converge", cusolver_code: CuSolverBindings::CUSOLVER_STATUS_EXECUTION_FAILED) end CUDA::RuntimeAPI.cudaDeviceSynchronize # Create result arrays result = { s: create_singular_values_array(s, min_mn, matrix.dtype) } if compute_uv result[:u] = create_u_array(u, m, min_mn, full_matrices, matrix.dtype) result[:vt] = create_vt_array(vt, n, min_mn, full_matrices, matrix.dtype) end result ensure workspace&.free! if defined?(workspace) && workspace rwork&.free! if defined?(rwork) && rwork info&.free! if defined?(info) && info end |
.rank(matrix, tol: nil) ⇒ Integer
Compute matrix rank using SVD
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/nvruby/solver/svd.rb', line 112 def rank(matrix, tol: nil) s = singular_values(matrix) s_host = s.to_a return 0 if s_host.empty? max_sv = s_host.first tol ||= [matrix.shape[0], matrix.shape[1]].max * Float::EPSILON * max_sv s_host.count { |sv| sv > tol } end |
.singular_values(matrix) ⇒ NvArray
Compute only singular values (faster than full SVD)
103 104 105 106 |
# File 'lib/nvruby/solver/svd.rb', line 103 def singular_values(matrix) result = gesvd(matrix, compute_uv: false) result[:s] end |