MNASolverGpuSparse.h 2.31 KB
Newer Older
Philipp Fensch's avatar
Philipp Fensch committed
1
2
3
#pragma once

#include <dpsim/MNASolver.h>
4
#include <dpsim/Cuda/CudaUtility.h>
Philipp Fensch's avatar
Philipp Fensch committed
5
6
7
8
9
10
11
12
13
14
15
16

namespace DPsim {

	template <typename VarType>
    class MnaSolverGpuSparse : public MnaSolver<VarType>{
	protected:

		// #### Attributes required for GPU ####
		/// Solver-Handle
		cusparseHandle_t mCusparsehandle;

		/// Systemmatrix on Device
17
		std::unique_ptr<cuda::CudaMatrix<double, int>> mSysMat;
Philipp Fensch's avatar
Philipp Fensch committed
18
19

		/// RHS-Vector
20
		cuda::Vector<double> mGpuRhsVec;
Philipp Fensch's avatar
Philipp Fensch committed
21
		/// LHS-Vector
22
		cuda::Vector<double> mGpuLhsVec;
Philipp Fensch's avatar
Philipp Fensch committed
23
		/// Intermediate Vector
24
		cuda::Vector<double> mGpuIntermediateVec;
Philipp Fensch's avatar
Philipp Fensch committed
25
26
27
28
29
30
31
32
33
34

		/// Initialize cuSparse-library
        void initialize();
		/// ILU factorization
		void iluPreconditioner();
		///
		void solve(Real time, Int timeStepCount);

	private:
		///Required shared Variables
35
		cuda::Vector<char> pBuffer;
Philipp Fensch's avatar
Philipp Fensch committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
		cusparseMatDescr_t descr_L = nullptr;
    	cusparseMatDescr_t descr_U = nullptr;
		csrsv2Info_t info_L = nullptr;
    	csrsv2Info_t info_U = nullptr;

	public:
		MnaSolverGpuSparse(String name,
			CPS::Domain domain = CPS::Domain::DP,
			CPS::Logger::Level logLevel = CPS::Logger::Level::info);

		virtual ~MnaSolverGpuSparse();

		CPS::Task::List getTasks();

		class SolveTask : public CPS::Task {
		public:
			SolveTask(MnaSolverGpuSparse<VarType>& solver) :
				Task(solver.mName + ".Solve"), mSolver(solver) {

				for (auto it : solver.mMNAComponents) {
					if (it->template attribute<Matrix>("right_vector")->get().size() != 0)
						mAttributeDependencies.push_back(it->attribute("right_vector"));
				}
				for (auto node : solver.mNodes) {
					mModifiedAttributes.push_back(node->attribute("v"));
				}
				mModifiedAttributes.push_back(solver.attribute("left_vector"));
			}

			void execute(Real time, Int timeStepCount) { mSolver.solve(time, timeStepCount); }

		private:
			MnaSolverGpuSparse<VarType>& mSolver;
		};

		class LogTask : public CPS::Task {
		public:
			LogTask(MnaSolverGpuSparse<VarType>& solver) :
				Task(solver.mName + ".Log"), mSolver(solver) {
				mAttributeDependencies.push_back(solver.attribute("left_vector"));
				mModifiedAttributes.push_back(Scheduler::external);
			}

			void execute(Real time, Int timeStepCount) { mSolver.log(time, timeStepCount); }

		private:
			MnaSolverGpuSparse<VarType>& mSolver;
		};
    };

template class DPsim::MnaSolverGpuSparse<Real>;
template class DPsim::MnaSolverGpuSparse<Complex>;

}