Beispiel #1
0
        /// <summary>
        /// Solves a system of linear equations, Ax = b.
        /// </summary>
        /// <param name="input">Right hand side vector b.</param>
        /// <param name="result">Solution vector x.</param>
        public void Solve(T[] input, T[] result)
        {
            if (!factorized)
            {
                Factorize();
            }

            var handles = new List <GCHandle>();

            try
            {
                var h_b = InteropHelper.Pin(input, handles);
                var h_x = InteropHelper.Pin(result, handles);

                int rows    = input.Length;
                int columns = result.Length;

                Cuda.CopyToDevice(d_b, h_b, sizeT * rows);

                // Solve A*x = b.
                Check(Solve(rows, columns));

                Cuda.CopyToHost(h_x, d_x, sizeT * columns);
            }
            finally
            {
                InteropHelper.Free(handles);
            }
        }
Beispiel #2
0
        /// <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);
            }
        }