Пример #1
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            // Free unmanaged objects.

            if (_p != IntPtr.Zero)
            {
                Check(NativeMethods.cusolverSpDestroy(_p));
                _p = IntPtr.Zero;
            }

            if (_buffer != IntPtr.Zero)
            {
                Cuda.Free(_buffer);
            }

            if (d_x != IntPtr.Zero)
            {
                Cuda.Free(d_x);
            }
            if (d_b != IntPtr.Zero)
            {
                Cuda.Free(d_b);
            }

            disposed = true;
        }
Пример #2
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);
            }
        }
Пример #3
0
        /// <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;
            }
        }
Пример #4
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);
            }
        }
Пример #5
0
        protected void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            // Free unmanaged objects.

            if (_p != IntPtr.Zero)
            {
                Check(NativeMethods.cusparseDestroy(_p));
                _p = IntPtr.Zero;
            }

            if (_matDescr != IntPtr.Zero)
            {
                Check(NativeMethods.cusparseDestroyMatDescr(_matDescr));
                _matDescr = IntPtr.Zero;
            }

            if (d_ap != IntPtr.Zero)
            {
                Cuda.Free(d_ap);
            }
            if (d_ai != IntPtr.Zero)
            {
                Cuda.Free(d_ai);
            }
            if (d_ax != IntPtr.Zero)
            {
                Cuda.Free(d_ax);
            }

            disposed = true;
        }