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 
13 #include "Teuchos_CompilerCodeTweakMacros.hpp"
14 #include "Teuchos_ENull.hpp"
16 
17 #ifdef HAVE_XPETRA_TPETRA
18 #include "Xpetra_TpetraMap.hpp"
19 #endif
20 #ifdef HAVE_XPETRA_EPETRA
21 #include "Xpetra_EpetraMap.hpp"
22 #endif
23 
24 #include "Xpetra_BlockedMap.hpp"
25 
26 namespace Xpetra {
27 
28 template <class LocalOrdinal, class GlobalOrdinal, class Node>
29 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
32  global_size_t numGlobalElements,
33  GlobalOrdinal indexBase,
34  const Teuchos::RCP<const Teuchos::Comm<int>>& comm,
35  LocalGlobal lg) {
36  XPETRA_MONITOR("MapFactory::Build");
37 
38 #ifdef HAVE_XPETRA_TPETRA
39  if (lib == UseTpetra)
40  return Teuchos::rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexBase, comm, lg));
41 #endif
42 
45 }
46 
47 template <class LocalOrdinal, class GlobalOrdinal, class Node>
48 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
51  global_size_t numGlobalElements,
52  size_t numLocalElements,
53  GlobalOrdinal indexBase,
54  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
55  XPETRA_MONITOR("MapFactory::Build");
56 
57 #ifdef HAVE_XPETRA_TPETRA
58  if (lib == UseTpetra)
59  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, numLocalElements, indexBase, comm));
60 #endif
61 
64 }
65 
66 template <class LocalOrdinal, class GlobalOrdinal, class Node>
67 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
70  global_size_t numGlobalElements,
71  const Teuchos::ArrayView<const GlobalOrdinal>& elementList,
72  GlobalOrdinal indexBase,
73  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
74  XPETRA_MONITOR("MapFactory::Build");
75 
76 #ifdef HAVE_XPETRA_TPETRA
77  if (lib == UseTpetra)
78  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, elementList, indexBase, comm));
79 #endif
80 
83 }
84 
85 template <class LocalOrdinal, class GlobalOrdinal, class Node>
86 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
88  Build(const Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>& nodeMap,
89  const LocalOrdinal numDofPerNode, const GlobalOrdinal gidOffset) {
90  XPETRA_MONITOR("MapFactory::Build");
91 
92  RCP<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>> bmap =
93  Teuchos::rcp_dynamic_cast<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>>(nodeMap);
94 
95  if (!bmap.is_null()) {
96  TEUCHOS_TEST_FOR_EXCEPTION(numDofPerNode != 1, Xpetra::Exceptions::RuntimeError,
97  "Xpetra::MapFactory::Build: When provided a BlockedMap numDofPerNode must set to be one. It is set to "
98  << numDofPerNode << ".");
100  }
101 
102 #ifdef HAVE_XPETRA_TPETRA
103  LocalOrdinal numLocalElements = nodeMap->getLocalNumElements();
104  Teuchos::ArrayView<const GlobalOrdinal> oldElements = nodeMap->getLocalElementList();
105  Teuchos::Array<GlobalOrdinal> newElements(nodeMap->getLocalNumElements() * numDofPerNode);
106  for (LocalOrdinal i = 0; i < numLocalElements; i++) {
107  for (LocalOrdinal j = 0; j < numDofPerNode; j++) {
108  newElements[i * numDofPerNode + j] = oldElements[i] * numDofPerNode + j + gidOffset;
109  }
110  }
111  if (nodeMap->lib() == UseTpetra) {
112  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(nodeMap->getGlobalNumElements() * numDofPerNode, newElements, nodeMap->getIndexBase(), nodeMap->getComm()));
113  }
114 #endif
115 
116  XPETRA_FACTORY_ERROR_IF_EPETRA(nodeMap->lib());
118  TEUCHOS_UNREACHABLE_RETURN(Teuchos::null);
119 }
120 
121 #ifdef HAVE_XPETRA_TPETRA
122 template <class LocalOrdinal, class GlobalOrdinal, class Node>
123 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
126  global_size_t numGlobalElements,
127  const Kokkos::View<const GlobalOrdinal*, typename Node::device_type>& indexList,
128  GlobalOrdinal indexBase,
129  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
130  XPETRA_MONITOR("MapFactory::Build");
131  if (lib == UseTpetra)
132  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexList, indexBase, comm));
135 }
136 #endif // HAVE_XPETRA_TPETRA
137 
138 template <class LocalOrdinal, class GlobalOrdinal, class Node>
139 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
142  size_t numElements,
143  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
144  XPETRA_MONITOR("MapFactory::Build");
145 
146 #ifdef HAVE_XPETRA_TPETRA
147  if (lib == UseTpetra) {
148  // Pre-ETI code called Tpetra::createLocalMap() but this can result in compile erros
149  // when Trilinos is built with multiple node-types, specifically the GCC 4.8.4 PR
150  // build generates an error because it would try to match Tpetra::Map objects where
151  // Node is Serial in one and OpenMP in the other. See Issue #5672 / PR #5723 for more
152  // information.
153  // return rcp(new Xpetra::TpetraMap<LocalOrdinal,GlobalOrdinal,Node>(Tpetra::createLocalMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numElements, comm))); // (old version)
154  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
155  }
156 #endif // HAVE_XPETRA_TPETRA
157 
160 }
161 
162 template <class LocalOrdinal, class GlobalOrdinal, class Node>
163 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
166  size_t numElements,
167  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
168  XPETRA_MONITOR("MapFactory::Build");
169 
170 #ifdef HAVE_XPETRA_TPETRA
171  if (lib == UseTpetra) {
172  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
173  }
174 #endif // HAVE_XPETRA_TPETRA
175 
178 }
179 
180 template <class LocalOrdinal, class GlobalOrdinal, class Node>
181 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
184  global_size_t numElements,
185  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
186  XPETRA_MONITOR("MapFactory::Build");
187 
188 #ifdef HAVE_XPETRA_TPETRA
189  if (lib == UseTpetra)
191  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
192 #endif // HAVE_XPETRA_TPETRA
193 
196 }
197 
198 template <class LocalOrdinal, class GlobalOrdinal, class Node>
199 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
202  global_size_t numElements,
203  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
204  XPETRA_MONITOR("MapFactory::Build");
205 
206 #ifdef HAVE_XPETRA_TPETRA
207  if (lib == UseTpetra)
209  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
210 #endif // HAVE_XPETRA_TPETRA
211 
214 }
215 
216 template <class LocalOrdinal, class GlobalOrdinal, class Node>
217 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
220  global_size_t numElements,
221  size_t localNumElements,
222  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
223  XPETRA_MONITOR("MapFactory::Build");
224 
225 #ifdef HAVE_XPETRA_TPETRA
226  if (lib == UseTpetra)
228  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
229 #endif // HAVE_XPETRA_TPETRA
230 
233 }
234 
235 template <class LocalOrdinal, class GlobalOrdinal, class Node>
236 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
239  global_size_t numElements,
240  size_t localNumElements,
241  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
242  XPETRA_MONITOR("MapFactory::Build");
243 
244 #ifdef HAVE_XPETRA_TPETRA
245  if (lib == UseTpetra) {
247  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
248  }
249 #endif // HAVE_XPETRA_TPETRA
250 
253 }
254 
255 template <class LocalOrdinal, class GlobalOrdinal, class Node>
256 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
259  const Teuchos::RCP<const Teuchos::Comm<int>>& newComm) {
260  XPETRA_MONITOR("MapFactory::Build");
262  global_size_t INVALID = Teuchos::OrdinalTraits<global_size_t>::invalid();
263 
264  size_t Nlocal = oldmap->getLocalNumElements();
265  global_size_t Nglobal = oldmap->getGlobalNumElements();
266 
267  // Sanity check -- if there's no comm, we can't keep elements on the map (vice versa is OK)
268  TEUCHOS_TEST_FOR_EXCEPTION(Nlocal && newComm.is_null(),
269  std::logic_error, "MapFactory::copyMapWithNewComm needs the comm to match the map.");
270 
271  // We'll return null if we don't have a Comm on this rank
272  RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>> newMap;
273  if (!newComm.is_null()) {
274  if (oldmap->isContiguous()) {
275  newMap = XMF::Build(oldmap->lib(), INVALID, Nlocal, oldmap->getIndexBase(), newComm);
276  } else {
277  newMap = XMF::Build(oldmap->lib(), Nglobal, oldmap->getLocalElementList(), oldmap->getIndexBase(), newComm);
278  }
279  }
280 
281  return newMap;
283 }
284 
285 } // namespace Xpetra
286 
287 #endif // XPETRA_MAPFACTORY_DEF_HPP
288 
289 // 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.