All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Xpetra_MapExtractor.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //             Xpetra: A linear algebra interface package
00006 //                  Copyright 2012 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact
00039 //                    Jonathan Hu       (jhu@sandia.gov)
00040 //                    Andrey Prokopenko (aprokop@sandia.gov)
00041 //                    Ray Tuminaro      (rstumin@sandia.gov)
00042 //
00043 // ***********************************************************************
00044 //
00045 // @HEADER
00046 #ifndef XPETRA_MAPEXTRACTOR_HPP_
00047 #define XPETRA_MAPEXTRACTOR_HPP_
00048 
00049 #include <map>
00050 
00051 #include <iostream>
00052 
00053 #include <Teuchos_RCP.hpp>
00054 #include <Teuchos_Describable.hpp>
00055 #include <Xpetra_Import.hpp>
00056 #include <Xpetra_Map.hpp>
00057 
00058 #include "Xpetra_Import.hpp"
00059 #include "Xpetra_ImportFactory.hpp"
00060 #include "Xpetra_MultiVector.hpp"
00061 #include "Xpetra_MultiVectorFactory.hpp"
00062 #include "Xpetra_Vector.hpp"
00063 #include "Xpetra_VectorFactory.hpp"
00064 
00065 
00066 namespace Xpetra {
00067 
00068   template <class Scalar = MultiVector<>::scalar_type,
00069             class LocalOrdinal = Map<>::local_ordinal_type,
00070             class GlobalOrdinal = typename Map<LocalOrdinal>::global_ordinal_type,
00071             class Node = typename Map<LocalOrdinal, GlobalOrdinal>::node_type>
00072   class MapExtractor : public Teuchos::Describable {
00073   public:
00074     typedef Scalar scalar_type;
00075     typedef LocalOrdinal local_ordinal_type;
00076     typedef GlobalOrdinal global_ordinal_type;
00077     typedef Node node_type;
00078 
00079   private:
00080 #undef XPETRA_MAPEXTRACTOR_SHORT
00081 #include "Xpetra_UseShortNames.hpp"
00082 
00083   public:
00084 
00086     MapExtractor(const RCP<const Map>& fullmap, const std::vector<RCP<const Map> >& maps) {
00087       fullmap_ = fullmap;
00088       maps_ = maps;
00089 
00090       importers_.resize(maps_.size());
00091       for (unsigned i = 0; i < maps_.size(); ++i)
00092         if (maps[i] != null)
00093           importers_[i] = ImportFactory::Build(fullmap_, maps[i]);
00094 
00095       TEUCHOS_TEST_FOR_EXCEPTION(CheckConsistency() == false, std::logic_error,
00096                                  "logic error. full map and sub maps are inconsistently distributed over the processors.");
00097     }
00098 
00101     void ExtractVector(const Vector& full, size_t block, Vector& partial) const {
00102       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00103             "ExtractVector: maps_[" << block << "] is null");
00104 
00105       partial.doImport(full, *importers_[block], Xpetra::INSERT);
00106     }
00107     void ExtractVector(const MultiVector& full, size_t block, MultiVector& partial) const {
00108       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00109             "ExtractVector: maps_[" << block << "] is null");
00110 
00111       partial.doImport(full, *importers_[block], Xpetra::INSERT);
00112     }
00113     void ExtractVector(RCP<const      Vector>& full, size_t block, RCP<     Vector>& partial) const { ExtractVector(*full, block, *partial); }
00114     void ExtractVector(RCP<           Vector>& full, size_t block, RCP<     Vector>& partial) const { ExtractVector(*full, block, *partial); }
00115     void ExtractVector(RCP<const MultiVector>& full, size_t block, RCP<MultiVector>& partial) const { ExtractVector(*full, block, *partial); }
00116     void ExtractVector(RCP<      MultiVector>& full, size_t block, RCP<MultiVector>& partial) const { ExtractVector(*full, block, *partial); }
00117 
00118     RCP<     Vector> ExtractVector(RCP<const      Vector>& full, size_t block) const {
00119       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00120             "ExtractVector: maps_[" << block << "] is null");
00121       const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
00122       ExtractVector(*full, block, *ret);
00123       return ret;
00124     }
00125     RCP<     Vector> ExtractVector(RCP<           Vector>& full, size_t block) const {
00126       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00127             "ExtractVector: maps_[" << block << "] is null");
00128       const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
00129       ExtractVector(*full, block, *ret);
00130       return ret;
00131     }
00132     RCP<MultiVector> ExtractVector(RCP<const MultiVector>& full, size_t block) const {
00133       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00134             "ExtractVector: maps_[" << block << "] is null");
00135       const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
00136       ExtractVector(*full, block, *ret);
00137       return ret;
00138     }
00139     RCP<MultiVector> ExtractVector(RCP<      MultiVector>& full, size_t block) const {
00140       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00141             "ExtractVector: maps_[" << block << "] is null");
00142       const RCP<Vector> ret = VectorFactory::Build(getMap(block), true);
00143       ExtractVector(*full, block, *ret);
00144       return ret;
00145     }
00147 
00150     void InsertVector(const Vector& partial, size_t block, Vector& full) const {
00151       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00152             "InsertVector: maps_[" << block << "] is null");
00153 
00154       full.doExport(partial, *importers_[block], Xpetra::INSERT);
00155     }
00156     void InsertVector(const MultiVector& partial, size_t block, MultiVector& full) const {
00157       TEUCHOS_TEST_FOR_EXCEPTION(maps_[block] == null, Xpetra::Exceptions::RuntimeError,
00158             "InsertVector: maps_[" << block << "] is null");
00159 
00160       full.doExport(partial, *importers_[block], Xpetra::INSERT);
00161     }
00162 
00163     void InsertVector(RCP<const      Vector>& partial, size_t block, RCP<     Vector>& full) const { InsertVector(*partial, block, *full); }
00164     void InsertVector(RCP<           Vector>& partial, size_t block, RCP<     Vector>& full) const { InsertVector(*partial, block, *full); }
00165     void InsertVector(RCP<const MultiVector>& partial, size_t block, RCP<MultiVector>& full) const { InsertVector(*partial, block, *full); }
00166     void InsertVector(RCP<      MultiVector>& partial, size_t block, RCP<MultiVector>& full) const { InsertVector(*partial, block, *full); }
00167 
00168 
00170 
00171     RCP<     Vector> getVector(size_t i) const                { return      VectorFactory::Build(getMap(i), true); }
00172     RCP<MultiVector> getVector(size_t i, size_t numvec) const { return MultiVectorFactory::Build(getMap(i), numvec, true); }
00173 
00176 
00178     size_t NumMaps() const { return maps_.size(); }
00179 
00181     const RCP<const Map> getMap(size_t i) const { return maps_[i]; }
00182 
00184     const RCP<const Map> getFullMap() const { return fullmap_; }
00185 
00187     size_t getMapIndexForGID(GlobalOrdinal gid) const {
00188       for (size_t i = 0; i < NumMaps(); i++)
00189         if (getMap(i)->isNodeGlobalElement(gid) == true)
00190           return i;
00191 
00192       TEUCHOS_TEST_FOR_EXCEPTION(false, Xpetra::Exceptions::RuntimeError,
00193                                  "getMapIndexForGID: GID " << gid << " is not contained by a map in mapextractor." );
00194       return 0;
00195     }
00196 
00198 
00199   private:
00200     bool CheckConsistency() const {
00201       const RCP<const Map> fullMap = getFullMap();
00202 
00203       for (size_t i = 0; i < NumMaps(); i++) {
00204         const RCP<const Map> map = getMap(i);
00205 
00206         ArrayView<const GlobalOrdinal> mapGids = map->getNodeElementList();
00207         for (typename ArrayView< const GlobalOrdinal >::const_iterator it = mapGids.begin(); it != mapGids.end(); it++)
00208           if (fullMap->isNodeGlobalElement(*it) == false)
00209             return false; // Global ID (*it) not found locally on this proc in fullMap -> error
00210       }
00211       return true;
00212     }
00213 
00214   private:
00215     RCP<const Map >               fullmap_;
00216     std::vector<RCP<const Map > > maps_;
00217     std::vector<RCP<Import > >    importers_;
00218   };
00219 }
00220 
00221 #define XPETRA_MAPEXTRACTOR_SHORT
00222 #endif /* XPETRA_MAPEXTRACTOR_HPP_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines