/
Thyra_AmesosLinearOpWithSolveFactory.cpp
519 lines (482 loc) · 18.4 KB
/
Thyra_AmesosLinearOpWithSolveFactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
// @HEADER
// ***********************************************************************
//
// Stratimikos: Thyra-based strategies for linear solvers
// Copyright (2006) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
//
// ***********************************************************************
// @HEADER
*/
#ifndef SUN_CXX
#include "Thyra_AmesosLinearOpWithSolveFactory.hpp"
#include "Thyra_AmesosLinearOpWithSolve.hpp"
#include "Thyra_EpetraOperatorViewExtractorStd.hpp"
#include "Amesos.h"
#include "Teuchos_dyn_cast.hpp"
#include "Teuchos_TimeMonitor.hpp"
#include "Teuchos_TypeNameTraits.hpp"
#include "Teuchos_VerboseObjectParameterListHelpers.hpp"
#ifdef HAVE_AMESOS_KLU
#include "Amesos_Klu.h"
#endif
#ifdef HAVE_AMESOS_PASTIX
#include "Amesos_Pastix.h"
#endif
#ifdef HAVE_AMESOS_LAPACK
#include "Amesos_Lapack.h"
#endif
#ifdef HAVE_AMESOS_MUMPS
#include "Amesos_Mumps.h"
#endif
#ifdef HAVE_AMESOS_SCALAPACK
#include "Amesos_Scalapack.h"
#endif
#ifdef HAVE_AMESOS_UMFPACK
#include "Amesos_Umfpack.h"
#endif
#ifdef HAVE_AMESOS_SUPERLUDIST
#include "Amesos_Superludist.h"
#endif
#ifdef HAVE_AMESOS_SUPERLU
#include "Amesos_Superlu.h"
#endif
#ifdef HAVE_AMESOS_DSCPACK
#include "Amesos_Dscpack.h"
#endif
#ifdef HAVE_AMESOS_PARDISO
#include "Amesos_Pardiso.h"
#endif
#ifdef HAVE_AMESOS_TAUCS
#include "Amesos_Taucs.h"
#endif
#ifdef HAVE_AMESOS_PARAKLETE
#include "Amesos_Paraklete.h"
#endif
namespace {
const std::string epetraFwdOp_str = "epetraFwdOp";
} // namespace
namespace Thyra {
// Parameter names for Paramter List
const std::string AmesosLinearOpWithSolveFactory::SolverType_name = "Solver Type";
const std::string AmesosLinearOpWithSolveFactory::RefactorizationPolicy_name = "Refactorization Policy";
const std::string AmesosLinearOpWithSolveFactory::ThrowOnPreconditionerInput_name = "Throw on Preconditioner Input";
const std::string AmesosLinearOpWithSolveFactory::Amesos_Settings_name = "Amesos Settings";
// Constructors/initializers/accessors
AmesosLinearOpWithSolveFactory::~AmesosLinearOpWithSolveFactory()
{
#ifdef TEUCHOS_DEBUG
if(paramList_.get())
paramList_->validateParameters(
*this->getValidParameters(),0 // Only validate this level for now!
);
#endif
}
AmesosLinearOpWithSolveFactory::AmesosLinearOpWithSolveFactory(
const Amesos::ESolverType solverType
,const Amesos::ERefactorizationPolicy refactorizationPolicy
,const bool throwOnPrecInput
)
:epetraFwdOpViewExtractor_(Teuchos::rcp(new EpetraOperatorViewExtractorStd()))
,solverType_(solverType)
,refactorizationPolicy_(refactorizationPolicy)
,throwOnPrecInput_(throwOnPrecInput)
{}
// Overridden from LinearOpWithSolveFactoryBase
bool AmesosLinearOpWithSolveFactory::isCompatible(
const LinearOpSourceBase<double> &fwdOpSrc
) const
{
using Teuchos::outArg;
RCP<const LinearOpBase<double> >
fwdOp = fwdOpSrc.getOp();
RCP<const Epetra_Operator> epetraFwdOp;
EOpTransp epetraFwdOpTransp;
EApplyEpetraOpAs epetraFwdOpApplyAs;
EAdjointEpetraOp epetraFwdOpAdjointSupport;
double epetraFwdOpScalar;
epetraFwdOpViewExtractor_->getEpetraOpView(
fwdOp,
outArg(epetraFwdOp), outArg(epetraFwdOpTransp),
outArg(epetraFwdOpApplyAs), outArg(epetraFwdOpAdjointSupport),
outArg(epetraFwdOpScalar)
);
if( !dynamic_cast<const Epetra_RowMatrix*>(&*epetraFwdOp) )
return false;
return true;
}
RCP<LinearOpWithSolveBase<double> >
AmesosLinearOpWithSolveFactory::createOp() const
{
return Teuchos::rcp(new AmesosLinearOpWithSolve());
}
void AmesosLinearOpWithSolveFactory::initializeOp(
const RCP<const LinearOpSourceBase<double> > &fwdOpSrc
,LinearOpWithSolveBase<double> *Op
,const ESupportSolveUse /* supportSolveUse */
) const
{
using Teuchos::outArg;
THYRA_FUNC_TIME_MONITOR("Stratimikos: AmesosLOWSF");
#ifdef TEUCHOS_DEBUG
TEUCHOS_TEST_FOR_EXCEPT(Op==NULL);
#endif
const RCP<const LinearOpBase<double> >
fwdOp = fwdOpSrc->getOp();
//
// Unwrap and get the forward Epetra_Operator object
//
RCP<const Epetra_Operator> epetraFwdOp;
EOpTransp epetraFwdOpTransp;
EApplyEpetraOpAs epetraFwdOpApplyAs;
EAdjointEpetraOp epetraFwdOpAdjointSupport;
double epetraFwdOpScalar;
epetraFwdOpViewExtractor_->getEpetraOpView(
fwdOp,
outArg(epetraFwdOp), outArg(epetraFwdOpTransp),
outArg(epetraFwdOpApplyAs), outArg(epetraFwdOpAdjointSupport),
outArg(epetraFwdOpScalar)
);
// Get the AmesosLinearOpWithSolve object
AmesosLinearOpWithSolve
*amesosOp = &Teuchos::dyn_cast<AmesosLinearOpWithSolve>(*Op);
//
// Determine if we must start over or not
//
bool startOver = ( amesosOp->get_amesosSolver()==Teuchos::null );
if(!startOver) {
startOver =
(
epetraFwdOpTransp != amesosOp->get_amesosSolverTransp() ||
epetraFwdOp.get() != amesosOp->get_epetraLP()->GetOperator()
// We must start over if the matrix object changes. This is a
// weakness of Amesos but there is nothing I can do about this right
// now!
);
}
//
// Update the amesos solver
//
if(startOver) {
//
// This LOWS object has not be initialized yet or is not compatible with the existing
//
// so this is where we setup everything from the ground up.
//
// Create the linear problem and set the operator with memory of RCP to Epetra_Operator view!
RCP<Epetra_LinearProblem>
epetraLP = Teuchos::rcp(new Epetra_LinearProblem());
epetraLP->SetOperator(const_cast<Epetra_Operator*>(&*epetraFwdOp));
Teuchos::set_extra_data< RCP<const Epetra_Operator> >( epetraFwdOp, epetraFwdOp_str,
Teuchos::inOutArg(epetraLP) );
// Create the concrete solver
RCP<Amesos_BaseSolver>
amesosSolver;
{
THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AmesosLOWSF:InitConstruct",
InitConstruct);
switch(solverType_) {
case Thyra::Amesos::LAPACK :
amesosSolver = Teuchos::rcp(new Amesos_Lapack(*epetraLP));
break;
#ifdef HAVE_AMESOS_KLU
case Thyra::Amesos::KLU :
amesosSolver = Teuchos::rcp(new Amesos_Klu(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_PASTIX
case Thyra::Amesos::PASTIX :
amesosSolver = Teuchos::rcp(new Amesos_Pastix(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_MUMPS
case Thyra::Amesos::MUMPS :
amesosSolver = Teuchos::rcp(new Amesos_Mumps(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_SCALAPACK
case Thyra::Amesos::SCALAPACK :
amesosSolver = Teuchos::rcp(new Amesos_Scalapack(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_UMFPACK
case Thyra::Amesos::UMFPACK :
amesosSolver = Teuchos::rcp(new Amesos_Umfpack(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_SUPERLUDIST
case Thyra::Amesos::SUPERLUDIST :
amesosSolver = Teuchos::rcp(new Amesos_Superludist(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_SUPERLU
case Thyra::Amesos::SUPERLU :
amesosSolver = Teuchos::rcp(new Amesos_Superlu(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_DSCPACK
case Thyra::Amesos::DSCPACK :
amesosSolver = Teuchos::rcp(new Amesos_Dscpack(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_PARDISO
case Thyra::Amesos::PARDISO :
amesosSolver = Teuchos::rcp(new Amesos_Pardiso(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_TAUCS
case Thyra::Amesos::TAUCS :
amesosSolver = Teuchos::rcp(new Amesos_Taucs(*epetraLP));
break;
#endif
#ifdef HAVE_AMESOS_PARAKLETE
case Thyra::Amesos::PARAKLETE :
amesosSolver = Teuchos::rcp(new Amesos_Paraklete(*epetraLP));
break;
#endif
default:
TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error
,"Error, the solver type ID = " << solverType_ << " is invalid!"
);
}
}
// Set the parameters
if(paramList_.get()) amesosSolver->SetParameters(paramList_->sublist("Amesos Settings"));
// Do the initial factorization
{
THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AmesosLOWSF:Symbolic", Symbolic);
const int err = amesosSolver->SymbolicFactorization();
TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure,
"Error, SymbolicFactorization() on amesos solver of type \'"<<Teuchos::typeName(*amesosSolver)<<"\'\n"
"returned error code "<<err<<"!" );
}
{
THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AmesosLOWSF:Factor", Factor);
const int err = amesosSolver->NumericFactorization();
TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure,
"Error, NumericFactorization() on amesos solver of type \'"<<Teuchos::typeName(*amesosSolver)<<"\'\n"
"returned error code "<<err<<"!" );
}
// Initialize the LOWS object and we are done!
amesosOp->initialize(fwdOp,fwdOpSrc,epetraLP,amesosSolver,epetraFwdOpTransp,epetraFwdOpScalar);
}
else {
//
// This LOWS object has already be initialized once so we must just reset
// the matrix and refactor it.
//
// Get non-const pointers to the linear problem and the amesos solver.
// These const-casts are just fine since the amesosOp in non-const.
RCP<Epetra_LinearProblem>
epetraLP = Teuchos::rcp_const_cast<Epetra_LinearProblem>(amesosOp->get_epetraLP());
RCP<Amesos_BaseSolver>
amesosSolver = amesosOp->get_amesosSolver();
// Reset the forward operator with memory of RCP to Epetra_Operator view!
epetraLP->SetOperator(const_cast<Epetra_Operator*>(&*epetraFwdOp));
Teuchos::get_nonconst_extra_data<RCP<const Epetra_Operator> >(epetraLP,epetraFwdOp_str) = epetraFwdOp;
// Reset the parameters
if(paramList_.get()) amesosSolver->SetParameters(paramList_->sublist(Amesos_Settings_name));
// Repivot if asked
if(refactorizationPolicy_==Amesos::REPIVOT_ON_REFACTORIZATION) {
THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AmesosLOWSF:Symbolic", Symbolic);
const int err = amesosSolver->SymbolicFactorization();
TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure,
"Error, SymbolicFactorization() on amesos solver of type \'"<<Teuchos::typeName(*amesosSolver)<<"\'\n"
"returned error code "<<err<<"!" );
}
{
THYRA_FUNC_TIME_MONITOR_DIFF("Stratimikos: AmesosLOWSF::Factor", Factor);
const int err = amesosSolver->NumericFactorization();
TEUCHOS_TEST_FOR_EXCEPTION( 0!=err, CatastrophicSolveFailure,
"Error, NumericFactorization() on amesos solver of type \'"<<Teuchos::typeName(*amesosSolver)<<"\'\n"
"returned error code "<<err<<"!" );
}
/* ToDo: Put this back in once PrintStatus accepts an std::ostream!
OsTab tab(out);
amesosSolver->PrintStatus()
*/
// Reinitialize the LOWS object and we are done! (we must do this to get the
// possibly new transpose and scaling factors back in)
amesosOp->initialize(fwdOp,fwdOpSrc,epetraLP,amesosSolver,epetraFwdOpTransp,epetraFwdOpScalar);
}
amesosOp->setOStream(this->getOStream());
amesosOp->setVerbLevel(this->getVerbLevel());
}
bool AmesosLinearOpWithSolveFactory::supportsPreconditionerInputType(const EPreconditionerInputType /* precOpType */) const
{
return false;
}
void AmesosLinearOpWithSolveFactory::initializePreconditionedOp(
const RCP<const LinearOpSourceBase<double> > &fwdOpSrc
,const RCP<const PreconditionerBase<double> > &/* prec */
,LinearOpWithSolveBase<double> *Op
,const ESupportSolveUse supportSolveUse
) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
this->throwOnPrecInput_, std::logic_error
,"Error, the concrete implementation described as \'"<<this->description()<<"\' does not support preconditioners "
"and has been configured to throw this exception when the initializePreconditionedOp(...) function is called!"
);
this->initializeOp(fwdOpSrc,Op,supportSolveUse); // Ignore the preconditioner!
}
void AmesosLinearOpWithSolveFactory::initializePreconditionedOp(
const RCP<const LinearOpSourceBase<double> > &fwdOpSrc
,const RCP<const LinearOpSourceBase<double> > &/* approxFwdOpSrc */
,LinearOpWithSolveBase<double> *Op
,const ESupportSolveUse supportSolveUse
) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
this->throwOnPrecInput_, std::logic_error
,"Error, the concrete implementation described as \'"<<this->description()<<"\' does not support preconditioners "
"and has been configured to throw this exception when the initializePreconditionedOp(...) function is called!"
);
this->initializeOp(fwdOpSrc,Op,supportSolveUse); // Ignore the preconditioner!
}
void AmesosLinearOpWithSolveFactory::uninitializeOp(
LinearOpWithSolveBase<double> *Op
,RCP<const LinearOpSourceBase<double> > *fwdOpSrc
,RCP<const PreconditionerBase<double> > *prec
,RCP<const LinearOpSourceBase<double> > *approxFwdOpSrc
,ESupportSolveUse * /* supportSolveUse */
) const
{
#ifdef TEUCHOS_DEBUG
TEUCHOS_TEST_FOR_EXCEPT(Op==NULL);
#endif
AmesosLinearOpWithSolve
*amesosOp = &Teuchos::dyn_cast<AmesosLinearOpWithSolve>(*Op);
RCP<const LinearOpSourceBase<double> >
_fwdOpSrc = amesosOp->extract_fwdOpSrc(); // Will be null if uninitialized!
if(_fwdOpSrc.get()) {
// Erase the Epetra_Operator view of the forward operator!
RCP<Epetra_LinearProblem> epetraLP = amesosOp->get_epetraLP();
Teuchos::get_nonconst_extra_data< RCP<const Epetra_Operator> >(
epetraLP,epetraFwdOp_str
)
= Teuchos::null;
// Note, we did not erase the address of the operator in
// epetraLP->GetOperator() since it seems that the amesos solvers do not
// recheck the value of GetProblem()->GetOperator() so you had better not
// rest this!
}
if(fwdOpSrc) *fwdOpSrc = _fwdOpSrc; // It is fine if the client does not want this object back!
if(prec) *prec = Teuchos::null; // We never keep a preconditioner!
if(approxFwdOpSrc) *approxFwdOpSrc = Teuchos::null; // We never keep an approximate fwd operator!
}
// Overridden from ParameterListAcceptor
void AmesosLinearOpWithSolveFactory::setParameterList(
RCP<Teuchos::ParameterList> const& paramList
)
{
TEUCHOS_TEST_FOR_EXCEPT(paramList.get()==NULL);
paramList->validateParameters(*this->getValidParameters(),0); // Only validate this level for now!
paramList_ = paramList;
solverType_ =
Amesos::solverTypeNameToEnumMap.get<Amesos::ESolverType>(
paramList_->get(
SolverType_name
,Amesos::toString(solverType_)
)
,paramList_->name()+"->"+SolverType_name
);
refactorizationPolicy_ =
Amesos::refactorizationPolicyNameToEnumMap.get<Amesos::ERefactorizationPolicy>(
paramList_->get(
RefactorizationPolicy_name
,Amesos::toString(refactorizationPolicy_)
)
,paramList_->name()+"->"+RefactorizationPolicy_name
);
throwOnPrecInput_ = paramList_->get(ThrowOnPreconditionerInput_name,throwOnPrecInput_);
Teuchos::readVerboseObjectSublist(&*paramList_,this);
}
RCP<Teuchos::ParameterList>
AmesosLinearOpWithSolveFactory::getNonconstParameterList()
{
return paramList_;
}
RCP<Teuchos::ParameterList>
AmesosLinearOpWithSolveFactory::unsetParameterList()
{
RCP<Teuchos::ParameterList> _paramList = paramList_;
paramList_ = Teuchos::null;
return _paramList;
}
RCP<const Teuchos::ParameterList>
AmesosLinearOpWithSolveFactory::getParameterList() const
{
return paramList_;
}
RCP<const Teuchos::ParameterList>
AmesosLinearOpWithSolveFactory::getValidParameters() const
{
return generateAndGetValidParameters();
}
// Public functions overridden from Teuchos::Describable
std::string AmesosLinearOpWithSolveFactory::description() const
{
std::ostringstream oss;
oss << "Thyra::AmesosLinearOpWithSolveFactory{";
oss << "solverType=" << toString(solverType_);
oss << "}";
return oss.str();
}
// private
RCP<const Teuchos::ParameterList>
AmesosLinearOpWithSolveFactory::generateAndGetValidParameters()
{
static RCP<Teuchos::ParameterList> validParamList;
if(validParamList.get()==NULL) {
validParamList = Teuchos::rcp(new Teuchos::ParameterList("Amesos"));
validParamList->set(
SolverType_name
#ifdef HAVE_AMESOS_KLU
,Amesos::toString(Amesos::KLU)
#else
,Amesos::toString(Amesos::LAPACK)
#endif
);
validParamList->set(RefactorizationPolicy_name,Amesos::toString(Amesos::REPIVOT_ON_REFACTORIZATION));
validParamList->set(ThrowOnPreconditionerInput_name,bool(true));
validParamList->sublist(Amesos_Settings_name).setParameters(::Amesos::GetValidParameters());
Teuchos::setupVerboseObjectSublist(&*validParamList);
}
return validParamList;
}
} // namespace Thyra
#endif // SUN_CXX