// @HEADER // ***************************************************************************** // Panzer: A partial differential equation assembly // engine for strongly coupled complex multiphysics systems // // Copyright 2011 NTESS and the Panzer contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef __Panzer_BlockedTpetraLinearObjFactory_impl_hpp__ #define __Panzer_BlockedTpetraLinearObjFactory_impl_hpp__ // Panzer #include "Panzer_BlockedVector_ReadOnly_GlobalEvaluationData.hpp" #ifdef PANZER_HAVE_EPETRA_STACK #include "Panzer_EpetraVector_Write_GlobalEvaluationData.hpp" // JMG: Remove this eventually. #endif #include "Panzer_TpetraVector_ReadOnly_GlobalEvaluationData.hpp" #include "Panzer_GlobalIndexer.hpp" // Thyra #include "Thyra_DefaultBlockedLinearOp.hpp" #include "Thyra_DefaultProductVectorSpace.hpp" #include "Thyra_SpmdVectorBase.hpp" #include "Thyra_TpetraLinearOp.hpp" #include "Thyra_TpetraThyraWrappers.hpp" // Tpetra #include "Tpetra_CrsMatrix.hpp" #include "Tpetra_MultiVector.hpp" #include "Tpetra_Vector.hpp" namespace panzer { using Teuchos::RCP; // ************************************************************ // class BlockedTpetraLinearObjFactory // ************************************************************ template BlockedTpetraLinearObjFactory:: BlockedTpetraLinearObjFactory(const Teuchos::RCP > & comm, const Teuchos::RCP & gidProvider) : blockProvider_(gidProvider), blockedDOFManager_(gidProvider), comm_(comm) { for(std::size_t i=0;igetFieldDOFManagers().size();i++) gidProviders_.push_back(gidProvider->getFieldDOFManagers()[i]); makeRoomForBlocks(gidProviders_.size()); // build and register the gather/scatter evaluators with // the base class. this->buildGatherScatterEvaluators(*this); } template BlockedTpetraLinearObjFactory:: BlockedTpetraLinearObjFactory(const Teuchos::RCP > & comm, const std::vector> & gidProviders) : gidProviders_(gidProviders), comm_(comm) { makeRoomForBlocks(gidProviders_.size()); } template BlockedTpetraLinearObjFactory:: ~BlockedTpetraLinearObjFactory() { } // LinearObjectFactory functions ///////////////////////////////////////////////////////////////////// template Teuchos::RCP BlockedTpetraLinearObjFactory:: buildLinearObjContainer() const { std::vector > blockMaps; std::size_t blockDim = gidProviders_.size(); for(std::size_t i=0;i container = Teuchos::rcp(new BTLOC); container->setMapsForBlocks(blockMaps); return container; } template Teuchos::RCP BlockedTpetraLinearObjFactory:: buildGhostedLinearObjContainer() const { std::vector > blockMaps; std::size_t blockDim = gidProviders_.size(); for(std::size_t i=0;i container = Teuchos::rcp(new BTLOC); container->setMapsForBlocks(blockMaps); return container; } template void BlockedTpetraLinearObjFactory:: globalToGhostContainer(const LinearObjContainer & in,LinearObjContainer & out,int mem) const { using Teuchos::is_null; typedef LinearObjContainer LOC; const BTLOC & b_in = Teuchos::dyn_cast(in); BTLOC & b_out = Teuchos::dyn_cast(out); // Operations occur if the GLOBAL container has the correct targets! // Users set the GLOBAL continer arguments if ( !is_null(b_in.get_x()) && !is_null(b_out.get_x()) && ((mem & LOC::X)==LOC::X)) globalToGhostThyraVector(b_in.get_x(),b_out.get_x()); if ( !is_null(b_in.get_dxdt()) && !is_null(b_out.get_dxdt()) && ((mem & LOC::DxDt)==LOC::DxDt)) globalToGhostThyraVector(b_in.get_dxdt(),b_out.get_dxdt()); if ( !is_null(b_in.get_f()) && !is_null(b_out.get_f()) && ((mem & LOC::F)==LOC::F)) globalToGhostThyraVector(b_in.get_f(),b_out.get_f()); } template void BlockedTpetraLinearObjFactory:: ghostToGlobalContainer(const LinearObjContainer & in,LinearObjContainer & out,int mem) const { using Teuchos::is_null; typedef LinearObjContainer LOC; const BTLOC & b_in = Teuchos::dyn_cast(in); BTLOC & b_out = Teuchos::dyn_cast(out); // Operations occur if the GLOBAL container has the correct targets! // Users set the GLOBAL continer arguments if ( !is_null(b_in.get_x()) && !is_null(b_out.get_x()) && ((mem & LOC::X)==LOC::X)) ghostToGlobalThyraVector(b_in.get_x(),b_out.get_x()); if ( !is_null(b_in.get_f()) && !is_null(b_out.get_f()) && ((mem & LOC::F)==LOC::F)) ghostToGlobalThyraVector(b_in.get_f(),b_out.get_f()); if ( !is_null(b_in.get_A()) && !is_null(b_out.get_A()) && ((mem & LOC::Mat)==LOC::Mat)) ghostToGlobalThyraMatrix(*b_in.get_A(),*b_out.get_A()); } template void BlockedTpetraLinearObjFactory:: adjustForDirichletConditions(const LinearObjContainer & localBCRows, const LinearObjContainer & globalBCRows, LinearObjContainer & ghostedObjs, bool zeroVectorRows, bool adjustX) const { using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; using Thyra::LinearOpBase; using Thyra::PhysicallyBlockedLinearOpBase; using Thyra::VectorBase; using Thyra::ProductVectorBase; std::size_t blockDim = gidProviders_.size(); // first cast to block LOCs const BTLOC & b_localBCRows = Teuchos::dyn_cast(localBCRows); const BTLOC & b_globalBCRows = Teuchos::dyn_cast(globalBCRows); BTLOC & b_ghosted = Teuchos::dyn_cast(ghostedObjs); TEUCHOS_ASSERT(b_localBCRows.get_f()!=Teuchos::null); TEUCHOS_ASSERT(b_globalBCRows.get_f()!=Teuchos::null); // cast each component as needed to their product form RCP > A = rcp_dynamic_cast >(b_ghosted.get_A()); RCP > f = rcp_dynamic_cast >(b_ghosted.get_f()); RCP > local_bcs = rcp_dynamic_cast >(b_localBCRows.get_f(),true); RCP > global_bcs = rcp_dynamic_cast >(b_globalBCRows.get_f(),true); if(adjustX) f = rcp_dynamic_cast >(b_ghosted.get_x()); // sanity check! if(A!=Teuchos::null) TEUCHOS_ASSERT(A->productRange()->numBlocks()==(int) blockDim); if(A!=Teuchos::null) TEUCHOS_ASSERT(A->productDomain()->numBlocks()==(int) blockDim); if(f!=Teuchos::null) TEUCHOS_ASSERT(f->productSpace()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(local_bcs->productSpace()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(global_bcs->productSpace()->numBlocks()==(int) blockDim); for(std::size_t i=0;i t_local_bcs = rcp_dynamic_cast(local_bcs->getVectorBlock(i),true)->getConstTpetraVector(); RCP t_global_bcs = rcp_dynamic_cast(global_bcs->getVectorBlock(i),true)->getConstTpetraVector(); // pull out epetra values RCP > th_f = (f==Teuchos::null) ? Teuchos::null : f->getNonconstVectorBlock(i); RCP t_f; if(th_f==Teuchos::null) t_f = Teuchos::null; else t_f = rcp_dynamic_cast(th_f,true)->getTpetraVector(); for(std::size_t j=0;j map_i = getGhostedMap(i); RCP map_j = getGhostedMap(j); // pull out epetra values RCP > th_A = (A== Teuchos::null)? Teuchos::null : A->getNonconstBlock(i,j); // don't do anyting if opertor is null RCP t_A; if(th_A==Teuchos::null) t_A = Teuchos::null; else { RCP t_A_op = rcp_dynamic_cast(th_A,true)->getTpetraOperator(); t_A = rcp_dynamic_cast(t_A_op,true); } // adjust Block operator if(t_A!=Teuchos::null) { t_A->resumeFill(); } adjustForDirichletConditions(*t_local_bcs,*t_global_bcs,t_f.ptr(),t_A.ptr(),zeroVectorRows); if(t_A!=Teuchos::null) { //t_A->fillComplete(map_j,map_i); } t_f = Teuchos::null; // this is so we only adjust it once on the first pass } } } template void BlockedTpetraLinearObjFactory:: adjustForDirichletConditions(const VectorType & local_bcs, const VectorType & global_bcs, const Teuchos::Ptr & f, const Teuchos::Ptr & A, bool zeroVectorRows) const { if(f==Teuchos::null && A==Teuchos::null) return; Teuchos::ArrayRCP f_array = f!=Teuchos::null ? f->get1dViewNonConst() : Teuchos::null; Teuchos::ArrayRCP local_bcs_array = local_bcs.get1dView(); Teuchos::ArrayRCP global_bcs_array = global_bcs.get1dView(); TEUCHOS_ASSERT(local_bcs.getLocalLength()==global_bcs.getLocalLength()); for(std::size_t i=0;igetNumEntriesInLocalRow(i); typename CrsMatrixType::nonconst_local_inds_host_view_type indices("indices", sz); typename CrsMatrixType::nonconst_values_host_view_type values("values", sz); A->getLocalRowCopy(i,indices,values,numEntries); for(std::size_t c=0;creplaceLocalValues(i,indices,values); } } else { // this boundary condition was set by this processor ScalarT scaleFactor = global_bcs_array[i]; // if they exist scale linear objects by scale factor if(!Teuchos::is_null(f)) f_array[i] /= scaleFactor; if(!Teuchos::is_null(A)) { std::size_t numEntries = 0; std::size_t sz = A->getNumEntriesInLocalRow(i); typename CrsMatrixType::nonconst_local_inds_host_view_type indices("indices", sz); typename CrsMatrixType::nonconst_values_host_view_type values("values", sz); A->getLocalRowCopy(i,indices,values,numEntries); for(std::size_t c=0;creplaceLocalValues(i,indices,values); } } } } template void BlockedTpetraLinearObjFactory:: applyDirichletBCs(const LinearObjContainer & /* counter */, LinearObjContainer & /* result */) const { TEUCHOS_ASSERT(false); // not yet implemented } /////////////////////////////////////////////////////////////////////////////// // // buildReadOnlyDomainContainer() // /////////////////////////////////////////////////////////////////////////////// template Teuchos::RCP BlockedTpetraLinearObjFactory:: buildReadOnlyDomainContainer() const { using std::vector; using Teuchos::RCP; using Teuchos::rcp; using BVROGED = panzer::BlockedVector_ReadOnly_GlobalEvaluationData; using TVROGED = panzer::TpetraVector_ReadOnly_GlobalEvaluationData; vector> gedBlocks; for (int i(0); i < getBlockColCount(); ++i) { auto tvroged = rcp(new TVROGED); tvroged->initialize(getGhostedImport(i), getGhostedMap(i), getMap(i)); gedBlocks.push_back(tvroged); } auto ged = rcp(new BVROGED); ged->initialize(getGhostedThyraDomainSpace(), getThyraDomainSpace(), gedBlocks); return ged; } // end of buildReadOnlyDomainContainer() #ifdef PANZER_HAVE_EPETRA_STACK /////////////////////////////////////////////////////////////////////////////// // // buildWriteDomainContainer() // /////////////////////////////////////////////////////////////////////////////// template Teuchos::RCP BlockedTpetraLinearObjFactory:: buildWriteDomainContainer() const { using std::logic_error; using Teuchos::rcp; using EVWGED = panzer::EpetraVector_Write_GlobalEvaluationData; auto ged = rcp(new EVWGED); TEUCHOS_TEST_FOR_EXCEPTION(true, logic_error, "NOT YET IMPLEMENTED") return ged; } // end of buildWriteDomainContainer() #endif // PANZER_HAVE_EPETRA_STACK template Teuchos::MpiComm BlockedTpetraLinearObjFactory:: getComm() const { return *comm_; } template void BlockedTpetraLinearObjFactory:: initializeContainer(int mem,LinearObjContainer & loc) const { BTLOC & bloc = Teuchos::dyn_cast(loc); initializeContainer(mem,bloc); } template void BlockedTpetraLinearObjFactory:: initializeGhostedContainer(int mem,LinearObjContainer & loc) const { BTLOC & bloc = Teuchos::dyn_cast(loc); initializeGhostedContainer(mem,bloc); } // Generic methods ///////////////////////////////////////////////////////////////////// template void BlockedTpetraLinearObjFactory:: initializeContainer(int mem,BTLOC & loc) const { typedef LinearObjContainer LOC; loc.clear(); if((mem & LOC::X) == LOC::X) loc.set_x(getThyraDomainVector()); if((mem & LOC::DxDt) == LOC::DxDt) loc.set_dxdt(getThyraDomainVector()); if((mem & LOC::F) == LOC::F) loc.set_f(getThyraRangeVector()); if((mem & LOC::Mat) == LOC::Mat) loc.set_A(getThyraMatrix()); } template void BlockedTpetraLinearObjFactory:: initializeGhostedContainer(int mem,BTLOC & loc) const { typedef LinearObjContainer LOC; loc.clear(); if((mem & LOC::X) == LOC::X) loc.set_x(getGhostedThyraDomainVector()); if((mem & LOC::DxDt) == LOC::DxDt) loc.set_dxdt(getGhostedThyraDomainVector()); if((mem & LOC::F) == LOC::F) { loc.set_f(getGhostedThyraRangeVector()); loc.setRequiresDirichletAdjustment(true); } if((mem & LOC::Mat) == LOC::Mat) { loc.set_A(getGhostedThyraMatrix()); loc.setRequiresDirichletAdjustment(true); } } template void BlockedTpetraLinearObjFactory:: addExcludedPair(int rowBlock,int colBlock) { excludedPairs_.insert(std::make_pair(rowBlock,colBlock)); } template void BlockedTpetraLinearObjFactory:: addExcludedPairs(const std::vector > & exPairs) { for(std::size_t i=0;i Teuchos::RCP BlockedTpetraLinearObjFactory:: getGlobalIndexer(int i) const { return gidProviders_[i]; } template void BlockedTpetraLinearObjFactory:: makeRoomForBlocks(std::size_t blockCnt) { maps_.resize(blockCnt); ghostedMaps_.resize(blockCnt); importers_.resize(blockCnt); exporters_.resize(blockCnt); } // Thyra methods ///////////////////////////////////////////////////////////////////// template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraDomainSpace() const { if(domainSpace_==Teuchos::null) { // loop over all vectors and build the vector space std::vector > > vsArray; for(std::size_t i=0;i(getMap(i))); domainSpace_ = Thyra::productVectorSpace(vsArray); } return domainSpace_; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraRangeSpace() const { if(rangeSpace_==Teuchos::null) { // loop over all vectors and build the vector space std::vector > > vsArray; for(std::size_t i=0;i(getMap(i))); rangeSpace_ = Thyra::productVectorSpace(vsArray); } return rangeSpace_; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraDomainSpace(int blk) const { if(domainSpace_==Teuchos::null) { getThyraDomainSpace(); } return domainSpace_->getBlock(blk); } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraRangeSpace(int blk) const { if(rangeSpace_==Teuchos::null) { getThyraRangeSpace(); } return rangeSpace_->getBlock(blk); } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraDomainVector() const { Teuchos::RCP > vec = Thyra::createMember(*getThyraDomainSpace()); Thyra::assign(vec.ptr(),0.0); Teuchos::RCP > p_vec = Teuchos::rcp_dynamic_cast >(vec); for(std::size_t i=0;i >(p_vec->getNonconstVectorBlock(i))->spmdSpace()->localSubDim()== Teuchos::as(getMap(i)->getLocalNumElements())); } return vec; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraRangeVector() const { Teuchos::RCP > vec = Thyra::createMember(*getThyraRangeSpace()); Thyra::assign(vec.ptr(),0.0); return vec; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getThyraMatrix() const { Teuchos::RCP > blockedOp = Thyra::defaultBlockedLinearOp(); // get the block dimension std::size_t blockDim = gidProviders_.size(); // this operator will be square blockedOp->beginBlockFill(blockDim,blockDim); // loop over each block for(std::size_t i=0;i > block = Thyra::createLinearOp(getTpetraMatrix(i,j)); blockedOp->setNonconstBlock(i,j,block); } } } // all done blockedOp->endBlockFill(); return blockedOp; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedThyraDomainSpace() const { if(ghostedDomainSpace_==Teuchos::null) { // loop over all vectors and build the vector space std::vector > > vsArray; for(std::size_t i=0;i(getGhostedMap(i))); ghostedDomainSpace_ = Thyra::productVectorSpace(vsArray); } return ghostedDomainSpace_; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedThyraRangeSpace() const { if(ghostedRangeSpace_==Teuchos::null) { // loop over all vectors and build the vector space std::vector > > vsArray; for(std::size_t i=0;i(getGhostedMap(i))); ghostedRangeSpace_ = Thyra::productVectorSpace(vsArray); } return ghostedRangeSpace_; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedThyraDomainVector() const { Teuchos::RCP > vec = Thyra::createMember(*getGhostedThyraDomainSpace()); Thyra::assign(vec.ptr(),0.0); return vec; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedThyraRangeVector() const { Teuchos::RCP > vec = Thyra::createMember(*getGhostedThyraRangeSpace()); Thyra::assign(vec.ptr(),0.0); return vec; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedThyraMatrix() const { Teuchos::RCP > blockedOp = Thyra::defaultBlockedLinearOp(); // get the block dimension std::size_t blockDim = gidProviders_.size(); // this operator will be square blockedOp->beginBlockFill(blockDim,blockDim); // loop over each block for(std::size_t i=0;i > block = Thyra::createLinearOp(getGhostedTpetraMatrix(i,j)); blockedOp->setNonconstBlock(i,j,block); } } } // all done blockedOp->endBlockFill(); return blockedOp; } template void BlockedTpetraLinearObjFactory:: ghostToGlobalThyraVector(const Teuchos::RCP > & in, const Teuchos::RCP > & out) const { using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; using Thyra::ProductVectorBase; std::size_t blockDim = gidProviders_.size(); // get product vectors RCP > prod_in = rcp_dynamic_cast >(in,true); RCP > prod_out = rcp_dynamic_cast >(out,true); TEUCHOS_ASSERT(prod_in->productSpace()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(prod_out->productSpace()->numBlocks()==(int) blockDim); for(std::size_t i=0;i tp_in = rcp_dynamic_cast(prod_in->getVectorBlock(i),true)->getConstTpetraVector(); RCP tp_out = rcp_dynamic_cast(prod_out->getNonconstVectorBlock(i),true)->getTpetraVector(); // use Tpetra to do global communication ghostToGlobalTpetraVector(i,*tp_in,*tp_out); } } template void BlockedTpetraLinearObjFactory:: ghostToGlobalThyraMatrix(const Thyra::LinearOpBase & in,Thyra::LinearOpBase & out) const { using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; using Teuchos::dyn_cast; using Thyra::LinearOpBase; using Thyra::PhysicallyBlockedLinearOpBase; std::size_t blockDim = gidProviders_.size(); // get product vectors const PhysicallyBlockedLinearOpBase & prod_in = dyn_cast >(in); PhysicallyBlockedLinearOpBase & prod_out = dyn_cast >(out); TEUCHOS_ASSERT(prod_in.productRange()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(prod_in.productDomain()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(prod_out.productRange()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(prod_out.productDomain()->numBlocks()==(int) blockDim); for(std::size_t i=0;i > th_in = prod_in.getBlock(i,j); RCP > th_out = prod_out.getNonconstBlock(i,j); // sanity check TEUCHOS_ASSERT(th_in!=Teuchos::null); TEUCHOS_ASSERT(th_out!=Teuchos::null); // get the epetra version of the blocks RCP tp_op_in = rcp_dynamic_cast(th_in,true)->getConstTpetraOperator(); RCP tp_op_out = rcp_dynamic_cast(th_out,true)->getTpetraOperator(); RCP tp_in = rcp_dynamic_cast(tp_op_in,true); RCP tp_out = rcp_dynamic_cast(tp_op_out,true); // use Tpetra to do global communication ghostToGlobalTpetraMatrix(i,*tp_in,*tp_out); } } } } template void BlockedTpetraLinearObjFactory:: globalToGhostThyraVector(const Teuchos::RCP > & in, const Teuchos::RCP > & out) const { using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; using Thyra::ProductVectorBase; std::size_t blockDim = gidProviders_.size(); // get product vectors RCP > prod_in = rcp_dynamic_cast >(in,true); RCP > prod_out = rcp_dynamic_cast >(out,true); TEUCHOS_ASSERT(prod_in->productSpace()->numBlocks()==(int) blockDim); TEUCHOS_ASSERT(prod_out->productSpace()->numBlocks()==(int) blockDim); for(std::size_t i=0;i tp_in = rcp_dynamic_cast(prod_in->getVectorBlock(i),true)->getConstTpetraVector(); RCP tp_out = rcp_dynamic_cast(prod_out->getNonconstVectorBlock(i),true)->getTpetraVector(); // use Tpetra to do global communication globalToGhostTpetraVector(i,*tp_in,*tp_out); } } // Tpetra methods ///////////////////////////////////////////////////////////////////// template void BlockedTpetraLinearObjFactory:: ghostToGlobalTpetraVector(int i,const VectorType & in,VectorType & out) const { using Teuchos::RCP; // do the global distribution RCP exporter = getGhostedExport(i); out.putScalar(0.0); out.doExport(in,*exporter,Tpetra::ADD); } template void BlockedTpetraLinearObjFactory:: ghostToGlobalTpetraMatrix(int blockRow,const CrsMatrixType & in,CrsMatrixType & out) const { using Teuchos::RCP; RCP map_i = out.getRangeMap(); RCP map_j = out.getDomainMap(); // do the global distribution RCP exporter = getGhostedExport(blockRow); out.resumeFill(); out.setAllToScalar(0.0); out.doExport(in,*exporter,Tpetra::ADD); out.fillComplete(map_j,map_i); } template void BlockedTpetraLinearObjFactory:: globalToGhostTpetraVector(int i,const VectorType & in,VectorType & out) const { using Teuchos::RCP; // do the global distribution RCP importer = getGhostedImport(i); out.putScalar(0.0); out.doImport(in,*importer,Tpetra::INSERT); } // get the map from the matrix template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getMap(int i) const { if(maps_[i]==Teuchos::null) maps_[i] = buildTpetraMap(i); return maps_[i]; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedMap(int i) const { if(ghostedMaps_[i]==Teuchos::null) ghostedMaps_[i] = buildTpetraGhostedMap(i); return ghostedMaps_[i]; } // get the graph of the crs matrix template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGraph(int i,int j) const { typedef std::unordered_map,Teuchos::RCP,panzer::pair_hash> GraphMap; typename GraphMap::const_iterator itr = graphs_.find(std::make_pair(i,j)); Teuchos::RCP graph; if(itr==graphs_.end()) { graph = buildTpetraGraph(i,j); graphs_[std::make_pair(i,j)] = graph; } else graph = itr->second; TEUCHOS_ASSERT(graph!=Teuchos::null); return graph; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedGraph(int i,int j) const { typedef std::unordered_map,Teuchos::RCP,panzer::pair_hash> GraphMap; typename GraphMap::const_iterator itr = ghostedGraphs_.find(std::make_pair(i,j)); Teuchos::RCP ghostedGraph; if(itr==ghostedGraphs_.end()) { ghostedGraph = buildTpetraGhostedGraph(i,j); ghostedGraphs_[std::make_pair(i,j)] = ghostedGraph; } else ghostedGraph = itr->second; TEUCHOS_ASSERT(ghostedGraph!=Teuchos::null); return ghostedGraph; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedImport(int i) const { if(importers_[i]==Teuchos::null) importers_[i] = Teuchos::rcp(new ImportType(getMap(i),getGhostedMap(i))); return importers_[i]; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedExport(int i) const { if(exporters_[i]==Teuchos::null) exporters_[i] = Teuchos::rcp(new ExportType(getGhostedMap(i),getMap(i))); return exporters_[i]; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: buildTpetraMap(int i) const { std::vector indices; // get the global indices getGlobalIndexer(i)->getOwnedIndices(indices); return Teuchos::rcp(new MapType(Teuchos::OrdinalTraits::invalid(),indices,0,comm_)); } // build the ghosted map template Teuchos::RCP > BlockedTpetraLinearObjFactory:: buildTpetraGhostedMap(int i) const { std::vector indices; // get the global indices getGlobalIndexer(i)->getOwnedAndGhostedIndices(indices); return Teuchos::rcp(new MapType(Teuchos::OrdinalTraits::invalid(),indices,0,comm_)); } // get the graph of the crs matrix template Teuchos::RCP > BlockedTpetraLinearObjFactory:: buildTpetraGraph(int i,int j) const { using Teuchos::RCP; using Teuchos::rcp; // build the map and allocate the space for the graph and // grab the ghosted graph RCP map_i = getMap(i); RCP map_j = getMap(j); RCP graph = rcp(new CrsGraphType(map_i,0)); RCP oGraph = getGhostedGraph(i,j); // perform the communication to finish building graph RCP exporter = getGhostedExport(i); graph->doExport( *oGraph, *exporter, Tpetra::INSERT ); graph->fillComplete(map_j,map_i); return graph; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: buildTpetraGhostedGraph(int i,int j) const { using Teuchos::RCP; using Teuchos::rcp; // build the map and allocate the space for the graph and // grab the ghosted graph RCP map_i = getGhostedMap(i); RCP map_j = getGhostedMap(j); std::vector elementBlockIds; Teuchos::RCP rowProvider, colProvider; rowProvider = getGlobalIndexer(i); colProvider = getGlobalIndexer(j); gidProviders_[0]->getElementBlockIds(elementBlockIds); // each sub provider "should" have the // same element blocks // Count number of entries in each row of graph; needed for graph constructor std::vector nEntriesPerRow(map_i->getLocalNumElements(), 0); std::vector::const_iterator blockItr; for(blockItr=elementBlockIds.begin();blockItr!=elementBlockIds.end();++blockItr) { std::string blockId = *blockItr; // grab elements for this block const std::vector & elements = gidProviders_[0]->getElementBlock(blockId); // each sub provider "should" have the // same elements in each element block // get information about number of indicies std::vector row_gids; std::vector col_gids; // loop over the elemnts for(std::size_t elmt=0;elmtgetElementGIDs(elements[elmt],row_gids); colProvider->getElementGIDs(elements[elmt],col_gids); for(std::size_t row=0;rowgetLocalElement(row_gids[row]); nEntriesPerRow[lid] += col_gids.size(); } } } Teuchos::ArrayView nEntriesPerRowView(nEntriesPerRow); RCP graph = rcp(new CrsGraphType(map_i,map_j, nEntriesPerRowView)); // graph information about the mesh for(blockItr=elementBlockIds.begin();blockItr!=elementBlockIds.end();++blockItr) { std::string blockId = *blockItr; // grab elements for this block const std::vector & elements = gidProviders_[0]->getElementBlock(blockId); // each sub provider "should" have the // same elements in each element block // get information about number of indicies std::vector row_gids; std::vector col_gids; // loop over the elemnts for(std::size_t elmt=0;elmtgetElementGIDs(elements[elmt],row_gids); colProvider->getElementGIDs(elements[elmt],col_gids); for(std::size_t row=0;rowinsertGlobalIndices(row_gids[row],col_gids); } } // finish filling the graph: Make sure the colmap and row maps coincide to // minimize calls to LID lookups graph->fillComplete(getMap(j),getMap(i)); return graph; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getTpetraMatrix(int i,int j) const { Teuchos::RCP map_i = getMap(i); Teuchos::RCP map_j = getMap(j); Teuchos::RCP tGraph = getGraph(i,j); Teuchos::RCP mat = Teuchos::rcp(new CrsMatrixType(tGraph)); mat->fillComplete(map_j,map_i); return mat; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedTpetraMatrix(int i,int j) const { Teuchos::RCP map_i = getGhostedMap(i); Teuchos::RCP map_j = getGhostedMap(j); Teuchos::RCP tGraph = getGhostedGraph(i,j); Teuchos::RCP mat = Teuchos::rcp(new CrsMatrixType(tGraph)); mat->fillComplete(getMap(j),getMap(i)); return mat; } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getTpetraDomainVector(int i) const { Teuchos::RCP tMap = getMap(i); return Teuchos::rcp(new VectorType(tMap)); } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedTpetraDomainVector(int i) const { Teuchos::RCP tMap = getGhostedMap(i); return Teuchos::rcp(new VectorType(tMap)); } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getTpetraRangeVector(int i) const { Teuchos::RCP tMap = getMap(i); return Teuchos::rcp(new VectorType(tMap)); } template Teuchos::RCP > BlockedTpetraLinearObjFactory:: getGhostedTpetraRangeVector(int i) const { Teuchos::RCP tMap = getGhostedMap(i); return Teuchos::rcp(new VectorType(tMap)); } template int BlockedTpetraLinearObjFactory:: getBlockRowCount() const { return gidProviders_.size(); } template int BlockedTpetraLinearObjFactory:: getBlockColCount() const { return gidProviders_.size(); } template void BlockedTpetraLinearObjFactory:: beginFill(LinearObjContainer & loc) const { BTLOC & tloc = Teuchos::dyn_cast(loc); if(tloc.get_A()!=Teuchos::null) tloc.beginFill(); } template void BlockedTpetraLinearObjFactory:: endFill(LinearObjContainer & loc) const { BTLOC & tloc = Teuchos::dyn_cast(loc); if(tloc.get_A()!=Teuchos::null) tloc.endFill(); } } #endif // __Panzer_BlockedTpetraLinearObjFactory_impl_hpp__