MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_CloneRepartitionInterface_def.hpp
Go to the documentation of this file.
1// @HEADER
2//
3// ***********************************************************************
4//
5// MueLu: A package for multigrid based preconditioning
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 PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_
47#define PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_
48
49#include <Xpetra_MultiVectorFactory.hpp>
50#include <Xpetra_VectorFactory.hpp>
51
53
54#include "MueLu_Level.hpp"
55#include "MueLu_Exceptions.hpp"
56#include "MueLu_Monitor.hpp"
57
58
59namespace MueLu {
60
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63 Teuchos::RCP<ParameterList> validParamList = rcp(new ParameterList());
64 validParamList->set< Teuchos::RCP<const FactoryBase> >("A", Teuchos::null, "Factory of the matrix A");
65 validParamList->set< Teuchos::RCP<const FactoryBase> >("number of partitions", Teuchos::null, "Instance of RepartitionHeuristicFactory.");
66 validParamList->set< Teuchos::RCP<const FactoryBase> >("Partition", Teuchos::null, "Factory generating the Partition array (e.g. ZoltanInterface)");
67
68 return validParamList;
69 }
70
71
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74 Input(currentLevel, "A");
75 Input(currentLevel, "Partition");
76 } //DeclareInput()
77
78 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
80 FactoryMonitor m(*this, "Build", currentLevel);
81 currentLevel.print(GetOStream(Statistics0,0));
82 // extract blocked operator A from current level
83 Teuchos::RCP<Matrix> A = Get< Teuchos::RCP<Matrix> > (currentLevel, "A");
84 Teuchos::RCP<const Teuchos::Comm< int > > comm = A->getRowMap()->getComm();
85
86 // number of Partitions only used for a shortcut.
87 if (currentLevel.IsAvailable("number of partitions")) {
88 GetOStream(Warnings0) << "Using user-provided \"number of partitions\", the performance is unknown" << std::endl;
89
90 }
91
92 // ======================================================================================================
93 // Construct decomposition vector
94 // ======================================================================================================
95 RCP<GOVector> decomposition = Teuchos::null;
96
97 // extract decomposition vector
98 decomposition = Get<RCP<GOVector> >(currentLevel, "Partition");
99 ArrayRCP<const GO> decompEntries = decomposition->getData(0);
100
101 if (decomposition.is_null()) {
102 GetOStream(Warnings0) << "No repartitioning necessary: partitions were left unchanged by the repartitioner" << std::endl;
103 Set<RCP<const Import> >(currentLevel, "Importer", Teuchos::null);
104 return;
105 }
106
107 // create new decomposition vector
108 Teuchos::RCP<GOVector> ret = Xpetra::VectorFactory<GO, LO, GO, NO>::Build(A->getRowMap(), false);
109 ArrayRCP<GO> retDecompEntries = ret->getDataNonConst(0);
110
111 // block size of output vector
112 LocalOrdinal blkSize = 1;
113
114 // check for blocking/striding information
115 if(A->IsView("stridedMaps") &&
116 Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap("stridedMaps")) != Teuchos::null) {
117 Xpetra::viewLabel_t oldView = A->SwitchToView("stridedMaps"); // note: "stridedMaps are always non-overlapping (correspond to range and domain maps!)
118 RCP<const StridedMap> strMap = Teuchos::rcp_dynamic_cast<const StridedMap>(A->getRowMap());
119 TEUCHOS_TEST_FOR_EXCEPTION(strMap == Teuchos::null,Exceptions::BadCast,"MueLu::CloneRepartitionInterface::Build: cast to strided row map failed.");
120 LocalOrdinal stridedBlock = strMap->getStridedBlockId();
121 if (stridedBlock == -1)
122 blkSize = strMap->getFixedBlockSize();
123 else {
124 std::vector<size_t> strInfo = strMap->getStridingData();
125 blkSize = strInfo[stridedBlock];
126 }
127 oldView = A->SwitchToView(oldView);
128 GetOStream(Statistics1) << "CloneRepartitionInterface::Build():" << " found blockdim=" << blkSize << " from strided maps."<< std::endl;
129 } else {
130 GetOStream(Statistics1) << "CloneRepartitionInterface::Build(): no striding information available. Use blockdim=" << blkSize << " (DofsPerNode)." << std::endl;
131 blkSize = A->GetFixedBlockSize();
132 }
133
134 // plausibility check!
135 size_t inLocalLength = decomposition->getLocalLength();
136 size_t outLocalLength = A->getRowMap()->getLocalNumElements();
137
138 // only for non-strided maps
139 size_t numLocalNodes = outLocalLength / blkSize;
140 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(outLocalLength % blkSize) != 0, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: inconsistent number of local DOFs (" << outLocalLength << ") and degrees of freedoms (" << blkSize <<")");
141
142 if (numLocalNodes > 0) {
143 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::as<size_t>(inLocalLength % numLocalNodes) != 0, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: inconsistent number of local DOFs (" << inLocalLength << ") and number of local nodes (" << numLocalNodes << ")");
144 LocalOrdinal inBlkSize = Teuchos::as<LocalOrdinal>(inLocalLength / numLocalNodes);
145 //TEUCHOS_TEST_FOR_EXCEPTION(blkSize != inBlkSize, MueLu::Exceptions::RuntimeError,"CloneRepartitionInterface: input block size = " << inBlkSize << " outpub block size = " << blkSize << ". They should be the same.");
146
147 for(LO i = 0; i<Teuchos::as<LO>(numLocalNodes); i++) {
148 for(LO j = 0; j < blkSize; j++) {
149 retDecompEntries[i*blkSize + j] = Teuchos::as<GO>(decompEntries[i*inBlkSize]);
150 }
151 }
152 } // end if numLocalNodes > 0
153 Set(currentLevel, "Partition", ret);
154 } //Build()
155
156
157
158} //namespace MueLu
159
160#endif /* PACKAGES_MUELU_SRC_REBALANCING_MUELU_CLONEREPARTITIONINTERFACE_DEF_HPP_ */
MueLu::DefaultLocalOrdinal LocalOrdinal
void DeclareInput(Level &level) const
Specifies the data that this class needs, and the factories that generate that data.
void Build(Level &level) const
Build an object with this factory.
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
Exception indicating invalid cast attempted.
Exception throws to report errors in the internal logical of the program.
Timer to be used in factories. Similar to Monitor but with additional timers.
void Input(Level &level, const std::string &varName) const
T Get(Level &level, const std::string &varName) const
void Set(Level &level, const std::string &varName, const T &data) const
Class that holds all level-specific information.
bool IsAvailable(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need's value has been saved.
void print(std::ostream &out, const VerbLevel verbLevel=Default) const
Printing method.
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Namespace for MueLu classes and methods.
@ Warnings0
Important warning messages (one line).
@ Statistics1
Print more statistics.
@ Statistics0
Print statistics that do not involve significant additional computation.