All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_StridedMapFactory.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 // Jeremie Gaidamour (jngaida@sandia.gov)
40 // Jonathan Hu (jhu@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 
47 // WARNING: This code is experimental. Backwards compatibility should not be expected.
48 
49 #ifndef XPETRA_STRIDEDMAPFACTORY_HPP
50 #define XPETRA_STRIDEDMAPFACTORY_HPP
51 
52 #include "Xpetra_ConfigDefs.hpp"
53 
54 #include "Xpetra_StridedMap.hpp"
55 
56 #ifdef HAVE_XPETRA_TPETRA
58 #endif
59 #ifdef HAVE_XPETRA_EPETRA
61 #endif
62 
63 #include "Xpetra_Exceptions.hpp"
64 
65 // This factory creates Xpetra::Map. User have to specify the exact class of object that he want to create (ie: a Xpetra::TpetraMap or a Xpetra::EpetraMap).
66 
67 namespace Xpetra {
68 
69  template <class LocalOrdinal, class GlobalOrdinal = LocalOrdinal, class Node = Kokkos::DefaultNode::DefaultNodeType>
70  class StridedMapFactory {
71 
72  private:
75 
76  public:
77 
79  static Teuchos::RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0, LocalGlobal lg=Xpetra::GloballyDistributed, const Teuchos::RCP<Node> &node = Kokkos::Details::getNode<Node> ()) {
80 
81 #ifdef HAVE_XPETRA_TPETRA
82  if (lib == UseTpetra)
83  return Teuchos::rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, lg, node) );
84 #endif
85 
88  }
89 
91  static Teuchos::RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, size_t numLocalElements, GlobalOrdinal indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0, const Teuchos::RCP<Node> &node = Kokkos::Details::getNode<Node> ()) {
92 
93 #ifdef HAVE_XPETRA_TPETRA
94  if (lib == UseTpetra)
95  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, numLocalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, node) );
96 #endif
97 
100  }
101 
102  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, const RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >& map, std::vector<size_t>& stridingInfo, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0) {
103 
104 #ifdef HAVE_XPETRA_TPETRA
105  if (lib == UseTpetra) {
107  TEUCHOS_TEST_FOR_EXCEPTION(tmap == Teuchos::null, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: bad cast. map is not a TpetraMap.");
108  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (tmap->getTpetra_Map(), stridingInfo, stridedBlockId, offset) );
109  }
110 #endif
113  }
114 
115  // special constructor for generating a given subblock of a strided map
117  TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockId < 0, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: constructor expects stridedBlockId > -1.");
118 #ifdef HAVE_XPETRA_TPETRA
119  if (map->lib() == UseTpetra) {
120  TEUCHOS_TEST_FOR_EXCEPTION(map->getStridedBlockId() != -1, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: constructor expects a full map (stridedBlockId == -1).");
121  std::vector<size_t> stridingInfo = map->getStridingData();
122 
124  Teuchos::ArrayView< const GlobalOrdinal > dofGids = map->getNodeElementList();
125  //std::sort(dofGids.begin(),dofGids.end()); // TODO: do i need this?
126 
127  // determine nStridedOffset
128  size_t nStridedOffset = 0;
129  for(int j=0; j<map->getStridedBlockId(); j++) {
130  nStridedOffset += stridingInfo[j];
131  }
132 
133  size_t numMyBlockDofs = (stridingInfo[stridedBlockId] * map->getNodeNumElements()) / map->getFixedBlockSize();
134  std::vector<GlobalOrdinal> subBlockDofGids(numMyBlockDofs);
135 
136  // TODO fill vector with dofs
137  typename Teuchos::ArrayView< const GlobalOrdinal >::iterator it;
138  for(it = dofGids.begin(); it!=dofGids.end(); ++it) {
139  if(map->GID2StridingBlockId( *it ) == stridedBlockId) {
140  subBlockDofGids.push_back( *it );
141  }
142  }
143 
144  const Teuchos::ArrayView<const LocalOrdinal> subBlockDofGids_view(&subBlockDofGids[0],subBlockDofGids.size());
145 
146  // call constructor for TpetraMap
147  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (subBlockDofGids.size(), subBlockDofGids_view, map->getIndexBase(), stridingInfo, map->getComm(), stridedBlockId, map->getNode()) );
149 
150  }
151 #endif
152  XPETRA_FACTORY_ERROR_IF_EPETRA(map->lib());
153  XPETRA_FACTORY_END;
154  }
155 
156 #if 0 // TODO
158  static Teuchos::RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, const Teuchos::ArrayView<const GlobalOrdinal> &elementList, GlobalOrdinal indexBase, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Node> &node = Kokkos::Details::getNode<Node> ()) {
159 
160 #ifdef HAVE_XPETRA_TPETRA
161  if (lib == UseTpetra)
162  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, elementList, indexBase, comm, node) );
163 #endif
164 
165  XPETRA_FACTORY_ERROR_IF_EPETRA(lib);
166  XPETRA_FACTORY_END;
167  }
168 #endif
169 
170 
171  };
172 
173  template <>
174  class StridedMapFactory<int, int> {
175 
176  typedef int LocalOrdinal;
177  typedef int GlobalOrdinal;
178  typedef Kokkos::DefaultNode::DefaultNodeType Node;
179 
180  private:
182  StridedMapFactory() {}
183 
184  public:
185 
186  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, int indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0, LocalGlobal lg=GloballyDistributed, const Teuchos::RCP<Kokkos::DefaultNode::DefaultNodeType> &node = Kokkos::Details::getNode<Node> ()) {
187 
188 #ifdef HAVE_XPETRA_TPETRA
189  if (lib == UseTpetra)
190  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, lg, node) );
191 #endif
192 
193 #ifdef HAVE_XPETRA_EPETRA
194  if (lib == UseEpetra)
195  return rcp( new StridedEpetraMap(numGlobalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, lg, node) );
196 #endif
197 
198  XPETRA_FACTORY_END;
199  }
200 
201  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, size_t numLocalElements, int indexBase, std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0, const Teuchos::RCP<Kokkos::DefaultNode::DefaultNodeType> &node = Kokkos::Details::getNode<Node> ()) {
202 
203 #ifdef HAVE_XPETRA_TPETRA
204  if (lib == UseTpetra)
205  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, numLocalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, node) );
206 #endif
207 
208 #ifdef HAVE_XPETRA_EPETRA
209  if (lib == UseEpetra)
210  return rcp( new StridedEpetraMap(numGlobalElements, numLocalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, node) );
211 #endif
212 
213  XPETRA_FACTORY_END;
214  }
215 
216  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, const RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> >& map, std::vector<size_t>& stridingInfo, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset = 0) {
217 
218 #ifdef HAVE_XPETRA_TPETRA
219  if (lib == UseTpetra) {
220  Teuchos::RCP<const TpetraMap<LocalOrdinal,GlobalOrdinal,Node> > tmap = Teuchos::rcp_dynamic_cast<const TpetraMap<LocalOrdinal, GlobalOrdinal, Node> >(map);
221  TEUCHOS_TEST_FOR_EXCEPTION(tmap == Teuchos::null, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: bad cast. map is not a TpetraMap.");
222  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (tmap->getTpetra_Map(), stridingInfo, stridedBlockId, offset) );
223  }
224 #endif
225 
226 #ifdef HAVE_XPETRA_EPETRA
227  if (lib == UseEpetra) {
228  Teuchos::RCP<const EpetraMap> emap = Teuchos::rcp_dynamic_cast<const EpetraMap>(map);
229  TEUCHOS_TEST_FOR_EXCEPTION(emap == Teuchos::null, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: bad cast. map is not a EpetraMap.");
230  return rcp( new StridedEpetraMap(Teuchos::rcpFromRef(emap->getEpetra_Map()), stridingInfo,stridedBlockId, offset) ); // ugly: EpetraMap and TpetraMap differ here
231  }
232 #endif
233 
234  XPETRA_FACTORY_END;
235  }
236 
237  // special constructor for generating a given subblock of a strided map
238  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(const RCP<const StridedMap<LocalOrdinal, GlobalOrdinal, Node> >& map, LocalOrdinal stridedBlockId) {
239  TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockId < 0, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: constructor expects stridedBlockId > -1.");
240  //typename Teuchos::ArrayView< const GlobalOrdinal >::iterator it;
241 #ifdef HAVE_XPETRA_TPETRA
242  if (map->lib() == UseTpetra) {
243  TEUCHOS_TEST_FOR_EXCEPTION(map->getStridedBlockId() != -1, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: constructor expects a full map (stridedBlockId == -1).");
244  std::vector<size_t> stridingInfo = map->getStridingData();
245 
247  Teuchos::ArrayView< const GlobalOrdinal > dofGids = map->getNodeElementList();
248  //std::sort(dofGids.begin(),dofGids.end()); // TODO: do i need this?
249 
250  // determine nStridedOffset
251  size_t nStridedOffset = 0;
252  for(int j=0; j<map->getStridedBlockId(); j++) {
253  nStridedOffset += stridingInfo[j];
254  }
255 
256  size_t numMyBlockDofs = stridingInfo[stridedBlockId] / map->getFixedBlockSize() * map->getNodeNumElements();
257  std::vector<GlobalOrdinal> subBlockDofGids(numMyBlockDofs);
258 
259  // TODO fill vector with dofs
260 
261  for(Teuchos::ArrayView< const GlobalOrdinal >::iterator it = dofGids.begin(); it!=dofGids.end(); ++it) {
262  if(map->GID2StridingBlockId( *it ) == Teuchos::as<size_t>(stridedBlockId)) {
263  subBlockDofGids.push_back( *it );
264  }
265  }
266 
267  const Teuchos::ArrayView<const LocalOrdinal> subBlockDofGids_view(&subBlockDofGids[0],subBlockDofGids.size());
268 
269  // call constructor for TpetraMap
270  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (/*subBlockDofGids.size()*/Teuchos::OrdinalTraits<global_size_t>::invalid(), subBlockDofGids_view, map->getIndexBase(), stridingInfo, map->getComm(), stridedBlockId, map->getNode()) );
272 
273  }
274 #endif
275 #ifdef HAVE_XPETRA_EPETRA
276  if (map->lib() == UseEpetra) {
277  TEUCHOS_TEST_FOR_EXCEPTION(map->getStridedBlockId() != -1, Exceptions::RuntimeError,"Xpetra::StridedMapFactory::Build: constructor expects a full map (stridedBlockId == -1).");
278  std::vector<size_t> stridingInfo = map->getStridingData();
279 
281  Teuchos::ArrayView< const GlobalOrdinal > dofGids = map->getNodeElementList();
282  //std::sort(dofGids.begin(),dofGids.end()); // TODO: do i need this?
283 
284  // determine nStridedOffset
285  size_t nStridedOffset = 0;
286  for(int j=0; j<map->getStridedBlockId(); j++) {
287  nStridedOffset += stridingInfo[j];
288  }
289 
290  size_t numMyBlockDofs = stridingInfo[stridedBlockId] / map->getFixedBlockSize() * map->getNodeNumElements();
291  std::vector<GlobalOrdinal> subBlockDofGids(numMyBlockDofs);
292 
293  // TODO fill vector with dofs
294  //Teuchos::ArrayView< const GlobalOrdinal >::iterator it;
295  for(Teuchos::ArrayView< const GlobalOrdinal >::iterator it = dofGids.begin(); it!=dofGids.end(); ++it) {
296  if(map->GID2StridingBlockId( *it ) == Teuchos::as<size_t>(stridedBlockId)) {
297  subBlockDofGids.push_back( *it );
298  }
299  }
300 
301  const Teuchos::ArrayView<const LocalOrdinal> subBlockDofGids_view(&subBlockDofGids[0],subBlockDofGids.size());
302 
303  // call constructor for TpetraMap
304  return rcp( new StridedEpetraMap(Teuchos::OrdinalTraits<global_size_t>::invalid(), subBlockDofGids_view, map->getIndexBase(), stridingInfo, map->getComm(), stridedBlockId, map->getNode()) );
306 
307  }
308 #endif
309  XPETRA_FACTORY_END;
310  }
311 
312 #if 0 // TODO
313  static RCP<StridedMap<LocalOrdinal,GlobalOrdinal, Node> > Build(UnderlyingLib lib, global_size_t numGlobalElements, const Teuchos::ArrayView<const int> &elementList, int indexBase, const Teuchos::RCP<const Teuchos::Comm<int> > &comm, const Teuchos::RCP<Kokkos::DefaultNode::DefaultNodeType> &node = Kokkos::Details::getNode<Node> ()) {
314 #ifdef HAVE_XPETRA_TPETRA
315  if (lib == UseTpetra)
316  return rcp( new StridedTpetraMap<LocalOrdinal,GlobalOrdinal, Node> (numGlobalElements, elementList, indexBase, comm, node) );
317 #endif
318 
319 #ifdef HAVE_XPETRA_EPETRA
320  if (lib == UseEpetra)
321  return rcp( new StridedEpetraMap(numGlobalElements, elementList, indexBase, comm, node) );
322 #endif
323  XPETRA_FACTORY_END;
324  }
325 #endif
326 
327 
328  };
329 
330 }
331 
332 #define XPETRA_STRIDEDMAPFACTORY_SHORT
333 #endif
334 //TODO: removed unused methods
static RCP< StridedMap< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, std::vector< size_t > &stridingInfo, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset=0)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Exception throws to report errors in the internal logical of the program.
#define XPETRA_FACTORY_ERROR_IF_EPETRA(lib)
static RCP< StridedMap< LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const StridedMap< LocalOrdinal, GlobalOrdinal, Node > > &map, LocalOrdinal stridedBlockId)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static Teuchos::RCP< StridedMap< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset=0, LocalGlobal lg=Xpetra::GloballyDistributed, const Teuchos::RCP< Node > &node=Kokkos::Details::getNode< Node >())
Map constructor with Xpetra-defined contiguous uniform distribution.
static Teuchos::RCP< StridedMap< LocalOrdinal, GlobalOrdinal, Node > > Build(UnderlyingLib lib, global_size_t numGlobalElements, size_t numLocalElements, GlobalOrdinal indexBase, std::vector< size_t > &stridingInfo, const Teuchos::RCP< const Teuchos::Comm< int > > &comm, LocalOrdinal stridedBlockId=-1, GlobalOrdinal offset=0, const Teuchos::RCP< Node > &node=Kokkos::Details::getNode< Node >())
Map constructor with a user-defined contiguous distribution.
#define XPETRA_FACTORY_END
size_t global_size_t
Global size_t object.
StridedMapFactory()
Private constructor. This is a static class.
Class that stores a strided map.