Panzer  Version of the Day
Panzer_Filtered_UniqueGlobalIndexer_impl.hpp
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_UniqueGlobalIndexer_impl_hpp__
44 #define __Panzer_Filtered_UniqueGlobalIndexer_impl_hpp__
45 
46 #include <unordered_set>
47 
48 #include "PanzerDofMgr_config.hpp"
49 #include "Panzer_NodeType.hpp"
50 
51 #include "Tpetra_Map.hpp"
52 #include "Tpetra_Import.hpp"
53 #include "Tpetra_Vector.hpp"
54 
55 namespace panzer {
56 
57 template <typename LocalOrdinalT,typename GlobalOrdinalT>
60 { }
61 
62 template <typename LocalOrdinalT,typename GlobalOrdinalT>
63 void
66  const std::vector<GlobalOrdinalT> & filtered)
67 {
68  typedef std::unordered_set<GlobalOrdinalT> HashTable;
69 
70  base_ = ugi;
71 
72  // ensure the localIDs match with the users
73  // this is essential for a class to be a decorator
74  this->shareLocalIDs(*base_);
75 
76  // from base global indexer build the filtered owned indices
77  std::vector<GlobalOrdinalT> baseOwned;
78  base_->getOwnedIndices(baseOwned);
79 
80  // build a hash table for fast searching
81  HashTable filteredHash;
82  for(std::size_t i=0;i<filtered.size();i++)
83  filteredHash.insert(filtered[i]);
84 
85  // search for indices in filtered array, add to owned_ if not found
86  for(std::size_t i=0;i<baseOwned.size();i++) {
87  typename HashTable::const_iterator itr = filteredHash.find(baseOwned[i]);
88 
89  if(itr==filteredHash.end())
90  owned_.push_back(baseOwned[i]);
91  }
92 }
93 
94 template <typename LocalOrdinalT,typename GlobalOrdinalT>
95 void
97 getOwnedAndGhostedNotFilteredIndicator(std::vector<int> & indicator) const
98 {
99  using Teuchos::RCP;
100 
101  typedef GlobalOrdinalT GO;
102  typedef LocalOrdinalT LO;
103  typedef panzer::TpetraNodeType Node;
104  typedef Tpetra::Map<LO, GO, Node> Map;
105  typedef Tpetra::Vector<GO,LO,GO,Node> Vector;
106  typedef Tpetra::Import<LO,GO,Node> Import;
107 
108  std::vector<GlobalOrdinalT> ownedIndices;
109  std::vector<GlobalOrdinalT> ghostedIndices;
110 
111  // build owned and ghosted maps
112  getOwnedIndices(ownedIndices);
113  getOwnedAndGhostedIndices(ghostedIndices);
114 
115  RCP<const Map> ownedMap
116  = Tpetra::createNonContigMap<LO,GO>(ownedIndices,getComm());
117  RCP<const Map> ghostedMap
118  = Tpetra::createNonContigMap<LO,GO>(ghostedIndices,getComm());
119 
120  // allocate the owned vector, mark those GIDs as unfiltered
121  // (they are by definition)
122  Vector ownedActive(ownedMap);
123  ownedActive.putScalar(1);
124 
125  // Initialize all indices to zero
126  Vector ghostedActive(ghostedMap);
127  ghostedActive.putScalar(0);
128 
129  // do communication, marking unfiltered indices as 1 (filtered
130  // indices locally are marked as zero)
131  Import importer(ownedMap,ghostedMap);
132  ghostedActive.doImport(ownedActive,importer,Tpetra::INSERT);
133 
134  Teuchos::ArrayRCP<const GO> data = ghostedActive.getData();
135 
136  // copy communicated data (clear it out first)
137  indicator.clear();
138  indicator.insert(indicator.end(),data.begin(),data.end());
139 }
140 
141 template <typename LocalOrdinalT,typename GlobalOrdinalT>
142 void
144 getFilteredOwnedAndGhostedIndices(std::vector<GlobalOrdinalT> & indices) const
145 {
146  using Teuchos::RCP;
147 
148  // get filtered/unfiltered indicator vector
149  std::vector<int> indicators;
150  getOwnedAndGhostedNotFilteredIndicator(indicators);
151 
152  // build ghosted maps
153  std::vector<GlobalOrdinalT> ghostedIndices;
154  getOwnedAndGhostedIndices(ghostedIndices);
155 
156  // filtered out filtered indices (isn't that a useful comment)
157  for(std::size_t i=0;i<indicators.size();i++) {
158  if(indicators[i]==1)
159  indices.push_back(ghostedIndices[i]);
160  }
161 }
162 
163 template <typename LocalOrdinalT,typename GlobalOrdinalT>
164 void
166 getOwnedIndices(std::vector<GlobalOrdinalT> & indices) const
167 {
168  indices.resize(owned_.size());
169  for (size_t i = 0; i < owned_.size(); ++i) {
170  indices[i]=owned_[i];
171  }
172 }
173 
174 template <typename LocalOrdinalT,typename GlobalOrdinalT>
175 void
177 ownedIndices(const std::vector<GlobalOrdinalT> & indices,std::vector<bool> & isOwned) const
178 {
179  //Resizes the isOwned array.
180  if(indices.size()!=isOwned.size())
181  isOwned.resize(indices.size(),false);
182  typename std::vector<GlobalOrdinalT>::const_iterator endOf = owned_.end();
183  for (std::size_t i = 0; i < indices.size(); ++i) {
184  isOwned[i] = ( std::find(owned_.begin(), owned_.end(), indices[i])!=endOf );
185  }
186 }
187 
188 }
189 
190 #endif
void initialize(const Teuchos::RCP< const UniqueGlobalIndexer< LocalOrdinalT, GlobalOrdinalT > > &ugi, const std::vector< GlobalOrdinalT > &filteredIndices)
void getOwnedAndGhostedNotFilteredIndicator(std::vector< int > &indicator) const
Kokkos::Compat::KokkosDeviceWrapperNode< PHX::Device > TpetraNodeType
iterator end() const
virtual void getOwnedIndices(std::vector< GlobalOrdinalT > &indices) const
virtual void ownedIndices(const std::vector< GlobalOrdinalT > &indices, std::vector< bool > &isOwned) const
iterator begin() const
void getFilteredOwnedAndGhostedIndices(std::vector< GlobalOrdinalT > &indices) const