FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
direct_sparse_solver_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
6// includes, FEAT
8#include <kernel/backend.hpp>
9#include <kernel/util/cuda_util.hpp>
10#include <kernel/solver/direct_sparse_solver.hpp>
11
12#ifdef FEAT_HAVE_CUDSS
13
14FEAT_DISABLE_WARNINGS
15#include <cudss.h>
16FEAT_RESTORE_WARNINGS
17
18namespace FEAT
19{
20 namespace Solver
21 {
22 namespace DSS
23 {
24 static_assert(sizeof(CUDSS_IT) == 4, "DirectSparseSolver: cuDSS: index type size mismatch!");
25
31 class CUDSS_Core
32 {
33 public:
34#ifdef FEAT_HAVE_MPI
35 MPI_Comm mpi_comm;
36#endif
37 cudssHandle_t handle;
38 cudssConfig_t config;
39 cudssData_t data;
40 cudssMatrix_t matrix;
41 cudssMatrix_t vec_sol;
42 cudssMatrix_t vec_rhs;
43
44 // peak memory usage estimate
45 std::int64_t memory_estimates[16];
46
47 // system dimensions
48 std::int64_t num_global_dofs, dof_offset, num_owned_dofs, num_owned_nzes, num_global_nzes, num_rhs;
49
50 // system data in host memory
51 std::vector<CUDSS_IT> row_ptr_host, col_idx_host;
52 std::vector<CUDSS_DT> mat_val_host, rhs_val_host, sol_val_host;
53
54 // system data in device memory
55 void *row_ptr_dev, *col_idx_dev;
56 void *mat_val_dev, *rhs_val_dev, *sol_val_dev;
57
58 explicit CUDSS_Core(const Dist::Comm& comm, Index num_global_dofs_, Index dof_offset_,
59 Index num_owned_dofs_, Index num_owned_nzes_, Index num_global_nzes_) :
60#ifdef FEAT_HAVE_MPI
61 mpi_comm(comm.mpi_comm()),
62#endif
63 handle(reinterpret_cast<cudssHandle_t>(Runtime::get_cudss_handle())),
64 config(nullptr),
65 data(nullptr),
66 matrix(nullptr),
67 vec_sol(nullptr),
68 vec_rhs(nullptr),
69 num_global_dofs(std::uint32_t(num_global_dofs_)),
70 dof_offset(std::uint32_t(dof_offset_)),
71 num_owned_dofs(std::uint32_t(num_owned_dofs_)),
72 num_owned_nzes(std::uint32_t(num_owned_nzes_)),
73 num_global_nzes(std::uint32_t(num_global_nzes_)),
74 num_rhs(1),
75 row_ptr_host(),
76 col_idx_host(),
77 mat_val_host(),
78 rhs_val_host(),
79 sol_val_host(),
80 row_ptr_dev(nullptr),
81 col_idx_dev(nullptr),
82 mat_val_dev(nullptr),
83 rhs_val_dev(nullptr),
84 sol_val_dev(nullptr)
85 {
86 // prevent unused parameter warnings
87 (void)comm;
88
89 XASSERTM(handle != nullptr, "Failed to retrieve cuDSS handle!");
90
91 if(CUDSS_STATUS_SUCCESS != cudssConfigCreate(&config))
92 throw InternalError(__func__, __FILE__, __LINE__, "cudssConfigCreate failed!");
93
94 if(CUDSS_STATUS_SUCCESS != cudssDataCreate(handle, &data))
95 throw InternalError(__func__, __FILE__, __LINE__, "cudssDataCreate failed!");
96
97#ifdef FEAT_HAVE_MPI
98 if(CUDSS_STATUS_SUCCESS != cudssDataSet(handle, data, CUDSS_DATA_COMM, &mpi_comm, sizeof(MPI_Comm*)))
99 throw InternalError(__func__, __FILE__, __LINE__, "cudssDataSet for 'CUDSS_DATA_COMM' failed!");
100#endif
101
102 // format memory estimates
103 Memory::memset_main(memory_estimates, 0, sizeof(memory_estimates));
104
105 // allocate matrix and vector arrays in host memory
106 row_ptr_host.resize(std::size_t(num_owned_dofs+1), 0u);
107 col_idx_host.resize(std::size_t(num_owned_nzes), 0u);
108 mat_val_host.resize(std::size_t(num_owned_nzes), 0.0);
109 rhs_val_host.resize(std::size_t(num_owned_dofs * num_rhs), 0.0);
110 sol_val_host.resize(std::size_t(num_owned_dofs * num_rhs), 0.0);
111
112 // allocate matrix and vector arrays in device memory
113 row_ptr_dev = Memory::alloc_cuda(sizeof(CUDSS_IT) * std::size_t(num_owned_dofs+1u));
114 col_idx_dev = Memory::alloc_cuda(sizeof(CUDSS_IT) * std::size_t(num_owned_nzes));
115 mat_val_dev = Memory::alloc_cuda(sizeof(CUDSS_DT) * std::size_t(num_owned_nzes));
116 rhs_val_dev = Memory::alloc_cuda(sizeof(CUDSS_DT) * std::size_t(num_owned_dofs * num_rhs));
117 sol_val_dev = Memory::alloc_cuda(sizeof(CUDSS_DT) * std::size_t(num_owned_dofs * num_rhs));
118 }
119
120 ~CUDSS_Core()
121 {
122 if(vec_sol)
123 cudssMatrixDestroy(vec_sol);
124 if(vec_rhs)
125 cudssMatrixDestroy(vec_rhs);
126 if(matrix)
127 cudssMatrixDestroy(matrix);
128
129 if(sol_val_dev)
130 Memory::free_cuda(sol_val_dev);
131 if(rhs_val_dev)
132 Memory::free_cuda(rhs_val_dev);
133 if(mat_val_dev)
134 Memory::free_cuda(mat_val_dev);
135 if(col_idx_dev)
136 Memory::free_cuda(col_idx_dev);
137 if(row_ptr_dev)
138 Memory::free_cuda(row_ptr_dev);
139 if(data)
140 cudssDataDestroy(handle, data);
141 if(config)
142 cudssConfigDestroy(config);
143
144 // wait until CUDA is done
145 Runtime::synchronize_devices();
146 }
147
148 void init_symbolic()
149 {
150 cudssStatus_t ret = CUDSS_STATUS_INTERNAL_ERROR;
151
152 // first and last DOF owned by this process
153 const std::int64_t last_owned_dof = dof_offset + num_owned_dofs - 1;
154
155 // copy structure host arrays to device
156 Memory::memcopy_main_to_cuda(row_ptr_dev, row_ptr_host.data(), sizeof(CUDSS_IT) * std::size_t(num_owned_dofs+1u));
157 Memory::memcopy_main_to_cuda(col_idx_dev, col_idx_host.data(), sizeof(CUDSS_IT) * std::size_t(num_owned_nzes));
158
159 // set the basic configuration
160 //cudssAlgType_t alg_type = CUDSS_ALG_DEFAULT;//CUDSS_ALG_1; // COLAMD based ordering
161 //ret = cudssConfigSet(config, CUDSS_CONFIG_REORDERING_ALG, &alg_type, sizeof(cudssAlgType_t));
162 //if(ret != CUDSS_STATUS_SUCCESS)
163 //throw DirectSparseSolverException("cuDSS", "cudssConfigSet() for 'CUDSS_CONFIG_REORDERING_ALG' failed!");
164
165 // set matrix data
166 ret = cudssMatrixCreateCsr(
167 &matrix,
168 num_global_dofs,
169 num_global_dofs,
170 num_global_nzes,
171 row_ptr_dev,
172 nullptr,
173 col_idx_dev,
174 mat_val_dev,
175 CUDA_R_32I,
176 CUDA_R_64F,
177 CUDSS_MTYPE_GENERAL,
178 CUDSS_MVIEW_FULL, // is ignored
179 CUDSS_BASE_ZERO);
180 if(ret != CUDSS_STATUS_SUCCESS)
181 throw DirectSparseSolverException("cuDSS", "cudssMatrixCreateCsr() for system matrix failed!");
182
183 // set row distribution of matrix
184 ret = cudssMatrixSetDistributionRow1d(matrix, dof_offset, last_owned_dof);
185 if(ret != CUDSS_STATUS_SUCCESS)
186 throw DirectSparseSolverException("cuDSS", "cudssMatrixSetDistributionRow1d() for solution vector failed!");
187
188 // allocate solution vector
189 ret = cudssMatrixCreateDn(
190 &vec_sol,
191 num_global_dofs,
192 num_rhs,
193 num_global_dofs,
194 nullptr,
195 CUDA_R_64F,
196 CUDSS_LAYOUT_COL_MAJOR);
197 if(ret != CUDSS_STATUS_SUCCESS)
198 throw DirectSparseSolverException("cuDSS", "cudssMatrixCreateDn() for solution vector failed!");
199
200 // set row distribution of solution vector
201 ret = cudssMatrixSetDistributionRow1d(vec_sol, dof_offset, last_owned_dof);
202 if(ret != CUDSS_STATUS_SUCCESS)
203 throw DirectSparseSolverException("cuDSS", "cudssMatrixSetDistributionRow1d() for solution vector failed!");
204
205 // allocate rhs vector
206 ret = cudssMatrixCreateDn(
207 &vec_rhs,
208 num_global_dofs,
209 num_rhs,
210 num_global_dofs,
211 nullptr,
212 CUDA_R_64F,
213 CUDSS_LAYOUT_COL_MAJOR);
214 if(ret != CUDSS_STATUS_SUCCESS)
215 throw DirectSparseSolverException("cuDSS", "cudssMatrixCreateDn() for rhs vector failed!");
216
217 // set row distribution of solution vector
218 ret = cudssMatrixSetDistributionRow1d(vec_rhs, dof_offset, last_owned_dof);
219 if(ret != CUDSS_STATUS_SUCCESS)
220 throw DirectSparseSolverException("cuDSS", "cudssMatrixSetDistributionRow1d() for rhs vector failed!");
221
222 // perform symbolic factorization
223 ret = cudssExecute(
224 handle,
225 CUDSS_PHASE_ANALYSIS,
226 config,
227 data,
228 matrix,
229 vec_sol,
230 vec_rhs);
231 if(ret != CUDSS_STATUS_SUCCESS)
232 throw DirectSparseSolverException("cuDSS", "cudssExecute() for phase 'CUDSS_PHASE_ANALYSIS' failed!");
233
234 // retrieve memory estimates for current device
235 std::size_t bytes_written(0u);
236 ret = cudssDataGet(
237 handle,
238 data,
239 CUDSS_DATA_MEMORY_ESTIMATES,
240 memory_estimates,
241 sizeof(memory_estimates),
242 &bytes_written);
243 if(ret != CUDSS_STATUS_SUCCESS)
244 throw DirectSparseSolverException("cuDSS", "cudssDataGet() for 'CUDSS_DATA_MEMORY_ESTIMATES' failed!");
245
246 // wait until CUDA is done
247 Runtime::synchronize_devices();
248 }
249
250 void init_numeric()
251 {
252 // copy value host arrays to device
253 Memory::memcopy_main_to_cuda(mat_val_dev, mat_val_host.data(), sizeof(CUDSS_DT) * std::size_t(num_owned_nzes));
254
255 // perform numeric factorization
256 cudssStatus_t ret = cudssExecute(
257 handle,
258 CUDSS_PHASE_FACTORIZATION,
259 config,
260 data,
261 matrix,
262 vec_sol,
263 vec_rhs);
264 if(ret != CUDSS_STATUS_SUCCESS)
265 throw DirectSparseSolverException("cuDSS", "cudssExecute() for phase 'CUDSS_PHASE_FACTORIZATION' failed!");
266
267 // wait until CUDA is done
268 Runtime::synchronize_devices();
269 }
270
271 void solve()
272 {
273 cudssStatus_t ret = CUDSS_STATUS_INTERNAL_ERROR;
274
275 // copy RHS value array to device
276 Memory::memcopy_main_to_cuda(rhs_val_dev, rhs_val_host.data(), sizeof(CUDSS_DT) * std::size_t(num_owned_dofs * num_rhs));
277
278 // set solution vector data array
279 ret = cudssMatrixSetValues(vec_sol, sol_val_dev);
280 if(ret != CUDSS_STATUS_SUCCESS)
281 throw DirectSparseSolverException("cuDSS", "cudssMatrixSetValues() failed for vec_sol!");
282
283 // set rhs vector data array
284 cudssMatrixSetValues(vec_rhs, rhs_val_dev);
285 if(ret != CUDSS_STATUS_SUCCESS)
286 throw DirectSparseSolverException("cuDSS", "cudssMatrixSetValues() failed for vec_rhs!");
287
288 // solve
289 ret = cudssExecute(
290 handle,
291 CUDSS_PHASE_SOLVE,
292 config,
293 data,
294 matrix,
295 vec_sol,
296 vec_rhs);
297 if(ret != CUDSS_STATUS_SUCCESS)
298 throw DirectSparseSolverException("cuDSS", "cudssExecute() for phase 'CUDSS_PHASE_SOLVE' failed!");
299
300 // copy sol values array to host
301 Memory::memcopy_cuda_to_main(sol_val_host.data(), sol_val_dev, sizeof(CUDSS_DT) * std::size_t(num_owned_dofs * num_rhs));
302
303 // wait until CUDA is done
304 Runtime::synchronize_devices();
305 }
306
307 std::int64_t get_peak_mem_device() const
308 {
309 return memory_estimates[1];
310 }
311
312 std::int64_t get_peak_mem_host() const
313 {
314 return memory_estimates[3];
315 }
316 }; // class CUDSS_Core
317
318 void* create_cudss_core(const Dist::Comm* comm, Index num_global_dofs, Index dof_offset,
319 Index num_owned_dofs, Index num_owned_nzes, Index num_global_nzes)
320 {
321 return new CUDSS_Core(*comm, num_global_dofs, dof_offset, num_owned_dofs, num_owned_nzes, num_global_nzes);
322 }
323
324 void destroy_cudss_core(void* core)
325 {
326 XASSERT(core != nullptr);
327 delete reinterpret_cast<CUDSS_Core*>(core);
328 }
329
330 CUDSS_IT* get_cudss_row_ptr(void* core)
331 {
332 XASSERT(core != nullptr);
333 return reinterpret_cast<CUDSS_Core*>(core)->row_ptr_host.data();
334 }
335
336 CUDSS_IT* get_cudss_col_idx(void* core)
337 {
338 XASSERT(core != nullptr);
339 return reinterpret_cast<CUDSS_Core*>(core)->col_idx_host.data();
340 }
341
342 CUDSS_DT* get_cudss_mat_val(void* core)
343 {
344 XASSERT(core != nullptr);
345 return reinterpret_cast<CUDSS_Core*>(core)->mat_val_host.data();
346 }
347
348 CUDSS_DT* get_cudss_rhs_val(void* core)
349 {
350 XASSERT(core != nullptr);
351 return reinterpret_cast<CUDSS_Core*>(core)->rhs_val_host.data();
352 }
353
354 CUDSS_DT* get_cudss_sol_val(void* core)
355 {
356 XASSERT(core != nullptr);
357 return reinterpret_cast<CUDSS_Core*>(core)->sol_val_host.data();
358 }
359
360 void init_cudss_symbolic(void* core)
361 {
362 XASSERT(core != nullptr);
363 reinterpret_cast<CUDSS_Core*>(core)->init_symbolic();
364 }
365
366 void init_cudss_numeric(void* core)
367 {
368 XASSERT(core != nullptr);
369 reinterpret_cast<CUDSS_Core*>(core)->init_numeric();
370 }
371
372 void solve_cudss(void* core)
373 {
374 XASSERT(core != nullptr);
375 reinterpret_cast<CUDSS_Core*>(core)->solve();
376 }
377
378 std::int64_t get_peak_mem_cudss_host(void* core)
379 {
380 XASSERT(core != nullptr);
381 return reinterpret_cast<CUDSS_Core*>(core)->get_peak_mem_host();
382 }
383
384 std::int64_t get_peak_mem_cudss_device(void* core)
385 {
386 XASSERT(core != nullptr);
387 return reinterpret_cast<CUDSS_Core*>(core)->get_peak_mem_device();
388 }
389 } // namespace DSS
390 } // namespace Solver
391} // namespace FEAT
392
393#else // no FEAT_HAVE_CUDSS
394
395void feat_direct_sparse_solver_cudss_dummy()
396{
397}
398
399#endif // FEAT_HAVE_CUDSS
#define XASSERT(expr)
Assertion macro definition.
Definition: assertion.hpp:262
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
Definition: assertion.hpp:263
FEAT Kernel base header.
Status solve(SolverBase< Vector_ > &solver, Vector_ &vec_sol, const Vector_ &vec_rhs, const Matrix_ &matrix, const Filter_ &filter)
Solve linear system with initial solution guess.
Definition: base.hpp:347
FEAT namespace.
Definition: adjactor.hpp:12