/// <summary> /// Factorize the sparse matrix associated to the solver instance. /// </summary> public void Factorize() { if (_buffer != IntPtr.Zero) { throw new Exception("Context already created."); } int rows = matrix.RowCount; int columns = matrix.ColumnCount; Cuda.Malloc(ref d_x, sizeT * columns); Cuda.Malloc(ref d_b, sizeT * rows); // TODO: can the original matrix really be disposed after factorization? using (var cusparse = new CuSparseContext <T>(stream, matrix, MatrixType.General, transpose)) { PrepareFactorize(); // Start the timer after the first call to cusolver. var timer = Stopwatch.StartNew(); Factorize(rows, columns, matrix.NonZerosCount, cusparse); factorized = true; timer.Stop(); FactorizationTime = TimeSpan.FromTicks(timer.ElapsedTicks).TotalSeconds; } }
/// <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); } }