Panzer Version of the Day
Loading...
Searching...
No Matches
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
56namespace panzer {
57
59
61void
63initialize(const Teuchos::RCP<const GlobalIndexer> & ugi,
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::createNonContigMapWithNode<LO,GO,Node>(baseOwned,getComm());
89 RCP<const Map> ghostedMap
90 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(baseGhosted,getComm());
91
92 Vector ownedFiltered(ownedMap);
93 Vector ghostedFiltered(ghostedMap);
94
95 ownedFiltered.putScalar(0.0);
96 ghostedFiltered.putScalar(0.0);
97
98 {
99 auto ownedFiltered_host = ownedFiltered.getLocalViewHost(Tpetra::Access::ReadWrite);
100 auto ghostedFiltered_host = ghostedFiltered.getLocalViewHost(Tpetra::Access::ReadWrite);
101
102 for(panzer::GlobalOrdinal f : filtered) {
103 bool isOwned = std::find(baseOwned.begin(),baseOwned.end(),f)!=baseOwned.end();
104 bool isGhosted = std::find(baseGhosted.begin(),baseGhosted.end(),f)!=baseGhosted.end();
105
106 if(isOwned)
107 ownedFiltered_host(ownedMap->getLocalElement(f),0) = 1.0;
108 else if(isGhosted)
109 ghostedFiltered_host(ghostedMap->getLocalElement(f),0) = 1.0;
110 // else no one cares...
111 }
112 }
113
114 Export exporter(ghostedMap,ownedMap);
115 ownedFiltered.doExport(ghostedFiltered, exporter, Tpetra::ADD);
116
117 Teuchos::ArrayRCP<const panzer::GlobalOrdinal> data = ownedFiltered.getData();
118
119 // Build a hash table for fast searching.
120 HashTable filteredHash;
121 for(int i(0); i < data.size(); ++i) {
122 if(data[i]!=0)
123 filteredHash.insert(baseOwned[i]);
124 }
125 // for (size_t i(0); i < filtered.size(); ++i) {
126 // filteredHash.insert(filtered[i]);
127 // }
128
129 // Search for indices in the filtered array; add to owned_ if not found, and
130 // add to ghosted_ otherwise.
131 for (size_t i(0); i < baseOwned.size(); ++i)
132 {
133 auto itr = filteredHash.find(baseOwned[i]);
134 if (itr == filteredHash.end())
135 owned_.push_back(baseOwned[i]);
136 else
137 ghosted_.push_back(baseOwned[i]);
138 }
139 ghosted_.insert(ghosted_.end(), baseGhosted.begin(), baseGhosted.end());
140
141 // Now that we've change the owned_ and ghosted_ vectors, we need to rebuild
142 // the local IDs.
143 this->buildLocalIds();
144} // end of initialize()
145
147void
149getOwnedAndGhostedNotFilteredIndicator(std::vector<int> & indicator) const
150{
151 using Teuchos::RCP;
152
153 using GO = panzer::GlobalOrdinal;
154 using LO = panzer::LocalOrdinal;
155 using Node = panzer::TpetraNodeType;
156 using Map = Tpetra::Map<LO, GO, Node>;
157 using Vector = Tpetra::Vector<GO,LO,GO,Node>;
158 using Import = Tpetra::Import<LO,GO,Node>;
159
160 std::vector<panzer::GlobalOrdinal> ownedIndices;
161 std::vector<panzer::GlobalOrdinal> ghostedIndices;
162
163 // build owned and ghosted maps
165 getOwnedAndGhostedIndices(ghostedIndices);
166
167 RCP<const Map> ownedMap
168 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(ownedIndices,getComm());
169 RCP<const Map> ghostedMap
170 = Tpetra::createNonContigMapWithNode<LO,GO,Node>(ghostedIndices,getComm());
171
172 // allocate the owned vector, mark those GIDs as unfiltered
173 // (they are by definition)
174 Vector ownedActive(ownedMap);
175 ownedActive.putScalar(1);
176
177 // Initialize all indices to zero
178 Vector ghostedActive(ghostedMap);
179 ghostedActive.putScalar(0);
180
181 // do communication, marking unfiltered indices as 1 (filtered
182 // indices locally are marked as zero)
183 Import importer(ownedMap,ghostedMap);
184 ghostedActive.doImport(ownedActive,importer,Tpetra::INSERT);
185
186 Teuchos::ArrayRCP<const GO> data = ghostedActive.getData();
187
188 // copy communicated data (clear it out first)
189 indicator.clear();
190 indicator.insert(indicator.end(),data.begin(),data.end());
191}
192
194void
196getFilteredOwnedAndGhostedIndices(std::vector<panzer::GlobalOrdinal> & indices) const
197{
198 using Teuchos::RCP;
199
200 // get filtered/unfiltered indicator vector
201 std::vector<int> indicators;
203
204 // build ghosted maps
205 std::vector<panzer::GlobalOrdinal> ghostedIndices;
206 getOwnedAndGhostedIndices(ghostedIndices);
207
208 // filtered out filtered indices (isn't that a useful comment)
209 for(std::size_t i=0;i<indicators.size();i++) {
210 if(indicators[i]==1)
211 indices.push_back(ghostedIndices[i]);
212 }
213}
214
216void
218ownedIndices(const std::vector<panzer::GlobalOrdinal> & indices,std::vector<bool> & isOwned) const
219{
220 //Resizes the isOwned array.
221 if(indices.size()!=isOwned.size())
222 isOwned.resize(indices.size(),false);
223 typename std::vector<panzer::GlobalOrdinal>::const_iterator endOf = owned_.end();
224 for (std::size_t i = 0; i < indices.size(); ++i) {
225 isOwned[i] = ( std::find(owned_.begin(), owned_.end(), indices[i])!=endOf );
226 }
227}
228
229}
230
231#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.
Kokkos::Compat::KokkosDeviceWrapperNode< PHX::Device > TpetraNodeType