// @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_SCATTER_RESIDUAL_TPETRA_IMPL_HPP #define PANZER_SCATTER_RESIDUAL_TPETRA_IMPL_HPP #include "Teuchos_RCP.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_ParameterList_GlobalEvaluationData.hpp" #include "Panzer_GlobalEvaluationDataContainer.hpp" #include "Phalanx_DataLayout_MDALayout.hpp" #include "Teuchos_FancyOStream.hpp" #include "Tpetra_Vector.hpp" #include "Tpetra_Map.hpp" #include "Tpetra_CrsMatrix.hpp" // ********************************************************************** // Specialization: Residual // ********************************************************************** template panzer::ScatterResidual_Tpetra:: ScatterResidual_Tpetra(const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) , globalDataKey_("Residual Scatter Container") { std::string scatterName = p.get("Scatter Name"); scatterHolder_ = Teuchos::rcp(new PHX::Tag(scatterName,Teuchos::rcp(new PHX::MDALayout(0)))); // get names to be evaluated const std::vector& names = *(p.get< Teuchos::RCP< std::vector > >("Dependent Names")); // grab map from evaluated names to field names fieldMap_ = p.get< Teuchos::RCP< std::map > >("Dependent Map"); Teuchos::RCP dl = p.get< Teuchos::RCP >("Basis")->functional; // build the vector of fields that this is dependent on scatterFields_.resize(names.size()); scratch_offsets_.resize(names.size()); for (std::size_t eq = 0; eq < names.size(); ++eq) { scatterFields_[eq] = PHX::MDField(names[eq],dl); // tell the field manager that we depend on this field this->addDependentField(scatterFields_[eq]); } // this is what this evaluator provides this->addEvaluatedField(*scatterHolder_); if (p.isType("Global Data Key")) globalDataKey_ = p.get("Global Data Key"); this->setName(scatterName+" Scatter Residual"); } // ********************************************************************** template void panzer::ScatterResidual_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager& /* fm */) { fieldIds_.resize(scatterFields_.size()); const Workset & workset_0 = (*d.worksets_)[0]; std::string blockId = this->wda(workset_0).block_id; // load required field numbers for fast use for(std::size_t fd=0;fdfind(scatterFields_[fd].fieldTag().name())->second; fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName); const std::vector & offsets = globalIndexer_->getGIDFieldOffsets(blockId,fieldIds_[fd]); 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",scatterFields_[0].extent(0), globalIndexer_->getElementBlockGIDCount(blockId)); } // ********************************************************************** template void panzer::ScatterResidual_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); } } // ********************************************************************** // Specialization: Tangent // ********************************************************************** template panzer::ScatterResidual_Tpetra:: ScatterResidual_Tpetra(const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) , globalDataKey_("Residual Scatter Container") { std::string scatterName = p.get("Scatter Name"); scatterHolder_ = Teuchos::rcp(new PHX::Tag(scatterName,Teuchos::rcp(new PHX::MDALayout(0)))); // get names to be evaluated const std::vector& names = *(p.get< Teuchos::RCP< std::vector > >("Dependent Names")); // grab map from evaluated names to field names fieldMap_ = p.get< Teuchos::RCP< std::map > >("Dependent Map"); Teuchos::RCP dl = p.get< Teuchos::RCP >("Basis")->functional; // build the vector of fields that this is dependent on scatterFields_.resize(names.size()); scratch_offsets_.resize(names.size()); for (std::size_t eq = 0; eq < names.size(); ++eq) { scatterFields_[eq] = PHX::MDField(names[eq],dl); // tell the field manager that we depend on this field this->addDependentField(scatterFields_[eq]); } // this is what this evaluator provides this->addEvaluatedField(*scatterHolder_); if (p.isType("Global Data Key")) globalDataKey_ = p.get("Global Data Key"); this->setName(scatterName+" Scatter Tangent"); } // ********************************************************************** template void panzer::ScatterResidual_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager& /* fm */) { fieldIds_.resize(scatterFields_.size()); const Workset & workset_0 = (*d.worksets_)[0]; std::string blockId = this->wda(workset_0).block_id; // load required field numbers for fast use for(std::size_t fd=0;fdfind(scatterFields_[fd].fieldTag().name())->second; fieldIds_[fd] = globalIndexer_->getFieldNum(fieldName); const std::vector & offsets = globalIndexer_->getGIDFieldOffsets(blockId,fieldIds_[fd]); 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",scatterFields_[0].extent(0), globalIndexer_->getElementBlockGIDCount(blockId)); } // ********************************************************************** template void panzer::ScatterResidual_Tpetra:: preEvaluate(typename TRAITS::PreEvalData d) { using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; typedef TpetraLinearObjContainer LOC; // this is the list of parameters and their names that this scatter has to account for std::vector activeParameters = rcp_dynamic_cast(d.gedc->getDataObject("PARAMETER_NAMES"))->getActiveParameters(); dfdpFieldsVoV_.initialize("ScatterResidual_Tpetra::dfdpFieldsVoV_",activeParameters.size()); for(std::size_t i=0;i vec = rcp_dynamic_cast(d.gedc->getDataObject(activeParameters[i]),true)->get_f(); auto dfdp_view = vec->getLocalViewDevice(Tpetra::Access::ReadWrite); dfdpFieldsVoV_.addView(dfdp_view,i); } dfdpFieldsVoV_.syncHostToDevice(); // 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); } } // ********************************************************************** // Specialization: Jacobian // ********************************************************************** template panzer::ScatterResidual_Tpetra:: ScatterResidual_Tpetra(const Teuchos::RCP & indexer, const Teuchos::ParameterList& p) : globalIndexer_(indexer) , globalDataKey_("Residual Scatter Container") , my_derivative_size_(0) , other_derivative_size_(0) { std::string scatterName = p.get("Scatter Name"); scatterHolder_ = Teuchos::rcp(new PHX::Tag(scatterName,Teuchos::rcp(new PHX::MDALayout(0)))); // get names to be evaluated const std::vector& names = *(p.get< Teuchos::RCP< std::vector > >("Dependent Names")); // grab map from evaluated names to field names fieldMap_ = p.get< Teuchos::RCP< std::map > >("Dependent Map"); Teuchos::RCP dl = p.get< Teuchos::RCP >("Basis")->functional; // build the vector of fields that this is dependent on scatterFields_.resize(names.size()); scratch_offsets_.resize(names.size()); for (std::size_t eq = 0; eq < names.size(); ++eq) { scatterFields_[eq] = PHX::MDField(names[eq],dl); // tell the field manager that we depend on this field this->addDependentField(scatterFields_[eq]); } // this is what this evaluator provides this->addEvaluatedField(*scatterHolder_); if (p.isType("Global Data Key")) globalDataKey_ = p.get("Global Data Key"); this->setName(scatterName+" Scatter Residual (Jacobian)"); } // ********************************************************************** template void panzer::ScatterResidual_Tpetra:: postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager& /* fm */) { fieldIds_.resize(scatterFields_.size()); const Workset & workset_0 = (*d.worksets_)[0]; std::string blockId = this->wda(workset_0).block_id; // load required field numbers for fast use for(std::size_t fd=0;fdfind(scatterFields_[fd].fieldTag().name())->second; 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())); } my_derivative_size_ = globalIndexer_->getElementBlockGIDCount(blockId); if (Teuchos::nonnull(workset_0.other)) { auto otherBlockId = workset_0.other->block_id; other_derivative_size_ = globalIndexer_->getElementBlockGIDCount(otherBlockId); } scratch_lids_ = Kokkos::View( "lids", scatterFields_[0].extent(0), my_derivative_size_ + other_derivative_size_ ); scratch_vals_ = Kokkos::View::type**, Kokkos::LayoutRight, PHX::Device>( "vals", scatterFields_[0].extent(0), my_derivative_size_ + other_derivative_size_ ); } // ********************************************************************** template void panzer::ScatterResidual_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); } } // ********************************************************************** namespace panzer { namespace { template class ScatterResidual_Jacobian_Functor { public: typedef typename PHX::Device execution_space; typedef PHX::MDField FieldType; bool fillResidual; Kokkos::View r_data; LocalMatrixT jac; // Kokkos jacobian type Kokkos::View lids; // local indices for unknowns. Kokkos::View::type**, Kokkos::LayoutRight, PHX::Device> vals; PHX::View offsets; // how to get a particular field FieldType field; KOKKOS_INLINE_FUNCTION void operator()(const unsigned int cell) const { int numIds = lids.extent(1); // loop over the basis functions (currently they are nodes) for(std::size_t basis=0; basis < offsets.extent(0); basis++) { typename FieldType::array_type::reference_type scatterField = field(cell,basis); int offset = offsets(basis); LO lid = lids(cell,offset); // Sum residual if(fillResidual) Kokkos::atomic_add(&r_data(lid,0), scatterField.val()); // loop over the sensitivity indices: all DOFs on a cell for(int sensIndex=0;sensIndex class ScatterResidual_Residual_Functor { public: typedef typename PHX::Device execution_space; typedef PHX::MDField FieldType; Kokkos::View r_data; PHX::View lids; // local indices for unknowns PHX::View offsets; // how to get a particular field FieldType field; KOKKOS_INLINE_FUNCTION void operator()(const unsigned int cell) const { // loop over the basis functions (currently they are nodes) for(std::size_t basis=0; basis < offsets.extent(0); basis++) { int offset = offsets(basis); LO lid = lids(cell,offset); Kokkos::atomic_add(&r_data(lid,0), field(cell,basis)); } // end basis } }; template class ScatterResidual_Tangent_Functor { public: typedef typename PHX::Device execution_space; typedef PHX::MDField FieldType; bool fillResidual; Kokkos::View r_data; Kokkos::View lids; // local indices for unknowns. PHX::View offsets; // how to get a particular field FieldType field; double num_params; Kokkos::View*> dfdp_fields; // tangent fields KOKKOS_INLINE_FUNCTION void operator()(const unsigned int cell) const { // loop over the basis functions (currently they are nodes) for(std::size_t basis=0; basis < offsets.extent(0); basis++) { typename FieldType::array_type::reference_type scatterField = field(cell,basis); int offset = offsets(basis); LO lid = lids(cell,offset); // Sum residual if(fillResidual) Kokkos::atomic_add(&r_data(lid,0), scatterField.val()); // loop over the tangents for(int i_param=0; i_param void panzer::ScatterResidual_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 r = tpetraContainer_->get_f(); globalIndexer_->getElementLIDs(this->wda(workset).cell_local_ids_k,scratch_lids_); ScatterResidual_Residual_Functor functor; functor.r_data = r->getLocalViewDevice(Tpetra::Access::ReadWrite); functor.lids = scratch_lids_; // for each field, do a parallel for loop for(std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) { functor.offsets = scratch_offsets_[fieldIndex]; functor.field = scatterFields_[fieldIndex]; Kokkos::parallel_for(workset.num_cells,functor); } } // ********************************************************************** template void panzer::ScatterResidual_Tpetra:: evaluateFields(typename TRAITS::EvalData workset) { typedef TpetraLinearObjContainer LOC; typedef typename LOC::CrsMatrixType::local_matrix_device_type LocalMatrixT; // for convenience pull out some objects from workset std::string blockId = this->wda(workset).block_id; Teuchos::RCP r = tpetraContainer_->get_f(); Teuchos::RCP Jac = tpetraContainer_->get_A(); // Cache scratch lids. For interface bc problems the derivative // dimension extent spans two cells. Use subviews to get the self // lids and the other lids. if (Teuchos::nonnull(workset.other)) { auto my_scratch_lids = Kokkos::subview(scratch_lids_,Kokkos::ALL,std::make_pair(0,my_derivative_size_)); globalIndexer_->getElementLIDs(this->wda(workset).cell_local_ids_k,my_scratch_lids); auto other_scratch_lids = Kokkos::subview(scratch_lids_,Kokkos::ALL,std::make_pair(my_derivative_size_,my_derivative_size_ + other_derivative_size_)); globalIndexer_->getElementLIDs(workset.other->cell_local_ids_k,other_scratch_lids); } else { globalIndexer_->getElementLIDs(this->wda(workset).cell_local_ids_k,scratch_lids_); } ScatterResidual_Jacobian_Functor functor; functor.fillResidual = (r!=Teuchos::null); if(functor.fillResidual) functor.r_data = r->getLocalViewDevice(Tpetra::Access::ReadWrite); functor.jac = Jac->getLocalMatrixDevice(); functor.lids = scratch_lids_; functor.vals = scratch_vals_; // for each field, do a parallel for loop for(std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) { functor.offsets = scratch_offsets_[fieldIndex]; functor.field = scatterFields_[fieldIndex]; Kokkos::parallel_for(workset.num_cells,functor); } } // ********************************************************************** template void panzer::ScatterResidual_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 r = tpetraContainer_->get_f(); globalIndexer_->getElementLIDs(this->wda(workset).getLocalCellIDs(),scratch_lids_); ScatterResidual_Tangent_Functor functor; functor.fillResidual = (r!=Teuchos::null); if(functor.fillResidual) functor.r_data = r->getLocalViewDevice(Tpetra::Access::ReadWrite); functor.lids = scratch_lids_; functor.dfdp_fields = dfdpFieldsVoV_.getViewDevice(); // for each field, do a parallel for loop for(std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) { functor.offsets = scratch_offsets_[fieldIndex]; functor.field = scatterFields_[fieldIndex]; functor.num_params = Kokkos::dimension_scalar(scatterFields_[fieldIndex].get_view())-1; Kokkos::parallel_for(workset.num_cells,functor); } } // ********************************************************************** #endif