MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_LocalOrdinalTransferFactory_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 MUELU_LOCALORDINALTRANSFER_FACTORY_DEF_HPP
47#define MUELU_LOCALORDINALTRANSFER_FACTORY_DEF_HPP
48
49#include "Xpetra_ImportFactory.hpp"
50#include "Xpetra_VectorFactory.hpp"
51#include "Xpetra_MapFactory.hpp"
52#include "Xpetra_CrsGraph.hpp"
53
54#include "Xpetra_IO.hpp"
55
56#include "MueLu_Aggregates.hpp"
58
59#include "MueLu_Level.hpp"
60#include "MueLu_Monitor.hpp"
61
62namespace MueLu {
63
64 template <class LocalOrdinal, class GlobalOrdinal, class Node>
66 RCP<ParameterList> validParamList = rcp(new ParameterList());
67
68 validParamList->set<RCP<const FactoryBase> >(TransferVecName_, Teuchos::null, "Factory for TransferVec generation");
69 validParamList->set<RCP<const FactoryBase> >("P Graph", Teuchos::null, "Factory for P generation");
70 validParamList->set<RCP<const FactoryBase> >("Aggregates", Teuchos::null, "Factory for aggregates generation");
71 validParamList->set<RCP<const FactoryBase> >("CoarseMap", Teuchos::null, "Generating factory of the coarse map");
72
73 return validParamList;
74 }
75
76 template <class LocalOrdinal, class GlobalOrdinal, class Node>
78 static bool isAvailableXfer = false;
79 if (coarseLevel.GetRequestMode() == Level::REQUEST) {
80 isAvailableXfer = coarseLevel.IsAvailable(TransferVecName_, this);
81 if (isAvailableXfer == false) {
82 Input(fineLevel, TransferVecName_);
83 Input(fineLevel, "CoarseMap");
84
86 Input(fineLevel, "Aggregates");
87 else {
88 Input(coarseLevel, "P Graph");
89 }
90 }
91 }
92
93 }
94
95 template <class LocalOrdinal, class GlobalOrdinal, class Node>
97 if(useAggregatesMode_) BuildAggregates(fineLevel,coarseLevel);
98 else BuildFC(fineLevel,coarseLevel);
99 }
100
101 template <class LocalOrdinal, class GlobalOrdinal, class Node>
103 FactoryMonitor m(*this, "Build", coarseLevel);
104
105 GetOStream(Runtime0) << "Transferring " <<TransferVecName_ << std::endl;
106 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
107
108 if (coarseLevel.IsAvailable(TransferVecName_, this)) {
109 GetOStream(Runtime0) << "Reusing "<<TransferVecName_ << std::endl;
110 return;
111 }
112
113 // Get everything we need
114 RCP<const CrsGraph> P = Get< RCP<const CrsGraph> >(coarseLevel,"P Graph");
115 RCP<LocalOrdinalVector> fineTV = Get< RCP<LocalOrdinalVector> >(fineLevel, TransferVecName_);
116 RCP<const Map> coarseMap = Get< RCP<const Map> > (fineLevel, "CoarseMap");
117 RCP<const Map> uniqueMap = fineTV->getMap();
118 ArrayRCP<const LO> fineData = fineTV->getData(0);
119
120 // Allocate new LO Vector
121 RCP<LocalOrdinalVector> coarseTV = LocalOrdinalVectorFactory::Build(coarseMap,1);
122 ArrayRCP<LO> coarseData = coarseTV->getDataNonConst(0);
123
124 // Invalidate everything first, to check for errors
125 for(LO i=0; i<coarseData.size(); i++)
126 coarseData[i] = LO_INVALID;
127
128 // Fill in coarse TV
129 LO domMapNumElements = P->getDomainMap()->getLocalNumElements();
130 for (LO row=0; row<(LO)P->getLocalNumRows(); row++) {
131 LO fineNumber = fineData[row];
132 ArrayView<const LO> indices;
133 P->getLocalRowView(row,indices);
134
135 for(LO j=0; j<(LO)indices.size(); j++) {
136 LO col = indices[j];
137 if (col >= domMapNumElements) {
138 // skip off rank entries of P
139 } else {
140 coarseData[col] = fineNumber;
141 }
142 }
143 }
144
145#ifdef HAVE_MUELU_DEBUG
146 size_t error_count = 0;
147 {
148 RCP<LocalOrdinalVector> coarseTVghosted;
149 RCP<const Import> importer = P->getImporter();
150 if (!importer.is_null()) {
151 coarseTVghosted = LocalOrdinalVectorFactory::Build(P->getColMap(),1);
152 coarseTVghosted->doImport(*coarseTV, *importer, Xpetra::INSERT);
153 } else {
154 coarseTVghosted = coarseTV;
155 }
156 ArrayRCP<LO> coarseDataGhosted = coarseTVghosted->getDataNonConst(0);
157 for (LO col=0; col<(LO)P->getColMap()->getLocalNumElements(); col++) {
158 if (coarseDataGhosted[col] == LO_INVALID)
159 error_count++;
160 }
161 for (LO row=0; row<(LO)P->getLocalNumRows(); row++) {
162 LO fineNumber = fineData[row];
163 ArrayView<const LO> indices;
164 P->getLocalRowView(row,indices);
165 for(LO j=0; j<(LO)indices.size(); j++) {
166 if (coarseDataGhosted[indices[j]] != fineNumber)
167 error_count++;
168 }
169 }
170 }
171
172 // Error checking: All nodes in an aggregate must share a local ordinal
173 if(error_count > 0) {
174 std::ostringstream ofs;
175 ofs << "LocalOrdinalTransferFactory("<<TransferVecName_<<"): ERROR: Each coarse dof must have a unique LO value. We had "<<std::to_string(error_count)<<" unknowns that did not match.";
176 throw std::runtime_error(ofs.str());
177 }
178#endif
179
180 Set<RCP<LocalOrdinalVector> >(coarseLevel, TransferVecName_, coarseTV);
181
182 }
183
184
185
186 template <class LocalOrdinal, class GlobalOrdinal, class Node>
188 FactoryMonitor m(*this, "Build", coarseLevel);
189
190 GetOStream(Runtime0) << "Transferring " <<TransferVecName_ << std::endl;
191 RCP<LocalOrdinalVector> coarseTV;
192 RCP<LocalOrdinalVector> fineTV;
193 LO LO_INVALID = Teuchos::OrdinalTraits<LO>::invalid();
194
195 if (coarseLevel.IsAvailable(TransferVecName_, this)) {
196 GetOStream(Runtime0) << "Reusing "<<TransferVecName_ << std::endl;
197 return;
198 }
199
200 RCP<Aggregates> aggregates = Get< RCP<Aggregates> > (fineLevel, "Aggregates");
201 fineTV = Get< RCP<LocalOrdinalVector> >(fineLevel, TransferVecName_);
202 RCP<const Map> coarseMap = Get< RCP<const Map> > (fineLevel, "CoarseMap");
203 RCP<const Map> uniqueMap = fineTV->getMap();
204
205 ArrayView<const GO> elementAList = coarseMap->getLocalElementList();
206
207 coarseTV = LocalOrdinalVectorFactory::Build(coarseMap,1);
208
209 // Create overlapped fine TV to reduce global communication
210 RCP<LocalOrdinalVector> ghostedTV = fineTV;
211 if (aggregates->AggregatesCrossProcessors()) {
212
213 RCP<const Map> nonUniqueMap = aggregates->GetMap();
214 RCP<const Import> importer = ImportFactory::Build(uniqueMap, nonUniqueMap);
215
216 ghostedTV = LocalOrdinalVectorFactory::Build(nonUniqueMap, 1);
217 ghostedTV->doImport(*fineTV, *importer, Xpetra::INSERT);
218 }
219
220 // Get some info about aggregates
221 int myPID = uniqueMap->getComm()->getRank();
222 ArrayRCP<LO> aggSizes = aggregates->ComputeAggregateSizesArrayRCP();
223 const ArrayRCP<const LO> vertex2AggID = aggregates->GetVertex2AggId()->getData(0);
224 const ArrayRCP<const LO> procWinner = aggregates->GetProcWinner()->getData(0);
225
226
227 ArrayRCP<const LO> fineData = ghostedTV->getData(0);
228 ArrayRCP<LO> coarseData = coarseTV->getDataNonConst(0);
229
230 // Invalidate everything first, to check for errors
231 for(LO i=0; i<coarseData.size(); i++)
232 coarseData[i] = LO_INVALID;
233
234 // Fill in coarse TV
235 size_t error_count = 0;
236 for (LO lnode = 0; lnode < vertex2AggID.size(); lnode++) {
237 if (procWinner[lnode] == myPID &&
238 //lnode < vertex2AggID.size() &&
239 lnode < fineData.size() && // TAW do not access off-processor data
240 vertex2AggID[lnode] < coarseData.size()) {
241 if(coarseData[vertex2AggID[lnode]] == LO_INVALID)
242 coarseData[vertex2AggID[lnode]] = fineData[lnode];
243 if(coarseData[vertex2AggID[lnode]] != fineData[lnode])
244 error_count++;
245 }
246 }
247
248 // Error checking: All nodes in an aggregate must share a local ordinal
249 if(error_count > 0) {
250 std::ostringstream ofs;
251 ofs << "LocalOrdinalTransferFactory: ERROR: Each aggregate must have a unique LO value. We had "<<std::to_string(error_count)<<" unknowns that did not match.";
252 throw std::runtime_error(ofs.str());
253 }
254
255 Set<RCP<LocalOrdinalVector> >(coarseLevel, TransferVecName_, coarseTV);
256
257 }
258
259} // namespace MueLu
260
261#endif // MUELU_LOCALORDINALTRANSFER_FACTORY_DEF_HPP
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.
RequestMode GetRequestMode() const
std::string TransferVecName_
The name for the vector to be transfered. This allows us to have multiple factories for different var...
void BuildFC(Level &fineLevel, Level &coarseLevel) const
RCP< const ParameterList > GetValidParameterList() const
Return a const parameter list of valid parameters that setParameterList() will accept.
void Build(Level &fineLevel, Level &coarseLevel) const
Build an object with this factory.
void BuildAggregates(Level &fineLevel, Level &coarseLevel) const
void DeclareInput(Level &finelevel, Level &coarseLevel) const
Specifies the data that this class needs, and the factories that generate that data.
bool useAggregatesMode_
Use aggregates mode (as opposed to FC mode).
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.
@ Runtime0
One-liner description of what is happening.