Grid 0.7.0
Deflation.h
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/algorithms/iterative/ImplicitlyRestartedLanczos.h
6
7 Copyright (C) 2015
8
9Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 See the full license in the file "LICENSE" in the top level distribution directory
26 *************************************************************************************/
27 /* END LEGAL */
28#ifndef GRID_DEFLATION_H
29#define GRID_DEFLATION_H
30
31namespace Grid {
32
33template<class Field>
34class ZeroGuesser: public LinearFunction<Field> {
35public:
36 using LinearFunction<Field>::operator();
37 virtual void operator()(const Field &src, Field &guess) { guess = Zero(); };
38};
39template<class Field>
40class DoNothingGuesser: public LinearFunction<Field> {
41public:
42 using LinearFunction<Field>::operator();
43 virtual void operator()(const Field &src, Field &guess) { };
44};
45template<class Field>
46class SourceGuesser: public LinearFunction<Field> {
47public:
48 using LinearFunction<Field>::operator();
49 virtual void operator()(const Field &src, Field &guess) { guess = src; };
50};
51
53// Fine grid deflation
55template<class Field>
56class DeflatedGuesser: public LinearFunction<Field> {
57private:
58 const std::vector<Field> &evec;
59 const std::vector<RealD> &eval;
60 const unsigned int N;
61
62public:
63 using LinearFunction<Field>::operator();
64
65 DeflatedGuesser(const std::vector<Field> & _evec,const std::vector<RealD> & _eval)
66 : DeflatedGuesser(_evec, _eval, _evec.size())
67 {}
68
69 DeflatedGuesser(const std::vector<Field> & _evec, const std::vector<RealD> & _eval, const unsigned int _N)
70 : evec(_evec), eval(_eval), N(_N)
71 {
72 assert(evec.size()==eval.size());
73 assert(N <= evec.size());
74 }
75
76 virtual void operator()(const Field &src,Field &guess) {
77 guess = Zero();
78 for (int i=0;i<N;i++) {
79 const Field& tmp = evec[i];
80 axpy(guess,TensorRemove(innerProduct(tmp,src)) / eval[i],tmp,guess);
81 }
82 guess.Checkerboard() = src.Checkerboard();
83 }
84};
85
86template<class FineField, class CoarseField>
88private:
89 const std::vector<FineField> &subspace;
90 const std::vector<CoarseField> &evec_coarse;
91 const std::vector<RealD> &eval_coarse;
92public:
93
94 using LinearFunction<FineField>::operator();
95 LocalCoherenceDeflatedGuesser(const std::vector<FineField> &_subspace,
96 const std::vector<CoarseField> &_evec_coarse,
97 const std::vector<RealD> &_eval_coarse)
98 : subspace(_subspace),
99 evec_coarse(_evec_coarse),
100 eval_coarse(_eval_coarse)
101 {
102 }
103
104 void operator()(const FineField &src,FineField &guess) {
105 int N = (int)evec_coarse.size();
106 CoarseField src_coarse(evec_coarse[0].Grid());
107 CoarseField guess_coarse(evec_coarse[0].Grid()); guess_coarse = Zero();
108 blockProject(src_coarse,src,subspace);
109 for (int i=0;i<N;i++) {
110 const CoarseField & tmp = evec_coarse[i];
111 axpy(guess_coarse,TensorRemove(innerProduct(tmp,src_coarse)) / eval_coarse[i],tmp,guess_coarse);
112 }
113 blockPromote(guess_coarse,guess,subspace);
114 guess.Checkerboard() = src.Checkerboard();
115 };
116
117 void operator()(const std::vector<FineField> &src,std::vector<FineField> &guess) {
118 int Nevec = (int)evec_coarse.size();
119 int Nsrc = (int)src.size();
120 // make temp variables
121 std::vector<CoarseField> src_coarse(Nsrc,evec_coarse[0].Grid());
122 std::vector<CoarseField> guess_coarse(Nsrc,evec_coarse[0].Grid());
123 //Preporcessing
124 std::cout << GridLogMessage << "Start BlockProject for loop" << std::endl;
125 for (int j=0;j<Nsrc;j++)
126 {
127 guess_coarse[j] = Zero();
128 std::cout << GridLogMessage << "BlockProject iter: " << j << std::endl;
129 blockProject(src_coarse[j],src[j],subspace);
130 }
131 //deflation set up for eigen vector batchsize 1 and source batch size equal number of sources
132 std::cout << GridLogMessage << "Start ProjectAccum for loop" << std::endl;
133 for (int i=0;i<Nevec;i++)
134 {
135 std::cout << GridLogMessage << "ProjectAccum Nvec: " << i << std::endl;
136 const CoarseField & tmp = evec_coarse[i];
137 for (int j=0;j<Nsrc;j++)
138 {
139 axpy(guess_coarse[j],TensorRemove(innerProduct(tmp,src_coarse[j])) / eval_coarse[i],tmp,guess_coarse[j]);
140 }
141 }
142 //postprocessing
143 std::cout << GridLogMessage << "Start BlockPromote for loop" << std::endl;
144 for (int j=0;j<Nsrc;j++)
145 {
146 std::cout << GridLogMessage << "BlockProject iter: " << j << std::endl;
147 blockPromote(guess_coarse[j],guess[j],subspace);
148 guess[j].Checkerboard() = src[j].Checkerboard();
149 }
150 };
151
152 };
153
154
155
156}
157#endif
void axpy(Lattice< vobj > &ret, sobj a, const Lattice< vobj > &x, const Lattice< vobj > &y)
ComplexD innerProduct(const Lattice< vobj > &left, const Lattice< vobj > &right)
void blockPromote(const Lattice< iVector< CComplex, nbasis > > &coarseData, Lattice< vobj > &fineData, const VLattice &Basis)
void blockProject(Lattice< iVector< CComplex, nbasis > > &coarseData, const Lattice< vobj > &fineData, const VLattice &Basis)
GridLogger GridLogMessage(1, "Message", GridLogColours, "NORMAL")
accelerator_inline std::enable_if<!isGridTensor< T >::value, T >::type TensorRemove(T arg)
const unsigned int N
Definition Deflation.h:60
virtual void operator()(const Field &src, Field &guess)
Definition Deflation.h:76
DeflatedGuesser(const std::vector< Field > &_evec, const std::vector< RealD > &_eval)
Definition Deflation.h:65
DeflatedGuesser(const std::vector< Field > &_evec, const std::vector< RealD > &_eval, const unsigned int _N)
Definition Deflation.h:69
const std::vector< RealD > & eval
Definition Deflation.h:59
const std::vector< Field > & evec
Definition Deflation.h:58
virtual void operator()(const Field &src, Field &guess)
Definition Deflation.h:43
void operator()(const FineField &src, FineField &guess)
Definition Deflation.h:104
const std::vector< FineField > & subspace
Definition Deflation.h:89
LocalCoherenceDeflatedGuesser(const std::vector< FineField > &_subspace, const std::vector< CoarseField > &_evec_coarse, const std::vector< RealD > &_eval_coarse)
Definition Deflation.h:95
void operator()(const std::vector< FineField > &src, std::vector< FineField > &guess)
Definition Deflation.h:117
const std::vector< CoarseField > & evec_coarse
Definition Deflation.h:90
const std::vector< RealD > & eval_coarse
Definition Deflation.h:91
virtual void operator()(const Field &src, Field &guess)
Definition Deflation.h:49
virtual void operator()(const Field &src, Field &guess)
Definition Deflation.h:37
Definition Simd.h:194