11 #ifndef PANZER_GATHER_SOLUTION_BLOCKED_TPETRA_IMPL_HPP
12 #define PANZER_GATHER_SOLUTION_BLOCKED_TPETRA_IMPL_HPP
14 #include "Teuchos_Assert.hpp"
15 #include "Phalanx_DataLayout.hpp"
20 #include "Panzer_TpetraLinearObjFactory.hpp"
25 #include "Teuchos_FancyOStream.hpp"
27 #include "Thyra_SpmdVectorBase.hpp"
28 #include "Thyra_ProductVectorBase.hpp"
30 #include "Tpetra_Map.hpp"
32 template <
typename EvalT,
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
38 const std::vector<std::string>& names =
44 for (std::size_t fd = 0; fd < names.size(); ++fd) {
46 this->addEvaluatedField(field.fieldTag());
49 this->setName(
"Gather Solution");
56 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
61 : globalIndexer_(indexer)
62 , has_tangent_fields_(false)
64 typedef std::vector< std::vector<std::string> > vvstring;
69 const std::vector<std::string> & names = input.
getDofNames();
78 gatherFields_.resize(names.size());
79 for (std::size_t fd = 0; fd < names.size(); ++fd) {
82 this->addEvaluatedField(gatherFields_[fd]);
86 if (tangent_field_names.size()>0) {
87 TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size());
89 has_tangent_fields_ =
true;
90 tangentFields_.resize(gatherFields_.size());
91 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
92 tangentFields_[fd].resize(tangent_field_names[fd].size());
93 for (std::size_t i=0; i<tangent_field_names[fd].size(); ++i) {
94 tangentFields_[fd][i] =
96 this->addDependentField(tangentFields_[fd][i]);
102 std::string firstName =
"<none>";
104 firstName = names[0];
106 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" (Residual)";
111 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
118 const Workset & workset_0 = (*d.worksets_)[0];
119 const std::string blockId = this->wda(workset_0).
block_id;
121 fieldIds_.resize(gatherFields_.size());
122 fieldOffsets_.resize(gatherFields_.size());
123 fieldGlobalIndexers_.resize(gatherFields_.size());
124 productVectorBlockIndex_.resize(gatherFields_.size());
125 int maxElementBlockGIDCount = -1;
126 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
128 const std::string& fieldName = indexerNames_[fd];
129 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
130 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
131 fieldGlobalIndexers_[fd] = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
132 fieldIds_[fd] = fieldGlobalIndexers_[fd]->getFieldNum(fieldName);
134 const std::vector<int>&
offsets = fieldGlobalIndexers_[fd]->getGIDFieldOffsets(blockId,fieldIds_[fd]);
135 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Residual):fieldOffsets",offsets.size());
136 auto hostFieldOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
137 for(std::size_t i=0; i < offsets.size(); ++i)
138 hostFieldOffsets(i) = offsets[i];
139 Kokkos::deep_copy(fieldOffsets_[fd],hostFieldOffsets);
141 maxElementBlockGIDCount = std::max(fieldGlobalIndexers_[fd]->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
147 worksetLIDs_ = PHX::View<LO**>(
"GatherSolution_BlockedTpetra(Residual):worksetLIDs",
148 gatherFields_[0].extent(0),
149 maxElementBlockGIDCount);
151 indexerNames_.clear();
155 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
160 blockedContainer_ = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
164 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
169 using Teuchos::rcp_dynamic_cast;
173 const PHX::View<const int*>& localCellIds = this->wda(workset).cell_local_ids_k;
176 if (useTimeDerivativeSolutionVector_)
177 thyraBlockSolution = rcp_dynamic_cast<ProductVectorBase<ScalarT>>(blockedContainer_->get_dxdt(),
true);
179 thyraBlockSolution = rcp_dynamic_cast<ProductVectorBase<ScalarT>>(blockedContainer_->get_x(),
true);
182 int currentWorksetLIDSubBlock = -1;
183 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
185 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
186 const std::string blockId = this->wda(workset).block_id;
187 const int num_dofs = fieldGlobalIndexers_[fieldIndex]->getElementBlockGIDCount(blockId);
188 fieldGlobalIndexers_[fieldIndex]->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
189 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
192 const auto& tpetraSolution = *((rcp_dynamic_cast<Thyra::TpetraVector<ScalarT,LO,GO,NodeT>>(thyraBlockSolution->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
193 const auto& kokkosSolution = tpetraSolution.getLocalViewDevice(Tpetra::Access::ReadOnly);
196 const auto& fieldOffsets = fieldOffsets_[fieldIndex];
197 const auto& worksetLIDs = worksetLIDs_;
198 const auto& fieldValues = gatherFields_[fieldIndex];
200 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
201 for(
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
202 const int lid = worksetLIDs(cell,fieldOffsets(basis));
203 fieldValues(cell,basis) = kokkosSolution(lid,0);
214 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
219 : globalIndexer_(indexer)
220 , has_tangent_fields_(false)
222 typedef std::vector< std::vector<std::string> > vvstring;
227 const std::vector<std::string> & names = input.
getDofNames();
236 gatherFields_.resize(names.size());
237 for (std::size_t fd = 0; fd < names.size(); ++fd) {
240 this->addEvaluatedField(gatherFields_[fd]);
244 if (tangent_field_names.size()>0) {
245 TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size());
247 has_tangent_fields_ =
true;
248 tangentFields_.resize(gatherFields_.size());
249 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
250 tangentFields_[fd].resize(tangent_field_names[fd].size());
251 for (std::size_t i=0; i<tangent_field_names[fd].size(); ++i) {
252 tangentFields_[fd][i] =
254 this->addDependentField(tangentFields_[fd][i]);
260 std::string firstName =
"<none>";
262 firstName = names[0];
264 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" (Tangent)";
269 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
276 const Workset & workset_0 = (*d.worksets_)[0];
277 const std::string blockId = this->wda(workset_0).
block_id;
279 fieldIds_.resize(gatherFields_.size());
280 fieldOffsets_.resize(gatherFields_.size());
281 productVectorBlockIndex_.resize(gatherFields_.size());
282 int maxElementBlockGIDCount = -1;
283 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
285 const std::string fieldName = indexerNames_[fd];
286 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
287 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
288 const auto& subGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
289 fieldIds_[fd] = subGlobalIndexer->getFieldNum(fieldName);
291 const std::vector<int>&
offsets = subGlobalIndexer->getGIDFieldOffsets(blockId,fieldIds_[fd]);
292 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Tangent):fieldOffsets",offsets.size());
293 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
294 for (std::size_t i=0; i < offsets.size(); ++i)
295 hostOffsets(i) = offsets[i];
296 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
297 maxElementBlockGIDCount = std::max(subGlobalIndexer->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
303 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Tangent):worksetLIDs",
304 gatherFields_[0].extent(0),
305 maxElementBlockGIDCount);
308 if (has_tangent_fields_) {
310 size_t inner_vector_max_size = 0;
311 for (std::size_t fd = 0; fd < tangentFields_.size(); ++fd)
312 inner_vector_max_size = std::max(inner_vector_max_size,tangentFields_[fd].size());
313 tangentFieldsVoV_.initialize(
"GatherSolution_BlockedTpetra<Tangent>::tangentFieldsVoV_",gatherFields_.size(),inner_vector_max_size);
315 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
316 for (std::size_t i=0; i<tangentFields_[fd].size(); ++i) {
317 tangentFieldsVoV_.addView(tangentFields_[fd][i].get_static_view(),fd,i);
321 tangentFieldsVoV_.syncHostToDevice();
324 indexerNames_.clear();
328 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
333 blockedContainer_ = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
337 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
343 using Teuchos::ptrFromRef;
344 using Teuchos::rcp_dynamic_cast;
347 using Thyra::SpmdVectorBase;
354 const PHX::View<const int*> & localCellIds = this->wda(workset).getLocalCellIDs();
357 if (useTimeDerivativeSolutionVector_)
358 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_dxdt());
360 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x());
363 int currentWorksetLIDSubBlock = -1;
364 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
366 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
367 const auto& blockIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]];
368 const std::string blockId = this->wda(workset).block_id;
369 const int num_dofs = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]]->getElementBlockGIDCount(blockId);
370 blockIndexer->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
371 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
374 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
375 const auto& subblockSolution = *((rcp_dynamic_cast<Thyra::TpetraVector<RealT,LO,GO,NodeT>>(blockedSolution->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
376 const auto kokkosSolution = subblockSolution.getLocalViewDevice(Tpetra::Access::ReadOnly);
379 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
380 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
381 const PHX::View<ScalarT**> fieldValues = gatherFields_[fieldIndex].get_static_view();
383 if (has_tangent_fields_) {
384 const int numTangents = tangentFields_[fieldIndex].size();
385 const auto tangentFieldsDevice = tangentFieldsVoV_.getViewDevice();
386 const auto kokkosTangents = Kokkos::subview(tangentFieldsDevice,fieldIndex,Kokkos::ALL());
387 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
388 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
389 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
390 fieldValues(cell,basis).zero();
391 fieldValues(cell,basis).val() = kokkosSolution(rowLID,0);
392 for (
int i_tangent=0; i_tangent<numTangents; ++i_tangent)
393 fieldValues(cell,basis).fastAccessDx(i_tangent) = kokkosTangents(i_tangent)(cell,basis);
397 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
398 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
399 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
400 fieldValues(cell,basis).zero();
401 fieldValues(cell,basis) = kokkosSolution(rowLID,0);
413 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
418 : globalIndexer_(indexer)
423 const std::vector<std::string> & names = input.
getDofNames();
432 gatherFields_.resize(names.size());
433 for (std::size_t fd = 0; fd < names.size(); ++fd) {
435 gatherFields_[fd] = f;
436 this->addEvaluatedField(gatherFields_[fd]);
440 std::string firstName =
"<none>";
442 firstName = names[0];
445 if(disableSensitivities_) {
446 std::string n =
"GatherSolution (BlockedTpetra, No Sensitivities): "+firstName+
" (Jacobian)";
450 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" ("+PHX::print<EvalT>()+
") ";
456 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
463 const Workset & workset_0 = (*d.worksets_)[0];
464 const std::string blockId = this->wda(workset_0).
block_id;
466 fieldIds_.resize(gatherFields_.size());
467 fieldOffsets_.resize(gatherFields_.size());
468 productVectorBlockIndex_.resize(gatherFields_.size());
469 int maxElementBlockGIDCount = -1;
470 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
472 const std::string fieldName = indexerNames_[fd];
473 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
474 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
475 const auto& subGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
476 fieldIds_[fd] = subGlobalIndexer->getFieldNum(fieldName);
478 const std::vector<int>&
offsets = subGlobalIndexer->getGIDFieldOffsets(blockId,fieldIds_[fd]);
479 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Jacobian):fieldOffsets",offsets.size());
480 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
481 for (std::size_t i=0; i < offsets.size(); ++i)
482 hostOffsets(i) = offsets[i];
483 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
484 maxElementBlockGIDCount = std::max(subGlobalIndexer->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
490 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Residual):worksetLIDs",
491 gatherFields_[0].extent(0),
492 maxElementBlockGIDCount);
495 const auto& blockGlobalIndexers = globalIndexer_->getFieldDOFManagers();
496 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
497 blockOffsets_ = PHX::View<LO*>(
"GatherSolution_BlockedTpetra(Jacobian):blockOffsets_",
499 const auto hostBlockOffsets = Kokkos::create_mirror_view(blockOffsets_);
500 for (
int blk=0;blk<numBlocks;++blk) {
501 int blockOffset = globalIndexer_->getBlockGIDOffset(blockId,blk);
502 hostBlockOffsets(blk) = blockOffset;
504 hostBlockOffsets(numBlocks) = hostBlockOffsets(numBlocks-1) + blockGlobalIndexers[blockGlobalIndexers.size()-1]->getElementBlockGIDCount(blockId);
505 Kokkos::deep_copy(blockOffsets_,hostBlockOffsets);
507 indexerNames_.clear();
510 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
515 blockedContainer_ = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
519 template <
typename TRAITS,
typename S,
typename LO,
typename GO,
typename NodeT>
524 using Teuchos::rcp_dynamic_cast;
528 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
532 if (useTimeDerivativeSolutionVector_) {
533 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_dxdt());
534 seedValue = workset.alpha;
537 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x());
538 seedValue = workset.beta;
544 if(disableSensitivities_)
548 int currentWorksetLIDSubBlock = -1;
549 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
551 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
552 const auto& blockIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]];
553 const std::string blockId = this->wda(workset).block_id;
554 const int num_dofs = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]]->getElementBlockGIDCount(blockId);
555 blockIndexer->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
556 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
559 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
560 const auto& subblockSolution = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(blockedSolution->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
561 const auto kokkosSolution = subblockSolution.getLocalViewDevice(Tpetra::Access::ReadOnly);
564 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
565 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
566 const PHX::View<ScalarT**> fieldValues = gatherFields_[fieldIndex].get_static_view();
567 const PHX::View<const LO*> blockOffsets = blockOffsets_;
568 auto blockOffsets_h = Kokkos::create_mirror_view(blockOffsets);
569 Kokkos::deep_copy(blockOffsets_h, blockOffsets);
570 const int blockStart = blockOffsets_h(blockRowIndex);
572 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
573 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
574 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
575 fieldValues(cell,basis).zero();
576 fieldValues(cell,basis).val() = kokkosSolution(rowLID,0);
577 fieldValues(cell,basis).fastAccessDx(blockStart+fieldOffsets(basis)) = seedValue;
GatherSolution_BlockedTpetra(const Teuchos::RCP< const BlockedDOFManager > &indexer)
void postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager< TRAITS > &vm)
basic_FancyOStream & setShowProcRank(const bool showProcRank)
T & get(const std::string &name, T def_value)
PHX::View< const int * > offsets
TRAITS::RealType RealType
void evaluateFields(typename TRAITS::EvalData d)
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual void preEvaluate(typename Traits::PreEvalData d)=0
std::string block_id
DEPRECATED - use: getElementBlock()
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we'll contribute, or in which we'll store, the result of computing this integral...
#define TEUCHOS_ASSERT(assertion_test)
Teuchos::RCP< PHX::DataLayout > functional
<Cell,Basis> or <Cell,Basis>