Amesos2 - Direct Sparse Solver Interfaces Version of the Day
Amesos2_Tacho_def.hpp
1// @HEADER
2// *****************************************************************************
3// Amesos2: Templated Direct Sparse Solver Package
4//
5// Copyright 2011 NTESS and the Amesos2 contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef AMESOS2_TACHO_DEF_HPP
11#define AMESOS2_TACHO_DEF_HPP
12
13#include <Teuchos_Tuple.hpp>
14#include <Teuchos_ParameterList.hpp>
15#include <Teuchos_StandardParameterEntryValidators.hpp>
16
18#include "Amesos2_Tacho_decl.hpp"
19#include "Amesos2_Util.hpp"
20
21namespace Amesos2 {
22
23template <class Matrix, class Vector>
25 Teuchos::RCP<const Matrix> A,
26 Teuchos::RCP<Vector> X,
27 Teuchos::RCP<const Vector> B )
28 : SolverCore<Amesos2::TachoSolver,Matrix,Vector>(A, X, B)
29{
30 data_.method = 1; // Cholesky
31 data_.variant = 2; // solver variant
32 data_.streams = 1; // # of streams
33 data_.dofs_per_node = 1; // DoFs / node
34 data_.pivot_pert = false; // Pertub small pivot
35 data_.diag_shift = false; // Shift diagonal
36 data_.verbose = false; // verbose
37 data_.team_on_user_stream = false; // use user stream-0 for team/batched kernels
38 data_.small_problem_threshold_size = 1024;
39 tacho_initialized_ = false;
40}
41
42
43template <class Matrix, class Vector>
45{
46 if ( this->root_ && tacho_initialized_) {
47 data_.solver.release();
48 tacho_initialized_ = false;
49 }
50}
51
52template <class Matrix, class Vector>
53std::string
55{
56 std::ostringstream oss;
57 oss << "Tacho solver interface";
58 return oss.str();
59}
60
61template<class Matrix, class Vector>
62int
67
68template <class Matrix, class Vector>
69int
71{
72#ifdef HAVE_AMESOS2_TIMERS
73 Teuchos::TimeMonitor symFactTime( this->timers_.symFactTime_ );
74#endif
75
76 int status = 0;
77 if ( this->root_ ) {
78 if (tacho_initialized_) {
79 // release inernal memory if symbolic was called before
80 data_.solver.release();
81 tacho_initialized_ = false;
82 }
83
84 if(do_optimization()) {
85 this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view_);
86 this->matrixA_->returnColInd_kokkos_view(host_cols_view_);
87 }
88
89 data_.solver.setSolutionMethod(data_.method);
90 data_.solver.setLevelSetOptionAlgorithmVariant(data_.variant);
91 data_.solver.setSmallProblemThresholdsize(data_.small_problem_threshold_size);
92 data_.solver.setVerbose(data_.verbose);
93 data_.solver.setLevelSetOptionNumStreams(data_.streams, data_.team_on_user_stream);
94 // TODO: Confirm param options
95 // data_.solver.setMaxNumberOfSuperblocks(data_.max_num_superblocks);
96
97 // Symbolic factorization currently must be done on host
98 if (data_.dofs_per_node > 1) {
99 data_.solver.analyze(this->globalNumCols_, data_.dofs_per_node, host_row_ptr_view_, host_cols_view_);
100 } else {
101 data_.solver.analyze(this->globalNumCols_, host_row_ptr_view_, host_cols_view_);
102 }
103 data_.solver.initialize();
104 tacho_initialized_ = true;
105 }
106 return status;
107}
108
109
110template <class Matrix, class Vector>
111int
113{
114#ifdef HAVE_AMESOS2_TIMERS
115 Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
116#endif
117
118 int status = 0;
119 if ( this->root_ ) {
120 if(do_optimization()) {
121 // instead of holding onto the device poinster
122 // this->matrixA_->returnValues_kokkos_view(device_nzvals_view_);
123 // make an explicit copy
124 device_value_type_array device_nzvals_temp;
125 this->matrixA_->returnValues_kokkos_view(device_nzvals_temp);
126 Kokkos::deep_copy(device_nzvals_view_, device_nzvals_temp);
127 }
128 if (data_.diag_shift) {
129 data_.solver.shiftDiagonal();
130 }
131 if (data_.pivot_pert) {
132 data_.solver.useDefaultPivotTolerance(2); // tol = sqrt(eps)
133 } else {
134 data_.solver.useNoPivotTolerance();
135 }
136 data_.solver.factorize(device_nzvals_view_);
137 }
138 return status;
139}
140
141template <class Matrix, class Vector>
142int
144 const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
145{
146 using Teuchos::as;
147
148 const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
149 const size_t nrhs = X->getGlobalNumVectors();
150
151 // don't allocate b since it's handled by the copy manager and might just be
152 // be assigned, not copied anyways.
153 // also don't allocate x since we will also use do_get to allocate this if
154 // necessary. When a copy is not necessary we'll solve directly to the x
155 // values in the MV.
156 bool bDidAssignX;
157 { // Get values from RHS B
158#ifdef HAVE_AMESOS2_TIMERS
159 Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
160#endif
161 const bool initialize_data = true;
162 const bool do_not_initialize_data = false;
163 Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
164 device_solve_array_t>::do_get(initialize_data, B, this->bValues_,
165 as<size_t>(ld_rhs),
166 ROOTED, this->rowIndexBase_);
167 bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
168 device_solve_array_t>::do_get(do_not_initialize_data, X, this->xValues_,
169 as<size_t>(ld_rhs),
170 ROOTED, this->rowIndexBase_);
171 }
172
173 int ierr = 0; // returned error code
174
175 if ( this->root_ ) { // Do solve!
176 // Bump up the workspace size if needed
177#ifdef HAVE_AMESOS2_TIMERS
178 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
179#endif
180 if (workspace_.extent(0) < this->globalNumRows_ || workspace_.extent(1) < nrhs) {
181 workspace_ = device_solve_array_t(
182 Kokkos::ViewAllocateWithoutInitializing("t"), this->globalNumRows_, nrhs);
183 }
184
185 data_.solver.solve(xValues_, bValues_, workspace_);
186
187 int status = 0; // TODO: determine what error handling will be
188 if(status != 0) {
189 ierr = status;
190 }
191 }
192
193 /* All processes should have the same error code */
194 Teuchos::broadcast(*(this->getComm()), 0, &ierr);
195
196 TEUCHOS_TEST_FOR_EXCEPTION( ierr != 0, std::runtime_error,
197 "tacho_solve has error code: " << ierr );
198
199 /* Update X's global values */
200
201 // if bDidAssignX, then we solved straight to the adapter's X memory space without
202 // requiring additional memory allocation, so the x data is already in place.
203 if(!bDidAssignX) {
204#ifdef HAVE_AMESOS2_TIMERS
205 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
206#endif
207
208 // This will do nothing is if the target view matches the src view, which
209 // can be the case if the memory spaces match. See comments above for do_get.
210 Util::template put_1d_data_helper_kokkos_view<
211 MultiVecAdapter<Vector>,device_solve_array_t>::do_put(X, xValues_,
212 as<size_t>(ld_rhs),
213 ROOTED, this->rowIndexBase_);
214 }
215
216 return(ierr);
217}
218
219
220template <class Matrix, class Vector>
221bool
223{
224 // Tacho can only apply the solve routines to square matrices
225 return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
226}
227
228
229template <class Matrix, class Vector>
230void
231TachoSolver<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
232{
233 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
234
235 // TODO: Confirm param options
236
237 // factorization type
238 auto method_name = parameterList->get<std::string> ("method", "chol");
239 if (method_name == "ldl-nopiv")
240 data_.method = 0;
241 else if (method_name == "chol")
242 data_.method = 1;
243 else if (method_name == "ldl")
244 data_.method = 2;
245 else if (method_name == "lu")
246 data_.method = 3;
247 else {
248 std::cout << "Error: not supported solution method\n";
249 }
250 // solver type
251 data_.variant = parameterList->get<int> ("variant", 2);
252 // small problem threshold
253 data_.small_problem_threshold_size = parameterList->get<int> ("small problem threshold size", 1024);
254 // verbosity
255 data_.verbose = parameterList->get<bool> ("verbose", false);
256 // # of streams
257 data_.streams = parameterList->get<int> ("num-streams", 1);
258 // use user stream-0 for batched kernels
259 data_.team_on_user_stream = parameterList->get<bool> ("team-on-user-stream", false);
260 // DoFs / node
261 data_.dofs_per_node = parameterList->get<int> ("dofs-per-node", 1);
262 // Perturb tiny pivots
263 data_.pivot_pert = parameterList->get<bool> ("perturb-pivot", false);
264 data_.diag_shift = parameterList->get<bool> ("shift-diag", false);
265 // TODO: Confirm param options
266 // data_.num_kokkos_threads = parameterList->get<int>("kokkos-threads", 1);
267 // data_.max_num_superblocks = parameterList->get<int>("max-num-superblocks", 4);
268}
269
270
271template <class Matrix, class Vector>
272Teuchos::RCP<const Teuchos::ParameterList>
274{
275 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
276
277 if( is_null(valid_params) ){
278 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
279
280 pl->set("method", "chol", "Type of factorization, chol, ldl, or lu");
281 pl->set("variant", 2, "Type of solver variant, 0, 1, or 2");
282 pl->set("small problem threshold size", 1024, "Problem size threshold below with Tacho uses LAPACK.");
283 pl->set("verbose", false, "Verbosity");
284 pl->set("num-streams", 1, "Number of GPU streams");
285 pl->set("dofs-per-node", 1, "DoFs per node");
286 pl->set("perturb-pivot", false, "Perturb tiny pivots");
287 pl->set("shift-diag", false, "Shift diagonal entries");
288 pl->set("team-on-user-stream", false, "Use user stream-0 for team/batched kernels");
289
290 // TODO: Confirm param options
291 // pl->set("kokkos-threads", 1, "Number of threads");
292 // pl->set("max-num-superblocks", 4, "Max number of superblocks");
293
294 valid_params = pl;
295 }
296
297 return valid_params;
298}
299
300template <class Matrix, class Vector>
301bool
303 return (this->root_ && (this->matrixA_->getComm()->getSize() == 1));
304}
305
306template <class Matrix, class Vector>
307bool
309{
310
311 if(current_phase == SOLVE) {
312 return(false);
313 }
314
315 if(!do_optimization()) {
316#ifdef HAVE_AMESOS2_TIMERS
317 Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
318#endif
319
320 // Note views are allocated but eventually we should remove this.
321 // The internal copy manager will decide if we can assign or deep_copy
322 // and then allocate if necessary. However the GPU solvers are serial right
323 // now so I didn't complete refactoring the matrix code for the parallel
324 // case. If we added that later, we should have it hooked up to the copy
325 // manager and then these allocations can go away.
326 {
327 if( this->root_ ) {
328 if (device_nzvals_view_.extent(0) != this->globalNumNonZeros_)
329 Kokkos::resize(device_nzvals_view_, this->globalNumNonZeros_);
330 if (host_cols_view_.extent(0) != this->globalNumNonZeros_)
331 Kokkos::resize(host_cols_view_, this->globalNumNonZeros_);
332 if (host_row_ptr_view_.extent(0) != this->globalNumRows_ + 1)
333 Kokkos::resize(host_row_ptr_view_, this->globalNumRows_ + 1);
334 } else {
335 Kokkos::resize(device_nzvals_view_, 0);
336 Kokkos::resize(host_cols_view_, 0);
337 Kokkos::resize(host_row_ptr_view_, 1);
338 }
339 }
340
341 typename host_size_type_array::value_type nnz_ret = 0;
342 {
343 #ifdef HAVE_AMESOS2_TIMERS
344 Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
345 #endif
346
347 TEUCHOS_TEST_FOR_EXCEPTION( this->rowIndexBase_ != this->columnIndexBase_,
348 std::runtime_error,
349 "Row and column maps have different indexbase ");
350
352 device_value_type_array, host_ordinal_type_array, host_size_type_array>::do_get(
353 this->matrixA_.ptr(),
354 device_nzvals_view_,
355 host_cols_view_,
356 host_row_ptr_view_,
357 nnz_ret,
359 this->columnIndexBase_);
360 }
361 }
362 else {
363 if( this->root_ ) {
364 // instead of holding onto the device poinster (which could cause issue)
365 // make an explicit copy
366 device_nzvals_view_ = device_value_type_array(
367 Kokkos::ViewAllocateWithoutInitializing("nzvals"), this->globalNumNonZeros_);
368 }
369 }
370
371 return true;
372}
373
374
375template <class Matrix, class Vector>
376void
378 const Teuchos::EVerbosityLevel verbLevel) const
379{
380 out << " Tacho current parameters:" << std::endl;
381 out << " > method = " << data_.method;
382 if (data_.method == 0) out << " (ldl-nopiv)" << std::endl;
383 if (data_.method == 1) out << " (chol)" << std::endl;
384 if (data_.method == 2) out << " (ldl)" << std::endl;
385 if (data_.method == 3) out << " (lu)" << std::endl;
386 out << " > variant = " << data_.variant << std::endl;
387 out << " > verbose = " << data_.verbose << std::endl;
388 out << " > num-streams = " << data_.streams << std::endl;
389 out << " > dofs-per-node = " << data_.dofs_per_node << std::endl;
390 out << " > perturb-pivo = " << (data_.pivot_pert ? "YES" : "NO") << std::endl;
391 out << " > shift-diag = " << (data_.diag_shift ? "YES" : "NO") << std::endl;
392 out << " > team-on-user-stream = " << (data_.team_on_user_stream ? "YES" : "NO") << std::endl;
393 out << " > small problem threshold size = " << data_.small_problem_threshold_size << std::endl;
394 out << std::endl;
395}
396
397
398template<class Matrix, class Vector>
399const char* TachoSolver<Matrix,Vector>::name = "Tacho";
400
401
402} // end namespace Amesos2
403
404#endif // AMESOS2_TACHO_DEF_HPP
@ ROOTED
Definition Amesos2_TypeDecl.hpp:93
@ ARBITRARY
Definition Amesos2_TypeDecl.hpp:109
EPhase
Used to indicate a phase in the direct solution.
Definition Amesos2_TypeDecl.hpp:31
Utility functions for Amesos2.
Teuchos::RCP< const MatrixAdapter< Matrix > > matrixA_
Definition Amesos2_SolverCore_decl.hpp:421
bool root_
Definition Amesos2_SolverCore_decl.hpp:472
SolverCore(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
global_size_type rowIndexBase_
Definition Amesos2_SolverCore_decl.hpp:451
global_size_type globalNumCols_
Definition Amesos2_SolverCore_decl.hpp:445
global_size_type columnIndexBase_
Definition Amesos2_SolverCore_decl.hpp:454
Timers timers_
Definition Amesos2_SolverCore_decl.hpp:463
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const override
Definition Amesos2_SolverCore_decl.hpp:329
global_size_type globalNumNonZeros_
Definition Amesos2_SolverCore_decl.hpp:448
global_size_type globalNumRows_
Definition Amesos2_SolverCore_decl.hpp:442
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition Amesos2_Tacho_def.hpp:222
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition Amesos2_Tacho_def.hpp:273
int numericFactorization_impl()
Tacho specific numeric factorization.
Definition Amesos2_Tacho_def.hpp:112
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
Tacho specific solve.
Definition Amesos2_Tacho_def.hpp:143
~TachoSolver()
Destructor.
Definition Amesos2_Tacho_def.hpp:44
bool do_optimization() const
can we optimize size_type and ordinal_type for straight pass through
Definition Amesos2_Tacho_def.hpp:302
static const char * name
Name of this solver interface.
Definition Amesos2_Tacho_decl.hpp:41
TachoSolver(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition Amesos2_Tacho_def.hpp:24
std::string description() const override
Returns a short description of this Solver.
Definition Amesos2_Tacho_def.hpp:54
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition Amesos2_Tacho_def.hpp:308
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using Tacho.
Definition Amesos2_Tacho_def.hpp:70
void describe_impl(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Prints the status information about the current solver with some level of verbosity.
Definition Amesos2_Tacho_def.hpp:377
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition Amesos2_Tacho_def.hpp:63
A templated MultiVector class adapter for Amesos2.
Definition Amesos2_MultiVecAdapter_decl.hpp:142
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition Amesos2_Util.hpp:600