Bridge++  Ver. 2.0.2
asolver_SAP-tmpl.h
Go to the documentation of this file.
1 
10 //====================================================================
11 namespace {
12 #ifndef AFILED_HAS_SUB
13  template<typename AFIELD>
14  inline void sub(AFIELD& v, const AFIELD& w)
15  {
16  typedef typename AFIELD::real_t real_t;
17  axpy(v, real_t(-1.0), w);
18  }
19 #endif
20 
21 #ifndef AFILED_HAS_ADD
22  template<typename AFIELD>
23  inline void add(AFIELD& v, const AFIELD& w)
24  {
25  typedef typename AFIELD::real_t real_t;
26  axpy(v, real_t(1.0), w);
27  }
28 #endif
29 }
30 
31 template<typename AFIELD>
32 const std::string ASolver_SAP<AFIELD>::class_name = "ASolver_SAP";
33 
34 //====================================================================
35 template<typename AFIELD>
37 {
39 
40  int nin = m_fopr->field_nin();
41  int nvol = m_fopr->field_nvol();
42  int nex = m_fopr->field_nex();
43 
44  m_r.reset(nin, nvol, nex);
45  m_p.reset(nin, nvol, nex);
46 
47  m_sap_minres.reset(new ASolver_SAP_MINRES<AFIELD>(m_fopr, m_block_index));
48 
49  m_sap_minres->set_parameters(m_min_res_iter, 0.0);
50 
51 
52  m_nconv = -1;
53 }
54 
55 
56 //====================================================================
57 template<typename AFIELD>
59 {
60  // ThreadManager::assert_single_thread(class_name);
61  //delete m_sap_minres;
62 }
63 
64 
65 //====================================================================
66 template<typename AFIELD>
68 {
69  const string str_vlevel = params.get_string("verbose_level");
70 
71  m_vl = vout.set_verbose_level(str_vlevel);
72 
73  //- fetch and check input parameters
74  int Niter, Nrestart;
75  double Stop_cond;
76 
77  int err = 0;
78  err += params.fetch_int("maximum_number_of_iteration", Niter);
79  err += params.fetch_int("maximum_number_of_restart", Nrestart);
80  err += params.fetch_double("convergence_criterion_squared", Stop_cond);
81 
82  if (err) {
83  vout.crucial(m_vl, "Error at %s: input parameter not found.\n",
84  class_name.c_str());
85  exit(EXIT_FAILURE);
86  }
87 
88  int Niter2 = Niter * Nrestart;
89  set_parameters(Niter2, Stop_cond);
90 }
91 
92 
93 //====================================================================
94 template<typename AFIELD>
96  const real_t Stop_cond)
97 {
99 
100  m_Niter = Niter;
101  m_Stop_cond = Stop_cond;
102  std::string prec = "double";
103  if (sizeof(real_t) == 4) prec = "float";
104 
105  // N.B. Stop_cond is irrelevant
106  vout.general(m_vl, "%s:\n", class_name.c_str());
107  vout.general(m_vl, " Precision: %s\n", prec.c_str());
108  vout.general(m_vl, " Niter = %d\n", m_Niter);
109  // vout.general(m_vl, " Stop_cond = %16.8e\n", m_Stop_cond);
110 }
111 
112 
113 //====================================================================
114 template<typename AFIELD>
116  int& Nconv, real_t& diff)
117 {
118  // vout.paranoiac(m_vl, "asolver_SAP: solve, started in %s\n", __func__);
119  vout.detailed(m_vl, "asolver_SAP: solve, started in %s\n", __func__);
120  using real_t = typename AFIELD::real_t;
121 
122  x.set(real_t(0.0));
123  copy(m_r, b);
124  m_r.scal(real_t(-1.0)); // -|r> = D|x> - |b>
125  int nconv = -1;
126  real_t diff0 = 0.0;
127 
128 #ifdef DEBUG
129  {
130  real_t r2 = norm2(m_r);
131  vout.general(m_vl, "initial, r2=%e\n", r2);
132  }
133 #endif
134  Nconv = m_Niter;
135  for (int iter = 0; iter < m_Niter; ++iter) {
136  int eo = 0;
137  m_sap_minres->solve(m_p, m_r, nconv, diff0, eo);
138 
139 #ifdef DEBUG
140  {
141  real_t p2 = norm2(m_p);
142  vout.general(m_vl, " after minres: |m_p|^2 = %e\n", p2);
143  }
144 #endif
145  sub(x, m_p);
146  m_fopr->mult(m_p, x);
147  sub(m_p, b); // |p> = -|r> = D|x> - |b>
148 
149  eo = 1;
150  m_sap_minres->solve(m_r, m_p, nconv, diff0, eo);
151 #ifdef DEBUG
152  {
153  double r2 = norm2(m_r);
154  vout.general(m_vl, " after minres: |m_r|^2 = %e\n", r2);
155  }
156 #endif
157 
158  sub(x, m_r);
159  m_fopr->mult(m_r, x);
160  sub(m_r, b);
161 
162 #ifdef DEBUG
163  {
164  double r2 = norm2(m_r);
165  double x2 = norm2(x);
166  vout.general(m_vl, "iter = %d, r2 = %e, x2 = %e\n", iter, r2, x2);
167  }
168 #endif
169  } // iter loop
170 
171 #ifdef DEBUG
172  real_t r2 = norm2(m_r);
173  diff = sqrt(r2);
174 #else
175  diff = -1.0;
176 #endif
177 
178  m_Nconv = Nconv;
179 
180 #pragma omp barrier
181 }
182 
183 
184 //====================================================================
185 template<typename AFIELD>
187 {
188  int Nin = m_fopr->field_nin();
189  int Nvol = m_fopr->field_nvol();
190  int Nex = m_fopr->field_nex();
191  int NPE = CommonParameters::NPE();
192 
193  double flop_minres = m_sap_minres->flop_count();
194  double flop_mult = m_fopr->flop_count();
195  double flop_sub = 2.0 * Nin * Nvol * NPE;
196  double flop
197  = m_Nconv * (2 * flop_minres + 2 * flop_mult + 2 * flop_sub);
198  return flop;
199 }
200 
201 
202 //============================================================END=====
ASolver_SAP_MINRES
Definition: asolver_SAP_MINRES.h:26
ASolver_SAP
Definition: asolver_SAP.h:29
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_SAP::tidyup
void tidyup(void)
Definition: asolver_SAP-tmpl.h:58
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
Field::real_t
double real_t
Definition: field.h:51
ASolver_SAP::init
void init(void)
Definition: asolver_SAP-tmpl.h:36
ASolver_SAP::flop_count
double flop_count()
returns the floating point operation count.
Definition: asolver_SAP-tmpl.h:186
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
ASolver_SAP::solve
void solve(AFIELD &xq, const AFIELD &b, int &nconv, real_t &diff)
solver main.
Definition: asolver_SAP-tmpl.h:115
ASolver_SAP::set_parameters
void set_parameters(const Parameters &params)
setting parameters by a Parameter object.
Definition: asolver_SAP-tmpl.h:67
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
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< Field >::real_t
Field ::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