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 // ***********************************************************************
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 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef XPETRA_MAPFACTORY_DEF_HPP
47 #define XPETRA_MAPFACTORY_DEF_HPP
48 
50 
51 #ifdef HAVE_XPETRA_TPETRA
52 #include "Xpetra_TpetraMap.hpp"
53 #endif
54 #ifdef HAVE_XPETRA_EPETRA
55 #include "Xpetra_EpetraMap.hpp"
56 #endif
57 
58 #include "Xpetra_BlockedMap.hpp"
59 
60 namespace Xpetra {
61 
62 template <class LocalOrdinal, class GlobalOrdinal, class Node>
63 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
66  global_size_t numGlobalElements,
67  GlobalOrdinal indexBase,
68  const Teuchos::RCP<const Teuchos::Comm<int>>& comm,
69  LocalGlobal lg) {
70  XPETRA_MONITOR("MapFactory::Build");
71 
72 #ifdef HAVE_XPETRA_TPETRA
73  if (lib == UseTpetra)
74  return Teuchos::rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexBase, comm, lg));
75 #endif
76 
79 }
80 
81 template <class LocalOrdinal, class GlobalOrdinal, class Node>
82 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
85  global_size_t numGlobalElements,
86  size_t numLocalElements,
87  GlobalOrdinal indexBase,
88  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
89  XPETRA_MONITOR("MapFactory::Build");
90 
91 #ifdef HAVE_XPETRA_TPETRA
92  if (lib == UseTpetra)
93  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, numLocalElements, indexBase, comm));
94 #endif
95 
98 }
99 
100 template <class LocalOrdinal, class GlobalOrdinal, class Node>
101 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
104  global_size_t numGlobalElements,
105  const Teuchos::ArrayView<const GlobalOrdinal>& elementList,
106  GlobalOrdinal indexBase,
107  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
108  XPETRA_MONITOR("MapFactory::Build");
109 
110 #ifdef HAVE_XPETRA_TPETRA
111  if (lib == UseTpetra)
112  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, elementList, indexBase, comm));
113 #endif
114 
117 }
118 
119 template <class LocalOrdinal, class GlobalOrdinal, class Node>
120 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
122  Build(const Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>& nodeMap,
123  const LocalOrdinal numDofPerNode, const GlobalOrdinal gidOffset) {
124  XPETRA_MONITOR("MapFactory::Build");
125 
126  RCP<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>> bmap =
127  Teuchos::rcp_dynamic_cast<const BlockedMap<LocalOrdinal, GlobalOrdinal, Node>>(nodeMap);
128 
129  if (!bmap.is_null()) {
130  TEUCHOS_TEST_FOR_EXCEPTION(numDofPerNode != 1, Xpetra::Exceptions::RuntimeError,
131  "Xpetra::MapFactory::Build: When provided a BlockedMap numDofPerNode must set to be one. It is set to "
132  << numDofPerNode << ".");
134  }
135 
136 #ifdef HAVE_XPETRA_TPETRA
137  LocalOrdinal numLocalElements = nodeMap->getLocalNumElements();
138  Teuchos::ArrayView<const GlobalOrdinal> oldElements = nodeMap->getLocalElementList();
139  Teuchos::Array<GlobalOrdinal> newElements(nodeMap->getLocalNumElements() * numDofPerNode);
140  for (LocalOrdinal i = 0; i < numLocalElements; i++) {
141  for (LocalOrdinal j = 0; j < numDofPerNode; j++) {
142  newElements[i * numDofPerNode + j] = oldElements[i] * numDofPerNode + j + gidOffset;
143  }
144  }
145  if (nodeMap->lib() == UseTpetra) {
146  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(nodeMap->getGlobalNumElements() * numDofPerNode, newElements, nodeMap->getIndexBase(), nodeMap->getComm()));
147  }
148 #endif
149 
150  XPETRA_FACTORY_ERROR_IF_EPETRA(nodeMap->lib());
152 }
153 
154 #ifdef HAVE_XPETRA_TPETRA
155 template <class LocalOrdinal, class GlobalOrdinal, class Node>
156 Teuchos::RCP<Map<LocalOrdinal, GlobalOrdinal, Node>>
159  global_size_t numGlobalElements,
160  const Kokkos::View<const GlobalOrdinal*, typename Node::device_type>& indexList,
161  GlobalOrdinal indexBase,
162  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
163  XPETRA_MONITOR("MapFactory::Build");
164  if (lib == UseTpetra)
165  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(numGlobalElements, indexList, indexBase, comm));
168 }
169 #endif // HAVE_XPETRA_TPETRA
170 
171 template <class LocalOrdinal, class GlobalOrdinal, class Node>
172 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
175  size_t numElements,
176  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
177  XPETRA_MONITOR("MapFactory::Build");
178 
179 #ifdef HAVE_XPETRA_TPETRA
180  if (lib == UseTpetra) {
181  // Pre-ETI code called Tpetra::createLocalMap() but this can result in compile erros
182  // when Trilinos is built with multiple node-types, specifically the GCC 4.8.4 PR
183  // build generates an error because it would try to match Tpetra::Map objects where
184  // Node is Serial in one and OpenMP in the other. See Issue #5672 / PR #5723 for more
185  // information.
186  // return rcp(new Xpetra::TpetraMap<LocalOrdinal,GlobalOrdinal,Node>(Tpetra::createLocalMapWithNode<LocalOrdinal,GlobalOrdinal,Node>(numElements, comm))); // (old version)
187  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
188  }
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  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) {
205  return rcp(new TpetraMap<LocalOrdinal, GlobalOrdinal, Node>(Tpetra::createLocalMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
206  }
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  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
219  XPETRA_MONITOR("MapFactory::Build");
220 
221 #ifdef HAVE_XPETRA_TPETRA
222  if (lib == UseTpetra)
224  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
225 #endif // HAVE_XPETRA_TPETRA
226 
229 }
230 
231 template <class LocalOrdinal, class GlobalOrdinal, class Node>
232 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
235  global_size_t numElements,
236  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
237  XPETRA_MONITOR("MapFactory::Build");
238 
239 #ifdef HAVE_XPETRA_TPETRA
240  if (lib == UseTpetra)
242  Tpetra::createUniformContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, comm)));
243 #endif // HAVE_XPETRA_TPETRA
244 
247 }
248 
249 template <class LocalOrdinal, class GlobalOrdinal, class Node>
250 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
253  global_size_t numElements,
254  size_t localNumElements,
255  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
256  XPETRA_MONITOR("MapFactory::Build");
257 
258 #ifdef HAVE_XPETRA_TPETRA
259  if (lib == UseTpetra)
261  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
262 #endif // HAVE_XPETRA_TPETRA
263 
266 }
267 
268 template <class LocalOrdinal, class GlobalOrdinal, class Node>
269 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
272  global_size_t numElements,
273  size_t localNumElements,
274  const Teuchos::RCP<const Teuchos::Comm<int>>& comm) {
275  XPETRA_MONITOR("MapFactory::Build");
276 
277 #ifdef HAVE_XPETRA_TPETRA
278  if (lib == UseTpetra) {
280  Tpetra::createContigMapWithNode<LocalOrdinal, GlobalOrdinal, Node>(numElements, localNumElements, comm)));
281  }
282 #endif // HAVE_XPETRA_TPETRA
283 
286 }
287 
288 template <class LocalOrdinal, class GlobalOrdinal, class Node>
289 Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>>
292  const Teuchos::RCP<const Teuchos::Comm<int>>& newComm) {
293  XPETRA_MONITOR("MapFactory::Build");
295  global_size_t INVALID = Teuchos::OrdinalTraits<global_size_t>::invalid();
296 
297  size_t Nlocal = oldmap->getLocalNumElements();
298  global_size_t Nglobal = oldmap->getGlobalNumElements();
299 
300  // Sanity check -- if there's no comm, we can't keep elements on the map (vice versa is OK)
301  TEUCHOS_TEST_FOR_EXCEPTION(Nlocal && newComm.is_null(),
302  std::logic_error, "MapFactory::copyMapWithNewComm needs the comm to match the map.");
303 
304  // We'll return null if we don't have a Comm on this rank
305  RCP<const Map<LocalOrdinal, GlobalOrdinal, Node>> newMap;
306  if (!newComm.is_null()) {
307  if (oldmap->isContiguous()) {
308  newMap = XMF::Build(oldmap->lib(), INVALID, Nlocal, oldmap->getIndexBase(), newComm);
309  } else {
310  newMap = XMF::Build(oldmap->lib(), Nglobal, oldmap->getLocalElementList(), oldmap->getIndexBase(), newComm);
311  }
312  }
313 
314  return newMap;
316 }
317 
318 } // namespace Xpetra
319 
320 #endif // XPETRA_MAPFACTORY_DEF_HPP
321 
322 // 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.