FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
cudss.cpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
7
8#ifdef FEAT_HAVE_CUDSS
9#include <kernel/util/cuda_util.hpp>
10#include <kernel/solver/cudss.hpp>
11
12#include <cudss.h>
13
14namespace FEAT
15{
16 namespace Solver
17 {
18 namespace Intern
19 {
20 struct CUDSSCore
21 {
22 cudssHandle_t handle;
23 cudssConfig_t config;
24 cudssData_t data;
25 cudssMatrix_t matrix;
26 cudssMatrix_t vec_sol;
27 cudssMatrix_t vec_rhs;
28 Memory::Arbiter row_ptr;
29 Memory::Arbiter col_idx;
30 //Memory::Arbiter mat_val;
31 };
32 } // namespace Intern
33
34 CUDSS::CUDSS(const MatrixType& system_matrix) :
35 BaseClass(),
36 _system_matrix(system_matrix),
37 _cudss_core(new Intern::CUDSSCore)
38 {
39 Intern::CUDSSCore& core = *reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
40 core.handle = reinterpret_cast<cudssHandle_t>(Runtime::get_cudss_handle());
41 if(CUDSS_STATUS_SUCCESS != cudssConfigCreate(&core.config))
42 throw InternalError(__func__, __FILE__, __LINE__, "cudssConfigCreate failed!");
43 if(CUDSS_STATUS_SUCCESS != cudssDataCreate(core.handle, &core.data))
44 throw InternalError(__func__, __FILE__, __LINE__, "cudssDataCreate failed!");
45 }
46
47 CUDSS::~CUDSS()
48 {
49 Intern::CUDSSCore* core = reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
50 if(core)
51 {
52 cudssDataDestroy(core->handle, core->data);
53 cudssConfigDestroy(core->config);
54 delete core;
55 }
56 }
57
58 void CUDSS::init_symbolic()
59 {
60 BaseClass::init_symbolic();
61
62 Intern::CUDSSCore& core = *reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
63
64 cudssStatus_t ret = CUDSS_STATUS_INTERNAL_ERROR;
65
66 // set the basic configuration
67 cudssAlgType_t alg_type = CUDSS_ALG_1; // COLAMD based ordering
68 ret = cudssConfigSet(core.config, CUDSS_CONFIG_REORDERING_ALG, &alg_type, sizeof(cudssAlgType_t));
69 if(ret != CUDSS_STATUS_SUCCESS)
70 throw SolverException("CUDSS: cudssConfigSet() for 'CUDSS_CONFIG_REORDERING_ALG' failed!");
71
72 // get the index arrays of the matrix
73 const Index neq = _system_matrix.num_rows();
74 const Index nze = _system_matrix.num_nzes();
75
76 // convert indices to 32 bit if necessary
77 if constexpr (sizeof(Index) == 4u)
78 {
79 core.row_ptr = _system_matrix.row_ptr_arbiter().attach();
80 core.col_idx = _system_matrix.col_idx_arbiter().attach();
81 }
82 else
83 {
84 core.row_ptr = Memory::Arbiter(std::size_t(4u*(neq+1u)));
85 core.col_idx = Memory::Arbiter(std::size_t(4u*nze));
86 core.row_ptr.template convert<std::uint32_t, Index>(_system_matrix.row_ptr_arbiter(), Memory::Location::main);
87 core.col_idx.template convert<std::uint32_t, Index>(_system_matrix.col_idx_arbiter(), Memory::Location::main);
88 }
89
90 // get device pointers
91 const Memory::TypedView<std::uint32_t> row_ptr_view(core.row_ptr.view(Memory::Location::cuda));
92 const Memory::TypedView<std::uint32_t> col_idx_view(core.col_idx.view(Memory::Location::cuda));
93 const Memory::TypedView<double> val_view(_system_matrix.val_view_r(Memory::Location::cuda));
94
95 // set matrix data
96 ret = cudssMatrixCreateCsr(
97 &core.matrix,
98 std::int64_t(neq),
99 std::int64_t(neq),
100 std::int64_t(nze),
101 const_cast<std::uint32_t*>(row_ptr_view.get_r()),
102 nullptr,
103 const_cast<std::uint32_t*>(col_idx_view.get_r()),
104 const_cast<double*>(val_view.get_r()),
105 CUDA_R_32I,
106 CUDA_R_64F,
107 CUDSS_MTYPE_GENERAL,
108 CUDSS_MVIEW_FULL, // is ignored
109 CUDSS_BASE_ZERO);
110 if(ret != CUDSS_STATUS_SUCCESS)
111 throw SolverException("CUDSS: cudssMatrixCreateCsr() for system matrix failed!");
112
113 // allocate solution vector
114 ret = cudssMatrixCreateDn(
115 &core.vec_sol,
116 std::int64_t(neq),
117 1,
118 std::int64_t(neq),
119 nullptr,
120 CUDA_R_64F,
121 CUDSS_LAYOUT_COL_MAJOR);
122
123 if(ret != CUDSS_STATUS_SUCCESS)
124 throw SolverException("CUDSS: cudssMatrixCreateDn() for solution vector failed!");
125
126 // allocate rhs vector
127 ret = cudssMatrixCreateDn(
128 &core.vec_rhs,
129 std::int64_t(neq),
130 1,
131 std::int64_t(neq),
132 nullptr,
133 CUDA_R_64F,
134 CUDSS_LAYOUT_COL_MAJOR);
135
136 if(ret != CUDSS_STATUS_SUCCESS)
137 throw SolverException("CUDSS: cudssMatrixCreateDn() for rhs vector failed!");
138
139 // perform symbolic factorization
140 ret = cudssExecute(
141 core.handle,
142 CUDSS_PHASE_ANALYSIS,
143 core.config,
144 core.data,
145 core.matrix,
146 core.vec_sol,
147 core.vec_rhs);
148 if(ret != CUDSS_STATUS_SUCCESS)
149 throw SolverException("CUDSS: cudssExecute() for phase 'CUDSS_PHASE_ANALYSIS' failed!");
150
151 // wait until CUDA is done
152 Util::cuda_synchronize();
153 }
154
155 void CUDSS::done_symbolic()
156 {
157 BaseClass::done_symbolic();
158 if(_cudss_core == nullptr)
159 return;
160
161 Intern::CUDSSCore& core = *reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
162
163 cudssMatrixDestroy(core.vec_sol);
164 cudssMatrixDestroy(core.vec_rhs);
165 cudssMatrixDestroy(core.matrix);
166
167 core.col_idx.release();
168 core.row_ptr.release();
169
170 // wait until CUDA is done
171 Util::cuda_synchronize();
172 }
173
174 void CUDSS::init_numeric()
175 {
176 BaseClass::init_numeric();
177
178 Intern::CUDSSCore& core = *reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
179
180 // get a cuda view of the numeric data to ensure that its uploaded to device memory
181 const Memory::TypedView<double> val_view(_system_matrix.val_view_r(Memory::Location::cuda));
182
183 // perform numeric factorization
184 cudssStatus_t ret = cudssExecute(
185 core.handle,
186 CUDSS_PHASE_FACTORIZATION,
187 core.config,
188 core.data,
189 core.matrix,
190 core.vec_sol,
191 core.vec_rhs);
192 if(ret != CUDSS_STATUS_SUCCESS)
193 throw SolverException("CUDSS: cudssExecute() for phase 'CUDSS_PHASE_FACTORIZATION' failed!");
194
195 val_view.release();
196
197 // wait until CUDA is done
198 Util::cuda_synchronize();
199 }
200
201 void CUDSS::done_numeric()
202 {
203 // nothing to do here
204 BaseClass::done_numeric();
205 }
206
207 Status CUDSS::apply(VectorType& vec_sol, const VectorType& vec_rhs)
208 {
209 Intern::CUDSSCore& core = *reinterpret_cast<Intern::CUDSSCore*>(_cudss_core);
210
211 cudssStatus_t ret = CUDSS_STATUS_INTERNAL_ERROR;
212
213 Memory::TypedView<double> sol_view(vec_sol.elements_view_w(Memory::Location::cuda));
214 Memory::TypedView<double> rhs_view(vec_rhs.elements_view_r(Memory::Location::cuda));
215
216 // set solution vector data array
217 ret = cudssMatrixSetValues(core.vec_sol, sol_view.get_w());
218 if(ret != CUDSS_STATUS_SUCCESS)
219 throw SolverException("CUDSS: cudssMatrixSetValues() failed for vec_sol!");
220
221 // set rhs vector data array
222 cudssMatrixSetValues(core.vec_rhs, const_cast<double*>(rhs_view.get_r()));
223 if(ret != CUDSS_STATUS_SUCCESS)
224 throw SolverException("CUDSS: cudssMatrixSetValues() failed for vec_rhs!");
225
226 // solve
227 ret = cudssExecute(
228 core.handle,
229 CUDSS_PHASE_SOLVE,
230 core.config,
231 core.data,
232 core.matrix,
233 core.vec_sol,
234 core.vec_rhs);
235
236 // wait until CUDA is done
237 Util::cuda_synchronize();
238
239 return (ret == CUDSS_STATUS_SUCCESS ? Status::success : Status::aborted);
240 }
241 } // namespace Solver
242} // namespace FEAT
243#else
244// insert dummy function to suppress linker warnings
245void dummy_function_cudss() {}
246#endif // FEAT_HAVE_CUDSS
FEAT Kernel base header.
CUDSS(const MatrixType &system_matrix)
Constructor.
Status
Solver status return codes enumeration.
Definition: base.hpp:47
FEAT namespace.
Definition: adjactor.hpp:12
std::uint64_t Index
Index data type.