Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_MapFactory_def.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 XPETRA_MAPFACTORY_DEF_HPP
11 #define XPETRA_MAPFACTORY_DEF_HPP
12 
14 
15 #ifdef HAVE_XPETRA_TPETRA
16 #include "Xpetra_TpetraMap.hpp"
17 #endif
18 #ifdef HAVE_XPETRA_EPETRA
19 #include "Xpetra_EpetraMap.hpp"
20 #endif
21 
22 #include "Xpetra_BlockedMap.hpp"
23 
24 namespace Xpetra {
25 
26 template <class LocalOrdinal, class GlobalOrdinal, class Node>
27 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
30  global_size_t numGlobalElements,
31  GlobalOrdinal indexBase,
32  const Teuchos::RCP<const Teuchos::Comm<int>>& comm,
33  LocalGlobal lg) {
34  XPETRA_MONITOR("MapFactory::Build");
35 
36 #ifdef HAVE_XPETRA_TPETRA
37  if (lib == UseTpetra)
38  return Teuchos::rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexBase, comm, lg));
39 #endif
40 
43 }
44 
45 template <class LocalOrdinal, class GlobalOrdinal, class Node>
46 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
49  global_size_t numGlobalElements,
50  size_t numLocalElements,
51  GlobalOrdinal indexBase,
52  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
53  XPETRA_MONITOR("MapFactory::Build");
54 
55 #ifdef HAVE_XPETRA_TPETRA
56  if (lib == UseTpetra)
57  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, numLocalElements, indexBase, comm));
58 #endif
59 
62 }
63 
64 template <class LocalOrdinal, class GlobalOrdinal, class Node>
65 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
68  global_size_t numGlobalElements,
69  const Teuchos::ArrayView<const GlobalOrdinal>& elementList,
70  GlobalOrdinal indexBase,
71  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
72  XPETRA_MONITOR("MapFactory::Build");
73 
74 #ifdef HAVE_XPETRA_TPETRA
75  if (lib == UseTpetra)
76  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, elementList, indexBase, comm));
77 #endif
78 
81 }
82 
83 template <class LocalOrdinal, class GlobalOrdinal, class Node>
84 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
86  Build(const Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>& nodeMap,
87  const LocalOrdinal numDofPerNode, const GlobalOrdinal gidOffset) {
88  XPETRA_MONITOR("MapFactory::Build");
89 
90  RCP<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>> bmap =
91  Teuchos::rcp_dynamic_cast<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>>(nodeMap);
92 
93  if (!bmap.is_null()) {
94  TEUCHOS_TEST_FOR_EXCEPTION(numDofPerNode != 1, Xpetra::Exceptions::RuntimeError,
95  "Xpetra::MapFactory::Build: When provided a BlockedMap numDofPerNode must set to be one. It is set to "
96  << numDofPerNode << ".");
98  }
99 
100 #ifdef HAVE_XPETRA_TPETRA
101  LocalOrdinal numLocalElements = nodeMap->getLocalNumElements();
102  Teuchos::ArrayView<const GlobalOrdinal> oldElements = nodeMap->getLocalElementList();
103  Teuchos::Array<GlobalOrdinal> newElements(nodeMap->getLocalNumElements() * numDofPerNode);
104  for (LocalOrdinal i = 0; i < numLocalElements; i++) {
105  for (LocalOrdinal j = 0; j < numDofPerNode; j++) {
106  newElements[i * numDofPerNode + j] = oldElements[i] * numDofPerNode + j + gidOffset;
107  }
108  }
109  if (nodeMap->lib() == UseTpetra) {
110  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(nodeMap->getGlobalNumElements() * numDofPerNode, newElements, nodeMap->getIndexBase(), nodeMap->getComm()));
111  }
112 #endif
113 
114  XPETRA_FACTORY_ERROR_IF_EPETRA(nodeMap->lib());
116 }
117 
118 #ifdef HAVE_XPETRA_TPETRA
119 template <class LocalOrdinal, class GlobalOrdinal, class Node>
120 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
123  global_size_t numGlobalElements,
124  const Kokkos::View<const GlobalOrdinal*, typename Node::device_type>& indexList,
125  GlobalOrdinal indexBase,
126  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
127  XPETRA_MONITOR("MapFactory::Build");
128  if (lib == UseTpetra)
129  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexList, indexBase, comm));
132 }
133 #endif // HAVE_XPETRA_TPETRA
134 
135 template <class LocalOrdinal, class GlobalOrdinal, class Node>
136 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
139  size_t numElements,
140  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
141  XPETRA_MONITOR("MapFactory::Build");
142 
143 #ifdef HAVE_XPETRA_TPETRA
144  if (lib == UseTpetra) {
145  // Pre-ETI code called Tpetra::createLocalMap() but this can result in compile erros
146  // when Trilinos is built with multiple node-types, specifically the GCC 4.8.4 PR
147  // build generates an error because it would try to match Tpetra::Map objects where
148  // Node is Serial in one and OpenMP in the other. See Issue #5672 / PR #5723 for more
149  // information.
150  // return rcp(new Xpetra::TpetraMap<LocalOrdinal,GlobalOrdinal,Node>(Tpetra::createLocalMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numElements, comm))); // (old version)
151  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
152  }
153 #endif // HAVE_XPETRA_TPETRA
154 
157 }
158 
159 template <class LocalOrdinal, class GlobalOrdinal, class Node>
160 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
163  size_t numElements,
164  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
165  XPETRA_MONITOR("MapFactory::Build");
166 
167 #ifdef HAVE_XPETRA_TPETRA
168  if (lib == UseTpetra) {
169  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
170  }
171 #endif // HAVE_XPETRA_TPETRA
172 
175 }
176 
177 template <class LocalOrdinal, class GlobalOrdinal, class Node>
178 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
181  global_size_t numElements,
182  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
183  XPETRA_MONITOR("MapFactory::Build");
184 
185 #ifdef HAVE_XPETRA_TPETRA
186  if (lib == UseTpetra)
188  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
189 #endif // HAVE_XPETRA_TPETRA
190 
193 }
194 
195 template <class LocalOrdinal, class GlobalOrdinal, class Node>
196 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
199  global_size_t numElements,
200  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
201  XPETRA_MONITOR("MapFactory::Build");
202 
203 #ifdef HAVE_XPETRA_TPETRA
204  if (lib == UseTpetra)
206  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
207 #endif // HAVE_XPETRA_TPETRA
208 
211 }
212 
213 template <class LocalOrdinal, class GlobalOrdinal, class Node>
214 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
217  global_size_t numElements,
218  size_t localNumElements,
219  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
220  XPETRA_MONITOR("MapFactory::Build");
221 
222 #ifdef HAVE_XPETRA_TPETRA
223  if (lib == UseTpetra)
225  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
226 #endif // HAVE_XPETRA_TPETRA
227 
230 }
231 
232 template <class LocalOrdinal, class GlobalOrdinal, class Node>
233 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
236  global_size_t numElements,
237  size_t localNumElements,
238  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
239  XPETRA_MONITOR("MapFactory::Build");
240 
241 #ifdef HAVE_XPETRA_TPETRA
242  if (lib == UseTpetra) {
244  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
245  }
246 #endif // HAVE_XPETRA_TPETRA
247 
250 }
251 
252 template <class LocalOrdinal, class GlobalOrdinal, class Node>
253 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
256  const Teuchos::RCP<const Teuchos::Comm<int>>& newComm) {
257  XPETRA_MONITOR("MapFactory::Build");
259  global_size_t INVALID = Teuchos::OrdinalTraits<global_size_t>::invalid();
260 
261  size_t Nlocal = oldmap->getLocalNumElements();
262  global_size_t Nglobal = oldmap->getGlobalNumElements();
263 
264  // Sanity check -- if there's no comm, we can't keep elements on the map (vice versa is OK)
265  TEUCHOS_TEST_FOR_EXCEPTION(Nlocal && newComm.is_null(),
266  std::logic_error, "MapFactory::copyMapWithNewComm needs the comm to match the map.");
267 
268  // We'll return null if we don't have a Comm on this rank
269  RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>> newMap;
270  if (!newComm.is_null()) {
271  if (oldmap->isContiguous()) {
272  newMap = XMF::Build(oldmap->lib(), INVALID, Nlocal, oldmap->getIndexBase(), newComm);
273  } else {
274  newMap = XMF::Build(oldmap->lib(), Nglobal, oldmap->getLocalElementList(), oldmap->getIndexBase(), newComm);
275  }
276  }
277 
278  return newMap;
280 }
281 
282 } // namespace Xpetra
283 
284 #endif // XPETRA_MAPFACTORY_DEF_HPP
285 
286 // TODO: remove unused methods
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > copyMapWithNewComm(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &oldmap, const Teuchos::RCP< const Teuchos::Comm< int >> &newComm)
Create a copy of the map, only using the new Comm object if the Comm would be valid.
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createLocalMap(UnderlyingLib lib, size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a locally replicated Map with the default node.
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createContigMap(UnderlyingLib lib, global_size_t numElements, size_t localNumElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a (potentially) non-uniform, contiguous Map with the default node.
Exception throws to report errors in the internal logical of the program.
#define XPETRA_FACTORY_ERROR_IF_EPETRA(lib)
static Teuchos::RCP< Map< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, const Teuchos::RCP< const Teuchos::Comm< int >> &comm, LocalGlobal lg=Xpetra::GloballyDistributed)
Map constructor with Xpetra-defined contiguous uniform distribution.
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createContigMapWithNode(UnderlyingLib lib, global_size_t numElements, size_t localNumElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a (potentially) non-uniform, contiguous Map with a user-specified node.
#define XPETRA_FACTORY_END
size_t global_size_t
Global size_t object.
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createUniformContigMapWithNode(UnderlyingLib lib, global_size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a uniform, contiguous Map with a user-specified node.
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createLocalMapWithNode(UnderlyingLib lib, size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a locally replicated Map with a specified node.
Create an Xpetra::Map instance.
#define XPETRA_MONITOR(funcName)
static Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > createUniformContigMap(UnderlyingLib lib, global_size_t numElements, const Teuchos::RCP< const Teuchos::Comm< int >> &comm)
Create a uniform, contiguous Map with the default node.