9 #include "magma_types.h"
10 #include <cusolverSp.h>
11 #include <dpsim/GpuMagmaAdapter.h>
13 using namespace DPsim;
17 magma_dmfree(&mDevSysMat, mMagmaQueue);
18 magma_dmfree(&mDevRhsVec, mMagmaQueue);
19 magma_dmfree(&mDevLhsVec, mMagmaQueue);
21 magma_queue_destroy(mMagmaQueue);
25 void GpuMagmaAdapter::performFactorization(SparseMatrix &systemMatrix) {
26 int size = systemMatrix.rows();
29 auto hMat = systemMatrix;
30 size_t nnz = hMat.nonZeros();
31 cusparseMatDescr_t descr_M = 0;
33 cusolverSpHandle_t mCusolverhandle;
34 if (cusolverSpCreate(&mCusolverhandle) != CUSOLVER_STATUS_SUCCESS) {
38 if (cusparseCreateMatDescr(&descr_M) != CUSPARSE_STATUS_SUCCESS) {
43 if (cusparseSetMatIndexBase(descr_M, CUSPARSE_INDEX_BASE_ZERO) !=
44 CUSPARSE_STATUS_SUCCESS) {
48 if (cusparseSetMatType(descr_M, CUSPARSE_MATRIX_TYPE_GENERAL) !=
49 CUSPARSE_STATUS_SUCCESS) {
54 if (cusolverSpDcsrzfdHost(mCusolverhandle, size, nnz, descr_M,
55 hMat.valuePtr(), hMat.outerIndexPtr(),
56 hMat.innerIndexPtr(), p,
57 &p_nnz) != CUSOLVER_STATUS_SUCCESS) {
63 mTransp = std::unique_ptr<Eigen::PermutationMatrix<Eigen::Dynamic>>(
64 new Eigen::PermutationMatrix<Eigen::Dynamic>(
65 Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, 1>>(p, size, 1)));
69 hMat = *mTransp * hMat;
73 magma_dcsrset(size, size, hMat.outerIndexPtr(), hMat.innerIndexPtr(),
76 mMagmaOpts.solver_par.solver = Magma_PIDRMERGE;
83 mMagmaOpts.precond_par.trisolver = Magma_CUSOLVE;
88 magma_dvinit(&mDevRhsVec, Magma_DEV, size, 1, 0.0, mMagmaQueue);
89 magma_dmtransfer(
mHostSysMat, &mDevSysMat, Magma_CPU, Magma_DEV, mMagmaQueue);
90 magma_d_precondsetup(mDevSysMat, mDevRhsVec, &
mMagmaOpts.solver_par,
93 cusolverSpDestroy(mCusolverhandle);
94 cusparseDestroyMatDescr(descr_M);
99 magma_queue_create(0, &mMagmaQueue);
101 mDevSysMat = {Magma_CSR};
103 mDevRhsVec = {Magma_CSR};
105 mDevLhsVec = {Magma_CSR};
109 SparseMatrix &systemMatrix,
110 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
115 performFactorization(systemMatrix);
119 performFactorization(systemMatrix);
123 SparseMatrix &systemMatrix,
124 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
125 performFactorization(systemMatrix);
129 Matrix leftSideVector = mRightHandSideVector;
130 int size = mRightHandSideVector.rows();
133 mRightHandSideVector = *mTransp * mRightHandSideVector;
136 magma_dvset(size, 1, mRightHandSideVector.data(), &
mHostRhsVec, mMagmaQueue);
137 magma_dmtransfer(
mHostRhsVec, &mDevRhsVec, Magma_CPU, Magma_DEV, mMagmaQueue);
138 magma_dvinit(&mDevLhsVec, Magma_DEV,
mHostRhsVec.num_rows,
142 magma_d_solver(mDevSysMat, mDevRhsVec, &mDevLhsVec, &
mMagmaOpts, mMagmaQueue);
145 magma_dmtransfer(mDevLhsVec, &
mHostLhsVec, Magma_DEV, Magma_CPU, mMagmaQueue);
146 magma_dvcopy(mDevLhsVec, &size, &one, leftSideVector.data(), mMagmaQueue);
148 return leftSideVector;
magma_d_matrix mHostLhsVec
LHS-Vector.
virtual void factorize(SparseMatrix &systemMatrix) override
factorization function with partial pivoting
virtual ~GpuMagmaAdapter()
Destructor.
virtual void refactorize(SparseMatrix &systemMatrix) override
refactorization without partial pivoting
magma_dopts mMagmaOpts
Solver-Handle.
magma_d_matrix mHostRhsVec
RHS-Vector.
virtual Matrix solve(Matrix &rightSideVector) override
solution function for a right hand side
magma_d_matrix mHostSysMat
Systemmatrix.
virtual void preprocessing(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
preprocessing function pre-ordering and scaling the matrix
virtual void partialRefactorize(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
partial refactorization withouth partial pivoting
GpuMagmaAdapter()
Constructor.