MueLu Version of the Day
Loading...
Searching...
No Matches
MueLu_PgPFactory_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_PGPFACTORY_DEF_HPP
47#define MUELU_PGPFACTORY_DEF_HPP
48
49#include <vector>
50
51#include <Xpetra_Vector.hpp>
52#include <Xpetra_MultiVectorFactory.hpp>
53#include <Xpetra_VectorFactory.hpp>
54#include <Xpetra_Import.hpp>
55#include <Xpetra_ImportFactory.hpp>
56#include <Xpetra_Export.hpp>
57#include <Xpetra_ExportFactory.hpp>
58#include <Xpetra_Matrix.hpp>
59#include <Xpetra_MatrixMatrix.hpp>
60
62#include "MueLu_Monitor.hpp"
63#include "MueLu_PerfUtils.hpp"
65#include "MueLu_Utilities.hpp"
66
67namespace MueLu {
68
69 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
71 RCP<ParameterList> validParamList = rcp(new ParameterList());
72
73 validParamList->set< RCP<const FactoryBase> >("A", Teuchos::null, "Generating factory of the matrix A used during the prolongator smoothing process");
74 validParamList->set< RCP<const FactoryBase> >("P", Teuchos::null, "Tentative prolongator factory");
75 validParamList->set< MinimizationNorm > ("Minimization norm", DINVANORM, "Norm to be minimized");
76 validParamList->set< bool > ("ReUseRowBasedOmegas", false, "Reuse omegas for prolongator for restrictor");
77
78 return validParamList;
79 }
80
81 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83 SetParameter("Minimization norm", ParameterEntry(minnorm)); // revalidate
84 }
85
86 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88 const ParameterList& pL = GetParameterList();
89 return pL.get<MueLu::MinimizationNorm>("Minimization norm");
90 }
91
92 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
94 Input(fineLevel, "A");
95
96 // Get default tentative prolongator factory
97 // Getting it that way ensure that the same factory instance will be used for both SaPFactory and NullspaceFactory.
98 // -- Warning: Do not use directly initialPFact_. Use initialPFact instead everywhere!
99 RCP<const FactoryBase> initialPFact = GetFactory("P");
100 if (initialPFact == Teuchos::null) { initialPFact = coarseLevel.GetFactoryManager()->GetFactory("Ptent"); }
101 coarseLevel.DeclareInput("P", initialPFact.get(), this);
102
103 /* If PgPFactory is reusing the row based damping parameters omega for
104 * restriction, it has to request the data here.
105 * we have the following scenarios:
106 * 1) Reuse omegas:
107 * PgPFactory.DeclareInput for prolongation mode requests A and P0
108 * PgPFactory.DeclareInput for restriction mode requests A, P0 and RowBasedOmega (call triggered by GenericRFactory)
109 * PgPFactory.Build for prolongation mode calculates RowBasedOmega and stores it (as requested)
110 * PgPFactory.Build for restriction mode reuses RowBasedOmega (and Releases the data with the Get call)
111 * 2) do not reuse omegas
112 * PgPFactory.DeclareInput for prolongation mode requests A and P0
113 * PgPFactory.DeclareInput for restriction mode requests A and P0
114 * PgPFactory.Build for prolongation mode calculates RowBasedOmega for prolongation operator
115 * PgPFactory.Build for restriction mode calculates RowBasedOmega for restriction operator
116 */
117 const ParameterList & pL = GetParameterList();
118 bool bReUseRowBasedOmegas = pL.get<bool>("ReUseRowBasedOmegas");
119 if( bReUseRowBasedOmegas == true && restrictionMode_ == true ) {
120 coarseLevel.DeclareInput("RowBasedOmega", this, this); // RowBasedOmega is calculated by this PgPFactory and requested by this PgPFactory
121 }
122 }
123
124 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
126 FactoryMonitor m(*this, "Prolongator smoothing (PG-AMG)", coarseLevel);
127
128 // Level Get
129 RCP<Matrix> A = Get< RCP<Matrix> >(fineLevel, "A");
130
131 // Get default tentative prolongator factory
132 // Getting it that way ensure that the same factory instance will be used for both SaPFactory and NullspaceFactory.
133 // -- Warning: Do not use directly initialPFact_. Use initialPFact instead everywhere!
134 RCP<const FactoryBase> initialPFact = GetFactory("P");
135 if (initialPFact == Teuchos::null) { initialPFact = coarseLevel.GetFactoryManager()->GetFactory("Ptent"); }
136 RCP<Matrix> Ptent = coarseLevel.Get< RCP<Matrix> >("P", initialPFact.get());
137
139 if(restrictionMode_) {
140 SubFactoryMonitor m2(*this, "Transpose A", coarseLevel);
141 A = Utilities::Transpose(*A, true); // build transpose of A explicitely
142 }
143
145 bool doFillComplete=true;
146 bool optimizeStorage=true;
147 RCP<Matrix> DinvAP0 = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *Ptent, false, GetOStream(Statistics2), doFillComplete, optimizeStorage);
148
149 doFillComplete=true;
150 optimizeStorage=false;
151 Teuchos::ArrayRCP<Scalar> diag = Utilities::GetMatrixDiagonal_arcp(*A);
152 Utilities::MyOldScaleMatrix(*DinvAP0, diag, true, doFillComplete, optimizeStorage); //scale matrix with reciprocal of diag
153
155
156 Teuchos::RCP<Vector > RowBasedOmega = Teuchos::null;
157
158 const ParameterList & pL = GetParameterList();
159 bool bReUseRowBasedOmegas = pL.get<bool>("ReUseRowBasedOmegas");
160 if(restrictionMode_ == false || bReUseRowBasedOmegas == false) {
161 // if in prolongation mode: calculate row based omegas
162 // if in restriction mode: calculate omegas only if row based omegas are not used from prolongation mode
163 ComputeRowBasedOmega(fineLevel, coarseLevel, A, Ptent, DinvAP0, RowBasedOmega);
164 } // if(bReUseRowBasedOmegas == false)
165 else {
166 // reuse row based omegas, calculated by this factory in the run before (with restrictionMode_ == false)
167 RowBasedOmega = coarseLevel.Get<Teuchos::RCP<Vector > >("RowBasedOmega", this);
168
169 // RowBasedOmega is now based on row map of A (not transposed)
170 // for restriction we use A^T instead of A
171 // -> recommunicate row based omega
172
173 // exporter: overlapping row map to nonoverlapping domain map (target map is unique)
174 // since A is already transposed we use the RangeMap of A
175 Teuchos::RCP<const Export> exporter =
176 ExportFactory::Build(RowBasedOmega->getMap(), A->getRangeMap());
177
178 Teuchos::RCP<Vector > noRowBasedOmega =
179 VectorFactory::Build(A->getRangeMap());
180
181 noRowBasedOmega->doExport(*RowBasedOmega, *exporter, Xpetra::INSERT);
182
183 // importer: nonoverlapping map to overlapping map
184
185 // importer: source -> target maps
186 Teuchos::RCP<const Import > importer =
187 ImportFactory::Build(A->getRangeMap(), A->getRowMap());
188
189 // doImport target->doImport(*source, importer, action)
190 RowBasedOmega->doImport(*noRowBasedOmega, *importer, Xpetra::INSERT);
191 }
192
193 Teuchos::ArrayRCP< Scalar > RowBasedOmega_local = RowBasedOmega->getDataNonConst(0);
194
196 RCP<Matrix> P_smoothed = Teuchos::null;
197 Utilities::MyOldScaleMatrix(*DinvAP0, RowBasedOmega_local, false, doFillComplete, optimizeStorage); //scale matrix with reciprocal of diag
198
199 Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::TwoMatrixAdd(*Ptent, false, Teuchos::ScalarTraits<Scalar>::one(),
200 *DinvAP0, false, -Teuchos::ScalarTraits<Scalar>::one(),
201 P_smoothed,GetOStream(Statistics2));
202 P_smoothed->fillComplete(Ptent->getDomainMap(), Ptent->getRangeMap());
203
205
206 RCP<ParameterList> params = rcp(new ParameterList());
207 params->set("printLoadBalancingInfo", true);
208
209 // Level Set
210 if (!restrictionMode_) {
211 // prolongation factory is in prolongation mode
212 Set(coarseLevel, "P", P_smoothed);
213
214 // RfromPFactory used to indicate to TogglePFactory that a factory
215 // capable or producing R can be invoked later. TogglePFactory
216 // replaces dummy value with an index into it's array of prolongators
217 // pointing to the correct prolongator factory. This is later used by
218 // RfromP_Or_TransP to invoke the prolongatorfactory in RestrictionMode
219 int dummy = 7;
220 Set(coarseLevel,"RfromPfactory", dummy);
221
222 if (IsPrint(Statistics1))
223 GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*P_smoothed, "P", params);
224
225 // NOTE: EXPERIMENTAL
226 if (Ptent->IsView("stridedMaps"))
227 P_smoothed->CreateView("stridedMaps", Ptent);
228
229 } else {
230 // prolongation factory is in restriction mode
231 RCP<Matrix> R = Utilities::Transpose(*P_smoothed, true);
232 Set(coarseLevel, "R", R);
233
234 if (IsPrint(Statistics1))
235 GetOStream(Statistics1) << PerfUtils::PrintMatrixInfo(*R, "P", params);
236
237 // NOTE: EXPERIMENTAL
238 if (Ptent->IsView("stridedMaps"))
239 R->CreateView("stridedMaps", Ptent, true);
240 }
241
242 }
243
244 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
245 void PgPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::ComputeRowBasedOmega(Level& /* fineLevel */, Level &coarseLevel, const RCP<Matrix>& A, const RCP<Matrix>& P0, const RCP<Matrix>& DinvAP0, RCP<Vector > & RowBasedOmega) const {
246 FactoryMonitor m(*this, "PgPFactory::ComputeRowBasedOmega", coarseLevel);
247
248 typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Magnitude;
249 Scalar sZero = Teuchos::ScalarTraits<Scalar>::zero();
250 Magnitude mZero = Teuchos::ScalarTraits<Scalar>::magnitude(sZero);
251
252 Teuchos::RCP<Vector > Numerator = Teuchos::null;
253 Teuchos::RCP<Vector > Denominator = Teuchos::null;
254
255 const ParameterList & pL = GetParameterList();
256 MinimizationNorm min_norm = pL.get<MinimizationNorm>("Minimization norm");
257
258 switch (min_norm)
259 {
260 case ANORM: {
261 // MUEMAT mode (=paper)
262 // Minimize with respect to the (A)' A norm.
263 // Need to be smart here to avoid the construction of A' A
264 //
265 // diag( P0' (A' A) D^{-1} A P0)
266 // omega = ------------------------------------------
267 // diag( P0' A' D^{-1}' ( A' A) D^{-1} A P0)
268 //
269 // expensive, since we have to recalculate AP0 due to the lack of an explicit scaling routine for DinvAP0
270
271 // calculate A * P0
272 bool doFillComplete=true;
273 bool optimizeStorage=false;
274 RCP<Matrix> AP0 = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *P0, false, GetOStream(Statistics2), doFillComplete, optimizeStorage);
275
276 // compute A * D^{-1} * A * P0
277 RCP<Matrix> ADinvAP0 = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *DinvAP0, false, GetOStream(Statistics2), doFillComplete, optimizeStorage);
278
279 Numerator = VectorFactory::Build(ADinvAP0->getColMap(), true);
280 Denominator = VectorFactory::Build(ADinvAP0->getColMap(), true);
281 MultiplyAll(AP0, ADinvAP0, Numerator);
282 MultiplySelfAll(ADinvAP0, Denominator);
283 }
284 break;
285 case L2NORM: {
286
287 // ML mode 1 (cheapest)
288 // Minimize with respect to L2 norm
289 // diag( P0' D^{-1} A P0)
290 // omega = -----------------------------
291 // diag( P0' A' D^{-1}' D^{-1} A P0)
292 //
293 Numerator = VectorFactory::Build(DinvAP0->getColMap(), true);
294 Denominator = VectorFactory::Build(DinvAP0->getColMap(), true);
295 MultiplyAll(P0, DinvAP0, Numerator);
296 MultiplySelfAll(DinvAP0, Denominator);
297 }
298 break;
299 case DINVANORM: {
300 // ML mode 2
301 // Minimize with respect to the (D^{-1} A)' D^{-1} A norm.
302 // Need to be smart here to avoid the construction of A' A
303 //
304 // diag( P0' ( A' D^{-1}' D^{-1} A) D^{-1} A P0)
305 // omega = --------------------------------------------------------
306 // diag( P0' A' D^{-1}' ( A' D^{-1}' D^{-1} A) D^{-1} A P0)
307 //
308
309 // compute D^{-1} * A * D^{-1} * A * P0
310 bool doFillComplete=true;
311 bool optimizeStorage=true;
312 Teuchos::ArrayRCP<Scalar> diagA = Utilities::GetMatrixDiagonal_arcp(*A);
313 RCP<Matrix> DinvADinvAP0 = Xpetra::MatrixMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Multiply(*A, false, *DinvAP0, false, GetOStream(Statistics2), doFillComplete, optimizeStorage);
314 Utilities::MyOldScaleMatrix(*DinvADinvAP0, diagA, true, doFillComplete, optimizeStorage); //scale matrix with reciprocal of diag
315 diagA = Teuchos::ArrayRCP<Scalar>();
316
317 Numerator = VectorFactory::Build(DinvADinvAP0->getColMap(), true);
318 Denominator = VectorFactory::Build(DinvADinvAP0->getColMap(), true);
319 MultiplyAll(DinvAP0, DinvADinvAP0, Numerator);
320 MultiplySelfAll(DinvADinvAP0, Denominator);
321 }
322 break;
323 default:
324 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MueLu::PgPFactory::Build: minimization mode not supported. error");
325 }
326
327
329 Teuchos::RCP<Vector > ColBasedOmega =
330 VectorFactory::Build(Numerator->getMap()/*DinvAP0->getColMap()*/, true);
331
332 ColBasedOmega->putScalar(-666);
333
334 Teuchos::ArrayRCP< const Scalar > Numerator_local = Numerator->getData(0);
335 Teuchos::ArrayRCP< const Scalar > Denominator_local = Denominator->getData(0);
336 Teuchos::ArrayRCP< Scalar > ColBasedOmega_local = ColBasedOmega->getDataNonConst(0);
337 GlobalOrdinal zero_local = 0; // count negative colbased omegas
338 GlobalOrdinal nan_local = 0; // count NaNs -> set them to zero
339 Magnitude min_local = 1000000.0; //Teuchos::ScalarTraits<Scalar>::one() * (Scalar) 1000000;
340 Magnitude max_local = 0.0;
341 for(LocalOrdinal i = 0; i < Teuchos::as<LocalOrdinal>(Numerator->getLocalLength()); i++) {
342 if(Teuchos::ScalarTraits<Scalar>::magnitude(Denominator_local[i]) == mZero)
343 {
344 ColBasedOmega_local[i] = 0.0; // fallback: nonsmoothed basis function since denominator == 0.0
345 nan_local++;
346 }
347 else
348 {
349 ColBasedOmega_local[i] = Numerator_local[i] / Denominator_local[i]; // default case
350 }
351
352 if(Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]) < mZero) { // negative omegas are not valid. set them to zero
353 ColBasedOmega_local[i] = Teuchos::ScalarTraits<Scalar>::zero();
354 zero_local++; // count zero omegas
355 }
356
357 // handle case that Nominator == Denominator -> Dirichlet bcs in A?
358 // fallback if ColBasedOmega == 1 -> very strong smoothing may lead to zero rows in P
359 // TAW: this is somewhat nonstandard and a rough fallback strategy to avoid problems
360 // also avoid "overshooting" with omega > 0.8
361 if(Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]) >= 0.8) {
362 ColBasedOmega_local[i] = 0.0;
363 }
364
365 if(Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]) < min_local)
366 { min_local = Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]); }
367 if(Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]) > max_local)
368 { max_local = Teuchos::ScalarTraits<Scalar>::magnitude(ColBasedOmega_local[i]); }
369 }
370
371 { // be verbose
372 GlobalOrdinal zero_all;
373 GlobalOrdinal nan_all;
374 Magnitude min_all;
375 Magnitude max_all;
376 MueLu_sumAll(A->getRowMap()->getComm(), zero_local, zero_all);
377 MueLu_sumAll(A->getRowMap()->getComm(), nan_local, nan_all);
378 MueLu_minAll(A->getRowMap()->getComm(), min_local, min_all);
379 MueLu_maxAll(A->getRowMap()->getComm(), max_local, max_all);
380
381 GetOStream(MueLu::Statistics1, 0) << "PgPFactory: smoothed aggregation (scheme: ";
382 switch (min_norm) {
383 case ANORM: GetOStream(Statistics1) << "Anorm)" << std::endl; break;
384 case L2NORM: GetOStream(Statistics1) << "L2norm)" << std::endl; break;
385 case DINVANORM: GetOStream(Statistics1) << "DinvAnorm)" << std::endl; break;
386 default: GetOStream(Statistics1) << "unknown)" << std::endl; break;
387 }
388 GetOStream(Statistics1) << "Damping parameter: min = " << min_all << ", max = " << max_all << std::endl;
389 GetOStream(Statistics) << "# negative omegas: " << zero_all << " out of " << ColBasedOmega->getGlobalLength() << " column-based omegas" << std::endl;
390 GetOStream(Statistics) << "# NaNs: " << nan_all << " out of " << ColBasedOmega->getGlobalLength() << " column-based omegas" << std::endl;
391 }
392
393 if(coarseLevel.IsRequested("ColBasedOmega", this)) {
394 coarseLevel.Set("ColBasedOmega", ColBasedOmega, this);
395 }
396
398 // transform column based omegas to row based omegas
399 RowBasedOmega =
400 VectorFactory::Build(DinvAP0->getRowMap(), true);
401
402 RowBasedOmega->putScalar(-666); // TODO bad programming style
403
404 bool bAtLeastOneDefined = false;
405 Teuchos::ArrayRCP< Scalar > RowBasedOmega_local = RowBasedOmega->getDataNonConst(0);
406 for(LocalOrdinal row = 0; row<Teuchos::as<LocalOrdinal>(A->getLocalNumRows()); row++) {
407 Teuchos::ArrayView<const LocalOrdinal> lindices;
408 Teuchos::ArrayView<const Scalar> lvals;
409 DinvAP0->getLocalRowView(row, lindices, lvals);
410 bAtLeastOneDefined = false;
411 for(size_t j=0; j<Teuchos::as<size_t>(lindices.size()); j++) {
412 Scalar omega = ColBasedOmega_local[lindices[j]];
413 if (Teuchos::ScalarTraits<Scalar>::magnitude(omega) != -666) { // TODO bad programming style
414 bAtLeastOneDefined = true;
415 if(Teuchos::ScalarTraits<Scalar>::magnitude(RowBasedOmega_local[row]) == -666) RowBasedOmega_local[row] = omega;
416 else if(Teuchos::ScalarTraits<Scalar>::magnitude(omega) < Teuchos::ScalarTraits<Scalar>::magnitude(RowBasedOmega_local[row])) RowBasedOmega_local[row] = omega;
417 }
418 }
419 if(bAtLeastOneDefined == true) {
420 if(Teuchos::ScalarTraits<Scalar>::magnitude(RowBasedOmega_local[row]) < mZero)
421 RowBasedOmega_local[row] = sZero;
422 }
423 }
424
425 if(coarseLevel.IsRequested("RowBasedOmega", this)) {
426 Set(coarseLevel, "RowBasedOmega", RowBasedOmega);
427 }
428 }
429
430 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
431 void PgPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MultiplySelfAll(const RCP<Matrix>& Op, Teuchos::RCP<Vector >& InnerProdVec) const {
432
433 // note: InnerProdVec is based on column map of Op
434 TEUCHOS_TEST_FOR_EXCEPTION(!InnerProdVec->getMap()->isSameAs(*Op->getColMap()), Exceptions::RuntimeError, "MueLu::PgPFactory::MultiplySelfAll: map of InnerProdVec must be same as column map of operator. error");
435
436 Teuchos::ArrayRCP< Scalar > InnerProd_local = InnerProdVec->getDataNonConst(0);
437
438 Teuchos::ArrayView<const LocalOrdinal> lindices;
439 Teuchos::ArrayView<const Scalar> lvals;
440
441 for(size_t n=0; n<Op->getLocalNumRows(); n++) {
442 Op->getLocalRowView(n, lindices, lvals);
443 for(size_t i=0; i<Teuchos::as<size_t>(lindices.size()); i++) {
444 InnerProd_local[lindices[i]] += lvals[i]*lvals[i];
445 }
446 }
447 InnerProd_local = Teuchos::ArrayRCP< Scalar >();
448
449 // exporter: overlapping map to nonoverlapping map (target map is unique)
450 Teuchos::RCP<const Export> exporter =
451 ExportFactory::Build(Op->getColMap(), Op->getDomainMap());
452
453 Teuchos::RCP<Vector > nonoverlap =
454 VectorFactory::Build(Op->getDomainMap());
455
456 nonoverlap->doExport(*InnerProdVec, *exporter, Xpetra::ADD);
457
458 // importer: nonoverlapping map to overlapping map
459
460 // importer: source -> target maps
461 Teuchos::RCP<const Import > importer =
462 ImportFactory::Build(Op->getDomainMap(), Op->getColMap());
463
464 // doImport target->doImport(*source, importer, action)
465 InnerProdVec->doImport(*nonoverlap, *importer, Xpetra::INSERT);
466
467 }
468
469 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
470 void PgPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MultiplyAll(const RCP<Matrix>& left, const RCP<Matrix>& right, Teuchos::RCP<Vector >& InnerProdVec) const {
471
472 TEUCHOS_TEST_FOR_EXCEPTION(!left->getDomainMap()->isSameAs(*right->getDomainMap()), Exceptions::RuntimeError, "MueLu::PgPFactory::MultiplyAll: domain maps of left and right do not match. Error.");
473 TEUCHOS_TEST_FOR_EXCEPTION(!left->getRowMap()->isSameAs(*right->getRowMap()), Exceptions::RuntimeError, "MueLu::PgPFactory::MultiplyAll: row maps of left and right do not match. Error.");
474#if 1 // 1=new "fast code, 0=old "slow", but safe code
475#if 0 // not necessary - remove me
476 if(InnerProdVec->getMap()->isSameAs(*left->getColMap())) {
477 // initialize NewRightLocal vector and assign all entries to
478 // left->getColMap()->getLocalNumElements() + 1
479 std::vector<LocalOrdinal> NewRightLocal(right->getColMap()->getLocalNumElements(), Teuchos::as<LocalOrdinal>(left->getColMap()->getLocalNumElements()+1));
480
481 LocalOrdinal i = 0;
482 for (size_t j=0; j < right->getColMap()->getLocalNumElements(); j++) {
483 while ( (i < Teuchos::as<LocalOrdinal>(left->getColMap()->getLocalNumElements())) &&
484 (left->getColMap()->getGlobalElement(i) < right->getColMap()->getGlobalElement(j)) ) i++;
485 if (left->getColMap()->getGlobalElement(i) == right->getColMap()->getGlobalElement(j)) {
486 NewRightLocal[j] = i;
487 }
488 }
489
490 Teuchos::ArrayRCP< Scalar > InnerProd_local = InnerProdVec->getDataNonConst(0);
491 std::vector<Scalar> temp_array(left->getColMap()->getLocalNumElements()+1, 0.0);
492
493 for(size_t n=0; n<right->getLocalNumRows(); n++) {
494 Teuchos::ArrayView<const LocalOrdinal> lindices_left;
495 Teuchos::ArrayView<const Scalar> lvals_left;
496 Teuchos::ArrayView<const LocalOrdinal> lindices_right;
497 Teuchos::ArrayView<const Scalar> lvals_right;
498
499 left->getLocalRowView (n, lindices_left, lvals_left);
500 right->getLocalRowView(n, lindices_right, lvals_right);
501
502 for(size_t j=0; j<Teuchos::as<size_t>(lindices_right.size()); j++) {
503 temp_array[NewRightLocal[lindices_right[j] ] ] = lvals_right[j];
504 }
505 for (size_t j=0; j < Teuchos::as<size_t>(lindices_left.size()); j++) {
506 InnerProd_local[lindices_left[j]] += temp_array[lindices_left[j] ]*lvals_left[j];
507 }
508 for (size_t j=0; j < Teuchos::as<size_t>(lindices_right.size()); j++) {
509 temp_array[NewRightLocal[lindices_right[j] ] ] = 0.0;
510 }
511 }
512 // exporter: overlapping map to nonoverlapping map (target map is unique)
513 Teuchos::RCP<const Export> exporter =
514 ExportFactory::Build(left->getColMap(), left->getDomainMap()); // TODO: change left to right?
515
516 Teuchos::RCP<Vector > nonoverlap =
517 VectorFactory::Build(left->getDomainMap()); // TODO: change left to right?
518
519 nonoverlap->doExport(*InnerProdVec, *exporter, Xpetra::ADD);
520
521 // importer: nonoverlapping map to overlapping map
522
523 // importer: source -> target maps
524 Teuchos::RCP<const Import > importer =
525 ImportFactory::Build(left->getDomainMap(), left->getColMap()); // TODO: change left to right?
526
527 // doImport target->doImport(*source, importer, action)
528 InnerProdVec->doImport(*nonoverlap, *importer, Xpetra::INSERT);
529
530
531 } else
532#endif // end remove me
533 if(InnerProdVec->getMap()->isSameAs(*right->getColMap())) {
534 size_t szNewLeftLocal = TEUCHOS_MAX(left->getColMap()->getLocalNumElements(), right->getColMap()->getLocalNumElements());
535 Teuchos::RCP<std::vector<LocalOrdinal> > NewLeftLocal = Teuchos::rcp(new std::vector<LocalOrdinal>(szNewLeftLocal, Teuchos::as<LocalOrdinal>(right->getColMap()->getMaxLocalIndex()+1)));
536
537 LocalOrdinal j = 0;
538 for (size_t i=0; i < left->getColMap()->getLocalNumElements(); i++) {
539 while ( (j < Teuchos::as<LocalOrdinal>(right->getColMap()->getLocalNumElements())) &&
540 (right->getColMap()->getGlobalElement(j) < left->getColMap()->getGlobalElement(i)) ) j++;
541 if (right->getColMap()->getGlobalElement(j) == left->getColMap()->getGlobalElement(i)) {
542 (*NewLeftLocal)[i] = j;
543 }
544 }
545
546 /*for (size_t i=0; i < right->getColMap()->getLocalNumElements(); i++) {
547 std::cout << "left col map: " << (*NewLeftLocal)[i] << " GID: " << left->getColMap()->getGlobalElement((*NewLeftLocal)[i]) << " GID: " << right->getColMap()->getGlobalElement(i) << " right col map: " << i << std::endl;
548 }*/
549
550 Teuchos::ArrayRCP< Scalar > InnerProd_local = InnerProdVec->getDataNonConst(0);
551 Teuchos::RCP<std::vector<Scalar> > temp_array = Teuchos::rcp(new std::vector<Scalar>(right->getColMap()->getMaxLocalIndex()+2, 0.0));
552
553 for(size_t n=0; n<left->getLocalNumRows(); n++) {
554 Teuchos::ArrayView<const LocalOrdinal> lindices_left;
555 Teuchos::ArrayView<const Scalar> lvals_left;
556 Teuchos::ArrayView<const LocalOrdinal> lindices_right;
557 Teuchos::ArrayView<const Scalar> lvals_right;
558
559 left->getLocalRowView (n, lindices_left, lvals_left);
560 right->getLocalRowView(n, lindices_right, lvals_right);
561
562 for(size_t i=0; i<Teuchos::as<size_t>(lindices_left.size()); i++) {
563 (*temp_array)[(*NewLeftLocal)[lindices_left[i] ] ] = lvals_left[i];
564 }
565 for (size_t i=0; i < Teuchos::as<size_t>(lindices_right.size()); i++) {
566 InnerProd_local[lindices_right[i]] += (*temp_array)[lindices_right[i] ] * lvals_right[i];
567 }
568 for (size_t i=0; i < Teuchos::as<size_t>(lindices_left.size()); i++) {
569 (*temp_array)[(*NewLeftLocal)[lindices_left[i] ] ] = 0.0;
570 }
571 }
572 InnerProd_local = Teuchos::ArrayRCP< Scalar >();
573 // exporter: overlapping map to nonoverlapping map (target map is unique)
574 Teuchos::RCP<const Export> exporter =
575 ExportFactory::Build(right->getColMap(), right->getDomainMap()); // TODO: change left to right?
576
577 Teuchos::RCP<Vector> nonoverlap =
578 VectorFactory::Build(right->getDomainMap()); // TODO: change left to right?
579
580 nonoverlap->doExport(*InnerProdVec, *exporter, Xpetra::ADD);
581
582 // importer: nonoverlapping map to overlapping map
583
584 // importer: source -> target maps
585 Teuchos::RCP<const Import > importer =
586 ImportFactory::Build(right->getDomainMap(), right->getColMap()); // TODO: change left to right?
587 // doImport target->doImport(*source, importer, action)
588 InnerProdVec->doImport(*nonoverlap, *importer, Xpetra::INSERT);
589 } else {
590 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MueLu::PgPFactory::MultiplyAll: map of InnerProdVec must be same as column map of left operator? Error.");
591 }
592
593#else // old "safe" code
594 if(InnerProdVec->getMap()->isSameAs(*left->getColMap())) {
595
596 Teuchos::ArrayRCP< Scalar > InnerProd_local = InnerProdVec->getDataNonConst(0);
597
598 // declare variables
599 Teuchos::ArrayView<const LocalOrdinal> lindices_left;
600 Teuchos::ArrayView<const Scalar> lvals_left;
601 Teuchos::ArrayView<const LocalOrdinal> lindices_right;
602 Teuchos::ArrayView<const Scalar> lvals_right;
603
604 for(size_t n=0; n<left->getLocalNumRows(); n++)
605 {
606
607 left->getLocalRowView (n, lindices_left, lvals_left);
608 right->getLocalRowView(n, lindices_right, lvals_right);
609
610 for(size_t i=0; i<Teuchos::as<size_t>(lindices_left.size()); i++)
611 {
612 GlobalOrdinal left_gid = left->getColMap()->getGlobalElement(lindices_left[i]);
613 for(size_t j=0; j<Teuchos::as<size_t>(lindices_right.size()); j++)
614 {
615 GlobalOrdinal right_gid= right->getColMap()->getGlobalElement(lindices_right[j]);
616 if(left_gid == right_gid)
617 {
618 InnerProd_local[lindices_left[i]] += lvals_left[i]*lvals_right[j];
619 break; // skip remaining gids of right operator
620 }
621 }
622 }
623 }
624
625 // exporter: overlapping map to nonoverlapping map (target map is unique)
626 Teuchos::RCP<const Export> exporter =
627 ExportFactory::Build(left->getColMap(), left->getDomainMap()); // TODO: change left to right?
628
629 Teuchos::RCP<Vector > nonoverlap =
630 VectorFactory::Build(left->getDomainMap()); // TODO: change left to right?
631
632 nonoverlap->doExport(*InnerProdVec, *exporter, Xpetra::ADD);
633
634 // importer: nonoverlapping map to overlapping map
635
636 // importer: source -> target maps
637 Teuchos::RCP<const Import > importer =
638 ImportFactory::Build(left->getDomainMap(), left->getColMap()); // TODO: change left to right?
639
640 // doImport target->doImport(*source, importer, action)
641 InnerProdVec->doImport(*nonoverlap, *importer, Xpetra::INSERT);
642 }
643 else if(InnerProdVec->getMap()->isSameAs(*right->getColMap())) {
644 Teuchos::ArrayRCP< Scalar > InnerProd_local = InnerProdVec->getDataNonConst(0);
645
646 Teuchos::ArrayView<const LocalOrdinal> lindices_left;
647 Teuchos::ArrayView<const Scalar> lvals_left;
648 Teuchos::ArrayView<const LocalOrdinal> lindices_right;
649 Teuchos::ArrayView<const Scalar> lvals_right;
650
651 for(size_t n=0; n<left->getLocalNumRows(); n++)
652 {
653 left->getLocalRowView(n, lindices_left, lvals_left);
654 right->getLocalRowView(n, lindices_right, lvals_right);
655
656 for(size_t i=0; i<Teuchos::as<size_t>(lindices_left.size()); i++)
657 {
658 GlobalOrdinal left_gid = left->getColMap()->getGlobalElement(lindices_left[i]);
659 for(size_t j=0; j<Teuchos::as<size_t>(lindices_right.size()); j++)
660 {
661 GlobalOrdinal right_gid= right->getColMap()->getGlobalElement(lindices_right[j]);
662 if(left_gid == right_gid)
663 {
664 InnerProd_local[lindices_right[j]] += lvals_left[i]*lvals_right[j];
665 break; // skip remaining gids of right operator
666 }
667 }
668 }
669 }
670
671 // exporter: overlapping map to nonoverlapping map (target map is unique)
672 Teuchos::RCP<const Export> exporter =
673 ExportFactory::Build(right->getColMap(), right->getDomainMap()); // TODO: change left to right?
674
675 Teuchos::RCP<Vector> nonoverlap =
676 VectorFactory::Build(right->getDomainMap()); // TODO: change left to right?
677
678 nonoverlap->doExport(*InnerProdVec, *exporter, Xpetra::ADD);
679
680 // importer: nonoverlapping map to overlapping map
681
682 // importer: source -> target maps
683 Teuchos::RCP<const Import > importer =
684 ImportFactory::Build(right->getDomainMap(), right->getColMap()); // TODO: change left to right?
685
686 // doImport target->doImport(*source, importer, action)
687 InnerProdVec->doImport(*nonoverlap, *importer, Xpetra::INSERT);
688 }
689 else {
690 TEUCHOS_TEST_FOR_EXCEPTION(true, Exceptions::RuntimeError, "MueLu::PgPFactory::MultiplyAll: map of InnerProdVec must be same as column map of left or right operator? Error.");
691 }
692#endif
693 }
694
695 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
696 void PgPFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node>::BuildP(Level &/* fineLevel */, Level &/* coarseLevel */) const {
697 std::cout << "TODO: remove me" << std::endl;
698 }
699
700 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
702 SetParameter("ReUseRowBasedOmegas", ParameterEntry(bReuse));
703 }
704
705} //namespace MueLu
706
707#endif /* MUELU_PGPFACTORY_DEF_HPP */
#define MueLu_maxAll(rcpComm, in, out)
#define MueLu_sumAll(rcpComm, in, out)
#define MueLu_minAll(rcpComm, in, out)
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
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.
Class that holds all level-specific information.
void DeclareInput(const std::string &ename, const FactoryBase *factory, const FactoryBase *requestedBy=NoFactory::get())
Callback from FactoryBase::CallDeclareInput() and FactoryBase::DeclareInput()
const RCP< const FactoryManagerBase > GetFactoryManager()
returns the current factory manager
T & Get(const std::string &ename, const FactoryBase *factory=NoFactory::get())
Get data without decrementing associated storage counter (i.e., read-only access)....
void Set(const std::string &ename, const T &entry, const FactoryBase *factory=NoFactory::get())
bool IsRequested(const std::string &ename, const FactoryBase *factory=NoFactory::get()) const
Test whether a need has been requested. Note: this tells nothing about whether the need's value exist...
static std::string PrintMatrixInfo(const Matrix &A, const std::string &msgTag, RCP< const Teuchos::ParameterList > params=Teuchos::null)
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 method.
void ReUseDampingParameters(bool bReuse)
void MultiplyAll(const RCP< Matrix > &left, const RCP< Matrix > &right, Teuchos::RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &InnerProdVec) const
void DeclareInput(Level &fineLevel, Level &coarseLevel) const
Input.
void ComputeRowBasedOmega(Level &fineLevel, Level &coarseLevel, const RCP< Matrix > &A, const RCP< Matrix > &P0, const RCP< Matrix > &DinvAP0, RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &RowBasedOmega) const
void SetMinimizationMode(MinimizationNorm minnorm)
Set minimization mode (L2NORM for cheapest, ANORM more expensive, DINVANORM = default)
MinimizationNorm GetMinimizationMode()
return minimization mode
void BuildP(Level &fineLevel, Level &coarseLevel) const
Abstract Build method.
void MultiplySelfAll(const RCP< Matrix > &Op, Teuchos::RCP< Xpetra::Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &InnerProdVec) const
Timer to be used in factories. Similar to SubMonitor but adds a timer level by level.
static Teuchos::ArrayRCP< Scalar > GetMatrixDiagonal_arcp(const Matrix &A)
Extract Matrix Diagonal.
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
Namespace for MueLu classes and methods.
@ Statistics2
Print even more statistics.
@ Statistics1
Print more statistics.
@ Statistics
Print all statistics.