Xpetra  Version of the Day
Xpetra_ImportUtils.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Ray Tuminaro (rstumin@sandia.gov)
41 // Chris Siefert (csiefer@sandia.gov)
42 // Luc Berger-Vergoat (lberge@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef PACKAGES_XPETRA_IMPORT_UTILS_HPP_
48 #define PACKAGES_XPETRA_IMPORT_UTILS_HPP_
49 
50 #include "Xpetra_ConfigDefs.hpp"
51 #include "Xpetra_Exceptions.hpp"
52 #include "Xpetra_Map.hpp" // definition of UnderlyingLib
53 #include "Xpetra_Import.hpp"
54 #include "Teuchos_Array.hpp"
55 #include "Teuchos_ArrayView.hpp"
56 
57 #include <utility>
58 
59 #ifdef HAVE_XPETRA_EPETRA
60 #include "Epetra_Util.h"
61 #include "Xpetra_EpetraImport.hpp"
62 #endif
63 
64 #ifdef HAVE_XPETRA_TPETRA
65 #include "Xpetra_TpetraImport.hpp"
66 #include "Tpetra_Import_Util.hpp"
67 #endif
68 
69 namespace Xpetra {
70 
78  template <class LocalOrdinal,
79  class GlobalOrdinal,
81  class ImportUtils {
82 #undef XPETRA_IMPORTUTILS_SHORT
83 
84 public:
91  void
93  Teuchos::Array< std::pair<int,GlobalOrdinal> >& gpids,
94  bool use_minus_one_for_local) {
95  UnderlyingLib lib = Importer.getSourceMap()->lib();
96  if(lib == Xpetra::UseEpetra) {
97 #if defined(HAVE_XPETRA_EPETRA)
98  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
99 #endif // HAVE_XPETRA_EPETRA
100  } else if(lib == Xpetra::UseTpetra) {
101 #ifdef HAVE_XPETRA_TPETRA
102  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer),gpids,use_minus_one_for_local);
103 #endif // HAVE_XPETRA_TPETRA
104  }
105  }
106 
107 
109  void
111  Teuchos::Array<int>& pids,
112  bool use_minus_one_for_local) {
113  UnderlyingLib lib = Importer.getSourceMap()->lib();
114  if(lib == Xpetra::UseEpetra) {
115 #if defined(HAVE_XPETRA_EPETRA)
116  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
117 #endif // HAVE_XPETRA_EPETRA
118  } else if(lib == Xpetra::UseTpetra) {
119 #ifdef HAVE_XPETRA_TPETRA
120  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
121 #endif // HAVE_XPETRA_TPETRA
122  }
123  }
124 
125 
127  // Like the above, but without the resize
128  void
131  bool use_minus_one_for_local) {
132  UnderlyingLib lib = Importer.getSourceMap()->lib();
133  if(lib == Xpetra::UseEpetra) {
134 #if defined(HAVE_XPETRA_EPETRA)
135  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
136 #endif // HAVE_XPETRA_EPETRA
137  } else if(lib == Xpetra::UseTpetra) {
138 #ifdef HAVE_XPETRA_TPETRA
139  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
140 #endif // HAVE_XPETRA_TPETRA
141  }
142  }
143 
144 
147  void
149  Teuchos::Array<int>& RemotePIDs) {
150  UnderlyingLib lib = Importer.getSourceMap()->lib();
151  if(lib == Xpetra::UseEpetra) {
152 #if defined(HAVE_XPETRA_EPETRA)
153  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
154 #endif // HAVE_XPETRA_EPETRA
155  } else if(lib == Xpetra::UseTpetra) {
156 #ifdef HAVE_XPETRA_TPETRA
157  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer),RemotePIDs);
158 #endif // HAVE_XPETRA_TPETRA
159  }
160  }
161 
162 
163  }; // end class ImportUtils
164 
165 #ifdef HAVE_XPETRA_EPETRA
166 // Specialization for int, int, EpetraNode
167  template <>
168  class ImportUtils<int,int,EpetraNode> {
169  typedef int LocalOrdinal;
170  typedef int GlobalOrdinal;
171  typedef EpetraNode Node;
172 #undef XPETRA_IMPORTUTILS_SHORT
173 
174  public:
175  void
177  Teuchos::Array< std::pair<int,GlobalOrdinal> >& gpids,
178  bool use_minus_one_for_local) {
179 
180  UnderlyingLib lib = Importer.getSourceMap()->lib();
181  if(lib == Xpetra::UseEpetra) {
182  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
183  std::vector< std::pair<int,GlobalOrdinal> > gpids_v(gpids.size());
184  Epetra_Util::GetPidGidPairs(*e_Importer,gpids_v,use_minus_one_for_local);
185  std::copy(gpids_v.begin(),gpids_v.end(),gpids.begin());
186  } else if(lib == Xpetra::UseTpetra) {
187 #ifdef HAVE_XPETRA_TPETRA
188  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer),gpids,use_minus_one_for_local);
189 #endif // HAVE_XPETRA_TPETRA
190  }
191  }
192 
193 
195  void
197  Teuchos::Array<int>& pids,
198  bool use_minus_one_for_local) {
199  UnderlyingLib lib = Importer.getSourceMap()->lib();
200  if(lib == Xpetra::UseEpetra) {
201  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
202  std::vector<int > pids_v(pids.size());
203  Epetra_Util::GetPids(*e_Importer,pids_v,use_minus_one_for_local);
204  std::copy(pids_v.begin(),pids_v.end(),pids.begin());
205  } else if(lib == Xpetra::UseTpetra) {
206 #ifdef HAVE_XPETRA_TPETRA
207  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
208 #endif // HAVE_XPETRA_TPETRA
209  }
210  }
211 
212 
214  // Like the above, but without the resize
215  void
218  bool use_minus_one_for_local) {
219  UnderlyingLib lib = Importer.getSourceMap()->lib();
220  if(lib == Xpetra::UseEpetra) {
221  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
222  std::vector<int> pids_v(pids.begin(),pids.end());
223  Epetra_Util::GetPids(*e_Importer,pids_v,use_minus_one_for_local);
224  } else if(lib == Xpetra::UseTpetra) {
225 #ifdef HAVE_XPETRA_TPETRA
226  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
227 #endif // HAVE_XPETRA_TPETRA
228  }
229  }
230 
231 
234  void
236  Teuchos::Array<int>& RemotePIDs) {
237  UnderlyingLib lib = Importer.getSourceMap()->lib();
238  if(lib == Xpetra::UseEpetra) {
239  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
240  std::vector<int> pids_v(RemotePIDs.size());
241  Epetra_Util::GetRemotePIDs(*e_Importer,pids_v);
242  std::copy(pids_v.begin(),pids_v.end(),RemotePIDs.begin());
243  } else if(lib == Xpetra::UseTpetra) {
244 #ifdef HAVE_XPETRA_TPETRA
245  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer),RemotePIDs);
246 #endif // HAVE_XPETRA_TPETRA
247  }
248  }
249 
250 
251  }; // end class ImportUtils
252 
253 
254 // Specialization for double, int, long long, EpetraNode
255  template <>
256  class ImportUtils<int,long long,EpetraNode> {
257  typedef int LocalOrdinal;
258  typedef long long GlobalOrdinal;
259  typedef EpetraNode Node;
260 #undef XPETRA_IMPORTUTILS_SHORT
261 
262  public:
263 
264 
265  void
267  Teuchos::Array< std::pair<int,GlobalOrdinal> >& gpids,
268  bool use_minus_one_for_local) {
269  UnderlyingLib lib = Importer.getSourceMap()->lib();
270  if(lib == Xpetra::UseEpetra) {
271  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
272  std::vector< std::pair<int,GlobalOrdinal> > gpids_v(gpids.size());
273  Epetra_Util::GetPidGidPairs(*e_Importer,gpids_v,use_minus_one_for_local);
274  std::copy(gpids_v.begin(),gpids_v.end(),gpids.begin());
275 
276  } else if(lib == Xpetra::UseTpetra) {
277 #ifdef HAVE_XPETRA_TPETRA
278  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer),gpids,use_minus_one_for_local);
279 #endif // HAVE_XPETRA_TPETRA
280  }
281  }
282 
283 
285  void
287  Teuchos::Array<int>& pids,
288  bool use_minus_one_for_local) {
289  UnderlyingLib lib = Importer.getSourceMap()->lib();
290  if(lib == Xpetra::UseEpetra) {
291  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
292  std::vector<int > pids_v(pids.size());
293  Epetra_Util::GetPids(*e_Importer,pids_v,use_minus_one_for_local);
294  std::copy(pids_v.begin(),pids_v.end(),pids.begin());
295  } else if(lib == Xpetra::UseTpetra) {
296 #ifdef HAVE_XPETRA_TPETRA
297  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
298 #endif // HAVE_XPETRA_TPETRA
299  }
300  }
301 
302 
304  // Like the above, but without the resize
305  void
308  bool use_minus_one_for_local) {
309  UnderlyingLib lib = Importer.getSourceMap()->lib();
310  if(lib == Xpetra::UseEpetra) {
311  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
312  std::vector<int > pids_v(pids.size());
313  Epetra_Util::GetPids(*e_Importer,pids_v,use_minus_one_for_local);
314  std::copy(pids_v.begin(),pids_v.end(),pids.begin());
315  } else if(lib == Xpetra::UseTpetra) {
316 #ifdef HAVE_XPETRA_TPETRA
317  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer),pids,use_minus_one_for_local);
318 #endif // HAVE_XPETRA_TPETRA
319  }
320  }
321 
322 
325  void
327  Teuchos::Array<int>& RemotePIDs) {
328  UnderlyingLib lib = Importer.getSourceMap()->lib();
329  if(lib == Xpetra::UseEpetra) {
330  RCP<const Epetra_Import> e_Importer=dynamic_cast<const EpetraImportT<GlobalOrdinal,Node>* >(&Importer)->getEpetra_Import();
331  std::vector<int> pids_v(RemotePIDs.size());
332  Epetra_Util::GetRemotePIDs(*e_Importer,pids_v);
333  std::copy(pids_v.begin(),pids_v.end(),RemotePIDs.begin());
334  } else if(lib == Xpetra::UseTpetra) {
335 #ifdef HAVE_XPETRA_TPETRA
336  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer),RemotePIDs);
337 #endif // HAVE_XPETRA_TPETRA
338  }
339  }
340 
341  }; // end class ImportUtils
342 #endif // HAVE_XPETRA_EPETRA for Epetra scpecialization
343 
344 } // end namespace Xpetra
345 
346 #define XPETRA_IMPORTUTILS_SHORT
347 
348 #endif // PACKAGES_XPETRA_IMPORT_UTILS_HPP_
static int GetRemotePIDs(const Epetra_Import &Importer, std::vector< int > &RemotePIDs)
static int GetPidGidPairs(const Epetra_Import &Importer, std::vector< std::pair< int, int > > &gpids, bool use_minus_one_for_local)
static int GetPids(const Epetra_Import &Importer, std::vector< int > &pids, bool use_minus_one_for_local)
iterator begin() const
iterator end() const
size_type size() const
iterator begin()
size_type size() const
Exception throws to report errors in the internal logical of the program.
void getRemotePIDs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &RemotePIDs)
Get a list of remote PIDs from an importer in the order corresponding to the remote LIDs.
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::ArrayView< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
void getPidGidPairs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< std::pair< int, GlobalOrdinal > > &gpids, bool use_minus_one_for_local)
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
void getPidGidPairs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< std::pair< int, GlobalOrdinal > > &gpids, bool use_minus_one_for_local)
void getRemotePIDs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &RemotePIDs)
Get a list of remote PIDs from an importer in the order corresponding to the remote LIDs.
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::ArrayView< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
Xpetra utility class for Import-related routines.
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::ArrayView< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
void getRemotePIDs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &RemotePIDs)
Get a list of remote PIDs from an importer in the order corresponding to the remote LIDs.
void getPidGidPairs(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< std::pair< int, GlobalOrdinal > > &gpids, bool use_minus_one_for_local)
For each GID in the TargetMap, find who owns the GID in the SourceMap.
void getPids(const Import< LocalOrdinal, GlobalOrdinal, Node > &Importer, Teuchos::Array< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getSourceMap() const =0
The Source Map used to construct this Import object.
Xpetra namespace
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)