9 #include <dpsim/GpuDenseAdapter.h>
11 using namespace DPsim;
22 cudaFree(mDeviceCopy.
matrix);
23 cudaFree(mDeviceCopy.
vector);
25 cudaFree(mDeviceCopy.
pivSeq);
34 cusolverStatus_t status = CUSOLVER_STATUS_SUCCESS;
35 cudaError_t error = cudaSuccess;
36 if ((status = cusolverDnCreate(&
mCusolverHandle)) != CUSOLVER_STATUS_SUCCESS)
37 std::cerr <<
"cusolverDnCreate() failed (initializing cusolver-library)"
39 if ((error = cudaStreamCreateWithFlags(&
mStream, cudaStreamNonBlocking)) !=
41 std::cerr << cudaGetErrorString(error) << std::endl;
43 CUSOLVER_STATUS_SUCCESS)
44 std::cerr <<
"cusolverDnSetStream() failed" << std::endl;
47 void GpuDenseAdapter::allocateDeviceMemory() {
51 cudaMalloc((
void **)&mDeviceCopy.
vector, mDeviceCopy.
size *
sizeof(Real)))
54 cudaMalloc((
void **)&mDeviceCopy.
matrix,
55 mDeviceCopy.
size * mDeviceCopy.
size *
sizeof(Real)))
58 cudaMalloc((
void **)&mDeviceCopy.
pivSeq, mDeviceCopy.
size *
sizeof(Real)))
60 CUDA_ERROR_HANDLER(cudaMalloc((
void **)&mDeviceCopy.
errInfo,
sizeof(
int)))
63 int workSpaceSize = 0;
64 cusolverStatus_t status = CUSOLVER_STATUS_SUCCESS;
65 if ((status = cusolverDnDgetrf_bufferSize(
67 mDeviceCopy.
matrix, mDeviceCopy.
size, &workSpaceSize)) !=
68 CUSOLVER_STATUS_SUCCESS)
69 std::cerr <<
"cusolverDnDgetrf_bufferSize() failed (calculating required "
70 "space for LU-factorization)"
72 CUDA_ERROR_HANDLER(cudaMalloc((
void **)&mDeviceCopy.
workSpace, workSpaceSize))
75 void GpuDenseAdapter::copySystemMatrixToDevice(Matrix systemMatrix) {
76 auto *data = systemMatrix.data();
78 cudaMemcpy(mDeviceCopy.
matrix, data,
79 mDeviceCopy.
size * mDeviceCopy.
size *
sizeof(Real),
80 cudaMemcpyHostToDevice))
83 void GpuDenseAdapter::LUfactorization() {
85 cusolverStatus_t status = CUSOLVER_STATUS_SUCCESS;
94 CUDA_ERROR_HANDLER(cudaDeviceSynchronize())
96 if (status != CUSOLVER_STATUS_SUCCESS) {
97 std::cerr <<
"cusolverDnDgetrf() failed (calculating LU-factorization)"
100 CUDA_ERROR_HANDLER(cudaMemcpy(&info, mDeviceCopy.
errInfo,
sizeof(
int),
101 cudaMemcpyDeviceToHost))
103 std::cerr << -info <<
"-th parameter is wrong" << std::endl;
108 SparseMatrix &systemMatrix,
109 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
110 mDeviceCopy.
size = systemMatrix.rows();
113 allocateDeviceMemory();
115 copySystemMatrixToDevice(Matrix(systemMatrix));
127 copySystemMatrixToDevice(Matrix(systemMatrix));
136 copySystemMatrixToDevice(Matrix(systemMatrix));
144 SparseMatrix &systemMatrix,
145 std::vector<std::pair<UInt, UInt>> &listVariableSystemMatrixEntries) {
147 copySystemMatrixToDevice(Matrix(systemMatrix));
155 Matrix leftSideVector = mRightHandSideVector;
157 CUDA_ERROR_HANDLER(cudaMemcpy(mDeviceCopy.
vector, &mRightHandSideVector(0),
158 mDeviceCopy.
size *
sizeof(Real),
159 cudaMemcpyHostToDevice))
161 cusolverStatus_t status = cusolverDnDgetrs(
166 CUDA_ERROR_HANDLER(cudaDeviceSynchronize())
168 if (status != CUSOLVER_STATUS_SUCCESS)
169 std::cerr <<
"cusolverDnDgetrs() failed (Solving A*x = b)" << std::endl;
171 CUDA_ERROR_HANDLER(cudaMemcpy(&info, mDeviceCopy.
errInfo,
sizeof(
int),
172 cudaMemcpyDeviceToHost))
174 std::cerr << -info <<
"-th parameter is wrong" << std::endl;
177 CUDA_ERROR_HANDLER(cudaMemcpy(&(leftSideVector)(0), mDeviceCopy.
vector,
178 mDeviceCopy.
size *
sizeof(Real),
179 cudaMemcpyDeviceToHost))
182 return leftSideVector;
GpuDenseAdapter()
Constructor.
cusolverDnHandle_t mCusolverHandle
Solver-Handle.
virtual Matrix solve(Matrix &rightSideVector) override
solution function for a right hand side
cudaStream_t mStream
Stream.
virtual void factorize(SparseMatrix &systemMatrix) override
factorization function with partial pivoting
virtual void partialRefactorize(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
partial refactorization withouth partial pivoting
virtual void preprocessing(SparseMatrix &systemMatrix, std::vector< std::pair< UInt, UInt >> &listVariableSystemMatrixEntries) override
preprocessing function pre-ordering and scaling the matrix
virtual ~GpuDenseAdapter()
Destructor.
virtual void refactorize(SparseMatrix &systemMatrix) override
refactorization without partial pivoting
double * matrix
Device copy of System-Matrix.
double * vector
Device copy of Vector.
int * pivSeq
Pivoting-Sequence.
UInt size
Size of one dimension.
double * workSpace
Device-Workspace for getrf.