13# ifdef HYPRE_SEQUENTIAL
14# error FEAT_HAVE_MPI and HYPRE_SEQUENTIAL are both defined
16# ifndef HYPRE_HAVE_MPI
17# define HYPRE_HAVE_MPI 1
21# error FEAT_HAVE_MPI is not defined, but HYPRE_HAVE_MPI is defined
23# ifndef HYPRE_SEQUENTIAL
24# define HYPRE_SEQUENTIAL 1
30#include <HYPRE_parcsr_ls.h>
49 const HYPRE_Int _dof_offset;
50 const HYPRE_Int _num_owned_dofs;
51 const HYPRE_Int _num_nonzeros;
53 std::vector<HYPRE_Int> _hypre_dof_idx;
55 std::vector<HYPRE_Int> _hypre_num_nze;
57 std::vector<HYPRE_Int> _hypre_row_ptr;
59 std::vector<HYPRE_Int> _hypre_col_idx;
61 std::vector<HYPRE_Real> _hypre_mat_val;
63 std::vector<HYPRE_Real> _hypre_def_val;
65 std::vector<HYPRE_Real> _hypre_cor_val;
69 HYPRE_IJMatrix _hypre_matrix;
71 HYPRE_IJVector _hypre_vec_def, _hypre_vec_cor;
73 HYPRE_ParCSRMatrix _hypre_parcsr;
75 HYPRE_ParVector _hypre_par_def, _hypre_par_cor;
77 explicit Core(
const void* comm, Index my_dof_offset, Index num_owned_dofs, Index num_nonzeros) :
79 _comm(*reinterpret_cast<const MPI_Comm*>(comm)),
81 _dof_offset(HYPRE_Int(my_dof_offset)),
82 _num_owned_dofs(HYPRE_Int(num_owned_dofs)),
83 _num_nonzeros(HYPRE_Int(num_nonzeros)),
87 _hypre_matrix(nullptr),
88 _hypre_vec_def(nullptr),
89 _hypre_vec_cor(nullptr),
90 _hypre_parcsr(nullptr),
91 _hypre_par_def(nullptr),
92 _hypre_par_cor(nullptr)
98 _hypre_dof_idx.resize(std::size_t(_num_owned_dofs));
99 for(HYPRE_Int i(0); i < _num_owned_dofs; ++i)
100 _hypre_dof_idx[std::size_t(i)] = _dof_offset + i;
103 const HYPRE_Int ilower = _hypre_dof_idx.front();
104 const HYPRE_Int iupper = _hypre_dof_idx.back();
107 HYPRE_IJMatrixCreate(_comm, ilower, iupper, ilower, iupper, &_hypre_matrix);
108 HYPRE_IJMatrixSetObjectType(_hypre_matrix, HYPRE_PARCSR);
109 HYPRE_IJMatrixInitialize(_hypre_matrix);
112 HYPRE_IJVectorCreate(_comm, ilower, iupper, &_hypre_vec_def);
113 HYPRE_IJVectorCreate(_comm, ilower, iupper, &_hypre_vec_cor);
114 HYPRE_IJVectorSetObjectType(_hypre_vec_def, HYPRE_PARCSR);
115 HYPRE_IJVectorSetObjectType(_hypre_vec_cor, HYPRE_PARCSR);
116 HYPRE_IJVectorInitialize(_hypre_vec_def);
117 HYPRE_IJVectorInitialize(_hypre_vec_cor);
120 _hypre_num_nze.resize(std::size_t(_num_owned_dofs));
121 _hypre_row_ptr.resize(std::size_t(_num_owned_dofs+1));
122 _hypre_col_idx.resize(std::size_t(_num_nonzeros));
123 _hypre_mat_val.resize(std::size_t(_num_nonzeros));
124 _hypre_def_val.resize(std::size_t(_num_owned_dofs));
125 _hypre_cor_val.resize(std::size_t(_num_owned_dofs));
130 HYPRE_IJVectorDestroy(_hypre_vec_cor);
131 HYPRE_IJVectorDestroy(_hypre_vec_def);
132 HYPRE_IJMatrixDestroy(_hypre_matrix);
135 void upload_symbolic()
138 std::size_t n = _hypre_num_nze.size();
139 for(std::size_t i(0); i < n; ++i)
140 _hypre_num_nze[i] = _hypre_row_ptr[i+1] - _hypre_row_ptr[i];
143 HYPRE_IJMatrixSetValues(_hypre_matrix, _num_owned_dofs, _hypre_num_nze.data(), _hypre_dof_idx.data(),
144 _hypre_col_idx.data(), _hypre_mat_val.data());
147 HYPRE_IJVectorSetValues(_hypre_vec_def, _num_owned_dofs, _hypre_dof_idx.data(), _hypre_def_val.data());
148 HYPRE_IJVectorSetValues(_hypre_vec_cor, _num_owned_dofs, _hypre_dof_idx.data(), _hypre_cor_val.data());
151 HYPRE_IJMatrixAssemble(_hypre_matrix);
152 HYPRE_IJMatrixGetObject(_hypre_matrix, (
void**) &_hypre_parcsr);
155 HYPRE_IJVectorAssemble(_hypre_vec_def);
156 HYPRE_IJVectorAssemble(_hypre_vec_cor);
157 HYPRE_IJVectorGetObject(_hypre_vec_def, (
void **) &_hypre_par_def);
158 HYPRE_IJVectorGetObject(_hypre_vec_cor, (
void **) &_hypre_par_cor);
161 void upload_mat_val()
163 HYPRE_IJMatrixSetValues(_hypre_matrix, _num_owned_dofs, _hypre_num_nze.data(), _hypre_dof_idx.data(),
164 _hypre_col_idx.data(), _hypre_mat_val.data());
167 void upload_vec_def()
169 HYPRE_IJVectorSetValues(_hypre_vec_def, _num_owned_dofs, _hypre_dof_idx.data(), _hypre_def_val.data());
172 void download_vec_cor()
174 HYPRE_IJVectorGetValues(_hypre_vec_cor, _num_owned_dofs, _hypre_dof_idx.data(), _hypre_cor_val.data());
177 void format_vec_cor()
179 HYPRE_ParVectorSetConstantValues(_hypre_par_cor, HYPRE_Real(0));
182 HYPRE_Int* get_row_ptr()
184 return _hypre_row_ptr.data();
187 HYPRE_Int* get_col_idx()
189 return _hypre_col_idx.data();
192 HYPRE_Real* get_mat_val()
194 return _hypre_mat_val.data();
197 HYPRE_Real* get_vec_def()
199 return _hypre_def_val.data();
202 HYPRE_Real* get_vec_cor()
204 return _hypre_cor_val.data();
208 void* create_core(
const void* comm, Index dof_offset, Index num_owned_dofs, Index num_nonzeros)
210 return new Core(comm, dof_offset, num_owned_dofs, num_nonzeros);
213 void destroy_core(
void* core)
215 delete reinterpret_cast<Core*
>(core);
218 HYPRE_Int* get_row_ptr(
void* core)
220 return reinterpret_cast<Core*
>(core)->get_row_ptr();
223 HYPRE_Int* get_col_idx(
void* core)
225 return reinterpret_cast<Core*
>(core)->get_col_idx();
228 HYPRE_Real* get_mat_val(
void* core)
230 return reinterpret_cast<Core*
>(core)->get_mat_val();
233 HYPRE_Real* get_vec_def(
void* core)
235 return reinterpret_cast<Core*
>(core)->get_vec_def();
238 HYPRE_Real* get_vec_cor(
void* core)
240 return reinterpret_cast<Core*
>(core)->get_vec_cor();
243 void upload_symbolic(
void* core)
245 reinterpret_cast<Core*
>(core)->upload_symbolic();
248 void upload_mat_val(
void* core)
250 reinterpret_cast<Core*
>(core)->upload_mat_val();
253 void upload_vec_def(
void* core)
255 reinterpret_cast<Core*
>(core)->upload_vec_def();
258 void download_vec_cor(
void* core)
260 reinterpret_cast<Core*
>(core)->download_vec_cor();
263 void format_vec_cor(
void* core)
265 reinterpret_cast<Core*
>(core)->format_vec_cor();
272 void* create_parasails(
void* cr,
int* iparam,
double* dparam)
274 Core* core =
reinterpret_cast<Core*
>(cr);
275 HYPRE_Solver* solver =
new HYPRE_Solver();
278 HYPRE_ParaSailsCreate(core->_comm, solver);
281 HYPRE_ParaSailsSetParams(*solver, dparam[0], iparam[0]);
282 HYPRE_ParaSailsSetFilter(*solver, dparam[1]);
283 HYPRE_ParaSailsSetSym(*solver, iparam[1]);
287 HYPRE_ParaSailsSetup(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
292 void destroy_parasails(
void* slv)
294 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
295 HYPRE_ParaSailsDestroy(*solver);
299 void solve_parasails(
void* cr,
void* slv)
301 Core* core =
reinterpret_cast<Core*
>(cr);
302 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
304 HYPRE_ParaSailsSolve(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
311 void* create_euclid(
void* cr,
int* iparam,
double* dparam)
313 Core* core =
reinterpret_cast<Core*
>(cr);
314 HYPRE_Solver* solver =
new HYPRE_Solver();
317 HYPRE_EuclidCreate(core->_comm, solver);
320 HYPRE_EuclidSetLevel(*solver, iparam[0]);
321 HYPRE_EuclidSetSparseA(*solver, dparam[0]);
325 HYPRE_EuclidSetup(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
330 void destroy_euclid(
void* slv)
332 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
333 HYPRE_EuclidDestroy(*solver);
337 void solve_euclid(
void* cr,
void* slv)
339 Core* core =
reinterpret_cast<Core*
>(cr);
340 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
342 HYPRE_EuclidSolve(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
349 void* create_boomeramg(
void* cr,
int* iparam,
double* dparam)
351 Core* core =
reinterpret_cast<Core*
>(cr);
352 HYPRE_Solver* solver =
new HYPRE_Solver();
355 HYPRE_BoomerAMGCreate(solver);
358 HYPRE_BoomerAMGSetMaxIter(*solver, iparam[0]);
359 HYPRE_BoomerAMGSetTol(*solver, dparam[0]);
363 HYPRE_BoomerAMGSetup(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
368 void destroy_boomeramg(
void* slv)
370 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
371 HYPRE_BoomerAMGDestroy(*solver);
375 void solve_boomeramg(
void* cr,
void* slv)
377 Core* core =
reinterpret_cast<Core*
>(cr);
378 HYPRE_Solver* solver =
reinterpret_cast<HYPRE_Solver*
>(slv);
380 HYPRE_BoomerAMGSolve(*solver, core->_hypre_parcsr, core->_hypre_par_def, core->_hypre_par_cor);
387void dummy_hypre_function() {}