Bridge++  Ver. 2.0.2
fopr_CRS.cpp
Go to the documentation of this file.
1 
14 #include "fopr_CRS.h"
15 
17 
18 #include <fstream>
19 
20 #ifdef USE_FACTORY_AUTOREGISTER
21 namespace {
22  bool init = Fopr_CRS::register_factory();
23 }
24 #endif
25 
26 const std::string Fopr_CRS::class_name = "Fopr_CRS";
27 
28 //====================================================================
30 {
31  std::string vlevel;
32  if (!params.fetch_string("verbose_level", vlevel)) {
33  m_vl = vout.set_verbose_level(vlevel);
34  }
35 }
36 
37 
38 //====================================================================
40 {
41  params.set_string("verbose_level", vout.get_verbose_level(m_vl));
42 }
43 
44 
45 //====================================================================
46 void Fopr_CRS::set_mode(const std::string mode)
47 {
48 #pragma omp barrier
49 
50  int ith = ThreadManager::get_thread_id();
51  if (ith == 0) m_mode = mode;
52 
53 #pragma omp barrier
54 }
55 
56 
57 //====================================================================
58 void Fopr_CRS::DdagD(Field& v, const Field& w)
59 {
60  Field w2(w.nin(), w.nvol(), w.nex());
61 
62  D(w2, w);
63  Ddag(v, w2);
64 }
65 
66 
67 //====================================================================
68 void Fopr_CRS::D(Field& v, const Field& w)
69 {
70  const int Nsize = w.size() / 2;
71 
72  assert(Nsize == m_Nsize);
73 
74  int Ncolumn = 0;
75 
76  // v = 0.0;
77  v.set(0.0);
78 
79  for (int j = 0; j < Nsize; ++j) {
80  if (j == Nsize - 1) {
81  Ncolumn = m_Nnz - m_rowidx_nz[j];
82  } else {
83  Ncolumn = m_rowidx_nz[j + 1] - m_rowidx_nz[j];
84  }
85 
86  double vr = 0.0;
87  double vi = 0.0;
88  for (int i = 0; i < Ncolumn; ++i) {
89  int i2 = i + m_rowidx_nz[j];
90  int k = m_column_nz[i2];
91  double wr = w.cmp(2 * k);
92  double wi = w.cmp(2 * k + 1);
93 
94  vr += m_elem_nz[2 * i2] * wr - m_elem_nz[2 * i2 + 1] * wi;
95  vi += m_elem_nz[2 * i2] * wi + m_elem_nz[2 * i2 + 1] * wr;
96 
97  v.set(2 * j, vr);
98  v.set(2 * j + 1, vi);
99  }
100  }
101 }
102 
103 
104 //====================================================================
105 void Fopr_CRS::Ddag(Field& v, const Field& w)
106 {
107  const int Nsize = w.size() / 2;
108 
109  assert(Nsize == m_Nsize);
110 
111  int Ncolumn = 0;
112 
113  // v = 0.0;
114  v.set(0.0);
115 
116  for (int j = 0; j < Nsize; ++j) {
117  if (j == Nsize - 1) {
118  Ncolumn = m_Nnz - m_rowidx_nz[j];
119  } else {
120  Ncolumn = m_rowidx_nz[j + 1] - m_rowidx_nz[j];
121  }
122 
123  double wr = w.cmp(2 * j);
124  double wi = w.cmp(2 * j + 1);
125 
126  for (int i = 0; i < Ncolumn; ++i) {
127  int i2 = i + m_rowidx_nz[j];
128  int k = m_column_nz[i2];
129  double vr = m_elem_nz[2 * i2] * wr + m_elem_nz[2 * i2 + 1] * wi;
130  double vi = m_elem_nz[2 * i2] * wi - m_elem_nz[2 * i2 + 1] * wr;
131 
132  v.add(2 * k, vr);
133  v.add(2 * k + 1, vi);
134  }
135  }
136 }
137 
138 
139 //====================================================================
141 {
142  // the implementation of this function assumes that the field
143  // is complex valued.
144 
145  assert(CommonParameters::NPE() == 1);
146 
147  m_Nin = m_fopr->field_nin();
148  m_Nvol = m_fopr->field_nvol();
149  m_Nex = m_fopr->field_nex();
150 
151  m_Nsize = (m_Nin / 2) * m_Nvol * m_Nex;
152 
153  Field w(m_Nin, m_Nvol, m_Nex), v(m_Nin, m_Nvol, m_Nex);
154 
155  int Nnz1;
156  std::vector<int> index_nz1(m_Nsize);
157  std::vector<double> elem_nz1(2 * m_Nsize);
158 
159  vout.general(m_vl, "Setting SRC matrix format.\n");
160  vout.general(m_vl, " Nex = %4d\n", m_Nex);
161 
162  //- estimate of matrix size
163  int Nnz_tot = 0;
164 
165  {
166  int in = 0;
167  {
168  int site = 0;
169  for (int ex = 0; ex < m_Nex; ++ex) {
170  w.set(0.0);
171  w.set(in, site, ex, 1.0);
172  m_fopr->mult_dag(v, w);
173  set_matrix_1row(Nnz1, index_nz1, elem_nz1, v);
174  Nnz_tot += Nnz1;
175  vout.general(m_vl, " ex = %4d Nnz1 = %d\n", ex, Nnz1);
176  }
177  }
178  }
179  Nnz_tot *= (m_Nin / 2) * m_Nvol;
180  vout.general(m_vl, " Nnz_tot = %d\n", Nnz_tot);
181 
182  //- resize data array
183  m_Nnz = Nnz_tot;
184  m_rowidx_nz.resize(m_Nsize);
185  m_column_nz.resize(m_Nnz);
186  m_elem_nz.resize(2 * m_Nnz);
187 
188  //- setting CRS matrix
189  Nnz_tot = 0;
190  for (int ex = 0; ex < m_Nex; ++ex) {
191  vout.general(m_vl, " ex = %4d starts.\n", ex);
192  for (int site = 0; site < m_Nvol; ++site) {
193  for (int in2 = 0; in2 < (m_Nin / 2); ++in2) {
194  int j = in2 + (m_Nin / 2) * (site + m_Nvol * ex);
195 
196  w.set(0.0);
197  w.set(2 * in2, site, ex, 1.0);
198  m_fopr->mult_dag(v, w);
199  set_matrix_1row(Nnz1, index_nz1, elem_nz1, v);
200  if (Nnz_tot + Nnz1 > m_Nnz) {
201  vout.crucial(m_vl, "Error at %s: unexpected data size ini set_matrix\n", class_name.c_str());
202  exit(EXIT_FAILURE);
203  }
204 
205  m_rowidx_nz[j] = Nnz_tot;
206  for (int i = 0; i < Nnz1; ++i) {
207  m_column_nz[Nnz_tot + i] = index_nz1[i];
208  m_elem_nz[2 * (Nnz_tot + i)] = elem_nz1[2 * i];
209  m_elem_nz[2 * (Nnz_tot + i) + 1] = elem_nz1[2 * i + 1];
210  }
211  Nnz_tot += Nnz1;
212  }
213  }
214  }
215 
216  m_Nnz = Nnz_tot;
217 
218  vout.general(m_vl, " Nnz(recalc) = %d\n", m_Nnz);
219 }
220 
221 
222 //====================================================================
223 void Fopr_CRS::set_matrix_1row(int& Nnz, std::vector<int>& index_nz,
224  std::vector<double>& elem_nz, const Field& v)
225 {
226  // the implementation of this function assumes that the field
227  // is complex valued.
228 
229  const int Nsize = v.size() / 2;
230 
231  int j = 0;
232 
233  for (int i = 0; i < Nsize; ++i) {
234  if ((v.cmp(2 * i) != 0.0) || (v.cmp(2 * i + 1) != 0.0)) {
235  index_nz[j] = i;
236  elem_nz[2 * j] = v.cmp(2 * i);
237  elem_nz[2 * j + 1] = -v.cmp(2 * i + 1);
238 
239  ++j;
240  }
241  }
242 
243  Nnz = j;
244 }
245 
246 
247 //====================================================================
248 void Fopr_CRS::set_matrix(const std::string fname)
249 {
250  std::fstream config;
251 
252  config.open(fname.c_str(), std::ios::in);
253  if (!config.is_open()) {
254  vout.crucial(m_vl, "Error at %s: Failed to open the next file %s in %s(%d)",
255  class_name.c_str(), fname.c_str(), __FILE__, __LINE__);
256  exit(EXIT_FAILURE);
257  }
258 
259  vout.general(m_vl, "Reading CRS matrix from %s\n", fname.c_str());
260 
261  config >> m_Nin;
262  config >> m_Nvol;
263  config >> m_Nex;
264  config >> m_Nsize;
265  config >> m_Nnz;
266 
267  m_rowidx_nz.resize(m_Nsize);
268  m_column_nz.resize(m_Nnz);
269  m_elem_nz.resize(2 * m_Nnz);
270 
271  for (int j = 0; j < m_Nsize; ++j) {
272  config >> m_rowidx_nz[j];
273  m_rowidx_nz[j] -= 1;
274  }
275 
276  for (int j = 0; j < m_Nnz; ++j) {
277  config >> m_column_nz[j];
278  m_column_nz[j] -= 1;
279  }
280 
281  for (int j = 0; j < m_Nnz; ++j) {
282  config >> m_elem_nz[2 * j];
283  config >> m_elem_nz[2 * j + 1];
284  }
285 
286  config.close();
287 
288  vout.general(m_vl, "Reading CRS matrix finished.\n");
289 }
290 
291 
292 //====================================================================
293 void Fopr_CRS::write_matrix(const std::string fname)
294 {
295  std::fstream config;
296 
297  config.open(fname.c_str(), std::ios::out);
298  if (!config.is_open()) {
299  vout.crucial(m_vl, "Error at %s: Failed to open the text file %s(%d)\n",
300  class_name.c_str(), __FILE__, __LINE__);
301  exit(EXIT_FAILURE);
302  }
303 
304  vout.general(m_vl, "Writing CRS matrix to %s\n", fname.c_str());
305 
306  config << m_Nin << std::endl;
307  config << m_Nvol << std::endl;
308  config << m_Nex << std::endl;
309  config << m_Nsize << std::endl;
310  config << m_Nnz << std::endl;
311 
312  for (int j = 0; j < m_Nsize; ++j) {
313  config << m_rowidx_nz[j] + 1 << std::endl;
314  }
315 
316  for (int j = 0; j < m_Nnz; ++j) {
317  config << m_column_nz[j] + 1 << std::endl;
318  }
319 
320  config.setf(std::ios_base::scientific, std::ios_base::floatfield);
321  config.precision(14);
322 
323  for (int j = 0; j < m_Nnz; ++j) {
324  config << m_elem_nz[2 * j] << std::endl;
325  config << m_elem_nz[2 * j + 1] << std::endl;
326  }
327 
328  config.close();
329 
330  vout.general(m_vl, "Writing CRS matrix finished.\n");
331 }
332 
333 
334 //====================================================================
336 {
337  //- Counting of floating point operations in giga unit.
338  // not implemented, yet.
339 
340  vout.general(m_vl, "Warning at %s: flop_count() has not been implemented.\n", class_name.c_str());
341 
342  const double gflop = 0.0;
343 
344  return gflop;
345 }
346 
347 
348 //====================================================================
349 //============================================================END=====
Parameters::set_string
void set_string(const string &key, const string &value)
Definition: parameters.cpp:39
Fopr_CRS::m_Nnz
int m_Nnz
Definition: fopr_CRS.h:46
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
AFopr::field_nex
virtual int field_nex()=0
returns the external degree of freedom of the fermion field.
Fopr_CRS::set_matrix
void set_matrix()
Definition: fopr_CRS.cpp:140
Field::nex
int nex() const
Definition: field.h:128
Fopr_CRS::m_mode
std::string m_mode
Definition: fopr_CRS.h:50
Fopr_CRS::D
void D(Field &, const Field &)
Definition: fopr_CRS.cpp:68
Fopr_CRS::get_parameters
void get_parameters(Parameters &) const
gets parameters by a Parameter object: to be implemented in a subclass.
Definition: fopr_CRS.cpp:39
Field::nin
int nin() const
Definition: field.h:126
Fopr_CRS::class_name
static const std::string class_name
Definition: fopr_CRS.h:40
Fopr_CRS::m_column_nz
std::vector< int > m_column_nz
Definition: fopr_CRS.h:48
AFopr::mult_dag
virtual void mult_dag(AFIELD &, const AFIELD &)
hermitian conjugate of mult.
Definition: afopr.h:102
Fopr_CRS::m_vl
Bridge::VerboseLevel m_vl
Definition: fopr_CRS.h:43
Fopr_CRS::flop_count
double flop_count()
this returns the number of floating point operations.
Definition: fopr_CRS.cpp:335
Fopr_CRS::Ddag
void Ddag(Field &, const Field &)
Definition: fopr_CRS.cpp:105
Fopr_CRS::m_Nvol
int m_Nvol
Definition: fopr_CRS.h:45
AFopr::field_nvol
virtual int field_nvol()=0
returns the volume of the fermion field.
Fopr_CRS::m_elem_nz
std::vector< double > m_elem_nz
Definition: fopr_CRS.h:49
Fopr_CRS::set_matrix_1row
void set_matrix_1row(int &, std::vector< int > &, std::vector< double > &, const Field &)
Definition: fopr_CRS.cpp:223
Field::size
int size() const
Definition: field.h:132
fopr_CRS.h
threadManager.h
Fopr_CRS::set_parameters
void set_parameters(const Parameters &)
sets parameters by a Parameter object: to be implemented in a subclass.
Definition: fopr_CRS.cpp:29
Fopr_CRS::m_Nsize
int m_Nsize
Definition: fopr_CRS.h:46
Field::nvol
int nvol() const
Definition: field.h:127
CommonParameters::NPE
static int NPE()
Definition: commonParameters.h:101
Fopr_CRS::set_mode
void set_mode(const std::string mode)
setting the mode of multiplication if necessary. Default implementation here is just to avoid irrelev...
Definition: fopr_CRS.cpp:46
Field::add
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:191
Field::cmp
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
Fopr_CRS::m_fopr
Fopr * m_fopr
Definition: fopr_CRS.h:51
Fopr_CRS::m_Nex
int m_Nex
Definition: fopr_CRS.h:45
Bridge::BridgeIO::set_verbose_level
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:133
Fopr_CRS::write_matrix
void write_matrix(const std::string)
Definition: fopr_CRS.cpp:293
Parameters::fetch_string
int fetch_string(const string &key, string &value) const
Definition: parameters.cpp:378
Bridge::BridgeIO::crucial
void crucial(const char *format,...)
Definition: bridgeIO.cpp:180
Fopr_CRS::m_rowidx_nz
std::vector< int > m_rowidx_nz
Definition: fopr_CRS.h:47
Field
Container of Field-type object.
Definition: field.h:46
Fopr_CRS::m_Nin
int m_Nin
Definition: fopr_CRS.h:45
AFopr::field_nin
virtual int field_nin()=0
returns the on-site degree of freedom of the fermion field.
ThreadManager::get_thread_id
static int get_thread_id()
returns thread id.
Definition: threadManager.cpp:253
Bridge::BridgeIO::general
void general(const char *format,...)
Definition: bridgeIO.cpp:200
Bridge::vout
BridgeIO vout
Definition: bridgeIO.cpp:512
Bridge::BridgeIO::get_verbose_level
static std::string get_verbose_level(const VerboseLevel vl)
Definition: bridgeIO.cpp:154
Fopr_CRS::DdagD
void DdagD(Field &, const Field &)
Definition: fopr_CRS.cpp:58