// @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_GATHER_SOLUTION_TPETRA_IMPL_HPP #define PANZER_GATHER_SOLUTION_TPETRA_IMPL_HPP #include "Teuchos_Assert.hpp" #include "Phalanx_DataLayout.hpp" #include "Panzer_GlobalIndexer.hpp" #include "Panzer_PureBasis.hpp" #include "Panzer_TpetraLinearObjContainer.hpp" #include "Panzer_LOCPair_GlobalEvaluationData.hpp" #include "Panzer_TpetraVector_ReadOnly_GlobalEvaluationData.hpp" #include "Panzer_GatherSolution_Input.hpp" #include "Panzer_GlobalEvaluationDataContainer.hpp" #include "Panzer_DOFManager.hpp" #include "Teuchos_FancyOStream.hpp" #include "Tpetra_Vector.hpp" #include "Tpetra_Map.hpp" // ********************************************************************** // Specialization: Residual // ********************************************************************** template panzer::GatherSolution_Tpetra:: GatherSolution_Tpetra( const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) , has_tangent_fields_(false) { typedef std::vector< std::vector > vvstring; GatherSolution_Input input; input.setParameterList(p); const std::vector & names = input.getDofNames(); Teuchos::RCP basis = input.getBasis(); const vvstring & tangent_field_names = input.getTangentNames(); indexerNames_ = input.getIndexerNames(); useTimeDerivativeSolutionVector_ = input.useTimeDerivativeSolutionVector(); globalDataKey_ = input.getGlobalDataKey(); // allocate fields gatherFields_.resize(names.size()); for (std::size_t fd = 0; fd < names.size(); ++fd) { gatherFields_[fd] = PHX::MDField(names[fd],basis->functional); this->addEvaluatedField(gatherFields_[fd]); } // Setup dependent tangent fields if requested if (tangent_field_names.size()>0) { TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size()); has_tangent_fields_ = true; tangentFields_.resize(gatherFields_.size()); for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) { tangentFields_[fd].resize(tangent_field_names[fd].size()); for (std::size_t i=0; i(tangent_field_names[fd][i],basis->functional); this->addDependentField(tangentFields_[fd][i]); } } } // figure out what the first active name is std::string firstName = ""; if(names.size()>0) firstName = names[0]; std::string n = "GatherSolution (Tpetra): "+firstName+" (Residual)"; this->setName(n); } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager& /* fm */) { TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size()); fieldIds_.resize(gatherFields_.size()); const Workset & workset_0 = (*d.worksets_)[0]; std::string blockId = this->wda(workset_0).block_id; scratch_offsets_.resize(gatherFields_.size()); for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) { const std::string& fieldName = indexerNames_[fd]; fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName); int fieldNum = fieldIds_[fd]; const std::vector & offsets = globalIndexer_->getGIDFieldOffsets(blockId,fieldNum); scratch_offsets_[fd] = PHX::View("offsets",offsets.size()); Kokkos::deep_copy(scratch_offsets_[fd], Kokkos::View(offsets.data(), offsets.size())); } scratch_lids_ = PHX::View("lids",gatherFields_[0].extent(0), globalIndexer_->getElementBlockGIDCount(blockId)); indexerNames_.clear(); // Don't need this anymore } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: preEvaluate(typename TRAITS::PreEvalData d) { typedef TpetraLinearObjContainer LOC; // extract linear object container tpetraContainer_ = Teuchos::rcp_dynamic_cast(d.gedc->getDataObject(globalDataKey_)); if(tpetraContainer_==Teuchos::null) { // extract linear object container Teuchos::RCP loc = Teuchos::rcp_dynamic_cast(d.gedc->getDataObject(globalDataKey_),true)->getGhostedLOC(); tpetraContainer_ = Teuchos::rcp_dynamic_cast(loc); } } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: evaluateFields(typename TRAITS::EvalData workset) { typedef TpetraLinearObjContainer LOC; // for convenience pull out some objects from workset std::string blockId = this->wda(workset).block_id; const std::vector & localCellIds = this->wda(workset).cell_local_ids; Teuchos::RCP x; if (useTimeDerivativeSolutionVector_) x = tpetraContainer_->get_dxdt(); else x = tpetraContainer_->get_x(); auto x_data = x->getLocalViewDevice(Tpetra::Access::ReadOnly); globalIndexer_->getElementLIDs(this->wda(workset).cell_local_ids_k,scratch_lids_); // NOTE: A reordering of these loops will likely improve performance // The "getGIDFieldOffsets may be expensive. However the // "getElementGIDs" can be cheaper. However the lookup for LIDs // may be more expensive! // gather operation for each cell in workset auto lids = scratch_lids_; for (std::size_t fieldIndex=0; fieldIndex panzer::GatherSolution_Tpetra:: GatherSolution_Tpetra( const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) , has_tangent_fields_(false) { typedef std::vector< std::vector > vvstring; GatherSolution_Input input; input.setParameterList(p); const std::vector & names = input.getDofNames(); Teuchos::RCP basis = input.getBasis(); const vvstring & tangent_field_names = input.getTangentNames(); indexerNames_ = input.getIndexerNames(); useTimeDerivativeSolutionVector_ = input.useTimeDerivativeSolutionVector(); globalDataKey_ = input.getGlobalDataKey(); // allocate fields gatherFields_.resize(names.size()); for (std::size_t fd = 0; fd < names.size(); ++fd) { gatherFields_[fd] = PHX::MDField(names[fd],basis->functional); this->addEvaluatedField(gatherFields_[fd]); // Don't allow for sharing so that we can avoid zeroing out the // off-diagonal values of the FAD derivative array. this->addUnsharedField(gatherFields_[fd].fieldTag().clone()); } // Setup dependent tangent fields if requested if (tangent_field_names.size()>0) { TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size()); has_tangent_fields_ = true; tangentFields_.resize(gatherFields_.size()); for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) { tangentFields_[fd].resize(tangent_field_names[fd].size()); for (std::size_t i=0; i(tangent_field_names[fd][i],basis->functional); this->addDependentField(tangentFields_[fd][i]); } } } // figure out what the first active name is std::string firstName = ""; if(names.size()>0) firstName = names[0]; std::string n = "GatherSolution (Tpetra): "+firstName+" (Tangent)"; this->setName(n); } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData /* d */, PHX::FieldManager& /* fm */) { TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size()); fieldIds_.resize(gatherFields_.size()); // Original implementation of tangentFields used vector of // vectors. The inner vectors could have different sizes for each // [fd]. With UVM removal, we need to use a rank 2 view of views. So // we need an extra vector to carry around the inner vector sizes. tangentInnerVectorSizes_ = PHX::View("tangentInnerVectorSizes_",gatherFields_.size()); auto tangentInnerVectorSizes_host = Kokkos::create_mirror_view(tangentInnerVectorSizes_); size_t inner_vector_max_size = 0; for (std::size_t fd = 0; fd < tangentFields_.size(); ++fd) { inner_vector_max_size = std::max(inner_vector_max_size,tangentFields_[fd].size()); tangentInnerVectorSizes_host(fd) = tangentFields_[fd].size(); } Kokkos::deep_copy(tangentInnerVectorSizes_,tangentInnerVectorSizes_host); gatherFieldsVoV_.initialize("GatherSolution_Tpetra::gatherFieldsVoV_",gatherFields_.size()); tangentFieldsVoV_.initialize("GatherSolution_Tpetra::tangentFieldsVoV_",gatherFields_.size(),inner_vector_max_size); for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) { const std::string& fieldName = indexerNames_[fd]; fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName); gatherFieldsVoV_.addView(gatherFields_[fd].get_static_view(),fd); if (has_tangent_fields_) { for (std::size_t i=0; i void panzer::GatherSolution_Tpetra:: preEvaluate(typename TRAITS::PreEvalData d) { typedef TpetraLinearObjContainer LOC; // extract linear object container tpetraContainer_ = Teuchos::rcp_dynamic_cast(d.gedc->getDataObject(globalDataKey_)); if(tpetraContainer_==Teuchos::null) { // extract linear object container Teuchos::RCP loc = Teuchos::rcp_dynamic_cast(d.gedc->getDataObject(globalDataKey_),true)->getGhostedLOC(); tpetraContainer_ = Teuchos::rcp_dynamic_cast(loc); } } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: evaluateFields(typename TRAITS::EvalData workset) { typedef TpetraLinearObjContainer LOC; // for convenience pull out some objects from workset std::string blockId = this->wda(workset).block_id; Teuchos::RCP x; if (useTimeDerivativeSolutionVector_) x = tpetraContainer_->get_dxdt(); else x = tpetraContainer_->get_x(); typedef typename PHX::MDField::array_type::reference_type reference_type; auto cellLocalIdsKokkos = this->wda(workset).getLocalCellIDs(); auto lids = globalIndexer_->getLIDs(); auto gidFieldOffsetsVoV = Teuchos::rcp_dynamic_cast(globalIndexer_,true)->getGIDFieldOffsetsKokkos(blockId,fieldIds_); auto gidFieldOffsets = gidFieldOffsetsVoV.getViewDevice(); auto gatherFieldsDevice = gatherFieldsVoV_.getViewDevice(); auto x_view = x->getLocalViewDevice(Tpetra::Access::ReadOnly); auto tangentInnerVectorSizes = this->tangentInnerVectorSizes_; if (has_tangent_fields_) { auto tangentFieldsDevice = tangentFieldsVoV_.getViewDevice(); Kokkos::parallel_for("GatherSolutionTpetra",cellLocalIdsKokkos.extent(0),KOKKOS_LAMBDA(const int worksetCellIndex) { for (size_t fieldIndex = 0; fieldIndex < gidFieldOffsets.extent(0); ++fieldIndex) { for(size_t basis=0;basis",cellLocalIdsKokkos.extent(0),KOKKOS_LAMBDA(const int worksetCellIndex) { for (size_t fieldIndex = 0; fieldIndex < gidFieldOffsets.extent(0); ++fieldIndex) { for(size_t basis=0;basis panzer::GatherSolution_Tpetra:: GatherSolution_Tpetra( const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) { // typedef std::vector< std::vector > vvstring; GatherSolution_Input input; input.setParameterList(p); const std::vector & names = input.getDofNames(); Teuchos::RCP basis = input.getBasis(); //const vvstring & tangent_field_names = input.getTangentNames(); indexerNames_ = input.getIndexerNames(); useTimeDerivativeSolutionVector_ = input.useTimeDerivativeSolutionVector(); globalDataKey_ = input.getGlobalDataKey(); gatherSeedIndex_ = input.getGatherSeedIndex(); sensitivitiesName_ = input.getSensitivitiesName(); disableSensitivities_ = !input.firstSensitivitiesAvailable(); gatherFields_.resize(names.size()); scratch_offsets_.resize(names.size()); for (std::size_t fd = 0; fd < names.size(); ++fd) { PHX::MDField f(names[fd],basis->functional); gatherFields_[fd] = f; this->addEvaluatedField(gatherFields_[fd]); // Don't allow for sharing so that we can avoid zeroing out the // off-diagonal values of the FAD derivative array. this->addUnsharedField(gatherFields_[fd].fieldTag().clone()); } // figure out what the first active name is std::string firstName = ""; if(names.size()>0) firstName = names[0]; // print out convenience if(disableSensitivities_) { std::string n = "GatherSolution (Tpetra, No Sensitivities): "+firstName+" (Jacobian)"; this->setName(n); } else { std::string n = "GatherSolution (Tpetra): "+firstName+" (Jacobian) "; this->setName(n); } } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager& /* fm */) { TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size()); fieldIds_.resize(gatherFields_.size()); const Workset & workset_0 = (*d.worksets_)[0]; std::string blockId = this->wda(workset_0).block_id; for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) { // get field ID from DOF manager const std::string& fieldName = indexerNames_[fd]; fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName); int fieldNum = fieldIds_[fd]; const std::vector & offsets = globalIndexer_->getGIDFieldOffsets(blockId,fieldNum); scratch_offsets_[fd] = PHX::View("offsets",offsets.size()); Kokkos::deep_copy(scratch_offsets_[fd], Kokkos::View(offsets.data(), offsets.size())); } scratch_lids_ = PHX::View("lids",gatherFields_[0].extent(0), globalIndexer_->getElementBlockGIDCount(blockId)); indexerNames_.clear(); // Don't need this anymore } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: preEvaluate(typename TRAITS::PreEvalData d) { using Teuchos::RCP; using Teuchos::rcp; using Teuchos::rcp_dynamic_cast; typedef TpetraLinearObjContainer LOC; typedef TpetraVector_ReadOnly_GlobalEvaluationData RO_GED; // manage sensitivities //////////////////////////////////////////////////////////// if(!disableSensitivities_) { if(d.first_sensitivities_name==sensitivitiesName_) applySensitivities_ = true; else applySensitivities_ = false; } else applySensitivities_ = false; //////////////////////////////////////////////////////////// RCP ged; // first try refactored ReadOnly container std::string post = useTimeDerivativeSolutionVector_ ? " - Xdot" : " - X"; if(d.gedc->containsDataObject(globalDataKey_+post)) { ged = d.gedc->getDataObject(globalDataKey_+post); RCP ro_ged = rcp_dynamic_cast(ged,true); x_vector = ro_ged->getGhostedVector_Tpetra(); return; } ged = d.gedc->getDataObject(globalDataKey_); // try to extract linear object container { RCP tpetraContainer = rcp_dynamic_cast(ged); RCP loc_pair = rcp_dynamic_cast(ged); if(loc_pair!=Teuchos::null) { Teuchos::RCP loc = loc_pair->getGhostedLOC(); // extract linear object container tpetraContainer = rcp_dynamic_cast(loc); } if(tpetraContainer!=Teuchos::null) { if (useTimeDerivativeSolutionVector_) x_vector = tpetraContainer->get_dxdt(); else x_vector = tpetraContainer->get_x(); return; // epetraContainer was found } } // try to extract an EpetraVector_ReadOnly object (this is the last resort!, it throws if not found) { RCP ro_ged = rcp_dynamic_cast(ged,true); x_vector = ro_ged->getGhostedVector_Tpetra(); } } // ********************************************************************** template void panzer::GatherSolution_Tpetra:: evaluateFields(typename TRAITS::EvalData workset) { // for convenience pull out some objects from workset std::string blockId = this->wda(workset).block_id; double seed_value = 0.0; if (useTimeDerivativeSolutionVector_) { seed_value = workset.alpha; } else if (gatherSeedIndex_<0) { seed_value = workset.beta; } else if(!useTimeDerivativeSolutionVector_) { seed_value = workset.gather_seeds[gatherSeedIndex_]; } else { TEUCHOS_ASSERT(false); } // turn off sensitivies: this may be faster if we don't expand the term // but I suspect not because anywhere it is used the full complement of // sensitivies will be needed anyway. if(!applySensitivities_) seed_value = 0.0; // Interface worksets handle DOFs from two element blocks. The // derivative offset for the other element block must be shifted by // the derivative side of my element block. functor_data.dos = 0; if (this->wda.getDetailsIndex() == 1) { // Get the DOF count for my element block. functor_data.dos = globalIndexer_->getElementBlockGIDCount(workset.details(0).block_id); } // switch to a faster assembly bool use_seed = true; if(seed_value==0.0) use_seed = false; globalIndexer_->getElementLIDs(this->wda(workset).cell_local_ids_k,scratch_lids_); // now setup the fuctor_data, and run the parallel_for loop ////////////////////////////////////////////////////////////////////////////////// functor_data.x_data = x_vector->getLocalViewDevice(Tpetra::Access::ReadOnly); functor_data.seed_value = seed_value; functor_data.lids = scratch_lids_; // loop over the fields to be gathered for(std::size_t fieldIndex=0; fieldIndex(0,workset.num_cells),*this); } functor_data.x_data = Kokkos::View(); } // ********************************************************************** template KOKKOS_INLINE_FUNCTION void panzer::GatherSolution_Tpetra:: operator()(const int worksetCellIndex) const { // loop over basis functions and fill the fields for(std::size_t basis=0;basis KOKKOS_INLINE_FUNCTION void panzer::GatherSolution_Tpetra:: operator()(const NoSeed,const int worksetCellIndex) const { // loop over basis functions and fill the fields for(std::size_t basis=0;basis