/// <summary> /// Initializes a new instance of the <see cref="CuSolverContext{T}"/> class. /// </summary> /// <param name="stream">The <see cref="CudaStream"/>.</param> /// <param name="A">The sparse matrix.</param> /// <param name="transpose">A value indicating, whether the storage should be transposed.</param> /// <remarks> /// Matrix transposition is done on a storage level, meaning, for complex matrices, values will not be /// conjugated. This is necessary, because CUDA expects CSR storage, while CSparse uses CSC storage. /// /// The value of <paramref name="transpose"/> should be true for all matrix types, except real /// valued, symmetric matrices. /// </remarks> public CuSolverContext(CudaStream stream, CompressedColumnStorage <T> A, bool transpose) { Check(NativeMethods.cusolverSpCreate(ref _p)); Check(NativeMethods.cusolverSpSetStream(_p, stream.Pointer)); sizeT = Marshal.SizeOf(typeof(T)); this.stream = stream; this.matrix = A; this.transpose = transpose; }
/// <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); } }