52 #ifndef AMESOS2_SUPERLUDIST_DEF_HPP 53 #define AMESOS2_SUPERLUDIST_DEF_HPP 55 #include <Teuchos_Tuple.hpp> 56 #include <Teuchos_StandardParameterEntryValidators.hpp> 57 #include <Teuchos_DefaultMpiComm.hpp> 67 template <
class Matrix,
class Vector>
69 Teuchos::RCP<Vector> X,
70 Teuchos::RCP<const Vector> B)
82 using Teuchos::MpiComm;
83 using Teuchos::outArg;
84 using Teuchos::ParameterList;
85 using Teuchos::parameterList;
88 using Teuchos::rcp_dynamic_cast;
89 using Teuchos::REDUCE_SUM;
90 using Teuchos::reduceAll;
91 typedef global_ordinal_type GO;
92 typedef Tpetra::Map<local_ordinal_type, GO, node_type> map_type;
98 RCP<const Comm<int> > comm = this->
getComm ();
99 const int myRank = comm->getRank ();
100 const int numProcs = comm->getSize ();
102 SLUD::int_t nprow, npcol;
109 RCP<const Comm<int> > matComm = this->
matrixA_->getComm ();
110 TEUCHOS_TEST_FOR_EXCEPTION(
111 matComm.is_null (), std::logic_error,
"Amesos2::Superlustdist " 112 "constructor: The matrix's communicator is null!");
113 RCP<const MpiComm<int> > matMpiComm =
114 rcp_dynamic_cast<
const MpiComm<int> > (matComm);
119 TEUCHOS_TEST_FOR_EXCEPTION(
120 matMpiComm.is_null (), std::logic_error,
"Amesos2::Superlustdist " 121 "constructor: The matrix's communicator is not an MpiComm!");
122 TEUCHOS_TEST_FOR_EXCEPTION(
123 matMpiComm->getRawMpiComm ().is_null (), std::logic_error,
"Amesos2::" 124 "Superlustdist constructor: The matrix's communicator claims to be a " 125 "Teuchos::MpiComm<int>, but its getRawPtrComm() method returns " 126 "Teuchos::null! This means that the underlying MPI_Comm doesn't even " 127 "exist, which likely implies that the Teuchos::MpiComm was constructed " 128 "incorrectly. It means something different than if the MPI_Comm were " 130 MPI_Comm rawMpiComm = (* (matMpiComm->getRawMpiComm ())) ();
131 data_.mat_comm = rawMpiComm;
137 SLUD::superlu_gridinit(data_.mat_comm, nprow, npcol, &(data_.grid));
146 SLUD::set_default_options_dist(&data_.options);
148 RCP<ParameterList> default_params =
153 data_.options.Fact = SLUD::DOFACT;
154 data_.equed = SLUD::NOEQUIL;
155 data_.options.SolveInitialized = SLUD::NO;
156 data_.options.RefineInitialized = SLUD::NO;
157 data_.rowequ =
false;
158 data_.colequ =
false;
166 data_.symb_comm = MPI_COMM_NULL;
171 data_.domains = (int) ( pow(2.0, floor(log10((
double)nprow*npcol)/log10(2.0))) );
173 const int color = (myRank < data_.domains) ? 0 : MPI_UNDEFINED;
174 MPI_Comm_split (data_.mat_comm, color, myRank, &(data_.symb_comm));
186 int myProcParticipates = 0;
187 if (myRank < nprow * npcol) {
189 myProcParticipates = 1;
194 int numParticipatingProcs = 0;
195 reduceAll<int, int> (*comm, REDUCE_SUM, myProcParticipates,
196 outArg (numParticipatingProcs));
197 TEUCHOS_TEST_FOR_EXCEPTION(
199 std::logic_error,
"Amesos2::Superludist constructor: The matrix has " 200 << this->
globalNumRows_ <<
" > 0 global row(s), but no processes in the " 201 "communicator want to participate in its factorization! nprow = " 202 << nprow <<
" and npcol = " << npcol <<
".");
205 size_t myNumRows = 0;
208 const GO quotient = (numParticipatingProcs == 0) ? static_cast<GO> (0) :
209 GNR /
static_cast<GO
> (numParticipatingProcs);
211 GNR - quotient *
static_cast<GO
> (numParticipatingProcs);
212 const GO lclNumRows = (
static_cast<GO
> (myRank) < remainder) ?
213 (quotient +
static_cast<GO
> (1)) : quotient;
214 myNumRows =
static_cast<size_t> (lclNumRows);
218 const GO indexBase = this->
matrixA_->getRowMap ()->getIndexBase ();
220 rcp (
new map_type (this->
globalNumRows_, myNumRows, indexBase, comm));
226 data_.A.Store = NULL;
228 SLUD::PStatInit(&(data_.stat));
231 data_.scale_perm.perm_r = data_.perm_r.getRawPtr();
232 data_.scale_perm.perm_c = data_.perm_c.getRawPtr();
236 template <
class Matrix,
class Vector>
248 if ( this->status_.getNumPreOrder() > 0 ){
250 free( data_.fstVtxSep );
255 if( data_.A.Store != NULL ){
256 SLUD::Destroy_SuperMatrix_Store_dist( &(data_.A) );
260 if ( this->status_.getNumNumericFact() > 0 ){
261 function_map::Destroy_LU(this->globalNumRows_, &(data_.grid), &(data_.lu));
263 function_map::LUstructFree(&(data_.lu));
269 if ( this->status_.symbolicFactorizationDone() &&
270 !this->status_.numericFactorizationDone() ){
271 if ( data_.pslu_freeable.xlsub != NULL ){
272 free( data_.pslu_freeable.xlsub );
273 free( data_.pslu_freeable.lsub );
275 if ( data_.pslu_freeable.xusub != NULL ){
276 free( data_.pslu_freeable.xusub );
277 free( data_.pslu_freeable.usub );
279 if ( data_.pslu_freeable.supno_loc != NULL ){
280 free( data_.pslu_freeable.supno_loc );
281 free( data_.pslu_freeable.xsup_beg_loc );
282 free( data_.pslu_freeable.xsup_end_loc );
284 free( data_.pslu_freeable.globToLoc );
287 SLUD::PStatFree( &(data_.stat) ) ;
292 if ( data_.options.SolveInitialized == SLUD::YES )
293 function_map::SolveFinalize(&(data_.options), &(data_.solve_struct));
295 SLUD::superlu_gridexit(&(data_.grid));
299 if ( data_.symb_comm != MPI_COMM_NULL ) MPI_Comm_free(&(data_.symb_comm));
302 template<
class Matrix,
class Vector>
311 SLUD::int_t slu_rows_ub = Teuchos::as<SLUD::int_t>(this->globalNumRows_);
312 for( SLUD::int_t i = 0; i < slu_rows_ub; ++i ) data_.perm_r[i] = i;
322 if( this->status_.getNumPreOrder() > 0 ){
324 free( data_.fstVtxSep );
326 #ifdef HAVE_AMESOS2_TIMERS 327 Teuchos::TimeMonitor preOrderTime( this->timers_.preOrderTime_ );
331 info = SLUD::get_perm_c_parmetis( &(data_.A),
332 data_.perm_r.getRawPtr(), data_.perm_c.getRawPtr(),
333 data_.grid.nprow * data_.grid.npcol, data_.domains,
334 &(data_.sizes), &(data_.fstVtxSep),
335 &(data_.grid), &(data_.symb_comm) );
337 TEUCHOS_TEST_FOR_EXCEPTION( info > 0.0,
339 "SuperLU_DIST pre-ordering ran out of memory after allocating " 340 << info <<
" bytes of memory" );
352 template <
class Matrix,
class Vector>
360 #ifdef HAVE_AMESOS2_TIMERS 361 Teuchos::TimeMonitor symFactTime( this->timers_.symFactTime_ );
365 info = SLUD::symbfact_dist((data_.grid.nprow) * (data_.grid.npcol),
366 data_.domains, &(data_.A), data_.perm_c.getRawPtr(),
367 data_.perm_r.getRawPtr(), data_.sizes,
368 data_.fstVtxSep, &(data_.pslu_freeable),
369 &(data_.grid.comm), &(data_.symb_comm),
372 TEUCHOS_TEST_FOR_EXCEPTION( info > 0.0,
374 "SuperLU_DIST symbolic factorization ran out of memory after" 375 " allocating " << info <<
" bytes of memory" );
377 same_symbolic_ =
false;
378 same_solve_struct_ =
false;
384 template <
class Matrix,
class Vector>
403 size_t nnz_loc = ((SLUD::NRformat_loc*)data_.A.Store)->nnz_loc;
404 for(
size_t i = 0; i < nnz_loc; ++i ) colind_[i] = data_.perm_c[colind_[i]];
407 if( same_symbolic_ ){
412 function_map::pdistribute(SLUD::SamePattern_SameRowPerm,
413 as<SLUD::int_t>(this->globalNumRows_),
414 &(data_.A), &(data_.scale_perm),
415 &(data_.glu_freeable), &(data_.lu),
418 function_map::dist_psymbtonum(SLUD::DOFACT,
419 as<SLUD::int_t>(this->globalNumRows_),
420 &(data_.A), &(data_.scale_perm),
421 &(data_.pslu_freeable), &(data_.lu),
426 double anorm = function_map::plangs((
char *)
"I", &(data_.A), &(data_.grid));
430 #ifdef HAVE_AMESOS2_TIMERS 431 Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
434 function_map::gstrf(&(data_.options), this->globalNumRows_,
435 this->globalNumCols_, anorm, &(data_.lu),
436 &(data_.grid), &(data_.stat), &info);
440 TEUCHOS_TEST_FOR_EXCEPTION( info > 0,
442 "L and U factors have been computed but U(" 443 << info <<
"," << info <<
") is exactly zero " 444 "(i.e. U is singular)");
451 data_.options.Fact = SLUD::FACTORED;
452 same_symbolic_ =
true;
458 template <
class Matrix,
class Vector>
467 const size_t local_len_rhs = superlu_rowmap_->getNodeNumElements();
468 const global_size_type nrhs = X->getGlobalNumVectors();
469 const global_ordinal_type first_global_row_b = superlu_rowmap_->getMinGlobalIndex();
472 bvals_.resize(nrhs * local_len_rhs);
473 xvals_.resize(nrhs * local_len_rhs);
479 #ifdef HAVE_AMESOS2_TIMERS 480 Teuchos::TimeMonitor convTimer(this->timers_.vecConvTime_);
490 #ifdef HAVE_AMESOS2_TIMERS 491 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
497 copy_helper::do_get(B,
500 Teuchos::ptrInArg(*superlu_rowmap_));
521 if( !same_solve_struct_ ){
522 if( data_.options.SolveInitialized == SLUD::YES ){
523 function_map::SolveFinalize(&(data_.options), &(data_.solve_struct));
525 function_map::SolveInit(&(data_.options), &(data_.A), data_.perm_r.getRawPtr(),
526 data_.perm_c.getRawPtr(), as<SLUD::int_t>(nrhs), &(data_.lu),
527 &(data_.grid), &(data_.solve_struct));
531 same_solve_struct_ =
true;
536 #ifdef HAVE_AMESOS2_TIMERS 537 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
540 function_map::gstrs(as<SLUD::int_t>(this->globalNumRows_), &(data_.lu),
541 &(data_.scale_perm), &(data_.grid), bvals_.getRawPtr(),
542 as<SLUD::int_t>(local_len_rhs), as<SLUD::int_t>(first_global_row_b),
543 as<SLUD::int_t>(local_len_rhs), as<int>(nrhs),
544 &(data_.solve_struct), &(data_.stat), &ierr);
547 TEUCHOS_TEST_FOR_EXCEPTION( ierr < 0,
549 "Argument " << -ierr <<
" to gstrs had an illegal value" );
564 #ifdef HAVE_AMESOS2_TIMERS 565 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
567 SLUD::int_t ld = as<SLUD::int_t>(local_len_rhs);
568 function_map::permute_Dense_Matrix(as<SLUD::int_t>(first_global_row_b),
569 as<SLUD::int_t>(local_len_rhs),
570 data_.solve_struct.row_to_proc,
571 data_.solve_struct.inv_perm_c,
572 bvals_.getRawPtr(), ld,
573 xvals_.getRawPtr(), ld,
581 #ifdef HAVE_AMESOS2_TIMERS 582 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
586 put_helper::do_put(X,
589 Teuchos::ptrInArg(*superlu_rowmap_));
596 template <
class Matrix,
class Vector>
601 return( this->globalNumRows_ == this->globalNumCols_ );
605 template <
class Matrix,
class Vector>
611 using Teuchos::getIntegralValue;
612 using Teuchos::ParameterEntryValidator;
614 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
616 if( parameterList->isParameter(
"npcol") || parameterList->isParameter(
"nprow") ){
617 TEUCHOS_TEST_FOR_EXCEPTION( !(parameterList->isParameter(
"nprow") &&
618 parameterList->isParameter(
"npcol")),
619 std::invalid_argument,
620 "nprow and npcol must be set together" );
622 SLUD::int_t nprow = parameterList->template get<SLUD::int_t>(
"nprow");
623 SLUD::int_t npcol = parameterList->template get<SLUD::int_t>(
"npcol");
625 TEUCHOS_TEST_FOR_EXCEPTION( nprow * npcol > this->getComm()->getSize(),
626 std::invalid_argument,
627 "nprow and npcol combination invalid" );
629 if( (npcol != data_.grid.npcol) || (nprow != data_.grid.nprow) ){
631 SLUD::superlu_gridexit(&(data_.grid));
633 SLUD::superlu_gridinit(data_.mat_comm, nprow, npcol, &(data_.grid));
637 TEUCHOS_TEST_FOR_EXCEPTION( this->control_.useTranspose_,
638 std::invalid_argument,
639 "SuperLU_DIST does not support solving the tranpose system" );
641 data_.options.Trans = SLUD::NOTRANS;
646 data_.options.Equil = SLUD::NO;
648 if( parameterList->isParameter(
"ColPerm") ){
649 RCP<const ParameterEntryValidator> colperm_validator = valid_params->getEntry(
"ColPerm").validator();
650 parameterList->getEntry(
"ColPerm").setValidator(colperm_validator);
652 data_.options.ColPerm = getIntegralValue<SLUD::colperm_t>(*parameterList,
"ColPerm");
659 data_.options.RowPerm = SLUD::NOROWPERM;
668 data_.options.IterRefine = SLUD::NOREFINE;
670 bool replace_tiny = parameterList->get<
bool>(
"ReplaceTinyPivot",
true);
671 data_.options.ReplaceTinyPivot = replace_tiny ? SLUD::YES : SLUD::NO;
675 template <
class Matrix,
class Vector>
676 Teuchos::RCP<const Teuchos::ParameterList>
680 using Teuchos::tuple;
681 using Teuchos::ParameterList;
682 using Teuchos::EnhancedNumberValidator;
683 using Teuchos::setStringToIntegralParameter;
684 using Teuchos::stringToIntegralParameterEntryValidator;
686 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
688 if( is_null(valid_params) ){
689 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
691 Teuchos::RCP<EnhancedNumberValidator<SLUD::int_t> > col_row_validator
692 = Teuchos::rcp(
new EnhancedNumberValidator<SLUD::int_t>() );
693 col_row_validator->setMin(1);
695 pl->set(
"npcol", data_.grid.npcol,
696 "Number of columns in the processor grid. " 697 "Must be set with nprow", col_row_validator);
698 pl->set(
"nprow", data_.grid.nprow,
699 "Number of rows in the SuperLU_DIST processor grid. " 700 "Must be set together with npcol", col_row_validator);
703 setStringToIntegralParameter<SLUD::trans_t>(
"Trans",
"NOTRANS",
704 "Solve for the transpose system or not",
705 tuple<string>(
"NOTRANS"),
706 tuple<string>(
"Do not solve with transpose"),
707 tuple<SLUD::trans_t>(SLUD::NOTRANS),
723 pl->set(
"ReplaceTinyPivot",
true,
724 "Specifies whether to replace tiny diagonals during LU factorization");
726 setStringToIntegralParameter<SLUD::colperm_t>(
"ColPerm",
"PARMETIS",
727 "Specifies how to permute the columns of the " 728 "matrix for sparsity preservation",
729 tuple<string>(
"NATURAL",
"PARMETIS"),
730 tuple<string>(
"Natural ordering",
731 "ParMETIS ordering on A^T + A"),
732 tuple<SLUD::colperm_t>(SLUD::NATURAL,
743 template <
class Matrix,
class Vector>
747 SLUD::int_t& npcol)
const {
748 TEUCHOS_TEST_FOR_EXCEPTION( nprocs < 1,
749 std::invalid_argument,
750 "Number of MPI processes must be at least 1" );
751 SLUD::int_t c, r = 1;
752 while( r*r <= nprocs ) r++;
755 while( (r--)*c != nprocs ){
761 if( r > 1 || nprocs < 9){
768 template <
class Matrix,
class Vector>
772 using Teuchos::Array;
773 using Teuchos::ArrayView;
774 using Teuchos::ptrInArg;
779 #ifdef HAVE_AMESOS2_TIMERS 780 Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
784 if( data_.A.Store != NULL ){
785 SLUD::Destroy_SuperMatrix_Store_dist( &(data_.A) );
786 data_.A.Store = NULL;
789 Teuchos::RCP<const MatrixAdapter<Matrix> > redist_mat
790 = this->matrixA_->get(ptrInArg(*superlu_rowmap_));
792 int_t l_nnz, l_rows, g_rows, g_cols, fst_global_row;
793 l_nnz = as<int_t>(redist_mat->getLocalNNZ());
794 l_rows = as<int_t>(redist_mat->getLocalNumRows());
795 g_rows = as<int_t>(redist_mat->getGlobalNumRows());
797 fst_global_row = as<int_t>(superlu_rowmap_->getMinGlobalIndex());
799 nzvals_.resize(l_nnz);
800 colind_.resize(l_nnz);
801 rowptr_.resize(l_rows + 1);
805 #ifdef HAVE_AMESOS2_TIMERS 806 Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
811 slu_type, int_t, int_t >::do_get(redist_mat.ptr(),
812 nzvals_(), colind_(), rowptr_(),
814 ptrInArg(*superlu_rowmap_),
818 TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != l_nnz,
820 "Did not get the expected number of non-zero vals");
823 SLUD::Dtype_t dtype = type_map::dtype;
826 function_map::create_CompRowLoc_Matrix(&(data_.A),
828 l_nnz, l_rows, fst_global_row,
833 dtype, SLUD::SLU_GE);
840 template<
class Matrix,
class Vector>
846 #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
void get_default_grid_size(int nprocs, SLUD::int_t &nprow, SLUD::int_t &npcol) const
Definition: Amesos2_Superludist_def.hpp:745
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition: Amesos2_Util.hpp:591
global_size_type globalNumCols_
Number of global columns in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:479
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 > ¶meterList)
Definition: Amesos2_Superludist_def.hpp:607
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Superludist_def.hpp:677
global_size_type globalNumRows_
Number of global rows in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:476
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:243
Definition: Amesos2_TypeDecl.hpp:142
Utility functions for Amesos2.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a const parameter list of all of the valid parameters that this->setParameterList(...) will accept.
Definition: Amesos2_SolverCore_def.hpp:310
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns a pointer to the Teuchos::Comm communicator with this operator.
Definition: Amesos2_SolverCore_decl.hpp:363
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:460
Provides definition of SuperLU_DIST types as well as conversions and type traits. ...
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Superludist_def.hpp:304
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Superludist_def.hpp:598
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
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:237
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:278
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using SuperLU_DIST.
Definition: Amesos2_Superludist_def.hpp:354
bool in_grid_
true if this processor is in SuperLU_DISTS'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:296
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:770
Teuchos::RCP< const MatrixAdapter< Matrix > > matrixA_
The LHS operator.
Definition: Amesos2_SolverCore_decl.hpp:455
int numericFactorization_impl()
SuperLU_DIST specific numeric factorization.
Definition: Amesos2_Superludist_def.hpp:386