// @HEADER // ***************************************************************************** // Amesos2: Templated Direct Sparse Solver Package // // Copyright 2011 NTESS and the Amesos2 contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER /** \file Amesos2_Basker_def.hpp \author Joshua Dennis Booth Siva Rajamanickam \brief Definitions for the Amesos2 Basker solver interface */ #ifndef AMESOS2_BASKER_DEF_HPP #define AMESOS2_BASKER_DEF_HPP #include #include #include #include "Amesos2_SolverCore_def.hpp" #include "Amesos2_Basker_decl.hpp" namespace Amesos2 { template Basker::Basker( Teuchos::RCP A, Teuchos::RCP X, Teuchos::RCP B ) : SolverCore(A, X, B) , is_contiguous_(true) // , basker() { //Nothing } template Basker::~Basker( ) {} template bool Basker::single_proc_optimization() const { return (this->root_ && (this->matrixA_->getComm()->getSize() == 1) && is_contiguous_); } template int Basker::preOrdering_impl() { /* TODO: Define what it means for Basker */ #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_); #endif return(0); } template int Basker::symbolicFactorization_impl() { /*No symbolic factoriztion*/ return(0); } template int Basker::numericFactorization_impl() { using Teuchos::as; int info = 0; if ( this->root_ ){ { // Do factorization #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_); #endif #ifdef HAVE_AMESOS2_VERBOSE_DEBUG std::cout << "Basker:: Before numeric factorization" << std::endl; std::cout << "nzvals_ : " << nzvals_.toString() << std::endl; std::cout << "rowind_ : " << rowind_.toString() << std::endl; std::cout << "colptr_ : " << colptr_.toString() << std::endl; #endif basker_dtype * pBaskerValues = function_map::convert_scalar(host_nzvals_view_.data()); info = basker.factor(this->globalNumRows_, this->globalNumCols_, this->globalNumNonZeros_, host_col_ptr_view_.data(), host_rows_view_.data(), pBaskerValues); // This is set after numeric factorization complete as pivoting can be used; // In this case, a discrepancy between symbolic and numeric nnz total can occur. this->setNnzLU( as(basker.get_NnzLU() ) ) ; } } /* All processes should have the same error code */ Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info); //global_size_type info_st = as(info); /* TODO : Proper error messages*/ TEUCHOS_TEST_FOR_EXCEPTION( (info == -1) , std::runtime_error, "Basker: Could not alloc space for L and U"); TEUCHOS_TEST_FOR_EXCEPTION( (info == -2), std::runtime_error, "Basker: Could not alloc needed work space"); TEUCHOS_TEST_FOR_EXCEPTION( (info == -3) , std::runtime_error, "Basker: Could not alloc additional memory needed for L and U"); TEUCHOS_TEST_FOR_EXCEPTION( (info > 0) , std::runtime_error, "Basker: Zero pivot found at: " << info ); return(info); } template int Basker::solve_impl( const Teuchos::Ptr > X, const Teuchos::Ptr > B) const { int ierr = 0; // returned error code using Teuchos::as; const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0; const size_t nrhs = X->getGlobalNumVectors(); bool bDidAssignX; { // Get values from RHS B #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_); Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ ); #endif const bool initialize_data = true; const bool do_not_initialize_data = false; if ( single_proc_optimization() && nrhs == 1 ) { // no msp creation Util::get_1d_copy_helper_kokkos_view, host_solve_array_t>::do_get(initialize_data, B, bValues_, as(ld_rhs)); bDidAssignX = Util::get_1d_copy_helper_kokkos_view, host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_, as(ld_rhs)); } else { Util::get_1d_copy_helper_kokkos_view, host_solve_array_t>::do_get(initialize_data, B, bValues_, as(ld_rhs), (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED, this->rowIndexBase_); // See Amesos2_Tacho_def.hpp for notes on why we 'get' x here. bDidAssignX = Util::get_1d_copy_helper_kokkos_view, host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_, as(ld_rhs), (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED, this->rowIndexBase_); } } if ( this->root_ ) { // do solve #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_); #endif basker_dtype * pxBaskerValues = function_map::convert_scalar(xValues_.data()); basker_dtype * pbBaskerValues = function_map::convert_scalar(bValues_.data()); ierr = basker.solveMultiple(nrhs, pbBaskerValues, pxBaskerValues); } /* All processes should have the same error code */ Teuchos::broadcast(*(this->getComm()), 0, &ierr); TEUCHOS_TEST_FOR_EXCEPTION( ierr > 0, std::runtime_error, "Encountered zero diag element at: " << ierr); TEUCHOS_TEST_FOR_EXCEPTION( ierr == -1, std::runtime_error, "Could not alloc needed working memory for solve" ); // if bDidAssignX, then we solved straight to the adapter's X memory space without // requiring additional memory allocation, so the x data is already in place. if(!bDidAssignX) { #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_); #endif Util::put_1d_data_helper_kokkos_view, host_solve_array_t>::do_put(X, xValues_, as(ld_rhs), (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED); } return(ierr); } template bool Basker::matrixShapeOK_impl() const { // The Basker can only handle square for right now return( this->globalNumRows_ == this->globalNumCols_ ); } template void Basker::setParameters_impl(const Teuchos::RCP & parameterList ) { using Teuchos::RCP; using Teuchos::getIntegralValue; using Teuchos::ParameterEntryValidator; RCP valid_params = getValidParameters_impl(); if(parameterList->isParameter("IsContiguous")) { is_contiguous_ = parameterList->get("IsContiguous"); } } template Teuchos::RCP Basker::getValidParameters_impl() const { using Teuchos::ParameterList; static Teuchos::RCP valid_params; if( is_null(valid_params) ){ Teuchos::RCP pl = Teuchos::parameterList(); pl->set("IsContiguous", true, "Are GIDs contiguous"); pl->set("alnnz", 2, "Approx number of nonzeros in L, default is 2*nnz(A)"); pl->set("aunnx", 2, "Approx number of nonzeros in I, default is 2*nnz(U)"); valid_params = pl; } return valid_params; } template bool Basker::loadA_impl(EPhase current_phase) { using Teuchos::as; if(current_phase == SOLVE) return (false); #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_); #endif // Only the root image needs storage allocated if( this->root_ ){ host_nzvals_view_ = host_value_type_array( Kokkos::ViewAllocateWithoutInitializing("host_nzvals_view_"), this->globalNumNonZeros_); host_rows_view_ = host_ordinal_type_array( Kokkos::ViewAllocateWithoutInitializing("host_rows_view_"), this->globalNumNonZeros_); host_col_ptr_view_ = host_ordinal_type_array( Kokkos::ViewAllocateWithoutInitializing("host_col_ptr_view_"), this->globalNumRows_ + 1); } local_ordinal_type nnz_ret = 0; { #ifdef HAVE_AMESOS2_TIMERS Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ ); #endif Util::get_ccs_helper_kokkos_view< MatrixAdapter,host_value_type_array,host_ordinal_type_array,host_ordinal_type_array> ::do_get(this->matrixA_.ptr(), host_nzvals_view_, host_rows_view_, host_col_ptr_view_, nnz_ret, (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED, ARBITRARY, this->rowIndexBase_); } if( this->root_ ){ TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as(this->globalNumNonZeros_), std::runtime_error, "Amesos2_Basker loadA_impl: Did not get the expected number of non-zero vals"); } return true; } template const char* Basker::name = "Basker"; } // end namespace Amesos2 #endif // AMESOS2_Basker_DEF_HPP