-
Notifications
You must be signed in to change notification settings - Fork 559
/
AnasaziLOBPCG.hpp
2309 lines (2056 loc) · 87.6 KB
/
AnasaziLOBPCG.hpp
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// @HEADER
// *****************************************************************************
// Anasazi: Block Eigensolvers Package
//
// Copyright 2004 NTESS and the Anasazi contributors.
// SPDX-License-Identifier: BSD-3-Clause
// *****************************************************************************
// @HEADER
/*! \file AnasaziLOBPCG.hpp
\brief Implementation of the locally-optimal block preconditioned conjugate gradient (LOBPCG) method
*/
/*
LOBPCG contains local storage of up to 10*blockSize_ vectors, representing 10 entities
X,H,P,R
KX,KH,KP (product of K and the above)
MX,MH,MP (product of M and the above, not allocated if we don't have an M matrix)
If full orthogonalization is enabled, one extra multivector of blockSize_ vectors is required to
compute the local update of X and P.
A solver is bound to an eigenproblem at declaration.
Other solver parameters (e.g., block size, auxiliary vectors) can be changed dynamically.
The orthogonalization manager is used to project away from the auxiliary vectors.
If full orthogonalization is enabled, the orthogonalization manager is also used to construct an M orthonormal basis.
The orthogonalization manager is subclass of MatOrthoManager, which LOBPCG assumes to be defined by the M inner product.
LOBPCG will not work correctly if the orthomanager uses a different inner product.
*/
#ifndef ANASAZI_LOBPCG_HPP
#define ANASAZI_LOBPCG_HPP
#include "AnasaziTypes.hpp"
#include "AnasaziEigensolver.hpp"
#include "AnasaziMultiVecTraits.hpp"
#include "AnasaziOperatorTraits.hpp"
#include "Teuchos_ScalarTraits.hpp"
#include "AnasaziMatOrthoManager.hpp"
#include "AnasaziSolverUtils.hpp"
#include "Teuchos_LAPACK.hpp"
#include "Teuchos_BLAS.hpp"
#include "Teuchos_SerialDenseMatrix.hpp"
#include "Teuchos_ParameterList.hpp"
#include "Teuchos_TimeMonitor.hpp"
/*! \class Anasazi::LOBPCG
\brief This class provides the Locally Optimal Block Preconditioned Conjugate Gradient (%LOBPCG) iteration, a preconditioned iteration for solving linear Hermitian eigenproblems.
This implementation is a modification of the one found in
A. Knyazev, "Toward the optimal preconditioned eigensolver:
Locally optimal block preconditioner conjugate gradient method",
SIAM J. Sci. Comput., vol 23, n 2, pp. 517-541.
The modification consists of the orthogonalization steps recommended in
U. Hetmaniuk and R. Lehoucq, "Basis Selection in LOBPCG", Journal of Computational Physics.
These modifcation are referred to as full orthogonalization, and consist of also conducting
the local optimization using an orthonormal basis.
\ingroup anasazi_solver_framework
\author Chris Baker, Ulrich Hetmaniuk, Rich Lehoucq, Heidi Thornquist
*/
namespace Anasazi {
//! @name LOBPCG Structures
//@{
/** \brief Structure to contain pointers to Anasazi state variables.
*
* This struct is utilized by LOBPCG::initialize() and LOBPCG::getState().
*/
template <class ScalarType, class MultiVector>
struct LOBPCGState {
//! The current test basis.
Teuchos::RCP<const MultiVector> V;
//! The image of the current test basis under K.
Teuchos::RCP<const MultiVector> KV;
//! The image of the current test basis under M, or Teuchos::null if M was not specified.
Teuchos::RCP<const MultiVector> MV;
//! The current eigenvectors.
Teuchos::RCP<const MultiVector> X;
//! The image of the current eigenvectors under K.
Teuchos::RCP<const MultiVector> KX;
//! The image of the current eigenvectors under M, or Teuchos::null if M was not specified.
Teuchos::RCP<const MultiVector> MX;
//! The current search direction.
Teuchos::RCP<const MultiVector> P;
//! The image of the current search direction under K.
Teuchos::RCP<const MultiVector> KP;
//! The image of the current search direction under M, or Teuchos::null if M was not specified.
Teuchos::RCP<const MultiVector> MP;
/*! \brief The current preconditioned residual vectors.
*
* H is only useful when LOBPCG::iterate() throw a LOBPCGRitzFailure exception.
*/
Teuchos::RCP<const MultiVector> H;
//! The image of the current preconditioned residual vectors under K.
Teuchos::RCP<const MultiVector> KH;
//! The image of the current preconditioned residual vectors under M, or Teuchos::null if M was not specified.
Teuchos::RCP<const MultiVector> MH;
//! The current residual vectors.
Teuchos::RCP<const MultiVector> R;
//! The current Ritz values.
Teuchos::RCP<const std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > T;
LOBPCGState() :
V(Teuchos::null),KV(Teuchos::null),MV(Teuchos::null),
X(Teuchos::null),KX(Teuchos::null),MX(Teuchos::null),
P(Teuchos::null),KP(Teuchos::null),MP(Teuchos::null),
H(Teuchos::null),KH(Teuchos::null),MH(Teuchos::null),
R(Teuchos::null),T(Teuchos::null) {};
};
//@}
//! @name LOBPCG Exceptions
//@{
/** \brief LOBPCGRitzFailure is thrown when the LOBPCG solver is unable to
* continue a call to LOBPCG::iterate() due to a failure of the algorithm.
*
* This signals that the Rayleigh-Ritz analysis over the subspace \c
* colsp([X H P]) detected ill-conditioning of the projected mass matrix
* and the inability to generate a set of orthogonal eigenvectors for
* the projected problem.
*
* This exception is only thrown from the LOBPCG::iterate() routine. After
* catching this exception, the user can recover the subspace via
* LOBPCG::getState(). This information can be used to restart the solver.
*
*/
class LOBPCGRitzFailure : public AnasaziError {public:
LOBPCGRitzFailure(const std::string& what_arg) : AnasaziError(what_arg)
{}};
/** \brief LOBPCGInitFailure is thrown when the LOBPCG solver is unable to
* generate an initial iterate in the LOBPCG::initialize() routine.
*
* This exception is thrown from the LOBPCG::initialize() method, which is
* called by the user or from the LOBPCG::iterate() method when isInitialized()
* == \c false.
*
* In the case that this exception is thrown, LOBPCG::hasP() and
* LOBPCG::isInitialized() will be \c false and the user will need to provide
* a new initial iterate to the solver.
*
*/
class LOBPCGInitFailure : public AnasaziError {public:
LOBPCGInitFailure(const std::string& what_arg) : AnasaziError(what_arg)
{}};
/** \brief LOBPCGOrthoFailure is thrown when an orthogonalization attempt
* fails.
*
* This is thrown in one of two scenarstd::ios. After preconditioning the residual,
* the orthogonalization manager is asked to orthogonalize the preconditioned
* residual (H) against the auxiliary vectors. If full orthogonalization
* is enabled, H is also orthogonalized against X and P and normalized.
*
* The second scenario involves the generation of new X and P from the
* basis [X H P]. When full orthogonalization is enabled, an attempt is
* made to select coefficients for X and P so that they will be
* mutually orthogonal and orthonormal.
*
* If either of these attempts fail, the solver throws an LOBPCGOrthoFailure
* exception.
*/
class LOBPCGOrthoFailure : public AnasaziError {public:
LOBPCGOrthoFailure(const std::string& what_arg) : AnasaziError(what_arg)
{}};
//@}
template <class ScalarType, class MV, class OP>
class LOBPCG : public Eigensolver<ScalarType,MV,OP> {
public:
//! @name Constructor/Destructor
//@{
/*! \brief %LOBPCG constructor with eigenproblem, solver utilities, and parameter list of solver options.
*
* This constructor takes pointers required by the eigensolver, in addition
* to a parameter list of options for the eigensolver. These options include the following:
* - "Block Size" - an \c int specifying the block size used by the algorithm. This can also be specified using the setBlockSize() method.
* - "Full Ortho" - a \c bool specifying whether the solver should employ a full orthogonalization technique. This can also be specified using the setFullOrtho() method.
*/
LOBPCG( const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
const Teuchos::RCP<SortManager<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
const Teuchos::RCP<OutputManager<ScalarType> > &printer,
const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
Teuchos::ParameterList ¶ms
);
//! %LOBPCG destructor
virtual ~LOBPCG() {};
//@}
//! @name Solver methods
//@{
/*! \brief This method performs %LOBPCG iterations until the status test
* indicates the need to stop or an error occurs (in which case, an
* exception is thrown).
*
* iterate() will first determine whether the solver is initialized; if
* not, it will call initialize() using default arguments. After
* initialization, the solver performs %LOBPCG iterations until the status
* test evaluates as Passed, at which point the method returns to the
* caller.
*
* The %LOBPCG iteration proceeds as follows:
* -# The current residual (R) is preconditioned to form H
* -# H is orthogonalized against the auxiliary vectors and, if full orthogonalization\n
* is enabled, against X and P.
* -# The basis [X H P] is used to project the problem matrices.
* -# The projected eigenproblem is solved, and the desired eigenvectors and eigenvalues are selected.
* -# These are used to form the new eigenvector estimates (X) and the search directions (P).\n
* If full orthogonalization is enabled, these are generated to be mutually orthogonal and with orthonormal columns.
* -# The new residual (R) is formed.
*
* The status test is queried at the beginning of the iteration.
*
* Possible exceptions thrown include std::logic_error, std::invalid_argument or
* one of the LOBPCG-specific exceptions.
*
*/
void iterate();
/*! \brief Initialize the solver to an iterate, optionally providing the
* Ritz values, residual, and search direction.
*
* \note LOBPCGState contains fields V, KV and MV: These are ignored by initialize()
*
* The %LOBPCG eigensolver contains a certain amount of state relating to
* the current iterate, including the current residual, the current search
* direction, and the images of these spaces under the eigenproblem operators.
*
* initialize() gives the user the opportunity to manually set these,
* although this must be done with caution, abiding by the rules
* given below. All notions of orthogonality and orthonormality are derived
* from the inner product specified by the orthogonalization manager.
*
* \post
* - isInitialized() == true (see post-conditions of isInitialize())
* - If newstate.P != Teuchos::null, hasP() == true.\n
* Otherwise, hasP() == false
*
* The user has the option of specifying any component of the state using
* initialize(). However, these arguments are assumed to match the
* post-conditions specified under isInitialized(). Any component of the
* state (i.e., KX) not given to initialize() will be generated.
*
*/
void initialize(LOBPCGState<ScalarType,MV>& newstate);
/*! \brief Initialize the solver with the initial vectors from the eigenproblem
* or random data.
*/
void initialize();
/*! \brief Indicates whether the solver has been initialized or not.
*
* \return bool indicating the state of the solver.
* \post
* If isInitialized() == \c true:
* - X is orthogonal to auxiliary vectors and has orthonormal columns
* - KX == Op*X
* - MX == M*X if M != Teuchos::null\n
* Otherwise, MX == Teuchos::null
* - getRitzValues() returns the sorted Ritz values with respect to X
* - getResNorms(), getRes2Norms(), getRitzResNorms() are correct
* - If hasP() == \c true,
* - P orthogonal to auxiliary vectors
* - If getFullOrtho() == \c true,
* - P is orthogonal to X and has orthonormal columns
* - KP == Op*P
* - MP == M*P if M != Teuchos::null\n
* Otherwise, MP == Teuchos::null
*/
bool isInitialized() const;
/*! \brief Get the current state of the eigensolver.
*
* The data is only valid if isInitialized() == \c true. The
* data for the search directions P is only meaningful if hasP() == \c
* true. Finally, the data for the preconditioned residual (H) is only meaningful in the situation where
* the solver throws an ::LOBPCGRitzFailure exception during iterate().
*
* \returns An LOBPCGState object containing const views to the current
* solver state.
*/
LOBPCGState<ScalarType,MV> getState() const;
//@}
//! @name Status methods
//@{
//! \brief Get the current iteration count.
int getNumIters() const;
//! \brief Reset the iteration count.
void resetNumIters();
/*! \brief Get the Ritz vectors from the previous iteration.
\return A multivector with getBlockSize() vectors containing
the sorted Ritz vectors corresponding to the most significant Ritz values.
The i-th vector of the return corresponds to the i-th Ritz vector; there is no need to use
getRitzIndex().
*/
Teuchos::RCP<const MV> getRitzVectors();
/*! \brief Get the Ritz values from the previous iteration.
*
* \return A vector of length getCurSubspaceDim() containing the Ritz values from the
* previous projected eigensolve.
*/
std::vector<Value<ScalarType> > getRitzValues();
/*! \brief Get the index used for extracting Ritz vectors from getRitzVectors().
*
* Because BlockDavidson is a Hermitian solver, all Ritz values are real and all Ritz vectors can be represented in a
* single column of a multivector. Therefore, getRitzIndex() is not needed when using the output from getRitzVectors().
*
* \return An \c int vector of size getCurSubspaceDim() composed of zeros.
*/
std::vector<int> getRitzIndex();
/*! \brief Get the current residual norms
*
* \return A vector of length getBlockSize() containing the norms of the
* residuals, with respect to the orthogonalization manager norm() method.
*/
std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getResNorms();
/*! \brief Get the current residual 2-norms
*
* \return A vector of length getBlockSize() containing the 2-norms of the
* residuals.
*/
std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRes2Norms();
/*! \brief Get the 2-norms of the residuals.
*
* The Ritz residuals are not defined for the %LOBPCG iteration. Hence, this method returns the
* 2-norms of the direct residuals, and is equivalent to calling getRes2Norms().
*
* \return A vector of length getBlockSize() containing the 2-norms of the direct residuals.
*/
std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> getRitzRes2Norms();
/*! \brief Get the dimension of the search subspace used to generate the current eigenvectors and eigenvalues.
*
* %LOBPCG employs a sequential subspace iteration, maintaining a fixed-rank basis, as opposed to an expanding subspace
* mechanism employed by Krylov-subspace solvers like BlockKrylovSchur and BlockDavidson.
*
* \return An integer specifying the rank of the subspace generated by the eigensolver. If isInitialized() == \c false,
* the return is 0. Otherwise, the return will be 2*getBlockSize() or 3*getBlockSize().
*/
int getCurSubspaceDim() const;
/*! \brief Get the maximum dimension allocated for the search subspace. For %LOBPCG, this always returns 3*getBlockSize(), the dimension of the
* subspace colspan([X H P]).
*/
int getMaxSubspaceDim() const;
//@}
//! @name Accessor routines from Eigensolver
//@{
//! Set a new StatusTest for the solver.
void setStatusTest(Teuchos::RCP<StatusTest<ScalarType,MV,OP> > test);
//! Get the current StatusTest used by the solver.
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > getStatusTest() const;
//! Get a constant reference to the eigenvalue problem.
const Eigenproblem<ScalarType,MV,OP>& getProblem() const;
/*! \brief Set the blocksize to be used by the iterative solver in solving
* this eigenproblem.
*
* If the block size is reduced, then the new iterate (and residual and
* search direction) are chosen as the subset of the current iterate
* preferred by the sort manager. Otherwise, the solver state is set to
* uninitialized.
*/
void setBlockSize(int blockSize);
//! Get the blocksize to be used by the iterative solver in solving this eigenproblem.
int getBlockSize() const;
/*! \brief Set the auxiliary vectors for the solver.
*
* Because the current iterate X and search direction P cannot be assumed
* orthogonal to the new auxiliary vectors, a call to setAuxVecs() with a
* non-empty argument will reset the solver to the uninitialized state.
*
* In order to preserve the current state, the user will need to extract
* it from the solver using getState(), orthogonalize it against the new
* auxiliary vectors, and manually reinitialize the solver using
* initialize().
*/
void setAuxVecs(const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs);
//! Get the current auxiliary vectors.
Teuchos::Array<Teuchos::RCP<const MV> > getAuxVecs() const;
//@}
//! @name %LOBPCG-specific accessor routines
//@{
/*! \brief Instruct the LOBPCG iteration to use full orthogonality.
*
* If the getFullOrtho() == \c false and isInitialized() == \c true and hasP() == \c true, then
* P will be invalidated by setting full orthogonalization to \c true.
*/
void setFullOrtho(bool fullOrtho);
//! Determine if the LOBPCG iteration is using full orthogonality.
bool getFullOrtho() const;
//! Indicates whether the search direction given by getState() is valid.
bool hasP();
//@}
//! @name Output methods
//@{
//! This method requests that the solver print out its current status to screen.
void currentStatus(std::ostream &os);
//@}
private:
//
//
//
void setupViews();
//
// Convenience typedefs
//
typedef SolverUtils<ScalarType,MV,OP> Utils;
typedef MultiVecTraits<ScalarType,MV> MVT;
typedef OperatorTraits<ScalarType,MV,OP> OPT;
typedef Teuchos::ScalarTraits<ScalarType> SCT;
typedef typename SCT::magnitudeType MagnitudeType;
const MagnitudeType ONE;
const MagnitudeType ZERO;
const MagnitudeType NANVAL;
//
// Internal structs
//
struct CheckList {
bool checkX, checkMX, checkKX;
bool checkH, checkMH;
bool checkP, checkMP, checkKP;
bool checkR, checkQ;
CheckList() : checkX(false),checkMX(false),checkKX(false),
checkH(false),checkMH(false),
checkP(false),checkMP(false),checkKP(false),
checkR(false),checkQ(false) {};
};
//
// Internal methods
//
std::string accuracyCheck(const CheckList &chk, const std::string &where) const;
//
// Classes inputed through constructor that define the eigenproblem to be solved.
//
const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > problem_;
const Teuchos::RCP<SortManager<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > sm_;
const Teuchos::RCP<OutputManager<ScalarType> > om_;
Teuchos::RCP<StatusTest<ScalarType,MV,OP> > tester_;
const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > orthman_;
//
// Information obtained from the eigenproblem
//
Teuchos::RCP<const OP> Op_;
Teuchos::RCP<const OP> MOp_;
Teuchos::RCP<const OP> Prec_;
bool hasM_;
//
// Internal timers
//
Teuchos::RCP<Teuchos::Time> timerOp_, timerMOp_, timerPrec_,
timerSort_,
timerLocalProj_, timerDS_,
timerLocalUpdate_, timerCompRes_,
timerOrtho_, timerInit_;
//
// Counters
//
// Number of operator applications
int count_ApplyOp_, count_ApplyM_, count_ApplyPrec_;
//
// Algorithmic parameters.
//
// blockSize_ is the solver block size
int blockSize_;
//
// fullOrtho_ dictates whether the orthogonalization procedures specified by Hetmaniuk and Lehoucq should
// be activated (see citations at the top of this file)
bool fullOrtho_;
//
// Current solver state
//
// initialized_ specifies that the basis vectors have been initialized and the iterate() routine
// is capable of running; _initialize is controlled by the initialize() member method
// For the implications of the state of initialized_, please see documentation for initialize()
bool initialized_;
//
// nevLocal_ reflects how much of the current basis is valid (0 <= nevLocal_ <= 3*blockSize_)
// this tells us how many of the values in theta_ are valid Ritz values
int nevLocal_;
//
// hasP_ tells us whether there is valid data in P (and KP,MP)
bool hasP_;
//
// State Multivecs
// V_, KV_ MV_ and R_ are primary pointers to allocated multivectors
Teuchos::RCP<MV> V_, KV_, MV_, R_;
// the rest are multivector views into V_, KV_ and MV_
Teuchos::RCP<MV> X_, KX_, MX_,
H_, KH_, MH_,
P_, KP_, MP_;
//
// if fullOrtho_ == true, then we must produce the following on every iteration:
// [newX newP] = [X H P] [CX;CP]
// the structure of [CX;CP] when using full orthogonalization does not allow us to
// do this in situ, and R_ does not have enough storage for newX and newP. therefore,
// we must allocate additional storage for this.
// otherwise, when not using full orthogonalization, the structure
// [newX newP] = [X H P] [CX1 0 ]
// [CX2 CP2] allows us to work using only R as work space
// [CX3 CP3]
Teuchos::RCP<MV> tmpmvec_;
//
// auxiliary vectors
Teuchos::Array<Teuchos::RCP<const MV> > auxVecs_;
int numAuxVecs_;
//
// Number of iterations that have been performed.
int iter_;
//
// Current eigenvalues, residual norms
std::vector<MagnitudeType> theta_, Rnorms_, R2norms_;
//
// are the residual norms current with the residual?
bool Rnorms_current_, R2norms_current_;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
// Constructor
template <class ScalarType, class MV, class OP>
LOBPCG<ScalarType,MV,OP>::LOBPCG(
const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
const Teuchos::RCP<SortManager<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> > &sorter,
const Teuchos::RCP<OutputManager<ScalarType> > &printer,
const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > &tester,
const Teuchos::RCP<MatOrthoManager<ScalarType,MV,OP> > &ortho,
Teuchos::ParameterList ¶ms
) :
ONE(Teuchos::ScalarTraits<MagnitudeType>::one()),
ZERO(Teuchos::ScalarTraits<MagnitudeType>::zero()),
NANVAL(Teuchos::ScalarTraits<MagnitudeType>::nan()),
// problem, tools
problem_(problem),
sm_(sorter),
om_(printer),
tester_(tester),
orthman_(ortho),
// timers, counters
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
timerOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation Op*x")),
timerMOp_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation M*x")),
timerPrec_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Operation Prec*x")),
timerSort_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Sorting eigenvalues")),
timerLocalProj_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Local projection")),
timerDS_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Direct solve")),
timerLocalUpdate_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Local update")),
timerCompRes_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Computing residuals")),
timerOrtho_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Orthogonalization")),
timerInit_(Teuchos::TimeMonitor::getNewTimer("Anasazi: LOBPCG::Initialization")),
#endif
count_ApplyOp_(0),
count_ApplyM_(0),
count_ApplyPrec_(0),
// internal data
blockSize_(0),
fullOrtho_(params.get("Full Ortho", true)),
initialized_(false),
nevLocal_(0),
hasP_(false),
auxVecs_( Teuchos::Array<Teuchos::RCP<const MV> >(0) ),
numAuxVecs_(0),
iter_(0),
Rnorms_current_(false),
R2norms_current_(false)
{
TEUCHOS_TEST_FOR_EXCEPTION(problem_ == Teuchos::null,std::invalid_argument,
"Anasazi::LOBPCG::constructor: user passed null problem pointer.");
TEUCHOS_TEST_FOR_EXCEPTION(sm_ == Teuchos::null,std::invalid_argument,
"Anasazi::LOBPCG::constructor: user passed null sort manager pointer.");
TEUCHOS_TEST_FOR_EXCEPTION(om_ == Teuchos::null,std::invalid_argument,
"Anasazi::LOBPCG::constructor: user passed null output manager pointer.");
TEUCHOS_TEST_FOR_EXCEPTION(tester_ == Teuchos::null,std::invalid_argument,
"Anasazi::LOBPCG::constructor: user passed null status test pointer.");
TEUCHOS_TEST_FOR_EXCEPTION(orthman_ == Teuchos::null,std::invalid_argument,
"Anasazi::LOBPCG::constructor: user passed null orthogonalization manager pointer.");
TEUCHOS_TEST_FOR_EXCEPTION(problem_->isProblemSet() == false, std::invalid_argument,
"Anasazi::LOBPCG::constructor: problem is not set.");
TEUCHOS_TEST_FOR_EXCEPTION(problem_->isHermitian() == false, std::invalid_argument,
"Anasazi::LOBPCG::constructor: problem is not Hermitian; LOBPCG requires Hermitian problem.");
// get the problem operators
Op_ = problem_->getOperator();
TEUCHOS_TEST_FOR_EXCEPTION(Op_ == Teuchos::null, std::invalid_argument,
"Anasazi::LOBPCG::constructor: problem provides no operator.");
MOp_ = problem_->getM();
Prec_ = problem_->getPrec();
hasM_ = (MOp_ != Teuchos::null);
// set the block size and allocate data
int bs = params.get("Block Size", problem_->getNEV());
setBlockSize(bs);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Set the block size and make necessary adjustments.
template <class ScalarType, class MV, class OP>
void LOBPCG<ScalarType,MV,OP>::setBlockSize (int newBS)
{
// time spent here counts towards timerInit_
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
Teuchos::TimeMonitor lcltimer( *timerInit_ );
#endif
// This routine only allocates space; it doesn't not perform any computation
// if size is decreased, take the first newBS vectors of all and leave state as is
// otherwise, grow/allocate space and set solver to unitialized
Teuchos::RCP<const MV> tmp;
// grab some Multivector to Clone
// in practice, getInitVec() should always provide this, but it is possible to use a
// Eigenproblem with nothing in getInitVec() by manually initializing with initialize();
// in case of that strange scenario, we will try to Clone from R_ because it is smaller
// than V_, and we don't want to keep V_ around longer than necessary
if (blockSize_ > 0) {
tmp = R_;
}
else {
tmp = problem_->getInitVec();
TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::logic_error,
"Anasazi::LOBPCG::setBlockSize(): eigenproblem did not specify initial vectors to clone from.");
}
TEUCHOS_TEST_FOR_EXCEPTION(newBS <= 0 || static_cast<ptrdiff_t>(newBS) > MVT::GetGlobalLength(*tmp),
std::invalid_argument, "Anasazi::LOBPCG::setBlockSize(): block size must be strictly positive.");
if (newBS == blockSize_) {
// do nothing
return;
}
else if (newBS < blockSize_ && initialized_) {
//
// shrink vectors
// release views so we can modify the bases
X_ = Teuchos::null;
KX_ = Teuchos::null;
MX_ = Teuchos::null;
H_ = Teuchos::null;
KH_ = Teuchos::null;
MH_ = Teuchos::null;
P_ = Teuchos::null;
KP_ = Teuchos::null;
MP_ = Teuchos::null;
// make new indices vectors
std::vector<int> newind(newBS), oldind(newBS);
for (int i=0; i<newBS; i++) {
newind[i] = i;
oldind[i] = i;
}
Teuchos::RCP<MV> newV, newMV, newKV, newR;
Teuchos::RCP<const MV> src;
// allocate R and newV
newR = MVT::Clone(*tmp,newBS);
newV = MVT::Clone(*tmp,newBS*3);
newKV = MVT::Clone(*tmp,newBS*3);
if (hasM_) {
newMV = MVT::Clone(*tmp,newBS*3);
}
//
// if we are initialized, we want to pull the data from V_ into newV:
// bs | bs | bs
// newV = [newX | **** |newP ]
// newKV = [newKX| **** |newKP]
// newMV = [newMX| **** |newMP]
// where
// oldbs | oldbs | oldbs
// V_ = [newX *** | ******* | newP ***]
// KV_ = [newKX *** | ******* | newKP ***]
// MV_ = [newMX *** | ******* | newMP ***]
//
// we don't care to copy the data corresponding to H
// we will not copy the M data if !hasM_, because it doesn't exist
//
// these are shrink operations which preserve their data
theta_.resize(3*newBS);
Rnorms_.resize(newBS);
R2norms_.resize(newBS);
// copy residual vectors: oldind,newind currently contains [0,...,newBS-1]
src = MVT::CloneView(*R_,newind);
MVT::SetBlock(*src,newind,*newR);
// free old memory and point to new memory
R_ = newR;
// copy in order: newX newKX newMX, then newP newKP newMP
// for X: [0,bs-1] <-- [0,bs-1]
src = MVT::CloneView(*V_,oldind);
MVT::SetBlock(*src,newind,*newV);
src = MVT::CloneView(*KV_,oldind);
MVT::SetBlock(*src,newind,*newKV);
if (hasM_) {
src = MVT::CloneView(*MV_,oldind);
MVT::SetBlock(*src,newind,*newMV);
}
// for P: [2*bs, 3*bs-1] <-- [2*oldbs, 2*oldbs+bs-1]
for (int i=0; i<newBS; i++) {
newind[i] += 2*newBS;
oldind[i] += 2*blockSize_;
}
src = MVT::CloneView(*V_,oldind);
MVT::SetBlock(*src,newind,*newV);
src = MVT::CloneView(*KV_,oldind);
MVT::SetBlock(*src,newind,*newKV);
if (hasM_) {
src = MVT::CloneView(*MV_,oldind);
MVT::SetBlock(*src,newind,*newMV);
}
// release temp view
src = Teuchos::null;
// release old allocations and point at new memory
V_ = newV;
KV_ = newKV;
if (hasM_) {
MV_ = newMV;
}
else {
MV_ = V_;
}
}
else {
// newBS > blockSize_ or not initialized
// this is also the scenario for our initial call to setBlockSize(), in the constructor
initialized_ = false;
hasP_ = false;
// release views
X_ = Teuchos::null;
KX_ = Teuchos::null;
MX_ = Teuchos::null;
H_ = Teuchos::null;
KH_ = Teuchos::null;
MH_ = Teuchos::null;
P_ = Teuchos::null;
KP_ = Teuchos::null;
MP_ = Teuchos::null;
// free allocated storage
R_ = Teuchos::null;
V_ = Teuchos::null;
// allocate scalar vectors
theta_.resize(3*newBS,NANVAL);
Rnorms_.resize(newBS,NANVAL);
R2norms_.resize(newBS,NANVAL);
// clone multivectors off of tmp
R_ = MVT::Clone(*tmp,newBS);
V_ = MVT::Clone(*tmp,newBS*3);
KV_ = MVT::Clone(*tmp,newBS*3);
if (hasM_) {
MV_ = MVT::Clone(*tmp,newBS*3);
}
else {
MV_ = V_;
}
}
// allocate tmp space
tmpmvec_ = Teuchos::null;
if (fullOrtho_) {
tmpmvec_ = MVT::Clone(*tmp,newBS);
}
// set new block size
blockSize_ = newBS;
// setup new views
setupViews();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Setup views into V,KV,MV
template <class ScalarType, class MV, class OP>
void LOBPCG<ScalarType,MV,OP>::setupViews()
{
std::vector<int> ind(blockSize_);
for (int i=0; i<blockSize_; i++) {
ind[i] = i;
}
X_ = MVT::CloneViewNonConst(*V_,ind);
KX_ = MVT::CloneViewNonConst(*KV_,ind);
if (hasM_) {
MX_ = MVT::CloneViewNonConst(*MV_,ind);
}
else {
MX_ = X_;
}
for (int i=0; i<blockSize_; i++) {
ind[i] += blockSize_;
}
H_ = MVT::CloneViewNonConst(*V_,ind);
KH_ = MVT::CloneViewNonConst(*KV_,ind);
if (hasM_) {
MH_ = MVT::CloneViewNonConst(*MV_,ind);
}
else {
MH_ = H_;
}
for (int i=0; i<blockSize_; i++) {
ind[i] += blockSize_;
}
P_ = MVT::CloneViewNonConst(*V_,ind);
KP_ = MVT::CloneViewNonConst(*KV_,ind);
if (hasM_) {
MP_ = MVT::CloneViewNonConst(*MV_,ind);
}
else {
MP_ = P_;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// Set the auxiliary vectors
template <class ScalarType, class MV, class OP>
void LOBPCG<ScalarType,MV,OP>::setAuxVecs(const Teuchos::Array<Teuchos::RCP<const MV> > &auxvecs) {
typedef typename Teuchos::Array<Teuchos::RCP<const MV> >::iterator tarcpmv;
// set new auxiliary vectors
auxVecs_ = auxvecs;
numAuxVecs_ = 0;
for (tarcpmv i=auxVecs_.begin(); i != auxVecs_.end(); i++) {
numAuxVecs_ += MVT::GetNumberVecs(**i);
}
// If the solver has been initialized, X and P are not necessarily orthogonal to new auxiliary vectors
if (numAuxVecs_ > 0 && initialized_) {
initialized_ = false;
hasP_ = false;
}
if (om_->isVerbosity( Debug ) ) {
// Check almost everything here
CheckList chk;
chk.checkQ = true;
om_->print( Debug, accuracyCheck(chk, ": in setAuxVecs()") );
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/* Initialize the state of the solver
*
* POST-CONDITIONS:
*
* initialized_ == true
* X is orthonormal, orthogonal to auxVecs_
* KX = Op*X
* MX = M*X if hasM_
* theta_ contains Ritz values of X
* R = KX - MX*diag(theta_)
* if hasP() == true,
* P orthogonal to auxVecs_
* if fullOrtho_ == true,
* P orthonormal and orthogonal to X
* KP = Op*P
* MP = M*P
*/
template <class ScalarType, class MV, class OP>
void LOBPCG<ScalarType,MV,OP>::initialize(LOBPCGState<ScalarType,MV>& newstate)
{
// NOTE: memory has been allocated by setBlockSize(). Use SetBlock below; do not Clone
// NOTE: Overall time spent in this routine is counted to timerInit_; portions will also be counted towards other primitives
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
Teuchos::TimeMonitor inittimer( *timerInit_ );
#endif
std::vector<int> bsind(blockSize_);
for (int i=0; i<blockSize_; i++) bsind[i] = i;
// in LOBPCG, X (the subspace iterate) is primary
// the order of dependence follows like so.
// --init-> X
// --op apply-> MX,KX
// --ritz analysis-> theta
// --optional-> P,MP,KP
//
// if the user specifies all data for a level, we will accept it.
// otherwise, we will generate the whole level, and all subsequent levels.
//
// the data members are ordered based on dependence, and the levels are
// partitioned according to the amount of work required to produce the
// items in a level.
//
// inconsitent multivectors widths and lengths will not be tolerated, and
// will be treated with exceptions.
// set up X, KX, MX: get them from "state" if user specified them
//----------------------------------------
// set up X, MX, KX
//----------------------------------------
if (newstate.X != Teuchos::null) {
TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetGlobalLength(*newstate.X) != MVT::GetGlobalLength(*X_),
std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.X not correct." );
// newstate.X must have blockSize_ vectors; any more will be ignored
TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.X) < blockSize_,
std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.X must have at least block size vectors.");
// put X data in X_
MVT::SetBlock(*newstate.X,bsind,*X_);
// put MX data in MX_
if (hasM_) {
if (newstate.MX != Teuchos::null) {
TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetGlobalLength(*newstate.MX) != MVT::GetGlobalLength(*MX_),
std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): vector length of newstate.MX not correct." );
// newstate.MX must have blockSize_ vectors; any more will be ignored
TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*newstate.MX) < blockSize_,
std::invalid_argument, "Anasazi::LOBPCG::initialize(newstate): newstate.MX must have at least block size vectors.");
MVT::SetBlock(*newstate.MX,bsind,*MX_);
}
else {
// user didn't specify MX, compute it
{
#ifdef ANASAZI_TEUCHOS_TIME_MONITOR
Teuchos::TimeMonitor lcltimer( *timerMOp_ );