Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Tacho_def.hpp
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Amesos2: Templated Direct Sparse Solver Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Sivasankaran Rajamanickam (srajama@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
44 #ifndef AMESOS2_TACHO_DEF_HPP
45 #define AMESOS2_TACHO_DEF_HPP
46 
47 #include <Teuchos_Tuple.hpp>
48 #include <Teuchos_ParameterList.hpp>
49 #include <Teuchos_StandardParameterEntryValidators.hpp>
50 
52 #include "Amesos2_Tacho_decl.hpp"
53 #include "Amesos2_Util.hpp"
54 
55 namespace Amesos2 {
56 
57 template <class Matrix, class Vector>
59  Teuchos::RCP<const Matrix> A,
60  Teuchos::RCP<Vector> X,
61  Teuchos::RCP<const Vector> B )
62  : SolverCore<Amesos2::TachoSolver,Matrix,Vector>(A, X, B)
63 {
64 }
65 
66 
67 template <class Matrix, class Vector>
69 {
70 }
71 
72 template <class Matrix, class Vector>
73 std::string
75 {
76  std::ostringstream oss;
77  oss << "Tacho solver interface";
78  return oss.str();
79 }
80 
81 template<class Matrix, class Vector>
82 int
84 {
85  return(0);
86 }
87 
88 template <class Matrix, class Vector>
89 int
91 {
92  int status = 0;
93  if ( this->root_ ) {
94  if(do_optimization()) {
95  this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view_);
96  this->matrixA_->returnColInd_kokkos_view(host_cols_view_);
97  }
98 
99  // TODO: Confirm param options
100  // data_.solver.setMaxNumberOfSuperblocks(data_.max_num_superblocks);
101 
102  // Symbolic factorization currently must be done on host
103  data_.solver.analyze(this->globalNumCols_, host_row_ptr_view_, host_cols_view_);
104  }
105  return status;
106 }
107 
108 
109 template <class Matrix, class Vector>
110 int
112 {
113  int status = 0;
114  if ( this->root_ ) {
115  if(do_optimization()) {
116  this->matrixA_->returnValues_kokkos_view(device_nzvals_view_);
117  }
118  data_.solver.factorize(device_nzvals_view_);
119  }
120  return status;
121 }
122 
123 template <class Matrix, class Vector>
124 int
126  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
127 {
128  using Teuchos::as;
129 
130  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
131  const size_t nrhs = X->getGlobalNumVectors();
132 
133  // don't allocate b since it's handled by the copy manager and might just be
134  // be assigned, not copied anyways.
135  // also don't allocate x since we will also use do_get to allocate this if
136  // necessary. When a copy is not necessary we'll solve directly to the x
137  // values in the MV.
138  bool bDidAssignX;
139  { // Get values from RHS B
140 #ifdef HAVE_AMESOS2_TIMERS
141  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
142  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
143 #endif
144  const bool initialize_data = true;
145  const bool do_not_initialize_data = false;
146  Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
147  device_solve_array_t>::do_get(initialize_data, B, this->bValues_,
148  as<size_t>(ld_rhs),
149  ROOTED, this->rowIndexBase_);
150  bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
151  device_solve_array_t>::do_get(do_not_initialize_data, X, this->xValues_,
152  as<size_t>(ld_rhs),
153  ROOTED, this->rowIndexBase_);
154  }
155 
156  int ierr = 0; // returned error code
157 
158  if ( this->root_ ) { // Do solve!
159 #ifdef HAVE_AMESOS2_TIMER
160  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
161 #endif
162  // Bump up the workspace size if needed
163  if (workspace_.extent(0) < this->globalNumRows_ || workspace_.extent(1) < nrhs) {
164  workspace_ = device_solve_array_t(
165  Kokkos::ViewAllocateWithoutInitializing("t"), this->globalNumRows_, nrhs);
166  }
167 
168  data_.solver.solve(xValues_, bValues_, workspace_);
169 
170  int status = 0; // TODO: determine what error handling will be
171  if(status != 0) {
172  ierr = status;
173  }
174  }
175 
176  /* All processes should have the same error code */
177  Teuchos::broadcast(*(this->getComm()), 0, &ierr);
178 
179  TEUCHOS_TEST_FOR_EXCEPTION( ierr != 0, std::runtime_error,
180  "tacho_solve has error code: " << ierr );
181 
182  /* Update X's global values */
183 
184  // if bDidAssignX, then we solved straight to the adapter's X memory space without
185  // requiring additional memory allocation, so the x data is already in place.
186  if(!bDidAssignX) {
187 #ifdef HAVE_AMESOS2_TIMERS
188  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
189 #endif
190 
191  // This will do nothing is if the target view matches the src view, which
192  // can be the case if the memory spaces match. See comments above for do_get.
193  Util::template put_1d_data_helper_kokkos_view<
194  MultiVecAdapter<Vector>,device_solve_array_t>::do_put(X, xValues_,
195  as<size_t>(ld_rhs),
196  ROOTED, this->rowIndexBase_);
197  }
198 
199  return(ierr);
200 }
201 
202 
203 template <class Matrix, class Vector>
204 bool
206 {
207  // Tacho can only apply the solve routines to square matrices
208  return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
209 }
210 
211 
212 template <class Matrix, class Vector>
213 void
214 TachoSolver<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
215 {
216  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
217 
218  // TODO: Confirm param options
219  // data_.num_kokkos_threads = parameterList->get<int>("kokkos-threads", 1);
220  // data_.max_num_superblocks = parameterList->get<int>("max-num-superblocks", 4);
221 }
222 
223 
224 template <class Matrix, class Vector>
225 Teuchos::RCP<const Teuchos::ParameterList>
227 {
228  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
229 
230  if( is_null(valid_params) ){
231  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
232 
233  // TODO: Confirm param options
234  // pl->set("kokkos-threads", 1, "Number of threads");
235  // pl->set("max-num-superblocks", 4, "Max number of superblocks");
236 
237  valid_params = pl;
238  }
239 
240  return valid_params;
241 }
242 
243 template <class Matrix, class Vector>
244 bool
246  return (this->root_ && (this->matrixA_->getComm()->getSize() == 1));
247 }
248 
249 template <class Matrix, class Vector>
250 bool
252 {
253 
254  if(current_phase == SOLVE) {
255  return(false);
256  }
257 
258  if(!do_optimization()) {
259 #ifdef HAVE_AMESOS2_TIMERS
260  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
261 #endif
262 
263  // Note views are allocated but eventually we should remove this.
264  // The internal copy manager will decide if we can assign or deep_copy
265  // and then allocate if necessary. However the GPU solvers are serial right
266  // now so I didn't complete refactoring the matrix code for the parallel
267  // case. If we added that later, we should have it hooked up to the copy
268  // manager and then these allocations can go away.
269  if( this->root_ ) {
270  device_nzvals_view_ = device_value_type_array(
271  Kokkos::ViewAllocateWithoutInitializing("nzvals"), this->globalNumNonZeros_);
272  host_cols_view_ = host_ordinal_type_array(
273  Kokkos::ViewAllocateWithoutInitializing("colind"), this->globalNumNonZeros_);
274  host_row_ptr_view_ = host_size_type_array(
275  Kokkos::ViewAllocateWithoutInitializing("rowptr"), this->globalNumRows_ + 1);
276  }
277 
278  typename host_size_type_array::value_type nnz_ret = 0;
279  {
280  #ifdef HAVE_AMESOS2_TIMERS
281  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
282  #endif
283 
284  TEUCHOS_TEST_FOR_EXCEPTION( this->rowIndexBase_ != this->columnIndexBase_,
285  std::runtime_error,
286  "Row and column maps have different indexbase ");
287 
289  device_value_type_array, host_ordinal_type_array, host_size_type_array>::do_get(
290  this->matrixA_.ptr(),
291  device_nzvals_view_,
292  host_cols_view_,
293  host_row_ptr_view_,
294  nnz_ret,
295  ROOTED, ARBITRARY,
296  this->columnIndexBase_);
297  }
298  }
299 
300  return true;
301 }
302 
303 
304 template<class Matrix, class Vector>
305 const char* TachoSolver<Matrix,Vector>::name = "Tacho";
306 
307 
308 } // end namespace Amesos2
309 
310 #endif // AMESOS2_TACHO_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
Amesos2 interface to the Tacho package.
Definition: Amesos2_Tacho_decl.hpp:67
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using Tacho.
Definition: Amesos2_Tacho_def.hpp:90
Definition: Amesos2_TypeDecl.hpp:143
Utility functions for Amesos2.
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Tacho_def.hpp:205
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
int numericFactorization_impl()
Tacho specific numeric factorization.
Definition: Amesos2_Tacho_def.hpp:111
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_Tacho_def.hpp:74
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Tacho_def.hpp:226
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Tacho_def.hpp:83
bool do_optimization() const
can we optimize size_type and ordinal_type for straight pass through
Definition: Amesos2_Tacho_def.hpp:245
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition: Amesos2_Util.hpp:970
TachoSolver(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_Tacho_def.hpp:58
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Tacho_def.hpp:251
Definition: Amesos2_TypeDecl.hpp:127
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:125
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176
~TachoSolver()
Destructor.
Definition: Amesos2_Tacho_def.hpp:68