Finite Element Domain Decomposition Library
FEDDLib
Loading...
Searching...
No Matches
Map_Xpetra_def.hpp
1#ifndef MAP_XPETRA_DEF_hpp
2#define MAP_XPETRA_DEF_hpp
3
12
13namespace FEDD {
14template < class LO, class GO, class NO>
15Map_Xpetra<LO,GO,NO>::Map_Xpetra():
16map_()
17{
18
19}
20
21template <class LO, class GO, class NO>
22Map_Xpetra<LO,GO,NO>::Map_Xpetra( XpetraMapConstPtrConst_Type& xpetraMapPtrIn ):
23map_( xpetraMapPtrIn )
24{
25
26}
27
28template < class LO, class GO, class NO>
29Map_Xpetra<LO,GO,NO>::Map_Xpetra( const Map_Type& mapIn ):
30map_()
31{
32 map_ = Xpetra::MapFactory<LO,GO,NO>::Build( Xpetra::UseTpetra, mapIn.getGlobalNumElements(), mapIn.getNodeElementList(), mapIn.getIndexBase(), mapIn.getComm() );
33}
34
35template < class LO, class GO, class NO>
36Map_Xpetra<LO,GO,NO>::Map_Xpetra(
37 GO numGlobalElements,
38 const Teuchos::ArrayView<const GO> &elementList,
39 GO indexBase,
40 const CommConstPtr_Type &comm):
41map_()
42{
43 map_ = Xpetra::MapFactory<LO,GO,NO>::Build( Xpetra::UseTpetra, numGlobalElements, elementList, indexBase, comm);
44}
45
46template < class LO, class GO, class NO>
47Map_Xpetra<LO,GO,NO>::Map_Xpetra(
48 GO numGlobalElements,
49 LO numLocalElements,
50 GO indexBase,
51 const CommConstPtr_Type &comm):
52map_()
53{
54 map_ = Xpetra::MapFactory<LO,GO,NO>::Build( Xpetra::UseTpetra, numGlobalElements, numLocalElements, indexBase, comm);
55}
56
57template < class LO, class GO, class NO>
58Map_Xpetra<LO,GO,NO>::~Map_Xpetra(){
59
60}
61
62template < class LO, class GO, class NO>
63typename Map_Xpetra<LO,GO,NO>::MapPtr_Type Map_Xpetra<LO,GO,NO>::buildVecFieldMap(UN numDofs, std::string ordering) const{
64
65 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
66 TEUCHOS_TEST_FOR_EXCEPTION(ordering.compare("NodeWise"), std::logic_error,"Select a valid ordering: NodeWise");
67 Teuchos::ArrayView<const GO> elementList = map_->getLocalElementList();
68 Teuchos::Array<GO> elementListField( numDofs * elementList.size() );
69 for (UN i=0; i<elementList.size(); i++) {
70 for (UN dof=0; dof<numDofs; dof++)
71 elementListField[ numDofs * i + dof ] = numDofs * elementList[i] + dof;
72 }
73 typedef Teuchos::OrdinalTraits<GO> GOOT;
74 return Teuchos::rcp(new Map_Type( GOOT::invalid(), elementListField(), map_->getIndexBase(), map_->getComm() ) );
75}
76
77template < class LO, class GO, class NO>
78typename Map_Xpetra<LO,GO,NO>::XpetraMapConstPtr_Type Map_Xpetra<LO,GO,NO>::getXpetraMap() const{
79
80 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"getXpetraMap(): map_ is null.");
81
82 return map_;
83}
84template < class LO, class GO, class NO>
85typename Map_Xpetra<LO,GO,NO>::ThyraVSBConstPtr_Type Map_Xpetra<LO,GO,NO>::getThyraVectorSpaceBase() const{
86
87 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"getThyraVectorSpaceBase(): map_ is null.");
88 return Xpetra::ThyraUtils<default_sc,LO,GO,NO>::toThyra( map_ );
89}
90
91
92template < class LO, class GO, class NO>
93GO Map_Xpetra<LO,GO,NO>::getGlobalElement(LO id) const{
94 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
95 return map_->getGlobalElement(id);
96}
98template < class LO, class GO, class NO>
99LO Map_Xpetra<LO,GO,NO>::getLocalElement(GO id) const{
100 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
101 return map_->getLocalElement(id);
102}
103
104template < class LO, class GO, class NO>
105LO Map_Xpetra<LO,GO,NO>::getNodeNumElements() const{
106 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
107 return map_->getLocalNumElements();
108}
109
110template < class LO, class GO, class NO>
111GO Map_Xpetra<LO,GO,NO>::getGlobalNumElements() const{
112 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
113 return map_->getGlobalNumElements();
114}
115
116template < class LO, class GO, class NO>
117Teuchos::ArrayView< const GO > Map_Xpetra<LO,GO,NO>::getNodeElementList() const{
118 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
119 return map_->getLocalElementList();
120}
121
122template < class LO, class GO, class NO>
123GO Map_Xpetra<LO,GO,NO>::getMaxAllGlobalIndex() const{
124 return map_->getMaxAllGlobalIndex();
125}
126
127template < class LO, class GO, class NO>
128LO Map_Xpetra<LO,GO,NO>::getMaxLocalIndex() const{
129 return map_->getMaxLocalIndex();
130}
131
132template < class LO, class GO, class NO>
133void Map_Xpetra<LO,GO,NO>::print(Teuchos::EVerbosityLevel verbLevel) const{
134
135 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
136 map_->describe(*out,verbLevel);
137}
138
139template < class LO, class GO, class NO>
140typename Map_Xpetra<LO,GO,NO>::CommConstPtr_Type Map_Xpetra<LO,GO,NO>::getComm() const{
141
142 return map_->getComm();
143}
144
145template < class LO, class GO, class NO>
146typename Map_Xpetra<LO,GO,NO>::CommPtr_Type Map_Xpetra<LO,GO,NO>::getCommNonConst() {
147 CommConstPtr_Type commConst = map_->getComm();
148 return Teuchos::rcp_const_cast<Comm_Type>(commConst);
149}
150
151template <class LO,class GO,class NO>
152Teuchos::RCP<Map_Xpetra<LO,GO,NO> > Map_Xpetra<LO,GO,NO>::buildUniqueMap( int numFreeProcs ) const
153{
154 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
155 if (numFreeProcs==0) {
156
157 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > myIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(map_);
158 myIndices->putScalar(map_->getComm()->getRank()+1);
159
160 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > linearMap = Xpetra::MapFactory<LO,GO,NO>::Build(Xpetra::UseTpetra,map_->getMaxAllGlobalIndex()+1,0,map_->getComm());
161 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > globalIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(linearMap);
162
163 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer = Xpetra::ImportFactory<LO,GO,NO>::Build(map_,linearMap);
164 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer2 = Xpetra::ImportFactory<LO,GO,NO>::Build(linearMap,map_);
165 globalIndices->doImport(*myIndices,*importer,Xpetra::INSERT);
166 myIndices->putScalar(0);
167 myIndices->doImport(*globalIndices,*importer2,Xpetra::ADD);
168
169 Teuchos::Array<GO> uniqueVector;
170 for (unsigned i=0; i<myIndices->getLocalLength(); i++) {
171 if (myIndices->getData(0)[i] == map_->getComm()->getRank()+1) {
172 uniqueVector.push_back(map_->getGlobalElement(i));
173 }
174 }
175 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > mapXpetra = Xpetra::MapFactory<LO,GO,NO>::Build(Xpetra::UseTpetra,-1,uniqueVector(),0,map_->getComm());
176 Teuchos::RCP<Map_Xpetra<LO,GO,NO> > map = Teuchos::rcp( new Map_Xpetra<LO,GO,NO>( mapXpetra ) );
177 return map;
178 }
179 else{
180 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > myIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(map_);
181 myIndices->putScalar(map_->getComm()->getRank()+1);
182 GO maxGID = map_->getMaxAllGlobalIndex();
183 int numAvailableRanks = map_->getComm()->getSize() - numFreeProcs;
184 int numElementsForAvailRank = (int) ( ( (maxGID+1) / numAvailableRanks ) + 100.*std::numeric_limits<double>::epsilon() );
185
186 int remainingElement = maxGID+1 - numAvailableRanks * numElementsForAvailRank;
187 bool hasOneMoreElement = false;
188
189 if ( remainingElement > map_->getComm()->getRank() ) {
190 numElementsForAvailRank++;
191 hasOneMoreElement = true;
192 }
193
194 if ( map_->getComm()->getRank() + 1 > map_->getComm()->getSize() - numFreeProcs){
195 numElementsForAvailRank = 0;
196 }
197
198 Teuchos::Array<GO> myElements( numElementsForAvailRank );
199 GO offset = numElementsForAvailRank * map_->getComm()->getRank();
200 if (!hasOneMoreElement) {
201 offset += remainingElement;
202 }
203 for (int i=0; i<myElements.size(); i++) {
204 myElements[i] = i + offset;
205 }
206
207 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > linearMapAvailRanks = Xpetra::MapFactory<LO,GO,NO>::Build( Xpetra::UseTpetra,Teuchos::OrdinalTraits<GO>::invalid(), myElements(), 0, map_->getComm() );
208
209 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > globalIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(linearMapAvailRanks);
210
211 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer = Xpetra::ImportFactory<LO,GO,NO>::Build( map_, linearMapAvailRanks );
212 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer2 = Xpetra::ImportFactory<LO,GO,NO>::Build( linearMapAvailRanks, map_ );
213
214 globalIndices->doImport(*myIndices,*importer,Xpetra::INSERT);
215
216 myIndices->putScalar(0);
217 myIndices->doImport(*globalIndices,*importer2,Xpetra::ADD);
218
219 Teuchos::Array<GO> uniqueVector;
220 for (unsigned i=0; i<myIndices->getLocalLength(); i++) {
221 if (myIndices->getData(0)[i] == map_->getComm()->getRank()+1) {
222 uniqueVector.push_back(map_->getGlobalElement(i));
223 }
224 }
225 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > mapXpetra = Xpetra::MapFactory<LO,GO,NO>::Build(Xpetra::UseTpetra,-1,uniqueVector(),0,map_->getComm());
226 Teuchos::RCP<Map_Xpetra<LO,GO,NO> > map = Teuchos::rcp( new Map_Xpetra<LO,GO,NO>( mapXpetra ) );
227 return map;
228 }
229
230}
231
232//merge with above function
233template <class LO,class GO,class NO>
234Teuchos::RCP<Map_Xpetra<LO,GO,NO> > Map_Xpetra<LO,GO,NO>::buildUniqueMap( tuple_intint_Type rankRange ) const
235{
236 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
237 int rank = map_->getComm()->getRank();
238 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > myIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(map_);
239 myIndices->putScalar(rank + 1);
240 GO maxGID = map_->getMaxAllGlobalIndex();
241
242 int numAvailableRanks = std::get<1>(rankRange) - std::get<0>(rankRange) + 1;
243 int numElementsForAvailRank = (int) ( ( (maxGID+1) / numAvailableRanks ) + 100.*std::numeric_limits<double>::epsilon() );
244
245 int remainingElement = maxGID+1 - numAvailableRanks * numElementsForAvailRank;
246 bool hasOneMoreElement = false;
247
248 if ( remainingElement > map_->getComm()->getRank() - std::get<0>(rankRange) ) {
249 numElementsForAvailRank++;
250 hasOneMoreElement = true;
251 }
252 if ( map_->getComm()->getRank() < std::get<0>(rankRange) || map_->getComm()->getRank() > std::get<1>(rankRange) )
253 numElementsForAvailRank = 0;
254
255
256 Teuchos::Array<GO> myElements( numElementsForAvailRank );
257 GO offset = numElementsForAvailRank * ( map_->getComm()->getRank() - std::get<0>(rankRange) );
258
259 if (!hasOneMoreElement)
260 offset += remainingElement;
261
262 for (int i=0; i<myElements.size(); i++)
263 myElements[i] = i + offset;
264
265
266 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > linearMapAvailRanks = Xpetra::MapFactory<LO,GO,NO>::Build( Xpetra::UseTpetra,Teuchos::OrdinalTraits<GO>::invalid(), myElements(), 0, map_->getComm() );
267
268 Teuchos::RCP<Xpetra::Vector<GO,LO,GO,NO> > globalIndices = Xpetra::VectorFactory<GO,LO,GO,NO>::Build(linearMapAvailRanks);
269
270 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer = Xpetra::ImportFactory<LO,GO,NO>::Build( map_, linearMapAvailRanks );
271 Teuchos::RCP<Xpetra::Import<LO,GO,NO> > importer2 = Xpetra::ImportFactory<LO,GO,NO>::Build( linearMapAvailRanks, map_ );
272
273 globalIndices->doImport(*myIndices,*importer,Xpetra::INSERT);
274
275 myIndices->putScalar(0);
276 myIndices->doImport(*globalIndices,*importer2,Xpetra::ADD);
277
278 Teuchos::Array<GO> uniqueVector;
279 for (unsigned i=0; i<myIndices->getLocalLength(); i++) {
280 if (myIndices->getData(0)[i] == map_->getComm()->getRank()+1) {
281 uniqueVector.push_back(map_->getGlobalElement(i));
282 }
283 }
284 Teuchos::RCP<Xpetra::Map<LO,GO,NO> > mapXpetra = Xpetra::MapFactory<LO,GO,NO>::Build(Xpetra::UseTpetra,-1,uniqueVector(),0,map_->getComm());
285 Teuchos::RCP<Map_Xpetra<LO,GO,NO> > map = Teuchos::rcp( new Map_Xpetra<LO,GO,NO>( mapXpetra ) );
286
287 return map;
288
289}
290
291template <class LO,class GO,class NO>
292GO Map_Xpetra<LO,GO,NO>::getIndexBase() const{
293 TEUCHOS_TEST_FOR_EXCEPTION(map_.is_null(),std::runtime_error,"map is null.");
294 return map_->getIndexBase();
295}
296}
297#endif
Teuchos::RCP< Map_Xpetra< LO, GO, NO > > buildUniqueMap(int numFreeProcs=0) const
Definition Map_Xpetra_def.hpp:152
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement_decl.hpp:36