Xpetra_MapExtractor.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_MAPEXTRACTOR_HPP_
47 #define XPETRA_MAPEXTRACTOR_HPP_
48 
49 #include <map>
50 
51 #include <iostream>
52 
53 #include <Teuchos_RCP.hpp>
54 #include <Teuchos_Describable.hpp>
55 #include <Xpetra_Import.hpp>
56 #include <Xpetra_Map.hpp>
57 
58 #include "Xpetra_Import.hpp"
59 #include "Xpetra_ImportFactory.hpp"
60 #include "Xpetra_MultiVector.hpp"
62 #include "Xpetra_Vector.hpp"
63 #include "Xpetra_VectorFactory.hpp"
64 
65 
66 namespace Xpetra {
67 
68  template <class Scalar = MultiVector<>::scalar_type,
69  class LocalOrdinal = Map<>::local_ordinal_type,
70  class GlobalOrdinal = typename Map<LocalOrdinal>::global_ordinal_type,
71  class Node = typename Map<LocalOrdinal, GlobalOrdinal>::node_type>
72  class MapExtractor : public Teuchos::Describable {
73  public:
74  typedef Scalar scalar_type;
75  typedef LocalOrdinal local_ordinal_type;
76  typedef GlobalOrdinal global_ordinal_type;
77  typedef Node node_type;
78 
79  private:
80 #undef XPETRA_MAPEXTRACTOR_SHORT
81 #include "Xpetra_UseShortNames.hpp"
82 
83  public:
84 
86  MapExtractor(const RCP<const Map>& fullmap, const std::vector<RCP<const Map> >& maps) {
87  fullmap_ = fullmap;
88  maps_ = maps;
89  importers_.resize(maps_.size());
90  for (unsigned i = 0; i < maps_.size(); ++i)
91  if (maps[i] != null)
93  TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency() == false, std::logic_error,
94  "logic error. full map and sub maps are inconsistently distributed over the processors.");
95  }
96 
99  void ExtractVector(const Vector& full, size_t block, Vector& partial) const {
101  "ExtractVector: maps_[" << block << "] is null");
102 
103  partial.doImport(full, *importers_[block], Xpetra::INSERT);
104  }
105  void ExtractVector(const MultiVector& full, size_t block, MultiVector& partial) const {
107  "ExtractVector: maps_[" << block << "] is null");
108 
109  partial.doImport(full, *importers_[block], Xpetra::INSERT);
110  }
111  void ExtractVector(RCP<const Vector>& full, size_t block, RCP< Vector>& partial) const { ExtractVector(*full, block, *partial); }
112  void ExtractVector(RCP< Vector>& full, size_t block, RCP< Vector>& partial) const { ExtractVector(*full, block, *partial); }
113  void ExtractVector(RCP<const MultiVector>& full, size_t block, RCP<MultiVector>& partial) const { ExtractVector(*full, block, *partial); }
114  void ExtractVector(RCP< MultiVector>& full, size_t block, RCP<MultiVector>& partial) const { ExtractVector(*full, block, *partial); }
115 
116  RCP< Vector> ExtractVector(RCP<const Vector>& full, size_t block) const {
118  "ExtractVector: maps_[" << block << "] is null");
119  const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
120  ExtractVector(*full, block, *ret);
121  return ret;
122  }
123  RCP< Vector> ExtractVector(RCP< Vector>& full, size_t block) const {
125  "ExtractVector: maps_[" << block << "] is null");
126  const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
127  ExtractVector(*full, block, *ret);
128  return ret;
129  }
132  "ExtractVector: maps_[" << block << "] is null");
133  const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
134  ExtractVector(*full, block, *ret);
135  return ret;
136  }
137  RCP<MultiVector> ExtractVector(RCP< MultiVector>& full, size_t block) const {
139  "ExtractVector: maps_[" << block << "] is null");
140  const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
141  ExtractVector(*full, block, *ret);
142  return ret;
143  }
145 
148  void InsertVector(const Vector& partial, size_t block, Vector& full) const {
150  "InsertVector: maps_[" << block << "] is null");
151 
152  full.doExport(partial, *importers_[block], Xpetra::INSERT);
153  }
154  void InsertVector(const MultiVector& partial, size_t block, MultiVector& full) const {
156  "InsertVector: maps_[" << block << "] is null");
157 
158  full.doExport(partial, *importers_[block], Xpetra::INSERT);
159  }
160 
161  void InsertVector(RCP<const Vector>& partial, size_t block, RCP< Vector>& full) const { InsertVector(*partial, block, *full); }
162  void InsertVector(RCP< Vector>& partial, size_t block, RCP< Vector>& full) const { InsertVector(*partial, block, *full); }
163  void InsertVector(RCP<const MultiVector>& partial, size_t block, RCP<MultiVector>& full) const { InsertVector(*partial, block, *full); }
164  void InsertVector(RCP< MultiVector>& partial, size_t block, RCP<MultiVector>& full) const { InsertVector(*partial, block, *full); }
165 
166 
168 
169  RCP< Vector> getVector(size_t i) const { return VectorFactory::Build(getMap(i), true); }
170  RCP<MultiVector> getVector(size_t i, size_t numvec) const { return MultiVectorFactory::Build(getMap(i), numvec, true); }
171 
174 
176  size_t NumMaps() const { return maps_.size(); }
177 
179  const RCP<const Map> getMap(size_t i) const { return maps_[i]; }
180 
182  const RCP<const Map> getFullMap() const { return fullmap_; }
183 
185  size_t getMapIndexForGID(GlobalOrdinal gid) const {
186  for (size_t i = 0; i < NumMaps(); i++)
187  if (getMap(i)->isNodeGlobalElement(gid) == true)
188  return i;
189 
191  "getMapIndexForGID: GID " << gid << " is not contained by a map in mapextractor." );
192  return 0;
193  }
194 
196 
197  private:
198  bool CheckConsistency() const {
199  const RCP<const Map> fullMap = getFullMap();
200 
201  for (size_t i = 0; i < NumMaps(); i++) {
202  const RCP<const Map> map = getMap(i);
203 
204  ArrayView<const GlobalOrdinal> mapGids = map->getNodeElementList();
205  for (typename ArrayView< const GlobalOrdinal >::const_iterator it = mapGids.begin(); it != mapGids.end(); it++)
206  if (fullMap->isNodeGlobalElement(*it) == false)
207  return false; // Global ID (*it) not found locally on this proc in fullMap -> error
208  }
209  return true;
210  }
211 
212  private:
214  std::vector<RCP<const Map > > maps_;
215  std::vector<RCP<Import > > importers_;
216  };
217 }
218 
219 #define XPETRA_MAPEXTRACTOR_SHORT
220 #endif /* XPETRA_MAPEXTRACTOR_HPP_ */
GlobalOrdinal global_ordinal_type
void ExtractVector(RCP< const Vector > &full, size_t block, RCP< Vector > &partial) const
static RCP< Import< LocalOrdinal, GlobalOrdinal, Node > > Build(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &source, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &target)
Constructor specifying the number of non-zeros for all rows.
void InsertVector(RCP< const MultiVector > &partial, size_t block, RCP< MultiVector > &full) const
LocalOrdinal local_ordinal_type
Definition: Xpetra_Map.hpp:70
size_t getMapIndexForGID(GlobalOrdinal gid) const
returns map index in map extractor which contains GID or -1 otherwise
virtual void doExport(const DistObject< Packet, LocalOrdinal, GlobalOrdinal, Node > &source, const Export< LocalOrdinal, GlobalOrdinal, Node > &exporter, CombineMode CM)=0
Export data into this object using an Export object ("forward mode").
void ExtractVector(const MultiVector &full, size_t block, MultiVector &partial) const
GlobalOrdinal global_ordinal_type
Definition: Xpetra_Map.hpp:71
void ExtractVector(RCP< const MultiVector > &full, size_t block, RCP< MultiVector > &partial) const
RCP< MultiVector > getVector(size_t i, size_t numvec) const
iterator begin() const
RCP< Vector > getVector(size_t i) const
RCP< MultiVector > ExtractVector(RCP< MultiVector > &full, size_t block) const
Node node_type
Definition: Xpetra_Map.hpp:72
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
const_pointer const_iterator
const RCP< const Map > getFullMap() const
the full map
Xpetra namespace
void InsertVector(const Vector &partial, size_t block, Vector &full) const
Exception throws to report errors in the internal logical of the program.
void InsertVector(RCP< const Vector > &partial, size_t block, RCP< Vector > &full) const
std::vector< RCP< const Map > > maps_
MapExtractor(const RCP< const Map > &fullmap, const std::vector< RCP< const Map > > &maps)
MapExtractor basic constructor.
RCP< Vector > ExtractVector(RCP< Vector > &full, size_t block) const
void InsertVector(RCP< MultiVector > &partial, size_t block, RCP< MultiVector > &full) const
virtual void doImport(const DistObject< Packet, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)=0
Import data into this object using an Import object ("forward mode").
RCP< Vector > ExtractVector(RCP< const Vector > &full, size_t block) const
const RCP< const Map > getMap(size_t i) const
get the map
void InsertVector(RCP< Vector > &partial, size_t block, RCP< Vector > &full) const
std::vector< RCP< Import > > importers_
static RCP< Vector > Build(const Teuchos::RCP< const Map > &map, bool zeroOut=true)
Constructor specifying the number of non-zeros for all rows.
void InsertVector(const MultiVector &partial, size_t block, MultiVector &full) const
void ExtractVector(const Vector &full, size_t block, Vector &partial) const
iterator end() const
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t NumVectors, bool zeroOut=true)
Constructor specifying the number of non-zeros for all rows.
void ExtractVector(RCP< MultiVector > &full, size_t block, RCP< MultiVector > &partial) const
size_t NumMaps() const
number of partial maps
void ExtractVector(RCP< Vector > &full, size_t block, RCP< Vector > &partial) const
RCP< MultiVector > ExtractVector(RCP< const MultiVector > &full, size_t block) const