Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_ImportUtils.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Xpetra: A linear algebra interface package
4 //
5 // Copyright 2012 NTESS and the Xpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef PACKAGES_XPETRA_IMPORT_UTILS_HPP_
11 #define PACKAGES_XPETRA_IMPORT_UTILS_HPP_
12 
13 #include "Xpetra_ConfigDefs.hpp"
14 #include "Xpetra_Exceptions.hpp"
15 #include "Xpetra_Map.hpp" // definition of UnderlyingLib
16 #include "Xpetra_Import.hpp"
17 #include "Teuchos_Array.hpp"
18 #include "Teuchos_ArrayView.hpp"
19 
20 #include <utility>
21 
22 #ifdef HAVE_XPETRA_EPETRA
23 #include "Epetra_Util.h"
24 #include "Xpetra_EpetraImport.hpp"
25 #endif
26 
27 #ifdef HAVE_XPETRA_TPETRA
28 #include "Xpetra_TpetraImport.hpp"
29 #include "Tpetra_Import_Util.hpp"
30 #endif
31 
32 namespace Xpetra {
33 
41 template <class LocalOrdinal,
42  class GlobalOrdinal,
43  class Node = Tpetra::KokkosClassic::DefaultNode::DefaultNodeType>
44 class ImportUtils {
45 #undef XPETRA_IMPORTUTILS_SHORT
46 
47  public:
54  void
56  Teuchos::Array<std::pair<int, GlobalOrdinal> >& gpids,
57  bool use_minus_one_for_local) {
58  UnderlyingLib lib = Importer.getSourceMap()->lib();
59  if (lib == Xpetra::UseEpetra) {
60 #if defined(HAVE_XPETRA_EPETRA)
61  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
62 #endif // HAVE_XPETRA_EPETRA
63  } else if (lib == Xpetra::UseTpetra) {
64 #ifdef HAVE_XPETRA_TPETRA
65  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer), gpids, use_minus_one_for_local);
66 #endif // HAVE_XPETRA_TPETRA
67  }
68  }
69 
71  void
73  Teuchos::Array<int>& pids,
74  bool use_minus_one_for_local) {
75  UnderlyingLib lib = Importer.getSourceMap()->lib();
76  if (lib == Xpetra::UseEpetra) {
77 #if defined(HAVE_XPETRA_EPETRA)
78  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
79 #endif // HAVE_XPETRA_EPETRA
80  } else if (lib == Xpetra::UseTpetra) {
81 #ifdef HAVE_XPETRA_TPETRA
82  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
83 #endif // HAVE_XPETRA_TPETRA
84  }
85  }
86 
88  // Like the above, but without the resize
89  void
91  Teuchos::ArrayView<int>& pids,
92  bool use_minus_one_for_local) {
93  UnderlyingLib lib = Importer.getSourceMap()->lib();
94  if (lib == Xpetra::UseEpetra) {
95 #if defined(HAVE_XPETRA_EPETRA)
96  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
97 #endif // HAVE_XPETRA_EPETRA
98  } else if (lib == Xpetra::UseTpetra) {
99 #ifdef HAVE_XPETRA_TPETRA
100  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
101 #endif // HAVE_XPETRA_TPETRA
102  }
103  }
104 
107  void
109  Teuchos::Array<int>& RemotePIDs) {
110  UnderlyingLib lib = Importer.getSourceMap()->lib();
111  if (lib == Xpetra::UseEpetra) {
112 #if defined(HAVE_XPETRA_EPETRA)
113  throw(Xpetra::Exceptions::RuntimeError("Xpetra::ImportUtils only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
114 #endif // HAVE_XPETRA_EPETRA
115  } else if (lib == Xpetra::UseTpetra) {
116 #ifdef HAVE_XPETRA_TPETRA
117  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer), RemotePIDs);
118 #endif // HAVE_XPETRA_TPETRA
119  }
120  }
121 
122 }; // end class ImportUtils
123 
124 #ifdef HAVE_XPETRA_EPETRA
125 // Specialization for int, int, EpetraNode
126 template <>
127 class ImportUtils<int, int, EpetraNode> {
128  typedef int LocalOrdinal;
129  typedef int GlobalOrdinal;
130  typedef EpetraNode Node;
131 #undef XPETRA_IMPORTUTILS_SHORT
132 
133  public:
134  void
136  Teuchos::Array<std::pair<int, GlobalOrdinal> >& gpids,
137  bool use_minus_one_for_local) {
138  UnderlyingLib lib = Importer.getSourceMap()->lib();
139  if (lib == Xpetra::UseEpetra) {
140  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
141  std::vector<std::pair<int, GlobalOrdinal> > gpids_v(gpids.size());
142  Epetra_Util::GetPidGidPairs(*e_Importer, gpids_v, use_minus_one_for_local);
143  std::copy(gpids_v.begin(), gpids_v.end(), gpids.begin());
144  } else if (lib == Xpetra::UseTpetra) {
145 #ifdef HAVE_XPETRA_TPETRA
146  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer), gpids, use_minus_one_for_local);
147 #endif // HAVE_XPETRA_TPETRA
148  }
149  }
150 
152  void
154  Teuchos::Array<int>& pids,
155  bool use_minus_one_for_local) {
156  UnderlyingLib lib = Importer.getSourceMap()->lib();
157  if (lib == Xpetra::UseEpetra) {
158  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
159  std::vector<int> pids_v(pids.size());
160  Epetra_Util::GetPids(*e_Importer, pids_v, use_minus_one_for_local);
161  std::copy(pids_v.begin(), pids_v.end(), pids.begin());
162  } else if (lib == Xpetra::UseTpetra) {
163 #ifdef HAVE_XPETRA_TPETRA
164  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
165 #endif // HAVE_XPETRA_TPETRA
166  }
167  }
168 
170  // Like the above, but without the resize
171  void
173  Teuchos::ArrayView<int>& pids,
174  bool use_minus_one_for_local) {
175  UnderlyingLib lib = Importer.getSourceMap()->lib();
176  if (lib == Xpetra::UseEpetra) {
177  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
178  std::vector<int> pids_v(pids.begin(), pids.end());
179  Epetra_Util::GetPids(*e_Importer, pids_v, use_minus_one_for_local);
180  } else if (lib == Xpetra::UseTpetra) {
181 #ifdef HAVE_XPETRA_TPETRA
182  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
183 #endif // HAVE_XPETRA_TPETRA
184  }
185  }
186 
189  void
191  Teuchos::Array<int>& RemotePIDs) {
192  UnderlyingLib lib = Importer.getSourceMap()->lib();
193  if (lib == Xpetra::UseEpetra) {
194  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
195  std::vector<int> pids_v(RemotePIDs.size());
196  Epetra_Util::GetRemotePIDs(*e_Importer, pids_v);
197  std::copy(pids_v.begin(), pids_v.end(), RemotePIDs.begin());
198  } else if (lib == Xpetra::UseTpetra) {
199 #ifdef HAVE_XPETRA_TPETRA
200  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer), RemotePIDs);
201 #endif // HAVE_XPETRA_TPETRA
202  }
203  }
204 
205 }; // end class ImportUtils
206 
207 // Specialization for double, int, long long, EpetraNode
208 template <>
209 class ImportUtils<int, long long, EpetraNode> {
210  typedef int LocalOrdinal;
211  typedef long long GlobalOrdinal;
212  typedef EpetraNode Node;
213 #undef XPETRA_IMPORTUTILS_SHORT
214 
215  public:
216  void
218  Teuchos::Array<std::pair<int, GlobalOrdinal> >& gpids,
219  bool use_minus_one_for_local) {
220  UnderlyingLib lib = Importer.getSourceMap()->lib();
221  if (lib == Xpetra::UseEpetra) {
222  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
223  std::vector<std::pair<int, GlobalOrdinal> > gpids_v(gpids.size());
224  Epetra_Util::GetPidGidPairs(*e_Importer, gpids_v, use_minus_one_for_local);
225  std::copy(gpids_v.begin(), gpids_v.end(), gpids.begin());
226 
227  } else if (lib == Xpetra::UseTpetra) {
228 #ifdef HAVE_XPETRA_TPETRA
229  Tpetra::Import_Util::getPidGidPairs(Xpetra::toTpetra(Importer), gpids, use_minus_one_for_local);
230 #endif // HAVE_XPETRA_TPETRA
231  }
232  }
233 
235  void
237  Teuchos::Array<int>& pids,
238  bool use_minus_one_for_local) {
239  UnderlyingLib lib = Importer.getSourceMap()->lib();
240  if (lib == Xpetra::UseEpetra) {
241  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
242  std::vector<int> pids_v(pids.size());
243  Epetra_Util::GetPids(*e_Importer, pids_v, use_minus_one_for_local);
244  std::copy(pids_v.begin(), pids_v.end(), pids.begin());
245  } else if (lib == Xpetra::UseTpetra) {
246 #ifdef HAVE_XPETRA_TPETRA
247  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
248 #endif // HAVE_XPETRA_TPETRA
249  }
250  }
251 
253  // Like the above, but without the resize
254  void
256  Teuchos::ArrayView<int>& pids,
257  bool use_minus_one_for_local) {
258  UnderlyingLib lib = Importer.getSourceMap()->lib();
259  if (lib == Xpetra::UseEpetra) {
260  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
261  std::vector<int> pids_v(pids.size());
262  Epetra_Util::GetPids(*e_Importer, pids_v, use_minus_one_for_local);
263  std::copy(pids_v.begin(), pids_v.end(), pids.begin());
264  } else if (lib == Xpetra::UseTpetra) {
265 #ifdef HAVE_XPETRA_TPETRA
266  Tpetra::Import_Util::getPids(Xpetra::toTpetra(Importer), pids, use_minus_one_for_local);
267 #endif // HAVE_XPETRA_TPETRA
268  }
269  }
270 
273  void
275  Teuchos::Array<int>& RemotePIDs) {
276  UnderlyingLib lib = Importer.getSourceMap()->lib();
277  if (lib == Xpetra::UseEpetra) {
278  RCP<const Epetra_Import> e_Importer = dynamic_cast<const EpetraImportT<GlobalOrdinal, Node>*>(&Importer)->getEpetra_Import();
279  std::vector<int> pids_v(RemotePIDs.size());
280  Epetra_Util::GetRemotePIDs(*e_Importer, pids_v);
281  std::copy(pids_v.begin(), pids_v.end(), RemotePIDs.begin());
282  } else if (lib == Xpetra::UseTpetra) {
283 #ifdef HAVE_XPETRA_TPETRA
284  Tpetra::Import_Util::getRemotePIDs(Xpetra::toTpetra(Importer), RemotePIDs);
285 #endif // HAVE_XPETRA_TPETRA
286  }
287  }
288 
289 }; // end class ImportUtils
290 #endif // HAVE_XPETRA_EPETRA for Epetra scpecialization
291 
292 } // end namespace Xpetra
293 
294 #define XPETRA_IMPORTUTILS_SHORT
295 
296 #endif // PACKAGES_XPETRA_IMPORT_UTILS_HPP_
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 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::Array< int > &pids, bool use_minus_one_for_local)
Like getPidGidPairs, but just gets the PIDs, ordered by the column Map.
Exception throws to report errors in the internal logical of the program.
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 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 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.
RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > toTpetra(const RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node > > &graph)
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.
Tpetra::KokkosCompat::KokkosSerialWrapperNode EpetraNode
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...
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getSourceMap() const =0
The Source Map used to construct this Import object.
Xpetra utility class for Import-related routines.
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)