Bridge++  Ver. 2.0.2
asolver_BiCGStab_Precond-tmpl.h
Go to the documentation of this file.
1 
10 
11 
12 template<typename AFIELD>
14  = "ASolver_BiCGStab_Precond";
15 //====================================================================
16 template<typename AFIELD>
18 {
20 
21  int nin = m_fopr->field_nin();
22  int nvol = m_fopr->field_nvol();
23  int nex = m_fopr->field_nex();
24 
25  m_r.reset(nin, nvol, nex);
26  m_x.reset(nin, nvol, nex);
27  m_p.reset(nin, nvol, nex);
28  m_s.reset(nin, nvol, nex);
29  m_v.reset(nin, nvol, nex);
30  m_t.reset(nin, nvol, nex);
31  m_rh.reset(nin, nvol, nex);
32  m_u.reset(nin, nvol, nex);
33 
34  m_nconv = -1;
35 }
36 
37 
38 //====================================================================
39 template<typename AFIELD>
41 {
42  // ThreadManager::assert_single_thread(class_name);
43  // nothing is to be deleted.
44 }
45 
46 
47 //====================================================================
48 template<typename AFIELD>
50  const Parameters& params)
51 {
52  const string str_vlevel = params.get_string("verbose_level");
53 
54  m_vl = vout.set_verbose_level(str_vlevel);
55 
56  //- fetch and check input parameters
57  int Niter, Nrestart;
58  double Stop_cond;
59 
60  int err = 0;
61  err += params.fetch_int("maximum_number_of_iteration", Niter);
62  err += params.fetch_int("maximum_number_of_restart", Nrestart);
63  err += params.fetch_double("convergence_criterion_squared", Stop_cond);
64 
65  if (err) {
66  vout.crucial(m_vl, "Error at %s: input parameter not found.\n",
67  class_name.c_str());
68  exit(EXIT_FAILURE);
69  }
70 
71  int Niter2 = Niter * Nrestart;
72  set_parameters(Niter2, Stop_cond);
73 }
74 
75 
76 //====================================================================
77 template<typename AFIELD>
79  const real_t Stop_cond)
80 {
82 
83  m_Niter = Niter;
84  m_Stop_cond = Stop_cond;
85  std::string prec = "double";
86  if (sizeof(real_t) == 4) prec = "float";
87 
88  vout.general(m_vl, "%s:\n", class_name.c_str());
89  vout.general(m_vl, " Precision: %s\n", prec.c_str());
90  vout.general(m_vl, " Niter = %d\n", m_Niter);
91  vout.general(m_vl, " Stop_cond = %16.8e\n", m_Stop_cond);
92 }
93 
94 
95 //====================================================================
96 template<typename AFIELD>
98  const AFIELD& b,
99  int& Nconv, real_t& diff)
100 {
101  vout.paranoiac(m_vl, "%s: solver start.\n", class_name.c_str());
102 
103  copy(m_s, b);
104 
105  real_t snorm, sr;
106  real_t rr;
107 
108  sr = norm2(m_s);
109  snorm = real_t(1.0) / sr;
110 
111  m_prec->reset_flop_count();
112 
113  int nconv = -1;
114 
115  solve_init(b, rr);
116  vout.detailed(m_vl, " init: %22.15e\n", rr * snorm);
117 
118  for (int iter = 0; iter < m_Niter; ++iter) {
119  solve_step(rr);
120  vout.detailed(m_vl, "%6d %22.15e\n", iter, rr * snorm);
121 
122  if (rr * snorm < m_Stop_cond) {
123  nconv = 2 * (iter + 1); // counting fermion mult
124  break;
125  }
126  }
127 
128  if (nconv == -1) {
129  vout.crucial(m_vl, "Error at %s: not converged\n",
130  class_name.c_str());
131  vout.crucial(m_vl, " iter(final): %8d %22.15e\n",
132  m_Niter, rr * snorm);
133  exit(EXIT_FAILURE);
134  }
135 
136  vout.detailed(m_vl, "converged:\n");
137  vout.detailed(m_vl, " nconv = %d\n", nconv);
138 
139  copy(xq, m_x);
140 
141  m_fopr->mult(m_s, xq);
142 
143  axpy(m_s, real_t(-1.0), b);
144 
145  real_t diff2 = norm2(m_s);
146 
147 #pragma omp master
148  {
149  m_nconv = nconv;
150  Nconv = m_nconv + 1; // include solve_init();
151  diff = diff2;
152  }
153 #pragma omp barrier
154 }
155 
156 
157 //====================================================================
158 template<typename AFIELD>
160  real_t& rr)
161 {
162  copy(m_x, m_s);
163 
164  m_fopr->mult(m_v, m_x);
165  copy(m_r, b);
166  axpy(m_r, real_t(-1.0), m_v);
167  copy(m_rh, m_r);
168 
169  rr = norm2(m_r);
170 
171  m_p.set(real_t(0.0));
172  m_v.set(real_t(0.0));
173 
174  m_rho_prev = real_t(1.0);
175  m_alpha_prev = real_t(1.0);
176  m_omega_prev = real_t(1.0);
177 }
178 
179 
180 //====================================================================
181 template<typename AFIELD>
183 {
184  real_t rho = dot(m_rh, m_r);
185  real_t bet = rho * m_alpha_prev / (m_rho_prev * m_omega_prev);
186 
187  // p = r + bet * (p - omega_prev * v);
188  axpy(m_p, -m_omega_prev, m_v);
189  aypx(bet, m_p, m_r);
190 
191  m_prec->mult(m_u, m_p);
192  m_fopr->mult(m_v, m_u);
193 
194  real_t aden = dot(m_rh, m_v); // dcomplex aden = rh * v;
195  real_t alpha = rho / aden;
196 
197  copy(m_s, m_r); // s = r
198  axpy(m_s, -alpha, m_v); // s += - alpha * v;
199 
200  m_prec->mult(m_v, m_s); // t = m_fopr->mult(s);
201  m_fopr->mult(m_t, m_v); // t = m_fopr->mult(s);
202 
203  real_t omega_n = dot(m_t, m_s); // omega_n = t * s;
204  real_t omega_d = dot(m_t, m_t); // omega_d = t * t;
205  real_t omega = omega_n / omega_d;
206 
207  // axpy(m_x, omega, m_s);
208  axpy(m_x, omega, m_v);
209  axpy(m_x, alpha, m_u);
210 
211  copy(m_r, m_s); // r = s
212  axpy(m_r, -omega, m_t); // r += - omega * t;
213 
214  rr = norm2(m_r); // rr = r * r;
215 
216  m_rho_prev = rho;
217  m_alpha_prev = alpha;
218  m_omega_prev = omega;
219 }
220 
221 
222 //====================================================================
223 template<typename AFIELD>
225 {
226  copy(v, w);
227 }
228 
229 
230 //====================================================================
231 template<typename AFIELD>
233 {
234  int Nin = m_fopr->field_nin();
235  int Nvol = m_fopr->field_nvol();
236  int Nex = m_fopr->field_nex();
237  int NPE = CommonParameters::NPE();
238 
239  int ninit = 1;
240 
241  double flop_field = static_cast<double>(Nin * Nvol * Nex) * NPE;
242 
243  double flop_vector = (6 + ninit * 4 + m_nconv * 11) * flop_field;
244  double flop_fopr = (1 + ninit + m_nconv) * m_fopr->flop_count();
245  double flop_prec = m_prec->flop_count();
246 
247  double flop = flop_vector + flop_fopr + flop_prec;
248 
249  return flop;
250 }
251 
252 
253 //============================================================END=====
ASolver_BiCGStab_Precond::solve
void solve(AFIELD &xq, const AFIELD &b, int &nconv, real_t &diff)
solver main.
Definition: asolver_BiCGStab_Precond-tmpl.h:97
ASolver_BiCGStab_Precond::solve_step
void solve_step(real_t &rr)
Definition: asolver_BiCGStab_Precond-tmpl.h:182
ASolver_BiCGStab_Precond::prec
void prec(AFIELD &, AFIELD &)
Definition: asolver_BiCGStab_Precond-tmpl.h:224
Parameters
Class for parameters.
Definition: parameters.h:46
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
aypx
void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:509
axpy
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
dot
double dot(const Field &y, const Field &x)
Definition: field.cpp:576
real_t
double real_t
Definition: bridgeQXS_Clover_coarse_double.cpp:16
norm2
REALTYPE norm2(const AField< REALTYPE, QXS > &v)
Definition: afield-inc.h:453
copy
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:212
Bridge::BridgeIO::paranoiac
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:238
ASolver_BiCGStab_Precond::init
void init(void)
initial setup.
Definition: asolver_BiCGStab_Precond-tmpl.h:17
ASolver_BiCGStab_Precond::solve_init
void solve_init(const AFIELD &b, real_t &rr)
Definition: asolver_BiCGStab_Precond-tmpl.h:159
asolver_BiCGStab_Precond.h
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
ASolver_BiCGStab_Precond::tidyup
void tidyup(void)
final tidy-up.
Definition: asolver_BiCGStab_Precond-tmpl.h:40
ASolver_BiCGStab_Precond::set_parameters
void set_parameters(const Parameters &params)
setting parameters by a Parameter object.
Definition: asolver_BiCGStab_Precond-tmpl.h:49
ASolver_BiCGStab_Precond
Definition: asolver_BiCGStab_Precond.h:24
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
Parameters::fetch_double
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:327
Parameters::get_string
string get_string(const string &key) const
Definition: parameters.cpp:221
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
ASolver_BiCGStab_Precond::flop_count
double flop_count()
returns the floating point operation count.
Definition: asolver_BiCGStab_Precond-tmpl.h:232
Field
Container of Field-type object.
Definition: field.h:46
Parameters::fetch_int
int fetch_int(const string &key, int &value) const
Definition: parameters.cpp:346
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
ASolver::real_t
AFIELD::real_t real_t
Definition: asolver.h:29
ThreadManager::assert_single_thread
static void assert_single_thread(const std::string &class_name)
assert currently running on single thread.
Definition: threadManager.cpp:372
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512