All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
Xpetra_StridedMapFactory.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 
00047 // WARNING: This code is experimental. Backwards compatibility should not be expected.
00048 
00049 #ifndef XPETRA_STRIDEDMAPFACTORY_HPP
00050 #define XPETRA_STRIDEDMAPFACTORY_HPP
00051 
00052 #include <Kokkos_DefaultNode.hpp>
00053 
00054 #include "Xpetra_ConfigDefs.hpp"
00055 #include "Xpetra_Exceptions.hpp"
00056 
00057 #include "Xpetra_StridedMap.hpp"
00058 
00059 // 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).
00060 
00061 namespace Xpetra {
00062 
00063   template <class LocalOrdinal = StridedMap<>::local_ordinal_type,
00064             class GlobalOrdinal =
00065               typename StridedMap<LocalOrdinal>::global_ordinal_type,
00066             class Node =
00067               typename StridedMap<LocalOrdinal, GlobalOrdinal>::node_type>
00068   class StridedMapFactory {
00069 #undef XPETRA_STRIDEDMAPFACTORY_SHORT
00070 #include "Xpetra_UseShortNamesOrdinal.hpp"
00071 
00072   private:
00074     StridedMapFactory() {}
00075 
00076   public:
00077 
00078     static Teuchos::RCP<Node> defaultArgNode() {
00079         // Workaround function for a deferred visual studio bug
00080         // http://connect.microsoft.com/VisualStudio/feedback/details/719847/erroneous-error-c2783-could-not-deduce-template-argument
00081         // Use this function for default arguments rather than calling
00082         // what is the return value below.  Also helps in reducing
00083         // duplication in various constructors.
00084         return KokkosClassic::Details::getNode<Node>();
00085     }
00086 
00088     static RCP<StridedMap> Build(UnderlyingLib lib, global_size_t numGlobalElements, GlobalOrdinal indexBase,
00089         std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00090         LocalOrdinal stridedBlockId = -1, GlobalOrdinal offset = 0, LocalGlobal lg = Xpetra::GloballyDistributed,
00091         const Teuchos::RCP<Node> &node = defaultArgNode()) {
00092 
00093       return rcp(new StridedMap(lib, numGlobalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, lg, node));
00094     }
00095 
00097     static RCP<StridedMap> Build(UnderlyingLib lib, global_size_t numGlobalElements, size_t numLocalElements, GlobalOrdinal indexBase,
00098         std::vector<size_t>& stridingInfo, const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00099         LocalOrdinal stridedBlockId = -1, GlobalOrdinal offset = 0,
00100         const Teuchos::RCP<Node> &node = defaultArgNode()) {
00101 
00102       return rcp(new StridedMap(lib, numGlobalElements, numLocalElements, indexBase, stridingInfo, comm, stridedBlockId, offset, node));
00103     }
00104 
00105     static RCP<StridedMap> Build(const RCP<const Map>& map, std::vector<size_t>& stridingInfo, LocalOrdinal stridedBlockId = -1, GlobalOrdinal offset = 0) {
00106       return rcp(new StridedMap(map, stridingInfo, map->getIndexBase(), stridedBlockId, offset));
00107     }
00108 
00109     // special constructor for generating a given subblock of a strided map
00110     static RCP<StridedMap> Build(const RCP<const StridedMap>& map, LocalOrdinal stridedBlockId) {
00111       TEUCHOS_TEST_FOR_EXCEPTION(stridedBlockId < 0, Exceptions::RuntimeError,
00112                                  "Xpetra::StridedMapFactory::Build: constructor expects stridedBlockId > -1.");
00113       TEUCHOS_TEST_FOR_EXCEPTION(map->getStridedBlockId() != -1, Exceptions::RuntimeError,
00114                                  "Xpetra::StridedMapFactory::Build: constructor expects a full map (stridedBlockId == -1).");
00115 
00116       std::vector<size_t> stridingInfo = map->getStridingData();
00117 
00118       Teuchos::ArrayView<const GlobalOrdinal> dofGids = map->getNodeElementList();
00119       // std::sort(dofGids.begin(),dofGids.end()); // TODO: do we need this?
00120 
00121       // determine nStridedOffset
00122       size_t nStridedOffset = 0;
00123       for (int j = 0; j < map->getStridedBlockId(); j++)
00124         nStridedOffset += stridingInfo[j];
00125 
00126       size_t numMyBlockDofs = (stridingInfo[stridedBlockId] * map->getNodeNumElements()) / map->getFixedBlockSize();
00127       std::vector<GlobalOrdinal> subBlockDofGids(numMyBlockDofs);
00128 
00129       // TODO fill vector with dofs
00130       LocalOrdinal ind = 0;
00131       for (typename Teuchos::ArrayView< const GlobalOrdinal >::iterator it = dofGids.begin(); it!=dofGids.end(); ++it)
00132         if (map->GID2StridingBlockId(*it) == Teuchos::as<size_t>(stridedBlockId))
00133           subBlockDofGids[ind++] = *it;
00134 
00135       const Teuchos::ArrayView<const GlobalOrdinal> subBlockDofGids_view(&subBlockDofGids[0],subBlockDofGids.size());
00136 
00137       return rcp(new StridedMap(map->lib(), Teuchos::OrdinalTraits<global_size_t>::invalid(), subBlockDofGids_view, map->getIndexBase(), stridingInfo, map->getComm(), stridedBlockId, map->getNode()));
00138     }
00139 
00141     static RCP<StridedMap> Build(const StridedMap& map) {
00142       XPETRA_MONITOR("MapFactory::Build");
00143 
00144       LocalOrdinal N = map.getNodeNumElements();
00145       Teuchos::ArrayView<const GlobalOrdinal> oldElements = map.getNodeElementList();
00146       Teuchos::Array<GlobalOrdinal> newElements(map.getNodeNumElements());
00147       for (LocalOrdinal i = 0; i < N; i++)
00148         newElements[i] = oldElements[i];
00149 
00150       std::vector<size_t> strData = map.getStridingData();
00151       return rcp(new StridedMap(map.lib(), map.getGlobalNumElements(), newElements, map.getIndexBase(), strData, map.getComm(), map.getStridedBlockId(), map.getNode()));
00152 
00153       //XPETRA_FACTORY_END;
00154     }
00155 
00157     static RCP<StridedMap>
00158     Build (UnderlyingLib lib,
00159            global_size_t numGlobalElements,
00160            const Teuchos::ArrayView<const GlobalOrdinal> &elementList,
00161            GlobalOrdinal indexBase,
00162            std::vector<size_t>& stridingInfo,
00163            const Teuchos::RCP<const Teuchos::Comm<int> > &comm,
00164            LocalOrdinal stridedBlockId = -1, // FIXME (mfh 03 Sep 2014) This breaks if LocalOrdinal is unsigned
00165            GlobalOrdinal offset = 0,
00166            const Teuchos::RCP<Node> &node = defaultArgNode())
00167     {
00168       return rcp (new StridedMap (lib, numGlobalElements, elementList,
00169                                   indexBase, stridingInfo, comm,
00170                                   stridedBlockId, node));
00171     }
00172   };
00173 }
00174 
00175 #define XPETRA_STRIDEDMAPFACTORY_SHORT
00176 #endif
00177 //TODO: removed unused methods
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines