Panzer  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Panzer_Filtered_GlobalIndexer.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Panzer: A partial differential equation assembly
5 // engine for strongly coupled complex multiphysics systems
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roger P. Pawlowski (rppawlo@sandia.gov) and
39 // Eric C. Cyr (eccyr@sandia.gov)
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef __Panzer_Filtered_GlobalIndexer_impl_hpp__
44 #define __Panzer_Filtered_GlobalIndexer_impl_hpp__
45 
46 #include <unordered_set>
47 
48 #include "PanzerDofMgr_config.hpp"
49 #include "Panzer_NodeType.hpp"
51 #include "Tpetra_Map.hpp"
52 #include "Tpetra_Import.hpp"
53 #include "Tpetra_Export.hpp"
54 #include "Tpetra_Vector.hpp"
55 
56 namespace panzer {
57 
59 
61 void
64  const std::vector<panzer::GlobalOrdinal>& filtered)
65 {
66  using GO = panzer::GlobalOrdinal;
67  using LO = panzer::LocalOrdinal;
68  using Node = panzer::TpetraNodeType;
69  using Map = Tpetra::Map<LO, GO, Node>;
70  using Vector = Tpetra::Vector<GO,LO,GO,Node>;
71  using Export = Tpetra::Export<LO,GO,Node>;
72 
73  using std::size_t;
74  using std::vector;
75  using HashTable = std::unordered_set<panzer::GlobalOrdinal>;
76  using Teuchos::RCP;
77 
78  owned_.clear();
79  ghosted_.clear();
80  base_ = ugi;
81 
82  // From the base global indexer, build the filtered owned indices.
83  vector<panzer::GlobalOrdinal> baseOwned, baseGhosted;
84  base_->getOwnedIndices(baseOwned);
85  base_->getGhostedIndices(baseGhosted);
86 
87  RCP<const Map> ownedMap
88  = Tpetra::createNonContigMap<LO,GO>(baseOwned,getComm());
89  RCP<const Map> ghostedMap
90  = Tpetra::createNonContigMap<LO,GO>(baseGhosted,getComm());
91 
92  Vector ownedFiltered(ownedMap);
93  Vector ghostedFiltered(ghostedMap);
94 
95  ownedFiltered.putScalar(0.0);
96  ghostedFiltered.putScalar(0.0);
97 
98  for(panzer::GlobalOrdinal f : filtered) {
99  bool isOwned = std::find(baseOwned.begin(),baseOwned.end(),f)!=baseOwned.end();
100  bool isGhosted = std::find(baseGhosted.begin(),baseGhosted.end(),f)!=baseGhosted.end();
101 
102  if(isOwned)
103  ownedFiltered.replaceGlobalValue(f,1.0);
104  else if(isGhosted)
105  ghostedFiltered.replaceGlobalValue(f,1.0);
106  // else no one cares...
107  }
108 
109  Export exporter(ghostedMap,ownedMap);
110  ownedFiltered.doExport(ghostedFiltered, exporter, Tpetra::ADD);
111 
112  Teuchos::ArrayRCP<const panzer::GlobalOrdinal> data = ownedFiltered.getData();
113 
114  // Build a hash table for fast searching.
115  HashTable filteredHash;
116  for(int i(0); i < data.size(); ++i) {
117  if(data[i]!=0)
118  filteredHash.insert(baseOwned[i]);
119  }
120  // for (size_t i(0); i < filtered.size(); ++i) {
121  // filteredHash.insert(filtered[i]);
122  // }
123 
124  // Search for indices in the filtered array; add to owned_ if not found, and
125  // add to ghosted_ otherwise.
126  for (size_t i(0); i < baseOwned.size(); ++i)
127  {
128  auto itr = filteredHash.find(baseOwned[i]);
129  if (itr == filteredHash.end())
130  owned_.push_back(baseOwned[i]);
131  else
132  ghosted_.push_back(baseOwned[i]);
133  }
134  ghosted_.insert(ghosted_.end(), baseGhosted.begin(), baseGhosted.end());
135 
136  // Now that we've change the owned_ and ghosted_ vectors, we need to rebuild
137  // the local IDs.
138  this->buildLocalIds();
139 } // end of initialize()
140 
142 void
144 getOwnedAndGhostedNotFilteredIndicator(std::vector<int> & indicator) const
145 {
146  using Teuchos::RCP;
147 
148  using GO = panzer::GlobalOrdinal;
149  using LO = panzer::LocalOrdinal;
150  using Node = panzer::TpetraNodeType;
151  using Map = Tpetra::Map<LO, GO, Node>;
152  using Vector = Tpetra::Vector<GO,LO,GO,Node>;
153  using Import = Tpetra::Import<LO,GO,Node>;
154 
155  std::vector<panzer::GlobalOrdinal> ownedIndices;
156  std::vector<panzer::GlobalOrdinal> ghostedIndices;
157 
158  // build owned and ghosted maps
159  getOwnedIndices(ownedIndices);
160  getOwnedAndGhostedIndices(ghostedIndices);
161 
162  RCP<const Map> ownedMap
163  = Tpetra::createNonContigMap<LO,GO>(ownedIndices,getComm());
164  RCP<const Map> ghostedMap
165  = Tpetra::createNonContigMap<LO,GO>(ghostedIndices,getComm());
166 
167  // allocate the owned vector, mark those GIDs as unfiltered
168  // (they are by definition)
169  Vector ownedActive(ownedMap);
170  ownedActive.putScalar(1);
171 
172  // Initialize all indices to zero
173  Vector ghostedActive(ghostedMap);
174  ghostedActive.putScalar(0);
175 
176  // do communication, marking unfiltered indices as 1 (filtered
177  // indices locally are marked as zero)
178  Import importer(ownedMap,ghostedMap);
179  ghostedActive.doImport(ownedActive,importer,Tpetra::INSERT);
180 
181  Teuchos::ArrayRCP<const GO> data = ghostedActive.getData();
182 
183  // copy communicated data (clear it out first)
184  indicator.clear();
185  indicator.insert(indicator.end(),data.begin(),data.end());
186 }
187 
189 void
191 getFilteredOwnedAndGhostedIndices(std::vector<panzer::GlobalOrdinal> & indices) const
192 {
193  using Teuchos::RCP;
194 
195  // get filtered/unfiltered indicator vector
196  std::vector<int> indicators;
198 
199  // build ghosted maps
200  std::vector<panzer::GlobalOrdinal> ghostedIndices;
201  getOwnedAndGhostedIndices(ghostedIndices);
202 
203  // filtered out filtered indices (isn't that a useful comment)
204  for(std::size_t i=0;i<indicators.size();i++) {
205  if(indicators[i]==1)
206  indices.push_back(ghostedIndices[i]);
207  }
208 }
209 
211 void
213 ownedIndices(const std::vector<panzer::GlobalOrdinal> & indices,std::vector<bool> & isOwned) const
214 {
215  //Resizes the isOwned array.
216  if(indices.size()!=isOwned.size())
217  isOwned.resize(indices.size(),false);
218  typename std::vector<panzer::GlobalOrdinal>::const_iterator endOf = owned_.end();
219  for (std::size_t i = 0; i < indices.size(); ++i) {
220  isOwned[i] = ( std::find(owned_.begin(), owned_.end(), indices[i])!=endOf );
221  }
222 }
223 
224 }
225 
226 #endif
void getOwnedAndGhostedNotFilteredIndicator(std::vector< int > &indicator) const
virtual void getOwnedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
Get the set of indices owned by this processor.
size_type size() const
std::vector< panzer::GlobalOrdinal > ghosted_
The list of ghosted indices.
std::vector< panzer::GlobalOrdinal > owned_
The list of owned indices.
void getFilteredOwnedAndGhostedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
virtual void ownedIndices(const std::vector< panzer::GlobalOrdinal > &indices, std::vector< bool > &isOwned) const
Kokkos::Compat::KokkosDeviceWrapperNode< PHX::Device > TpetraNodeType
virtual Teuchos::RCP< Teuchos::Comm< int > > getComm() const
virtual void getOwnedAndGhostedIndices(std::vector< panzer::GlobalOrdinal > &indices) const
Get the set of owned and ghosted indices for this processor.
iterator end() const
Teuchos::RCP< const GlobalIndexer > base_
void initialize(const Teuchos::RCP< const GlobalIndexer > &ugi, const std::vector< panzer::GlobalOrdinal > &filteredIndices)
iterator begin() const