11 #ifndef PANZER_SCATTER_DIRICHLET_RESIDUAL_BLOCEDTPETRA_IMPL_HPP
12 #define PANZER_SCATTER_DIRICHLET_RESIDUAL_BLOCEDTPETRA_IMPL_HPP
15 #include "Teuchos_Assert.hpp"
17 #include "Phalanx_DataLayout.hpp"
29 #include "Phalanx_DataLayout_MDALayout.hpp"
31 #include "Thyra_SpmdVectorBase.hpp"
32 #include "Thyra_ProductVectorBase.hpp"
33 #include "Thyra_BlockedLinearOpBase.hpp"
36 #include "Teuchos_FancyOStream.hpp"
38 #include <unordered_map>
40 template <
typename EvalT,
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
45 std::string scatterName = p.
get<std::string>(
"Scatter Name");
50 const std::vector<std::string>& names =
57 for (std::size_t eq = 0; eq < names.size(); ++eq) {
61 this->addDependentField(scatterField.fieldTag());
65 this->addEvaluatedField(*scatterHolder);
67 this->setName(scatterName+
" Scatter Residual");
75 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
79 : globalIndexer_(indexer)
80 , globalDataKey_(
"Residual Scatter Container")
82 std::string scatterName = p.
get<std::string>(
"Scatter Name");
87 const std::vector<std::string>& names =
94 scatterIC_ = p.
isParameter(
"Scatter Initial Condition") ? p.
get<
bool>(
"Scatter Initial Condition") :
false;
100 side_subcell_dim_ = p.
get<
int>(
"Side Subcell Dimension");
101 local_side_id_ = p.
get<
int>(
"Local Side ID");
105 scatterFields_.resize(names.size());
106 for (std::size_t eq = 0; eq < names.size(); ++eq) {
110 this->addDependentField(scatterFields_[eq]);
113 checkApplyBC_ = p.
isParameter(
"Check Apply BC") ? p.
get<
bool>(
"Check Apply BC") :
false;
114 applyBC_.resize(names.size());
116 for (std::size_t eq = 0; eq < names.size(); ++eq) {
118 this->addDependentField(applyBC_[eq]);
123 this->addEvaluatedField(*scatterHolder_);
125 if (p.
isType<std::string>(
"Global Data Key"))
126 globalDataKey_ = p.
get<std::string>(
"Global Data Key");
128 this->setName(scatterName+
" Scatter Residual");
132 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
137 const Workset & workset_0 = (*d.worksets_)[0];
138 const std::string blockId = this->wda(workset_0).
block_id;
140 fieldIds_.resize(scatterFields_.size());
141 fieldOffsets_.resize(scatterFields_.size());
142 basisIndexForMDFieldOffsets_.resize(scatterFields_.size());
143 fieldGlobalIndexers_.resize(scatterFields_.size());
144 productVectorBlockIndex_.resize(scatterFields_.size());
145 int maxElementBlockGIDCount = -1;
146 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
148 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
150 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
151 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
152 fieldGlobalIndexers_[fd] = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
153 fieldIds_[fd] = fieldGlobalIndexers_[fd]->getFieldNum(fieldName);
157 const auto& offsetPair = fieldGlobalIndexers_[fd]->getGIDFieldOffsets_closure(blockId,fieldIds_[fd],side_subcell_dim_,local_side_id_);
159 const auto&
offsets = offsetPair.first;
160 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):fieldOffsets",
offsets.size());
161 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
162 for (std::size_t i=0; i <
offsets.size(); ++i)
164 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
167 const auto& basisIndex = offsetPair.second;
168 basisIndexForMDFieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):basisIndexForMDFieldOffsets",basisIndex.size());
169 auto hostBasisIndex = Kokkos::create_mirror_view(basisIndexForMDFieldOffsets_[fd]);
170 for (std::size_t i=0; i < basisIndex.size(); ++i)
171 hostBasisIndex(i) = basisIndex[i];
172 Kokkos::deep_copy(basisIndexForMDFieldOffsets_[fd], hostBasisIndex);
177 const std::vector<int>&
offsets = fieldGlobalIndexers_[fd]->getGIDFieldOffsets(blockId,fieldIds_[fd]);
178 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):fieldOffsets",offsets.size());
179 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
180 for (std::size_t i=0; i < offsets.size(); ++i)
181 hostOffsets(i) = offsets[i];
182 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
185 maxElementBlockGIDCount = std::max(fieldGlobalIndexers_[fd]->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
191 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Residual):worksetLIDs",
192 scatterFields_[0].extent(0),
193 maxElementBlockGIDCount);
197 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
203 = Teuchos::rcp_dynamic_cast<
ContainerType>(d.gedc->getDataObject(
"Dirichlet Counter"),
true);
209 blockedContainer_ = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
214 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
219 using Teuchos::rcp_dynamic_cast;
223 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
226 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_f(),
true) :
227 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x(),
true);
230 int currentWorksetLIDSubBlock = -1;
231 for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
233 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
234 fieldGlobalIndexers_[fieldIndex]->getElementLIDs(localCellIds,worksetLIDs_);
235 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
239 auto& tpetraScatterTarget = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(thyraScatterTarget->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
240 const auto& kokkosScatterTarget = tpetraScatterTarget.getLocalViewDevice(Tpetra::Access::ReadWrite);
243 auto& tpetraDirichletCounter = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(dirichletCounter_->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
244 const auto& kokkosDirichletCounter = tpetraDirichletCounter.getLocalViewDevice(Tpetra::Access::ReadWrite);
247 const auto fieldOffsets = fieldOffsets_[fieldIndex];
248 const auto basisIndices = basisIndexForMDFieldOffsets_[fieldIndex];
249 const auto worksetLIDs = worksetLIDs_;
250 const auto fieldValues = scatterFields_[fieldIndex].get_static_view();
251 const auto applyBC = applyBC_[fieldIndex].get_static_view();
252 const bool checkApplyBC = checkApplyBC_;
256 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
257 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
258 const int lid = worksetLIDs(cell,fieldOffsets(basis));
261 const int basisIndex = basisIndices(basis);
265 if (!applyBC(cell,basisIndex))
268 kokkosScatterTarget(lid,0) = fieldValues(cell,basisIndex);
269 kokkosDirichletCounter(lid,0) = 1.0;
275 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
276 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
277 const int lid = worksetLIDs(cell,fieldOffsets(basis));
280 kokkosScatterTarget(lid,0) = fieldValues(cell,basis);
281 kokkosDirichletCounter(lid,0) = 1.0;
294 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
298 : globalIndexer_(indexer)
299 , globalDataKey_(
"Residual Scatter Container")
301 std::string scatterName = p.
get<std::string>(
"Scatter Name");
306 const std::vector<std::string>& names =
315 side_subcell_dim_ = p.
get<
int>(
"Side Subcell Dimension");
316 local_side_id_ = p.
get<
int>(
"Local Side ID");
319 scatterFields_.resize(names.size());
320 for (std::size_t eq = 0; eq < names.size(); ++eq) {
324 this->addDependentField(scatterFields_[eq]);
327 checkApplyBC_ = p.
get<
bool>(
"Check Apply BC");
328 applyBC_.resize(names.size());
330 for (std::size_t eq = 0; eq < names.size(); ++eq) {
332 this->addDependentField(applyBC_[eq]);
337 this->addEvaluatedField(*scatterHolder_);
339 if (p.
isType<std::string>(
"Global Data Key"))
340 globalDataKey_ = p.
get<std::string>(
"Global Data Key");
342 this->setName(scatterName+
" Scatter Residual (Jacobian)");
346 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
351 const Workset & workset_0 = (*d.worksets_)[0];
352 const std::string blockId = this->wda(workset_0).
block_id;
354 fieldIds_.resize(scatterFields_.size());
355 fieldOffsets_.resize(scatterFields_.size());
356 basisIndexForMDFieldOffsets_.resize(scatterFields_.size());
357 productVectorBlockIndex_.resize(scatterFields_.size());
358 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
360 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
361 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
362 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
363 const auto& fieldGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
364 fieldIds_[fd] = fieldGlobalIndexer->getFieldNum(fieldName);
366 const auto& offsetPair = fieldGlobalIndexer->getGIDFieldOffsets_closure(blockId,fieldIds_[fd],side_subcell_dim_,local_side_id_);
368 const auto&
offsets = offsetPair.first;
369 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):fieldOffsets",
offsets.size());
370 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
371 for (std::size_t i=0; i <
offsets.size(); ++i)
373 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
376 const auto& basisIndex = offsetPair.second;
377 basisIndexForMDFieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):basisIndexForMDFieldOffsets",basisIndex.size());
378 auto hostBasisIndex = Kokkos::create_mirror_view(basisIndexForMDFieldOffsets_[fd]);
379 for (std::size_t i=0; i < basisIndex.size(); ++i)
380 hostBasisIndex(i) = basisIndex[i];
381 Kokkos::deep_copy(basisIndexForMDFieldOffsets_[fd], hostBasisIndex);
388 int elementBlockGIDCount = 0;
389 for (
const auto& blockDOFMgr : globalIndexer_->getFieldDOFManagers())
390 elementBlockGIDCount += blockDOFMgr->getElementBlockGIDCount(blockId);
392 worksetLIDs_ = PHX::View<LO**>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):worksetLIDs",
393 scatterFields_[0].extent(0),
394 elementBlockGIDCount);
397 const auto& blockGlobalIndexers = globalIndexer_->getFieldDOFManagers();
398 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
399 blockOffsets_ = PHX::View<LO*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):blockOffsets_",
401 const auto hostBlockOffsets = Kokkos::create_mirror_view(blockOffsets_);
402 for (
int blk=0;blk<numBlocks;blk++) {
403 int blockOffset = globalIndexer_->getBlockGIDOffset(blockId,blk);
404 hostBlockOffsets(blk) = blockOffset;
406 hostBlockOffsets(numBlocks) = hostBlockOffsets(numBlocks-1) + blockGlobalIndexers[blockGlobalIndexers.size()-1]->getElementBlockGIDCount(blockId);
407 Kokkos::deep_copy(blockOffsets_,hostBlockOffsets);
412 for (
int blk=0;blk<numBlocks;blk++) {
413 const int blockDerivativeSize = hostBlockOffsets(blk+1) - hostBlockOffsets(blk);
415 "ERROR: the derivative dimension for sub block "
416 << blk <<
"with a value of " << blockDerivativeSize
417 <<
"is larger than the size allocated for cLIDs and vals "
418 <<
"in the evaluate call! You must manually increase the "
419 <<
"size and recompile!");
424 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
430 = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(
"Dirichlet Counter"),
true);
436 blockedContainer_ = Teuchos::rcp_dynamic_cast<
const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
441 template <
typename TRAITS,
typename LO,
typename GO,
typename NodeT>
446 using Teuchos::rcp_dynamic_cast;
451 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
453 const int numFieldBlocks = globalIndexer_->getNumFieldBlocks();
463 using LocalMatrixType = KokkosSparse::CrsMatrix<double,LO,PHX::Device,Kokkos::MemoryTraits<Kokkos::Unmanaged>,
size_t>;
464 typename PHX::View<LocalMatrixType**>::HostMirror
465 hostJacTpetraBlocks(
"panzer::ScatterResidual_BlockTpetra<Jacobian>::hostJacTpetraBlocks", numFieldBlocks,numFieldBlocks);
467 PHX::View<int**> blockExistsInJac = PHX::View<int**>(
"blockExistsInJac_",numFieldBlocks,numFieldBlocks);
468 auto hostBlockExistsInJac = Kokkos::create_mirror_view(blockExistsInJac);
470 for (
int row=0; row < numFieldBlocks; ++row) {
471 for (
int col=0; col < numFieldBlocks; ++col) {
472 const auto thyraTpetraOperator = rcp_dynamic_cast<Thyra::TpetraLinearOp<double,LO,GO,NodeT>>(Jac->getNonconstBlock(row,col),
false);
473 if (
nonnull(thyraTpetraOperator)) {
489 const auto tpetraCrsMatrix = rcp_dynamic_cast<Tpetra::CrsMatrix<double,LO,GO,NodeT>>(thyraTpetraOperator->getTpetraOperator(),
true);
490 const auto managedMatrix = tpetraCrsMatrix->getLocalMatrixDevice();
491 const auto managedGraph = managedMatrix.graph;
494 using StaticCrsGraphType =
typename LocalMatrixType::StaticCrsGraphType;
495 StaticCrsGraphType unmanagedGraph;
496 unmanagedGraph.entries =
typename StaticCrsGraphType::entries_type(managedGraph.entries.data(),managedGraph.entries.extent(0));
497 unmanagedGraph.row_map =
typename StaticCrsGraphType::row_map_type(managedGraph.row_map.data(),managedGraph.row_map.extent(0));
498 unmanagedGraph.row_block_offsets =
typename StaticCrsGraphType::row_block_type(managedGraph.row_block_offsets.data(),managedGraph.row_block_offsets.extent(0));
500 typename LocalMatrixType::values_type unmanagedValues(managedMatrix.values.data(),managedMatrix.values.extent(0));
501 LocalMatrixType unmanagedMatrix(managedMatrix.values.label(), managedMatrix.numCols(), unmanagedValues, unmanagedGraph);
502 new (&hostJacTpetraBlocks(row,col)) LocalMatrixType(unmanagedMatrix);
505 hostBlockExistsInJac(row,col) = 1;
508 hostBlockExistsInJac(row,col) = 0;
512 typename PHX::View<LocalMatrixType**>
513 jacTpetraBlocks(
"panzer::ScatterResidual_BlockedTpetra<Jacobian>::jacTpetraBlocks",numFieldBlocks,numFieldBlocks);
514 Kokkos::deep_copy(jacTpetraBlocks,hostJacTpetraBlocks);
515 Kokkos::deep_copy(blockExistsInJac,hostBlockExistsInJac);
522 const auto& globalIndexers = globalIndexer_->getFieldDOFManagers();
523 auto blockOffsets_h = Kokkos::create_mirror_view(blockOffsets_);
524 Kokkos::deep_copy(blockOffsets_h, blockOffsets_);
525 for (
size_t block=0; block < globalIndexers.size(); ++block) {
526 const auto subviewOfBlockLIDs = Kokkos::subview(worksetLIDs_,Kokkos::ALL(), std::make_pair(blockOffsets_h(block),blockOffsets_h(block+1)));
527 globalIndexers[block]->getElementLIDs(localCellIds,subviewOfBlockLIDs);
531 for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
533 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
534 typename Tpetra::Vector<double,LO,GO,PHX::Device>::dual_view_type::t_dev kokkosResidual;
536 auto& tpetraResidual = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(thyraBlockResidual->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
537 kokkosResidual = tpetraResidual.getLocalViewDevice(Tpetra::Access::ReadWrite);
541 auto& tpetraDirichletCounter = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(dirichletCounter_->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
542 const auto& kokkosDirichletCounter = tpetraDirichletCounter.getLocalViewDevice(Tpetra::Access::ReadWrite);
545 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
546 const auto& basisIndices = basisIndexForMDFieldOffsets_[fieldIndex];
547 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
548 const PHX::View<const ScalarT**> fieldValues = scatterFields_[fieldIndex].get_static_view();
549 const PHX::View<const LO*> blockOffsets = blockOffsets_;
550 const auto& applyBC = applyBC_[fieldIndex].get_static_view();
551 const bool checkApplyBC = checkApplyBC_;
553 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
554 LO cLIDs[maxDerivativeArraySize_];
555 typename Sacado::ScalarType<ScalarT>::type
vals[maxDerivativeArraySize_];
557 for(
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
558 const int block_row_offset = blockOffsets(blockRowIndex);
559 const int rowLID = worksetLIDs(cell,block_row_offset+fieldOffsets(basis));
564 const int basisIndex = basisIndices(basis);
568 if (!applyBC(cell,basisIndex))
572 typename FieldType::array_type::reference_type tmpFieldVal = fieldValues(cell,basisIndex);
575 kokkosResidual(rowLID,0) = tmpFieldVal.val();
577 kokkosDirichletCounter(rowLID,0) = 1.0;
580 for (
int blockColIndex=0; blockColIndex < numFieldBlocks; ++blockColIndex) {
581 if (blockExistsInJac(blockRowIndex,blockColIndex)) {
582 const auto& rowEntries = jacTpetraBlocks(blockRowIndex,blockColIndex).row(rowLID);
583 for (
int i=0; i < rowEntries.length; ++i)
589 for (
int blockColIndex=0; blockColIndex < numFieldBlocks; ++blockColIndex) {
590 if (blockExistsInJac(blockRowIndex,blockColIndex)) {
591 const int start = blockOffsets(blockColIndex);
592 const int stop = blockOffsets(blockColIndex+1);
593 const int sensSize = stop-start;
595 for (
int i=0; i < sensSize; ++i) {
596 cLIDs[i] = worksetLIDs(cell,start+i);
597 vals[i] = tmpFieldVal.fastAccessDx(start+i);
599 jacTpetraBlocks(blockRowIndex,blockColIndex).replaceValues(rowLID,cLIDs,sensSize,vals,
true,
true);
608 for (
int row=0; row < numFieldBlocks; ++row) {
609 for (
int col=0; col < numFieldBlocks; ++col) {
610 if (hostBlockExistsInJac(row,col)) {
611 hostJacTpetraBlocks(row,col).~CrsMatrix();
T & get(const std::string &name, T def_value)
bool nonnull(const std::shared_ptr< T > &p)
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ScatterDirichletResidual_BlockedTpetra(const Teuchos::RCP< const BlockedDOFManager > &)
FieldType
The type of discretization to use for a field pattern.
PHX::View< const int * > offsets
bool isParameter(const std::string &name) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Kokkos::View< typename Sacado::ScalarType< ScalarT >::type **, Kokkos::LayoutRight, PHX::Device > vals
void evaluateFields(typename TRAITS::EvalData)
virtual void preEvaluate(typename Traits::PreEvalData d)=0
bool nonnull(const boost::shared_ptr< T > &p)
std::string block_id
DEPRECATED - use: getElementBlock()
bool isType(const std::string &name) const
#define TEUCHOS_ASSERT(assertion_test)
void postRegistrationSetup(typename TRAITS::SetupData, PHX::FieldManager< TRAITS > &)