Panzer Version of the Day
Loading...
Searching...
No Matches
Panzer_Filtered_GlobalIndexer.cpp
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// Panzer: A partial differential equation assembly
4// engine for strongly coupled complex multiphysics systems
5//
6// Copyright 2011 NTESS and the Panzer contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
11#ifndef __Panzer_Filtered_GlobalIndexer_impl_hpp__
12#define __Panzer_Filtered_GlobalIndexer_impl_hpp__
13
14#include <unordered_set>
15
16#include "PanzerDofMgr_config.hpp"
17#include "Panzer_NodeType.hpp"
19#include "Tpetra_Map.hpp"
20#include "Tpetra_Import.hpp"
21#include "Tpetra_Export.hpp"
22#include "Tpetra_Vector.hpp"
23
24namespace panzer {
25
27
29void
31initialize(const Teuchos::RCP<const GlobalIndexer> & ugi,
32 const std::vector<panzer::GlobalOrdinal>& filtered)
33{
34 using GO = panzer::GlobalOrdinal;
35 using LO = panzer::LocalOrdinal;
36 using Node = panzer::TpetraNodeType;
37 using Map = Tpetra::Map<LO, GO, Node>;
38 using Vector = Tpetra::Vector<GO,LO,GO,Node>;
39 using Export = Tpetra::Export<LO,GO,Node>;
40
41 using std::size_t;
42 using std::vector;
43 using HashTable = std::unordered_set<panzer::GlobalOrdinal>;
44 using Teuchos::RCP;
45
46 owned_.clear();
47 ghosted_.clear();
48 base_ = ugi;
49
50 // From the base global indexer, build the filtered owned indices.
51 vector<panzer::GlobalOrdinal> baseOwned, baseGhosted;
52 base_->getOwnedIndices(baseOwned);
53 base_->getGhostedIndices(baseGhosted);
54
55 RCP<const Map> ownedMap
56 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(baseOwned,getComm());
57 RCP<const Map> ghostedMap
58 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(baseGhosted,getComm());
59
60 Vector ownedFiltered(ownedMap);
61 Vector ghostedFiltered(ghostedMap);
62
63 ownedFiltered.putScalar(0.0);
64 ghostedFiltered.putScalar(0.0);
65
66 {
67 auto ownedFiltered_host = ownedFiltered.getLocalViewHost(Tpetra::Access::ReadWrite);
68 auto ghostedFiltered_host = ghostedFiltered.getLocalViewHost(Tpetra::Access::ReadWrite);
69
70 for(panzer::GlobalOrdinal f : filtered) {
71 bool isOwned = std::find(baseOwned.begin(),baseOwned.end(),f)!=baseOwned.end();
72 bool isGhosted = std::find(baseGhosted.begin(),baseGhosted.end(),f)!=baseGhosted.end();
73
74 if(isOwned)
75 ownedFiltered_host(ownedMap->getLocalElement(f),0) = 1.0;
76 else if(isGhosted)
77 ghostedFiltered_host(ghostedMap->getLocalElement(f),0) = 1.0;
78 // else no one cares...
79 }
80 }
81
82 Export exporter(ghostedMap,ownedMap);
83 ownedFiltered.doExport(ghostedFiltered, exporter, Tpetra::ADD);
84
85 Teuchos::ArrayRCP<const panzer::GlobalOrdinal> data = ownedFiltered.getData();
86
87 // Build a hash table for fast searching.
88 HashTable filteredHash;
89 for(int i(0); i < data.size(); ++i) {
90 if(data[i]!=0)
91 filteredHash.insert(baseOwned[i]);
92 }
93 // for (size_t i(0); i < filtered.size(); ++i) {
94 // filteredHash.insert(filtered[i]);
95 // }
96
97 // Search for indices in the filtered array; add to owned_ if not found, and
98 // add to ghosted_ otherwise.
99 for (size_t i(0); i < baseOwned.size(); ++i)
100 {
101 auto itr = filteredHash.find(baseOwned[i]);
102 if (itr == filteredHash.end())
103 owned_.push_back(baseOwned[i]);
104 else
105 ghosted_.push_back(baseOwned[i]);
106 }
107 ghosted_.insert(ghosted_.end(), baseGhosted.begin(), baseGhosted.end());
108
109 // Now that we've change the owned_ and ghosted_ vectors, we need to rebuild
110 // the local IDs.
111 this->buildLocalIds();
112} // end of initialize()
113
115void
117getOwnedAndGhostedNotFilteredIndicator(std::vector<int> & indicator) const
118{
119 using Teuchos::RCP;
120
121 using GO = panzer::GlobalOrdinal;
122 using LO = panzer::LocalOrdinal;
123 using Node = panzer::TpetraNodeType;
124 using Map = Tpetra::Map<LO, GO, Node>;
125 using Vector = Tpetra::Vector<GO,LO,GO,Node>;
126 using Import = Tpetra::Import<LO,GO,Node>;
127
128 std::vector<panzer::GlobalOrdinal> ownedIndices;
129 std::vector<panzer::GlobalOrdinal> ghostedIndices;
130
131 // build owned and ghosted maps
133 getOwnedAndGhostedIndices(ghostedIndices);
134
135 RCP<const Map> ownedMap
136 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(ownedIndices,getComm());
137 RCP<const Map> ghostedMap
138 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(ghostedIndices,getComm());
139
140 // allocate the owned vector, mark those GIDs as unfiltered
141 // (they are by definition)
142 Vector ownedActive(ownedMap);
143 ownedActive.putScalar(1);
144
145 // Initialize all indices to zero
146 Vector ghostedActive(ghostedMap);
147 ghostedActive.putScalar(0);
148
149 // do communication, marking unfiltered indices as 1 (filtered
150 // indices locally are marked as zero)
151 Import importer(ownedMap,ghostedMap);
152 ghostedActive.doImport(ownedActive,importer,Tpetra::INSERT);
153
154 Teuchos::ArrayRCP<const GO> data = ghostedActive.getData();
155
156 // copy communicated data (clear it out first)
157 indicator.clear();
158 indicator.insert(indicator.end(),data.begin(),data.end());
159}
160
162void
164getFilteredOwnedAndGhostedIndices(std::vector<panzer::GlobalOrdinal> & indices) const
165{
166 using Teuchos::RCP;
167
168 // get filtered/unfiltered indicator vector
169 std::vector<int> indicators;
171
172 // build ghosted maps
173 std::vector<panzer::GlobalOrdinal> ghostedIndices;
174 getOwnedAndGhostedIndices(ghostedIndices);
175
176 // filtered out filtered indices (isn't that a useful comment)
177 for(std::size_t i=0;i<indicators.size();i++) {
178 if(indicators[i]==1)
179 indices.push_back(ghostedIndices[i]);
180 }
181}
182
184void
186ownedIndices(const std::vector<panzer::GlobalOrdinal> & indices,std::vector<bool> & isOwned) const
187{
188 //Resizes the isOwned array.
189 if(indices.size()!=isOwned.size())
190 isOwned.resize(indices.size(),false);
191 typename std::vector<panzer::GlobalOrdinal>::const_iterator endOf = owned_.end();
192 for (std::size_t i = 0; i < indices.size(); ++i) {
193 isOwned[i] = ( std::find(owned_.begin(), owned_.end(), indices[i])!=endOf );
194 }
195}
196
197}
198
199#endif
virtual Teuchos::RCP< Teuchos::Comm< int > > getComm() const
void initialize(const Teuchos::RCP< const GlobalIndexer > &ugi, const std::vector< panzer::GlobalOrdinal > &filteredIndices)
virtual void getOwnedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
Get the set of indices owned by this processor.
std::vector< panzer::GlobalOrdinal > owned_
The list of owned indices.
void getOwnedAndGhostedNotFilteredIndicator(std::vector< int > &indicator) const
void getFilteredOwnedAndGhostedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
virtual void getOwnedAndGhostedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
Get the set of owned and ghosted indices for this processor.
Teuchos::RCP< const GlobalIndexer > base_
virtual void ownedIndices(const std::vector< panzer::GlobalOrdinal > &indices, std::vector< bool > &isOwned) const
std::vector< panzer::GlobalOrdinal > ghosted_
The list of ghosted indices.
Tpetra::KokkosCompat::KokkosDeviceWrapperNode< PHX::Device > TpetraNodeType