Bridge++  Ver. 2.0.2
asolver_Richardson-tmpl.h
Go to the documentation of this file.
1 
14 #include "asolver_Richardson.h"
15 
17 
18 
19 template<typename AFIELD>
21  = "ASolver_Richardson";
22 //====================================================================
23 template<typename AFIELD>
25 {
27 
28  int nin = m_fopr->field_nin();
29  int nvol = m_fopr->field_nvol();
30  int nex = m_fopr->field_nex();
31 
32  m_r.reset(nin, nvol, nex);
33  m_s.reset(nin, nvol, nex);
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 
72  // "restart" is not yet supported
73  int Niter2 = Niter * Nrestart;
74  set_parameters(Niter2, Stop_cond);
75 }
76 
77 
78 //====================================================================
79 template<typename AFIELD>
81  const real_t Stop_cond,
82  const InitialGuess init_guess_mode)
83 {
85 
86  m_Niter = Niter;
87  m_Stop_cond = Stop_cond;
88  m_initial_mode = init_guess_mode;
89  std::string prec = "double";
90  if (sizeof(real_t) == 4) prec = "float";
91 
92  vout.general(m_vl, "%s:\n", class_name.c_str());
93  vout.general(m_vl, " Precision: %s\n", prec.c_str());
94  vout.general(m_vl, " Niter = %d\n", m_Niter);
95  vout.general(m_vl, " Stop_cond = %16.8e\n", m_Stop_cond);
96  vout.general(m_vl, " InitialGuess = %d\n", m_initial_mode);
97 
98  if (m_initial_mode != InitialGuess::ZERO) {
99  vout.crucial(m_vl, "%s: WARNING: initial guess is not InitialGuess::ZERO\n",
100  class_name.c_str());
101  }
102 }
103 
104 
105 //====================================================================
106 template<typename AFIELD>
108  const real_t Stop_cond)
109 {
110  set_parameters(Niter, Stop_cond, InitialGuess::ZERO);
111 }
112 
113 
114 //====================================================================
115 template<typename AFIELD>
117  const real_t Stop_cond,
118  const bool use_init_guess)
119 {
120  // for backward compatibility
121  if (use_init_guess) {
122  set_parameters(Niter, Stop_cond, InitialGuess::GIVEN);
123  } else {
124  set_parameters(Niter, Stop_cond, InitialGuess::ZERO);
125  }
126 }
127 
128 
129 //====================================================================
130 template<typename AFIELD>
132  const AFIELD& b,
133  int& Nconv, real_t& diff)
134 {
135  vout.paranoiac(m_vl, "%s: solver start.\n", class_name.c_str());
136 
137 
138  real_t bnorm2, bnorm, bnorm_inv, bnorm2_inv;
139 
140  // initial barrier
141 #pragma omp barrier
142 
143  bnorm2 = norm2(b);
144  if (!(bnorm2 > 0.0)) {
145  xq.set(0.0);
146  Nconv = 0;
147  diff = 0.0;
148  return;
149  }
150 
151  bnorm = sqrt(bnorm2);
152  bnorm_inv = real_t(1.0) / bnorm;
153  bnorm2_inv = real_t(1.0) / bnorm2;
154 
155  copy(m_r, b);
156 
157  // solve_init(b, xq, rr);
158  m_r.scal(-1.0); // assumes the initial guess is zero
159  xq.set(0.0);
160 
161  m_prec->reset_flop_count();
162 
163 
164  int nconv = -1;
165 
166 
167  double r2 = bnorm2; // m_s.norm2();
168  vout.detailed(m_vl, " Richardson init: %22.15e\n", r2);
169 #pragma omp barrier
170 
171  for (int iter = 0; iter < m_Niter; ++iter) {
172  if (r2 < bnorm2 * m_Stop_cond) {
173  diff = r2 * bnorm2_inv;
174  nconv = iter; // counting fermion mult
175  break;
176  }
177  double normalization = sqrt(r2);
178  m_r.scal(1.0 / normalization);
179 #pragma omp barrier
180  m_prec->mult(m_s, m_r);
181 
182  axpy(xq, -normalization, m_s);
183  m_fopr->mult(m_r, xq);
184  axpy(m_r, -1.0, b);
185 
186  r2 = m_r.norm2();
187  }
188  if (nconv == -1) {
189  vout.detailed(m_vl, "Richardson NOT converged:\n");
190  // vout.crucial(m_vl, "Error at %s: not converged\n",
191  // class_name.c_str());
192  // vout.crucial(m_vl, " iter(final): %8d %22.15e\n",
193  // m_Niter, rr*snorm);
194  // exit(EXIT_FAILURE);
195  } else {
196  vout.detailed(m_vl, "Richardson converged:\n");
197  }
198 
199  // check the solution
200  m_fopr->mult(m_r, xq);
201  axpy(m_r, real_t(-1.0), b);
202  real_t diff2 = norm2(m_r);
203  vout.general(m_vl, "Richardson nconv = %d, diff2 = %22.15e\n", nconv, diff2 * bnorm2_inv);
204 
205 #pragma omp master
206  {
207  m_nconv = nconv;
208  Nconv = m_nconv; // include solve_init();
209  diff = sqrt(diff2 * bnorm2_inv);
210  }
211 
212 #pragma omp barrier
213 }
214 
215 
216 //====================================================================
217 template<typename AFIELD>
219  const AFIELD& xq,
220  real_t& rr)
221 {
222 }
223 
224 
225 //====================================================================
226 template<typename AFIELD>
228 {
229  return flop_count_intermediate(m_nconv);
230 }
231 
232 
233 //====================================================================
234 template<typename AFIELD>
236 {
237  int Nin = m_fopr->field_nin();
238  int Nvol = m_fopr->field_nvol();
239  int Nex = m_fopr->field_nex();
240  int NPE = CommonParameters::NPE();
241 
242  int ninit = 1;
243 
244  double flop_field = static_cast<double>(Nin * Nvol * Nex) * NPE;
245  // norm: 2
246  // scal: 1
247  // axpy: 2
248  // sub: 1
249  double flop_vector = (1 + (2 + 1 + 2 + 1) * iter) * flop_field;
250  double flop_fopr = iter * m_fopr->flop_count();
251  double flop_prec = m_prec->flop_count();
252 
253  double flop = flop_vector + flop_fopr + flop_prec;
254 
255  return flop;
256 }
257 
258 
259 //====================================================================
ASolver_Richardson::solve_init
void solve_init(const AFIELD &b, const AFIELD &xq, real_t &rr)
Definition: asolver_Richardson-tmpl.h:218
ASolver_Richardson::tidyup
void tidyup(void)
final tidy-up.
Definition: asolver_Richardson-tmpl.h:40
Field::set
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
Parameters
Class for parameters.
Definition: parameters.h:46
ASolver_Richardson::flop_count
double flop_count()
returns the floating point operation count.
Definition: asolver_Richardson-tmpl.h:227
ASolver_Richardson::flop_count_intermediate
double flop_count_intermediate(const int iter)
Definition: asolver_Richardson-tmpl.h:235
Bridge::BridgeIO::detailed
void detailed(const char *format,...)
Definition: bridgeIO.cpp:219
axpy
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:380
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_Richardson.h
threadManager.h
ASolver_Richardson::solve
void solve(AFIELD &xq, const AFIELD &b, int &nconv, real_t &diff)
solver main.
Definition: asolver_Richardson-tmpl.h:131
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
ASolver_Richardson::init
void init(void)
initial setup.
Definition: asolver_Richardson-tmpl.h:24
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
ASolver_Richardson::set_parameters
void set_parameters(const Parameters &params)
setting parameters by a Parameter object.
Definition: asolver_Richardson-tmpl.h:49
Parameters::fetch_double
int fetch_double(const string &key, double &value) const
Definition: parameters.cpp:327
ASolver::InitialGuess
InitialGuess
Definition: asolver.h:31
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
Field
Container of Field-type object.
Definition: field.h:46
ASolver_Richardson
Definition: asolver_Richardson.h:30
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