Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Superludist_def.hpp
Go to the documentation of this file.
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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
52 #ifndef AMESOS2_SUPERLUDIST_DEF_HPP
53 #define AMESOS2_SUPERLUDIST_DEF_HPP
54 
55 #include <Teuchos_Tuple.hpp>
56 #include <Teuchos_StandardParameterEntryValidators.hpp>
57 #include <Teuchos_DefaultMpiComm.hpp>
58 
61 #include "Amesos2_Util.hpp"
62 
63 
64 namespace Amesos2 {
65 
66 
67  template <class Matrix, class Vector>
68  Superludist<Matrix,Vector>::Superludist(Teuchos::RCP<const Matrix> A,
69  Teuchos::RCP<Vector> X,
70  Teuchos::RCP<const Vector> B)
71  : SolverCore<Amesos2::Superludist,Matrix,Vector>(A, X, B)
72  , nzvals_() // initialization to empty arrays
73  , colind_()
74  , rowptr_()
75  , bvals_()
76  , xvals_()
77  , in_grid_(false)
78  {
80  // Set up the SuperLU_DIST processor grid //
82 
83  int nprocs = this->getComm()->getSize();
84  SLUD::int_t nprow, npcol;
85  get_default_grid_size(nprocs, nprow, npcol);
86  data_.mat_comm = dynamic_cast<const Teuchos::MpiComm<int>* >(this->matrixA_->getComm().getRawPtr())->getRawMpiComm()->operator()();
87  SLUD::superlu_gridinit(data_.mat_comm, nprow, npcol, &(data_.grid));
88 
90  // Set Some default parameters. //
91  // //
92  // Must do this after grid has been created in //
93  // case user specifies the nprow and npcol parameters //
95  Teuchos::RCP<Teuchos::ParameterList> default_params
96  = Teuchos::parameterList( *(this->getValidParameters()) );
97  this->setParameters(default_params);
98 
99  // Set some internal options
100  data_.options.Fact = SLUD::DOFACT;
101  data_.equed = SLUD::NOEQUIL; // No equilibration has yet been performed
102  data_.options.SolveInitialized = SLUD::NO;
103  data_.options.RefineInitialized = SLUD::NO;
104  data_.rowequ = false;
105  data_.colequ = false;
106  data_.perm_r.resize(this->globalNumRows_);
107  data_.perm_c.resize(this->globalNumCols_);
108 
110  // Set up a communicator for the parallel column ordering and //
111  // parallel symbolic factorization. //
113  data_.symb_comm = MPI_COMM_NULL;
114  int color = MPI_UNDEFINED;
115  int my_rank = this->rank_;
116 
117  /* domains is the next power of 2 less than nprow*npcol. This
118  * value will be used for creating an MPI communicator for the
119  * pre-ordering and symbolic factorization methods.
120  */
121  data_.domains = (int) ( pow(2.0, floor(log10((double)nprow*npcol)/log10(2.0))) );
122 
123  if( this->rank_ < data_.domains ) color = 0;
124  MPI_Comm_split (data_.mat_comm, color, my_rank, &(data_.symb_comm));
125 
127  // Set up a row map that maps to only processors that are in the //
128  // SuperLU processor grid. This will be used for redistributing A. //
130 
131  int my_weight = 0;
132  if( this->rank_ < nprow * npcol ){
133  in_grid_ = true; my_weight = 1; // I am in the grid, and I get some of the matrix rows
134  }
135  // TODO: might only need to initialize if parallel symbolic factorization is requested.
136  // TODO: Need to fix this map for indexbase ?
138  = Tpetra::createWeightedContigMapWithNode<local_ordinal_type,
139  global_ordinal_type,
140  node_type>(my_weight,
141  this->globalNumRows_,
142  this->getComm(),
143  Kokkos::DefaultNode::getDefaultNode());
144  // TODO: the node above should technically come from the matrix
145  // itself. Might need to add a getNode method to the matrix
146  // adapter.
147 
149  // Do some other initialization //
151 
152  data_.A.Store = NULL;
153  function_map::LUstructInit(this->globalNumRows_, this->globalNumCols_, &(data_.lu));
154  SLUD::PStatInit(&(data_.stat));
155  // We do not use ScalePermstructInit because we will use our own
156  // arrays for storing perm_r and perm_c
157  data_.scale_perm.perm_r = data_.perm_r.getRawPtr();
158  data_.scale_perm.perm_c = data_.perm_c.getRawPtr();
159  }
160 
161 
162  template <class Matrix, class Vector>
164  {
165  /* Free SuperLU_DIST data_types
166  * - Matrices
167  * - Vectors
168  * - Stat object
169  * - ScalePerm, LUstruct, grid, and solve objects
170  *
171  * Note: the function definitions are the same regardless whether
172  * complex or real, so we arbitrarily use the D namespace
173  */
174  if ( this->status_.getNumPreOrder() > 0 ){
175  free( data_.sizes );
176  free( data_.fstVtxSep );
177  }
178 
179  // Cleanup old matrix store memory if it's non-NULL. Our
180  // Teuchos::Array's will destroy rowind, colptr, and nzval for us
181  if( data_.A.Store != NULL ){
182  SLUD::Destroy_SuperMatrix_Store_dist( &(data_.A) );
183  }
184 
185  // LU data is initialized in numericFactorization_impl()
186  if ( this->status_.getNumNumericFact() > 0 ){
187  function_map::Destroy_LU(this->globalNumRows_, &(data_.grid), &(data_.lu));
188  }
189  function_map::LUstructFree(&(data_.lu));
190 
191  // If a symbolic factorization is ever performed without a
192  // follow-up numericfactorization, there are some arrays in the
193  // Pslu_freeable struct which will never be free'd by
194  // SuperLU_DIST.
195  if ( this->status_.symbolicFactorizationDone() &&
196  !this->status_.numericFactorizationDone() ){
197  if ( data_.pslu_freeable.xlsub != NULL ){
198  free( data_.pslu_freeable.xlsub );
199  free( data_.pslu_freeable.lsub );
200  }
201  if ( data_.pslu_freeable.xusub != NULL ){
202  free( data_.pslu_freeable.xusub );
203  free( data_.pslu_freeable.usub );
204  }
205  if ( data_.pslu_freeable.supno_loc != NULL ){
206  free( data_.pslu_freeable.supno_loc );
207  free( data_.pslu_freeable.xsup_beg_loc );
208  free( data_.pslu_freeable.xsup_end_loc );
209  }
210  free( data_.pslu_freeable.globToLoc );
211  }
212 
213  SLUD::PStatFree( &(data_.stat) ) ;
214 
215  // Teuchos::Arrays will free R, C, perm_r, and perm_c
216  // SLUD::D::ScalePermstructFree(&(data_.scale_perm));
217 
218  if ( data_.options.SolveInitialized == SLUD::YES )
219  function_map::SolveFinalize(&(data_.options), &(data_.solve_struct));
220 
221  SLUD::superlu_gridexit(&(data_.grid)); // TODO: are there any
222  // cases where grid
223  // wouldn't be initialized?
224 
225  if ( data_.symb_comm != MPI_COMM_NULL ) MPI_Comm_free(&(data_.symb_comm));
226  }
227 
228  template<class Matrix, class Vector>
229  int
231  {
232  // We will always use the NATURAL row ordering to avoid the
233  // sequential bottleneck present when doing any other row
234  // ordering scheme from SuperLU_DIST
235  //
236  // Set perm_r to be the natural ordering
237  SLUD::int_t slu_rows_ub = Teuchos::as<SLUD::int_t>(this->globalNumRows_);
238  for( SLUD::int_t i = 0; i < slu_rows_ub; ++i ) data_.perm_r[i] = i;
239 
240  // loadA_impl(); // Refresh matrix values
241 
242  if( in_grid_ ){
243  // If this function has been called at least once, then the
244  // sizes, and fstVtxSep arrays were allocated in
245  // get_perm_c_parmetis. Delete them before calling that
246  // function again. These arrays will also be dealloc'd in the
247  // deconstructor.
248  if( this->status_.getNumPreOrder() > 0 ){
249  free( data_.sizes );
250  free( data_.fstVtxSep );
251  }
252 #ifdef HAVE_AMESOS2_TIMERS
253  Teuchos::TimeMonitor preOrderTime( this->timers_.preOrderTime_ );
254 #endif
255 
256  float info = 0.0;
257  info = SLUD::get_perm_c_parmetis( &(data_.A),
258  data_.perm_r.getRawPtr(), data_.perm_c.getRawPtr(),
259  data_.grid.nprow * data_.grid.npcol, data_.domains,
260  &(data_.sizes), &(data_.fstVtxSep),
261  &(data_.grid), &(data_.symb_comm) );
262 
263  TEUCHOS_TEST_FOR_EXCEPTION( info > 0.0,
264  std::runtime_error,
265  "SuperLU_DIST pre-ordering ran out of memory after allocating "
266  << info << " bytes of memory" );
267  }
268 
269  // Ordering will be applied directly before numeric factorization,
270  // after we have a chance to get updated coefficients from the
271  // matrix
272 
273  return EXIT_SUCCESS;
274  }
275 
276 
277 
278  template <class Matrix, class Vector>
279  int
281  {
282  // loadA_impl(); // Refresh matrix values
283 
284  if( in_grid_ ){
285 
286 #ifdef HAVE_AMESOS2_TIMERS
287  Teuchos::TimeMonitor symFactTime( this->timers_.symFactTime_ );
288 #endif
289 
290  float info = 0.0;
291  info = SLUD::symbfact_dist((data_.grid.nprow) * (data_.grid.npcol),
292  data_.domains, &(data_.A), data_.perm_c.getRawPtr(),
293  data_.perm_r.getRawPtr(), data_.sizes,
294  data_.fstVtxSep, &(data_.pslu_freeable),
295  &(data_.grid.comm), &(data_.symb_comm),
296  &(data_.mem_usage));
297 
298  TEUCHOS_TEST_FOR_EXCEPTION( info > 0.0,
299  std::runtime_error,
300  "SuperLU_DIST symbolic factorization ran out of memory after"
301  " allocating " << info << " bytes of memory" );
302  }
303  same_symbolic_ = false;
304  same_solve_struct_ = false;
305 
306  return EXIT_SUCCESS;
307  }
308 
309 
310  template <class Matrix, class Vector>
311  int
313  using Teuchos::as;
314 
315  // loadA_impl(); // Refresh the matrix values
316 
317  // if( data_.options.Equil == SLUD::YES ){
318  // // Apply the scalings computed in preOrdering
319  // function_map::laqgs(&(data_.A), data_.R.getRawPtr(),
320  // data_.C.getRawPtr(), data_.rowcnd, data_.colcnd,
321  // data_.amax, &(data_.equed));
322 
323  // data_.rowequ = (data_.equed == SLUD::ROW) || (data_.equed == SLUD::BOTH);
324  // data_.colequ = (data_.equed == SLUD::COL) || (data_.equed == SLUD::BOTH);
325  // }
326 
327  if( in_grid_ ){
328  // Apply the column ordering, so that AC is the column-permuted A, and compute etree
329  size_t nnz_loc = ((SLUD::NRformat_loc*)data_.A.Store)->nnz_loc;
330  for( size_t i = 0; i < nnz_loc; ++i ) colind_[i] = data_.perm_c[colind_[i]];
331 
332  // Distribute data from the symbolic factorization
333  if( same_symbolic_ ){
334  // Note: with the SamePattern_SameRowPerm options, it does not
335  // matter that the glu_freeable member has never been
336  // initialized, because it is never accessed. It is a
337  // placeholder arg. The real work is done in data_.lu
338  function_map::pdistribute(SLUD::SamePattern_SameRowPerm,
339  as<SLUD::int_t>(this->globalNumRows_), // aka "n"
340  &(data_.A), &(data_.scale_perm),
341  &(data_.glu_freeable), &(data_.lu),
342  &(data_.grid));
343  } else {
344  function_map::dist_psymbtonum(SLUD::DOFACT,
345  as<SLUD::int_t>(this->globalNumRows_), // aka "n"
346  &(data_.A), &(data_.scale_perm),
347  &(data_.pslu_freeable), &(data_.lu),
348  &(data_.grid));
349  }
350 
351  // Retrieve the normI of A (required by gstrf).
352  double anorm = function_map::plangs((char *)"I", &(data_.A), &(data_.grid));
353 
354  int info = 0;
355  {
356 #ifdef HAVE_AMESOS2_TIMERS
357  Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
358 #endif
359 
360  function_map::gstrf(&(data_.options), this->globalNumRows_,
361  this->globalNumCols_, anorm, &(data_.lu),
362  &(data_.grid), &(data_.stat), &info);
363  }
364 
365  // Check output
366  TEUCHOS_TEST_FOR_EXCEPTION( info > 0,
367  std::runtime_error,
368  "L and U factors have been computed but U("
369  << info << "," << info << ") is exactly zero "
370  "(i.e. U is singular)");
371  }
372 
373  // The other option, that info_st < 0, denotes invalid parameters
374  // to the function, but we'll assume for now that that won't
375  // happen.
376 
377  data_.options.Fact = SLUD::FACTORED;
378  same_symbolic_ = true;
379 
380  return EXIT_SUCCESS;
381  }
382 
383 
384  template <class Matrix, class Vector>
385  int
387  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
388  {
389  using Teuchos::as;
390 
391  // local_len_rhs is how many of the multivector rows belong to
392  // this processor in the SuperLU_DIST processor grid.
393  const size_t local_len_rhs = superlu_rowmap_->getNodeNumElements();
394  const global_size_type nrhs = X->getGlobalNumVectors();
395  const global_ordinal_type first_global_row_b = superlu_rowmap_->getMinGlobalIndex();
396 
397  // make sure our multivector storage is sized appropriately
398  bvals_.resize(nrhs * local_len_rhs);
399  xvals_.resize(nrhs * local_len_rhs);
400 
401  // We assume the global length of the two vectors have already been
402  // checked for compatibility
403 
404  { // get the values from B
405 #ifdef HAVE_AMESOS2_TIMERS
406  Teuchos::TimeMonitor convTimer(this->timers_.vecConvTime_);
407 #endif
408 
409  {
410  // The input dense matrix for B should be distributed in the
411  // same manner as the superlu_dist matrix. That is, if a
412  // processor has m_loc rows of A, then it should also have
413  // m_loc rows of B (and the same rows). We accomplish this by
414  // distributing the multivector rows with the same Map that
415  // the matrix A's rows are distributed.
416 #ifdef HAVE_AMESOS2_TIMERS
417  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
418 #endif
419 
420  // get grid-distributed mv data. The multivector data will be
421  // distributed across the processes in the SuperLU_DIST grid.
422  typedef Util::get_1d_copy_helper<MultiVecAdapter<Vector>,slu_type> copy_helper;
423  copy_helper::do_get(B,
424  bvals_(),
425  local_len_rhs,
426  Teuchos::ptrInArg(*superlu_rowmap_));
427  }
428  } // end block for conversion time
429 
430  if( in_grid_ ){
431  // if( data_.options.trans == SLUD::NOTRANS ){
432  // if( data_.rowequ ){ // row equilibration has been done on AC
433  // // scale bxvals_ by diag(R)
434  // Util::scale(bxvals_(), as<size_t>(len_rhs), ldbx_, data_.R(),
435  // SLUD::slu_mt_mult<slu_type,magnitude_type>());
436  // }
437  // } else if( data_.colequ ){ // column equilibration has been done on AC
438  // // scale bxvals_ by diag(C)
439  // Util::scale(bxvals_(), as<size_t>(len_rhs), ldbx_, data_.C(),
440  // SLUD::slu_mt_mult<slu_type,magnitude_type>());
441  // }
442 
443  // Initialize the SOLVEstruct_t.
444  //
445  // We are able to reuse the solve struct if we have not changed
446  // the sparsity pattern of L and U since the last solve
447  if( !same_solve_struct_ ){
448  if( data_.options.SolveInitialized == SLUD::YES ){
449  function_map::SolveFinalize(&(data_.options), &(data_.solve_struct));
450  }
451  function_map::SolveInit(&(data_.options), &(data_.A), data_.perm_r.getRawPtr(),
452  data_.perm_c.getRawPtr(), as<SLUD::int_t>(nrhs), &(data_.lu),
453  &(data_.grid), &(data_.solve_struct));
454  // Flag that we can reuse this solve_struct unless another
455  // symbolicFactorization is called between here and the next
456  // solve.
457  same_solve_struct_ = true;
458  }
459 
460  int ierr = 0; // returned error code
461  {
462 #ifdef HAVE_AMESOS2_TIMERS
463  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
464 #endif
465 
466  function_map::gstrs(as<SLUD::int_t>(this->globalNumRows_), &(data_.lu),
467  &(data_.scale_perm), &(data_.grid), bvals_.getRawPtr(),
468  as<SLUD::int_t>(local_len_rhs), as<SLUD::int_t>(first_global_row_b),
469  as<SLUD::int_t>(local_len_rhs), as<int>(nrhs),
470  &(data_.solve_struct), &(data_.stat), &ierr);
471  } // end block for solve time
472 
473  TEUCHOS_TEST_FOR_EXCEPTION( ierr < 0,
474  std::runtime_error,
475  "Argument " << -ierr << " to gstrs had an illegal value" );
476 
477  // "Un-scale" the solution so that it is a solution of the original system
478  // if( data_.options.trans == SLUD::NOTRANS ){
479  // if( data_.colequ ){ // column equilibration has been done on AC
480  // // scale bxvals_ by diag(C)
481  // Util::scale(bxvals_(), as<size_t>(len_rhs), ldbx_, data_.C(),
482  // SLUD::slu_mt_mult<slu_type,magnitude_type>());
483  // }
484  // } else if( data_.rowequ ){ // row equilibration has been done on AC
485  // // scale bxvals_ by diag(R)
486  // Util::scale(bxvals_(), as<size_t>(len_rhs), ldbx_, data_.R(),
487  // SLUD::slu_mt_mult<slu_type,magnitude_type>());
488  // }
489  { // permute B to a solution of the original system
490 #ifdef HAVE_AMESOS2_TIMERS
491  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
492 #endif
493  SLUD::int_t ld = as<SLUD::int_t>(local_len_rhs);
494  function_map::permute_Dense_Matrix(as<SLUD::int_t>(first_global_row_b),
495  as<SLUD::int_t>(local_len_rhs),
496  data_.solve_struct.row_to_proc,
497  data_.solve_struct.inv_perm_c,
498  bvals_.getRawPtr(), ld,
499  xvals_.getRawPtr(), ld,
500  as<int>(nrhs),
501  &(data_.grid));
502  }
503  }
504 
505  /* Update X's global values */
506  {
507 #ifdef HAVE_AMESOS2_TIMERS
508  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
509 #endif
510 
511  typedef Util::put_1d_data_helper<MultiVecAdapter<Vector>,slu_type> put_helper;
512  put_helper::do_put(X,
513  xvals_(),
514  local_len_rhs,
515  Teuchos::ptrInArg(*superlu_rowmap_));
516  }
517 
518  return EXIT_SUCCESS;
519  }
520 
521 
522  template <class Matrix, class Vector>
523  bool
525  {
526  // SuperLU_DIST requires square matrices
527  return( this->globalNumRows_ == this->globalNumCols_ );
528  }
529 
530 
531  template <class Matrix, class Vector>
532  void
533  Superludist<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
534  {
535  using Teuchos::as;
536  using Teuchos::RCP;
537  using Teuchos::getIntegralValue;
538  using Teuchos::ParameterEntryValidator;
539 
540  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
541 
542  if( parameterList->isParameter("npcol") || parameterList->isParameter("nprow") ){
543  TEUCHOS_TEST_FOR_EXCEPTION( !(parameterList->isParameter("nprow") &&
544  parameterList->isParameter("npcol")),
545  std::invalid_argument,
546  "nprow and npcol must be set together" );
547 
548  SLUD::int_t nprow = parameterList->template get<SLUD::int_t>("nprow");
549  SLUD::int_t npcol = parameterList->template get<SLUD::int_t>("npcol");
550 
551  TEUCHOS_TEST_FOR_EXCEPTION( nprow * npcol > this->getComm()->getSize(),
552  std::invalid_argument,
553  "nprow and npcol combination invalid" );
554 
555  if( (npcol != data_.grid.npcol) || (nprow != data_.grid.nprow) ){
556  // De-allocate the default grid that was initialized in the constructor
557  SLUD::superlu_gridexit(&(data_.grid));
558  // Create a new grid
559  SLUD::superlu_gridinit(data_.mat_comm, nprow, npcol, &(data_.grid));
560  } // else our grid has not changed size since the last initialization
561  }
562 
563  TEUCHOS_TEST_FOR_EXCEPTION( this->control_.useTranspose_,
564  std::invalid_argument,
565  "SuperLU_DIST does not support solving the tranpose system" );
566 
567  data_.options.Trans = SLUD::NOTRANS; // should always be set this way;
568 
569  // TODO: Uncomment when supported
570  // bool equil = parameterList->get<bool>("Equil", true);
571  // data_.options.Equil = equil ? SLUD::YES : SLUD::NO;
572  data_.options.Equil = SLUD::NO;
573 
574  if( parameterList->isParameter("ColPerm") ){
575  RCP<const ParameterEntryValidator> colperm_validator = valid_params->getEntry("ColPerm").validator();
576  parameterList->getEntry("ColPerm").setValidator(colperm_validator);
577 
578  data_.options.ColPerm = getIntegralValue<SLUD::colperm_t>(*parameterList, "ColPerm");
579  }
580 
581  // Always use the "NOROWPERM" option to avoid a serial bottleneck
582  // with the weighted bipartite matching algorithm used for the
583  // "LargeDiag" RowPerm. Note the inconsistency with the SuperLU
584  // User guide (which states that the value should be "NATURAL").
585  data_.options.RowPerm = SLUD::NOROWPERM;
586 
587  // TODO: Uncomment when supported
588  // if( parameterList->isParameter("IterRefine") ){
589  // RCP<const ParameterEntryValidator> iter_refine_validator = valid_params->getEntry("IterRefine").validator();
590  // parameterList->getEntry("IterRefine").setValidator(iter_refine_validator);
591 
592  // data_.options.IterRefine = getIntegralValue<SLUD::IterRefine_t>(*parameterList, "IterRefine");
593  // }
594  data_.options.IterRefine = SLUD::NOREFINE;
595 
596  bool replace_tiny = parameterList->get<bool>("ReplaceTinyPivot", true);
597  data_.options.ReplaceTinyPivot = replace_tiny ? SLUD::YES : SLUD::NO;
598  }
599 
600 
601  template <class Matrix, class Vector>
602  Teuchos::RCP<const Teuchos::ParameterList>
604  {
605  using std::string;
606  using Teuchos::tuple;
607  using Teuchos::ParameterList;
608  using Teuchos::EnhancedNumberValidator;
609  using Teuchos::setStringToIntegralParameter;
610  using Teuchos::stringToIntegralParameterEntryValidator;
611 
612  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
613 
614  if( is_null(valid_params) ){
615  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
616 
617  Teuchos::RCP<EnhancedNumberValidator<SLUD::int_t> > col_row_validator
618  = Teuchos::rcp( new EnhancedNumberValidator<SLUD::int_t>() );
619  col_row_validator->setMin(1);
620 
621  pl->set("npcol", data_.grid.npcol,
622  "Number of columns in the processor grid. "
623  "Must be set with nprow", col_row_validator);
624  pl->set("nprow", data_.grid.nprow,
625  "Number of rows in the SuperLU_DIST processor grid. "
626  "Must be set together with npcol", col_row_validator);
627 
628  // validator will catch any value besides NOTRANS
629  setStringToIntegralParameter<SLUD::trans_t>("Trans", "NOTRANS",
630  "Solve for the transpose system or not",
631  tuple<string>("NOTRANS"),
632  tuple<string>("Do not solve with transpose"),
633  tuple<SLUD::trans_t>(SLUD::NOTRANS),
634  pl.getRawPtr());
635 
636  // TODO: uncomment when supported
637  // pl->set("Equil", false, "Whether to equilibrate the system before solve");
638 
639  // TODO: uncomment when supported
640  // setStringToIntegralParameter<SLUD::IterRefine_t>("IterRefine", "NOREFINE",
641  // "Type of iterative refinement to use",
642  // tuple<string>("NOREFINE", "DOUBLE"),
643  // tuple<string>("Do not use iterative refinement",
644  // "Do double iterative refinement"),
645  // tuple<SLUD::IterRefine_t>(SLUD::NOREFINE,
646  // SLUD::DOUBLE),
647  // pl.getRawPtr());
648 
649  pl->set("ReplaceTinyPivot", true,
650  "Specifies whether to replace tiny diagonals during LU factorization");
651 
652  setStringToIntegralParameter<SLUD::colperm_t>("ColPerm", "PARMETIS",
653  "Specifies how to permute the columns of the "
654  "matrix for sparsity preservation",
655  tuple<string>("NATURAL", "PARMETIS"),
656  tuple<string>("Natural ordering",
657  "ParMETIS ordering on A^T + A"),
658  tuple<SLUD::colperm_t>(SLUD::NATURAL,
659  SLUD::PARMETIS),
660  pl.getRawPtr());
661 
662  valid_params = pl;
663  }
664 
665  return valid_params;
666  }
667 
668 
669  template <class Matrix, class Vector>
670  void
672  SLUD::int_t& nprow,
673  SLUD::int_t& npcol) const {
674  TEUCHOS_TEST_FOR_EXCEPTION( nprocs < 1,
675  std::invalid_argument,
676  "Number of MPI processes must be at least 1" );
677  SLUD::int_t c, r = 1;
678  while( r*r <= nprocs ) r++;
679  nprow = npcol = --r; // fall back to square grid
680  c = nprocs / r;
681  while( (r--)*c != nprocs ){
682  c = nprocs / r; // note integer division
683  }
684  ++r;
685  // prefer the square grid over a single row (which will only happen
686  // in the case of a prime nprocs
687  if( r > 1 || nprocs < 9){ // nprocs < 9 is a heuristic for the small cases
688  nprow = r;
689  npcol = c;
690  }
691  }
692 
693 
694  template <class Matrix, class Vector>
695  bool
697  // Extract the necessary information from mat and call SLU function
698  using Teuchos::Array;
699  using Teuchos::ArrayView;
700  using Teuchos::ptrInArg;
701  using Teuchos::as;
702 
703  using SLUD::int_t;
704 
705 #ifdef HAVE_AMESOS2_TIMERS
706  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
707 #endif
708 
709  // Cleanup old store memory if it's non-NULL
710  if( data_.A.Store != NULL ){
711  SLUD::Destroy_SuperMatrix_Store_dist( &(data_.A) );
712  data_.A.Store = NULL;
713  }
714 
715  Teuchos::RCP<const MatrixAdapter<Matrix> > redist_mat
716  = this->matrixA_->get(ptrInArg(*superlu_rowmap_));
717 
718  int_t l_nnz, l_rows, g_rows, g_cols, fst_global_row;
719  l_nnz = as<int_t>(redist_mat->getLocalNNZ());
720  l_rows = as<int_t>(redist_mat->getLocalNumRows());
721  g_rows = as<int_t>(redist_mat->getGlobalNumRows());
722  g_cols = g_rows; // we deal with square matrices
723  fst_global_row = as<int_t>(superlu_rowmap_->getMinGlobalIndex());
724 
725  nzvals_.resize(l_nnz);
726  colind_.resize(l_nnz);
727  rowptr_.resize(l_rows + 1);
728 
729  int_t nnz_ret = 0;
730  {
731 #ifdef HAVE_AMESOS2_TIMERS
732  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
733 #endif
734 
737  slu_type, int_t, int_t >::do_get(redist_mat.ptr(),
738  nzvals_(), colind_(), rowptr_(),
739  nnz_ret,
740  ptrInArg(*superlu_rowmap_),
741  ARBITRARY);
742  }
743 
744  TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != l_nnz,
745  std::runtime_error,
746  "Did not get the expected number of non-zero vals");
747 
748  // Get the SLU data type for this type of matrix
749  SLUD::Dtype_t dtype = type_map::dtype;
750 
751  if( in_grid_ ){
752  function_map::create_CompRowLoc_Matrix(&(data_.A),
753  g_rows, g_cols,
754  l_nnz, l_rows, fst_global_row,
755  nzvals_.getRawPtr(),
756  colind_.getRawPtr(),
757  rowptr_.getRawPtr(),
758  SLUD::SLU_NR_loc,
759  dtype, SLUD::SLU_GE);
760  }
761 
762  return true;
763 }
764 
765 
766  template<class Matrix, class Vector>
767  const char* Superludist<Matrix,Vector>::name = "SuperLU_DIST";
768 
769 
770 } // end namespace Amesos2
771 
772 #endif // AMESOS2_SUPERLUDIST_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
Amesos2 interface to the distributed memory version of SuperLU.
Definition: Amesos2_Superludist_decl.hpp:90
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition: Amesos2_Util.hpp:586
global_size_type globalNumCols_
Number of global columns in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:476
Superludist(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_Superludist_def.hpp:68
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Definition: Amesos2_Superludist_def.hpp:533
global_size_type globalNumRows_
Number of global rows in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:473
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:243
Utility functions for Amesos2.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Superludist_def.hpp:603
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a const parameter list of all of the valid parameters that this-&gt;setParameterList(...) will accept.
Definition: Amesos2_SolverCore_def.hpp:307
Provides definition of SuperLU_DIST types as well as conversions and type traits. ...
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Superludist_def.hpp:230
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns a pointer to the Teuchos::Comm communicator with this operator.
Definition: Amesos2_SolverCore_decl.hpp:360
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Superludist_def.hpp:524
Teuchos::RCP< const Tpetra::Map< local_ordinal_type, global_ordinal_type, node_type > > superlu_rowmap_
Maps rows of the matrix to processors in the SuperLU_DIST processor grid.
Definition: Amesos2_Superludist_decl.hpp:327
~Superludist()
Destructor.
Definition: Amesos2_Superludist_def.hpp:163
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:275
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using SuperLU_DIST.
Definition: Amesos2_Superludist_def.hpp:280
int rank_
The MPI rank of this image.
Definition: Amesos2_SolverCore_decl.hpp:501
void get_default_grid_size(int nprocs, SLUD::int_t &nprow, SLUD::int_t &npcol) const
Definition: Amesos2_Superludist_def.hpp:671
bool in_grid_
true if this processor is in SuperLU_DISTS&#39;s 2D process grid
Definition: Amesos2_Superludist_decl.hpp:320
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:292
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
SuperLU_DIST specific solve.
Definition: Amesos2_Superludist_def.hpp:386
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:175
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal solver structures.
Definition: Amesos2_Superludist_def.hpp:696
Teuchos::RCP< const MatrixAdapter< Matrix > > matrixA_
The LHS operator.
Definition: Amesos2_SolverCore_decl.hpp:452
int numericFactorization_impl()
SuperLU_DIST specific numeric factorization.
Definition: Amesos2_Superludist_def.hpp:312