/// <summary> /// Creates a sparse QR factorization. /// </summary> /// <param name="order">Ordering method to use.</param> /// <param name="A">Column-compressed matrix.</param> public SparseQR(CompressedColumnStorage <double> A, ColumnOrdering order) { this.m = A.RowCount; this.n = A.ColumnCount; if (this.m >= this.n) { // Ordering and symbolic analysis SymbolicAnalysis(order, A); // Numeric QR factorization Factorize(A); } else { // Ax=b is underdetermined var AT = A.Transpose(); // Ordering and symbolic analysis SymbolicAnalysis(order, AT); // Numeric QR factorization of A' Factorize(AT); } }
private void CheckForSymmetricity(CompressedColumnStorage cs) { var cr = cs.Transpose(); for (int i = 0; i < cs.NonZerosCount; i++) { if (cr.RowIndices[i] != cs.RowIndices[i]) { throw new Exception(); } if (Math.Abs(cr.Values[i] - cs.Values[i]) > 1e-5) { throw new Exception(); } } }
/// <summary> /// Initializes a new instance of the <see cref="CuSparseContext{T}"/> class. /// </summary> /// <param name="stream">The <see cref="CudaStream"/>.</param> /// <param name="A">The sparse matrix.</param> /// <param name="type">The matrix type.</param> /// <param name="transpose">A value indicating, whether the storage should be transposed.</param> public CuSparseContext(CudaStream stream, CompressedColumnStorage <T> A, MatrixType type, bool transpose) { Check(NativeMethods.cusparseCreate(ref _p)); Check(NativeMethods.cusparseSetStream(_p, stream.Pointer)); Check(NativeMethods.cusparseCreateMatDescr(ref _matDescr)); Check(NativeMethods.cusparseSetMatType(_matDescr, type)); Check(NativeMethods.cusparseSetMatIndexBase(_matDescr, IndexBase.Zero)); var sizeT = Marshal.SizeOf(typeof(T)); int rows = A.RowCount; int nnz = A.NonZerosCount; Cuda.Malloc(ref d_ap, sizeof(int) * (rows + 1)); Cuda.Malloc(ref d_ai, sizeof(int) * nnz); Cuda.Malloc(ref d_ax, sizeT * nnz); var handles = new List <GCHandle>(); try { // Convert storage to CSR format. var C = transpose ? A.Transpose(true) : A; var h_ap = InteropHelper.Pin(C.ColumnPointers, handles); var h_ai = InteropHelper.Pin(C.RowIndices, handles); var h_ax = InteropHelper.Pin(C.Values, handles); Cuda.CopyToDevice(d_ap, h_ap, sizeof(int) * (rows + 1)); Cuda.CopyToDevice(d_ai, h_ai, sizeof(int) * nnz); Cuda.CopyToDevice(d_ax, h_ax, sizeT * nnz); } finally { InteropHelper.Free(handles); } }
/// <summary> /// Creates a sparse QR factorization. /// </summary> /// <param name="A">Column-compressed matrix, symmetric positive definite.</param> /// <param name="order">Ordering method to use (natural or A+A').</param> public static SparseQR Create(CompressedColumnStorage <double> A, ColumnOrdering order, IProgress progress) { Check.NotNull(A, "A"); int m = A.RowCount; int n = A.ColumnCount; var C = new SparseQR(m, n); if (m >= n) { var p = AMD.Generate(A, order); // Ordering and symbolic analysis C.SymbolicAnalysis(A, p, order == ColumnOrdering.Natural); // Numeric QR factorization C.Factorize(A, progress); } else { // Ax=b is underdetermined var AT = A.Transpose(); var p = AMD.Generate(AT, order); // Ordering and symbolic analysis C.SymbolicAnalysis(AT, p, order == ColumnOrdering.Natural); // Numeric QR factorization of A' C.Factorize(AT, progress); } return(C); }