37 const Teuchos::RCP<const BlockedDOFManager> & indexer,
38 const Teuchos::ParameterList& p)
40 const std::vector<std::string>& names =
41 *(p.get< Teuchos::RCP< std::vector<std::string> > >(
"DOF Names"));
43 Teuchos::RCP<panzer::PureBasis> basis =
44 p.get< Teuchos::RCP<panzer::PureBasis> >(
"Basis");
46 for (std::size_t fd = 0; fd < names.size(); ++fd) {
47 PHX::MDField<ScalarT,Cell,NODE>
field = PHX::MDField<ScalarT,Cell,NODE>(names[fd],basis->functional);
48 this->addEvaluatedField(
field.fieldTag());
51 this->setName(
"Gather Solution");
61 const Teuchos::RCP<const BlockedDOFManager> & indexer,
62 const Teuchos::ParameterList& p)
63 : globalIndexer_(indexer)
64 , has_tangent_fields_(false)
66 typedef std::vector< std::vector<std::string> > vvstring;
71 const std::vector<std::string> & names = input.
getDofNames();
72 Teuchos::RCP<const panzer::PureBasis> basis = input.
getBasis();
80 gatherFields_.resize(names.size());
81 for (std::size_t fd = 0; fd < names.size(); ++fd) {
83 PHX::MDField<ScalarT,Cell,NODE>(names[fd],basis->functional);
84 this->addEvaluatedField(gatherFields_[fd]);
88 if (tangent_field_names.size()>0) {
89 TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size());
91 has_tangent_fields_ =
true;
92 tangentFields_.resize(gatherFields_.size());
93 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
94 tangentFields_[fd].resize(tangent_field_names[fd].size());
95 for (std::size_t i=0; i<tangent_field_names[fd].size(); ++i) {
96 tangentFields_[fd][i] =
97 PHX::MDField<const ScalarT,Cell,NODE>(tangent_field_names[fd][i],basis->functional);
98 this->addDependentField(tangentFields_[fd][i]);
104 std::string firstName =
"<none>";
106 firstName = names[0];
108 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" (Residual)";
118 TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size());
120 const Workset & workset_0 = (*d.worksets_)[0];
121 const std::string blockId = this->wda(workset_0).
block_id;
123 fieldIds_.resize(gatherFields_.size());
124 fieldOffsets_.resize(gatherFields_.size());
125 fieldGlobalIndexers_.resize(gatherFields_.size());
126 productVectorBlockIndex_.resize(gatherFields_.size());
127 int maxElementBlockGIDCount = -1;
128 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
130 const std::string& fieldName = indexerNames_[fd];
131 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
132 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
133 fieldGlobalIndexers_[fd] = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
134 fieldIds_[fd] = fieldGlobalIndexers_[fd]->getFieldNum(fieldName);
136 const std::vector<int>&
offsets = fieldGlobalIndexers_[fd]->getGIDFieldOffsets(blockId,fieldIds_[fd]);
137 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Residual):fieldOffsets",
offsets.size());
138 auto hostFieldOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
139 for(std::size_t i=0; i <
offsets.size(); ++i)
140 hostFieldOffsets(i) =
offsets[i];
141 Kokkos::deep_copy(fieldOffsets_[fd],hostFieldOffsets);
143 maxElementBlockGIDCount = std::max(fieldGlobalIndexers_[fd]->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
149 worksetLIDs_ = PHX::View<LO**>(
"GatherSolution_BlockedTpetra(Residual):worksetLIDs",
150 gatherFields_[0].extent(0),
151 maxElementBlockGIDCount);
153 indexerNames_.clear();
171 using Teuchos::rcp_dynamic_cast;
175 const PHX::View<const int*>& localCellIds = this->wda(workset).cell_local_ids_k;
177 RCP<ProductVectorBase<ScalarT>> thyraBlockSolution;
178 if (useTimeDerivativeSolutionVector_)
179 thyraBlockSolution = rcp_dynamic_cast<ProductVectorBase<ScalarT>>(blockedContainer_->get_dxdt(),
true);
181 thyraBlockSolution = rcp_dynamic_cast<ProductVectorBase<ScalarT>>(blockedContainer_->get_x(),
true);
184 int currentWorksetLIDSubBlock = -1;
185 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
187 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
188 const std::string blockId = this->wda(workset).block_id;
189 const int num_dofs = fieldGlobalIndexers_[fieldIndex]->getElementBlockGIDCount(blockId);
190 fieldGlobalIndexers_[fieldIndex]->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
191 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
194 const auto& tpetraSolution = *((rcp_dynamic_cast<Thyra::TpetraVector<ScalarT,LO,GO,NodeT>>(thyraBlockSolution->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
195 const auto& kokkosSolution = tpetraSolution.getLocalViewDevice(Tpetra::Access::ReadOnly);
198 const auto& fieldOffsets = fieldOffsets_[fieldIndex];
199 const auto& worksetLIDs = worksetLIDs_;
200 const auto& fieldValues = gatherFields_[fieldIndex];
202 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
203 for(
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
204 const int lid = worksetLIDs(cell,fieldOffsets(basis));
205 fieldValues(cell,basis) = kokkosSolution(lid,0);
219 const Teuchos::RCP<const BlockedDOFManager> & indexer,
220 const Teuchos::ParameterList& p)
221 : globalIndexer_(indexer)
222 , has_tangent_fields_(false)
224 typedef std::vector< std::vector<std::string> > vvstring;
229 const std::vector<std::string> & names = input.
getDofNames();
230 Teuchos::RCP<const panzer::PureBasis> basis = input.
getBasis();
238 gatherFields_.resize(names.size());
239 for (std::size_t fd = 0; fd < names.size(); ++fd) {
241 PHX::MDField<ScalarT,Cell,NODE>(names[fd],basis->functional);
242 this->addEvaluatedField(gatherFields_[fd]);
246 if (tangent_field_names.size()>0) {
247 TEUCHOS_ASSERT(gatherFields_.size() == tangent_field_names.size());
249 has_tangent_fields_ =
true;
250 tangentFields_.resize(gatherFields_.size());
251 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
252 tangentFields_[fd].resize(tangent_field_names[fd].size());
253 for (std::size_t i=0; i<tangent_field_names[fd].size(); ++i) {
254 tangentFields_[fd][i] =
255 PHX::MDField<const RealT,Cell,NODE>(tangent_field_names[fd][i],basis->functional);
256 this->addDependentField(tangentFields_[fd][i]);
262 std::string firstName =
"<none>";
264 firstName = names[0];
266 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" (Tangent)";
276 TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size());
278 const Workset & workset_0 = (*d.worksets_)[0];
279 const std::string blockId = this->wda(workset_0).
block_id;
281 fieldIds_.resize(gatherFields_.size());
282 fieldOffsets_.resize(gatherFields_.size());
283 productVectorBlockIndex_.resize(gatherFields_.size());
284 int maxElementBlockGIDCount = -1;
285 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
287 const std::string fieldName = indexerNames_[fd];
288 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
289 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
290 const auto& subGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
291 fieldIds_[fd] = subGlobalIndexer->getFieldNum(fieldName);
293 const std::vector<int>&
offsets = subGlobalIndexer->getGIDFieldOffsets(blockId,fieldIds_[fd]);
294 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Tangent):fieldOffsets",
offsets.size());
295 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
296 for (std::size_t i=0; i <
offsets.size(); ++i)
298 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
299 maxElementBlockGIDCount = std::max(subGlobalIndexer->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
305 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Tangent):worksetLIDs",
306 gatherFields_[0].extent(0),
307 maxElementBlockGIDCount);
310 if (has_tangent_fields_) {
312 size_t inner_vector_max_size = 0;
313 for (std::size_t fd = 0; fd < tangentFields_.size(); ++fd)
314 inner_vector_max_size = std::max(inner_vector_max_size,tangentFields_[fd].size());
315 tangentFieldsVoV_.initialize(
"GatherSolution_BlockedTpetra<Tangent>::tangentFieldsVoV_",gatherFields_.size(),inner_vector_max_size);
317 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
318 for (std::size_t i=0; i<tangentFields_[fd].size(); ++i) {
319 tangentFieldsVoV_.addView(tangentFields_[fd][i].get_static_view(),fd,i);
323 tangentFieldsVoV_.syncHostToDevice();
326 indexerNames_.clear();
344 using Teuchos::ArrayRCP;
345 using Teuchos::ptrFromRef;
346 using Teuchos::rcp_dynamic_cast;
349 using Thyra::SpmdVectorBase;
352 Teuchos::FancyOStream out(Teuchos::rcpFromRef(std::cout));
353 out.setShowProcRank(
true);
354 out.setOutputToRootOnly(-1);
356 const PHX::View<const int*> & localCellIds = this->wda(workset).getLocalCellIDs();
358 Teuchos::RCP<ProductVectorBase<double> > blockedSolution;
359 if (useTimeDerivativeSolutionVector_)
360 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_dxdt());
362 blockedSolution = rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x());
365 int currentWorksetLIDSubBlock = -1;
366 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
368 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
369 const auto& blockIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]];
370 const std::string blockId = this->wda(workset).block_id;
371 const int num_dofs = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]]->getElementBlockGIDCount(blockId);
372 blockIndexer->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
373 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
376 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
377 const auto& subblockSolution = *((rcp_dynamic_cast<Thyra::TpetraVector<RealT,LO,GO,NodeT>>(blockedSolution->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
378 const auto kokkosSolution = subblockSolution.getLocalViewDevice(Tpetra::Access::ReadOnly);
381 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
382 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
383 const PHX::View<ScalarT**> fieldValues = gatherFields_[fieldIndex].get_static_view();
385 if (has_tangent_fields_) {
386 const int numTangents = tangentFields_[fieldIndex].size();
387 const auto tangentFieldsDevice = tangentFieldsVoV_.getViewDevice();
388 const auto kokkosTangents = Kokkos::subview(tangentFieldsDevice,fieldIndex,Kokkos::ALL());
389 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
390 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
391 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
392 fieldValues(cell,basis).zero();
393 fieldValues(cell,basis).val() = kokkosSolution(rowLID,0);
394 for (
int i_tangent=0; i_tangent<numTangents; ++i_tangent)
395 fieldValues(cell,basis).fastAccessDx(i_tangent) = kokkosTangents(i_tangent)(cell,basis);
399 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
400 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
401 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
402 fieldValues(cell,basis).zero();
403 fieldValues(cell,basis) = kokkosSolution(rowLID,0);
418 const Teuchos::RCP<const BlockedDOFManager> & indexer,
419 const Teuchos::ParameterList& p)
420 : globalIndexer_(indexer)
425 const std::vector<std::string> & names = input.
getDofNames();
426 Teuchos::RCP<const panzer::PureBasis> basis = input.
getBasis();
434 gatherFields_.resize(names.size());
435 for (std::size_t fd = 0; fd < names.size(); ++fd) {
436 PHX::MDField<ScalarT,Cell,NODE> f(names[fd],basis->functional);
437 gatherFields_[fd] = f;
438 this->addEvaluatedField(gatherFields_[fd]);
442 std::string firstName =
"<none>";
444 firstName = names[0];
447 if(disableSensitivities_) {
448 std::string n =
"GatherSolution (BlockedTpetra, No Sensitivities): "+firstName+
" (Jacobian)";
452 std::string n =
"GatherSolution (BlockedTpetra): "+firstName+
" ("+PHX::print<EvalT>()+
") ";
463 TEUCHOS_ASSERT(gatherFields_.size() == indexerNames_.size());
465 const Workset & workset_0 = (*d.worksets_)[0];
466 const std::string blockId = this->wda(workset_0).
block_id;
468 fieldIds_.resize(gatherFields_.size());
469 fieldOffsets_.resize(gatherFields_.size());
470 productVectorBlockIndex_.resize(gatherFields_.size());
471 int maxElementBlockGIDCount = -1;
472 for (std::size_t fd = 0; fd < gatherFields_.size(); ++fd) {
474 const std::string fieldName = indexerNames_[fd];
475 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
476 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
477 const auto& subGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
478 fieldIds_[fd] = subGlobalIndexer->getFieldNum(fieldName);
480 const std::vector<int>&
offsets = subGlobalIndexer->getGIDFieldOffsets(blockId,fieldIds_[fd]);
481 fieldOffsets_[fd] = PHX::View<int*>(
"GatherSolution_BlockedTpetra(Jacobian):fieldOffsets",
offsets.size());
482 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
483 for (std::size_t i=0; i <
offsets.size(); ++i)
485 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
486 maxElementBlockGIDCount = std::max(subGlobalIndexer->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
492 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Residual):worksetLIDs",
493 gatherFields_[0].extent(0),
494 maxElementBlockGIDCount);
497 const auto& blockGlobalIndexers = globalIndexer_->getFieldDOFManagers();
498 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
499 blockOffsets_ = PHX::View<LO*>(
"GatherSolution_BlockedTpetra(Jacobian):blockOffsets_",
501 const auto hostBlockOffsets = Kokkos::create_mirror_view(blockOffsets_);
502 for (
int blk=0;blk<numBlocks;++blk) {
503 int blockOffset = globalIndexer_->getBlockGIDOffset(blockId,blk);
504 hostBlockOffsets(blk) = blockOffset;
506 hostBlockOffsets(numBlocks) = hostBlockOffsets(numBlocks-1) + blockGlobalIndexers[blockGlobalIndexers.size()-1]->getElementBlockGIDCount(blockId);
507 Kokkos::deep_copy(blockOffsets_,hostBlockOffsets);
509 indexerNames_.clear();
517 using Teuchos::rcp_dynamic_cast;
521 solutionBlocks_.clear();
526 const std::string post = useTimeDerivativeSolutionVector_ ?
" - Xdot" :
" - X";
527 if(d.gedc->containsDataObject(globalDataKey_+post)) {
528 RCP<BVROGED> blockedGed = rcp_dynamic_cast<BVROGED>(d.gedc->getDataObject(globalDataKey_+post),
true);
529 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
530 for(
int blk=0;blk<numBlocks;++blk)
531 solutionBlocks_.push_back(rcp_dynamic_cast<TVROGED>(blockedGed->getGEDBlock(blk),
true)->getGhostedVector_Tpetra());
537 blockedContainer_ = rcp_dynamic_cast<const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
539 RCP<const Thyra::VectorBase<double> > blockedSolution = useTimeDerivativeSolutionVector_
540 ? blockedContainer_->get_dxdt()
541 : blockedContainer_->get_x();
542 TEUCHOS_TEST_FOR_EXCEPTION(blockedSolution==Teuchos::null,std::logic_error,
543 "GatherSolution_BlockedTpetra: the container under \"" << globalDataKey_ <<
"\" holds no "
544 << (useTimeDerivativeSolutionVector_ ?
"dxdt" :
"x") <<
" vector to gather from.");
546 auto prodSolution = rcp_dynamic_cast<const Thyra::ProductVectorBase<double> >(blockedSolution,
true);
547 for(
int blk=0;blk<prodSolution->productSpace()->numBlocks();++blk)
548 solutionBlocks_.push_back(rcp_dynamic_cast<
const Thyra::TpetraVector<S,LO,GO,NodeT> >(prodSolution->getVectorBlock(blk),
true)->getConstTpetraVector());
557 using Teuchos::rcp_dynamic_cast;
561 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
564 if (useTimeDerivativeSolutionVector_)
565 seedValue = workset.alpha;
567 seedValue = workset.beta;
572 if(disableSensitivities_)
576 int currentWorksetLIDSubBlock = -1;
577 for (std::size_t fieldIndex = 0; fieldIndex < gatherFields_.size(); fieldIndex++) {
579 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
580 const auto& blockIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]];
581 const std::string blockId = this->wda(workset).block_id;
582 const int num_dofs = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fieldIndex]]->getElementBlockGIDCount(blockId);
583 blockIndexer->getElementLIDs(localCellIds,worksetLIDs_,num_dofs);
584 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
587 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
588 const auto kokkosSolution = solutionBlocks_[blockRowIndex]->getLocalViewDevice(Tpetra::Access::ReadOnly);
591 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
592 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
593 const PHX::View<ScalarT**> fieldValues = gatherFields_[fieldIndex].get_static_view();
594 const PHX::View<const LO*> blockOffsets = blockOffsets_;
595 auto blockOffsets_h = Kokkos::create_mirror_view(blockOffsets);
596 Kokkos::deep_copy(blockOffsets_h, blockOffsets);
597 const int blockStart = blockOffsets_h(blockRowIndex);
599 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
600 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
601 const int rowLID = worksetLIDs(cell,fieldOffsets(basis));
602 fieldValues(cell,basis).zero();
603 fieldValues(cell,basis).val() = kokkosSolution(rowLID,0);
604 fieldValues(cell,basis).fastAccessDx(blockStart+fieldOffsets(basis)) = seedValue;