Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
field.h
Go to the documentation of this file.
1 
15 #ifndef FIELD_INCLUDED
16 #define FIELD_INCLUDED
17 
18 #include <iostream>
19 #include <valarray>
20 #include <string>
21 #include <assert.h>
22 
25 
26 #include "bridge_complex.h"
27 
29 
39 class Field
40 {
41  public:
42  enum element_type { REAL = 1, COMPLEX = 2 };
43 
44  protected:
45  int m_Nin; // internal d.o.f.
46  int m_Nvol; // lattice volume
47  int m_Nex; // external d.o.f.
49  // total degree of freedom is Nin * Nsite * Nex.
50 
51  std::valarray<double> field;
52 
53  inline
54  int myindex(const int jin, const int site, const int jex) const
55  {
56  return jin + m_Nin * (site + m_Nvol * jex);
57  }
58 
60 
61  public:
62 
63  Field() :
65  field(0),
66  m_vl(CommonParameters::Vlevel())
67  {
68  check();
69  }
70 
71  Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl = COMPLEX) :
72  m_Nin(Nin), m_Nvol(Nvol), m_Nex(Nex), m_element_type(cmpl),
73  field(ntot()),
74  m_vl(CommonParameters::Vlevel())
75  {
76  check();
77  }
78 
79  Field clone() const
80  {
82  }
83 
84  void reset(const int Nin, const int Nvol, const int Nex,
85  const element_type cmpl = COMPLEX)
86  {
87  if ((m_Nin == Nin) &&
88  (m_Nvol == Nvol) &&
89  (m_Nex == Nex) &&
90  (m_element_type == cmpl)) return;
91 
92  m_Nin = Nin;
93  m_Nvol = Nvol;
94  m_Nex = Nex;
95  m_element_type = cmpl;
96 
97  field.resize(ntot());
98  }
99 
100  // assignment
101  //Field& operator=(const double a) { field = a; return *this; }
102  Field& operator=(const Field& v)
103  {
104  assert(m_Nin == v.nin());
105  assert(m_Nvol == v.nvol());
106  assert(m_Nex == v.nex());
107  copy(*this, v);
108  return *this;
109  }
110 
111  private:
112  void check();
113 
114  public:
115  int nin() const { return m_Nin; }
116  int nvol() const { return m_Nvol; }
117  int nex() const { return m_Nex; }
119 
120  int ntot() const { return m_Nin * m_Nvol * m_Nex; }
121  int size() const { return ntot(); }
122 
124  bool check_size(const int nin, const int nvol, const int nex) const
125  {
126  bool chk = true;
127 
128  if ((m_Nin != nin) || (m_Nvol != nvol) || (m_Nex != nex)) chk = false;
129  return chk;
130  }
131 
132  double cmp(const int jin, const int site, const int jex) const
133  {
134  return field[myindex(jin, site, jex)];
135  }
136 
137  double cmp(const int i) const
138  {
139  return field[i];
140  }
141 
142  const double *ptr(const int jin, const int site, const int jex) const
143  {
144  // when using c++03, const_cast is necessary.
145  return &(const_cast<std::valarray<double>&>(field)[myindex(jin, site, jex)]);
146  }
147 
148  double *ptr(const int jin, const int site, const int jex)
149  {
150  return &field[myindex(jin, site, jex)];
151  }
152 
153  const double *ptr(const int i) const
154  {
155  // when using c++03, const_cast is necessary.
156  return &(const_cast<std::valarray<double>&>(field)[i]);
157  }
158 
159  double *ptr(const int i)
160  {
161  return &field[i];
162  }
163 
164  void set(const int jin, const int site, const int jex, double v)
165  {
166  field[myindex(jin, site, jex)] = v;
167  }
168 
169  void set(const int i, double v)
170  {
171  field[i] = v;
172  }
173 
174  void set(double a);
175 
176  void add(const int jin, const int site, const int jex, double v)
177  {
178  field[myindex(jin, site, jex)] += v;
179  }
180 
181  void add(const int i, double v)
182  {
183  field[i] += v;
184  }
185 
186  void setpart_ex(int ex, const Field& w, int exw)
187  {
188  assert(ex < nex());
189  assert(exw < w.nex());
190  return copy(*this, ex, w, exw);
191  }
192 
193  void addpart_ex(int ex, const Field& w, int exw)
194  {
195  assert(ex < nex());
196  assert(exw < w.nex());
197  return axpy(*this, ex, 1.0, w, exw);
198  }
199 
200  void addpart_ex(int ex, const Field& w, int exw, double prf)
201  {
202  assert(ex < nex());
203  assert(exw < w.nex());
204  return axpy(*this, ex, prf, w, exw);
205  }
206 
207  // BLAS-like routine (friend declaration)
208 
209  double norm2() const;
210 
211  double norm() const { return sqrt(norm2()); }
212 
213  friend
214  double dot(const Field& y, const Field& x);
215 
216  friend
217  double dot(const Field& y, const int exy, const Field& x, const int exx);
218 
219  friend
220  dcomplex dotc(const Field& y, const Field& x);
221 
222  friend
223  dcomplex dotc(const Field& y, const int exy, const Field& x, const int exx);
224 
225  friend
226  void axpy(Field& y, const double a, const Field& x);
227 
228  friend
229  void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
230 
231  friend
232  void axpy(Field& y, const dcomplex a, const Field& x);
233 
234  friend
235  void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
236 
237  friend
238  void scal(Field& x, const double a);
239 
240  friend
241  void scal(Field& x, const int exx, const double a);
242 
243  friend
244  void scal(Field& x, const dcomplex a);
245 
246  friend
247  void scal(Field& x, const int exx, const dcomplex a);
248 
249  friend
250  void copy(Field& y, const Field& x);
251 
252  friend
253  void copy(Field& y, const int exy, const Field& x, const int exx);
254 
255  friend
256  void aypx(const double a, Field& y, const Field& x);
257 
258  friend
259  void aypx(const dcomplex a, Field& y, const Field& x);
260 
261 
267  void stat(double& Fave, double& Fmax, double& Fdev) const;
268 };
269 
270 //----------------------------------------------------------------
271 // BLAS-like routines defined outside the Field class.
272 
275 double dot(const Field& y, const Field& x);
276 
278 double dot(const Field& y, const int exy, const Field& x, const int exx);
279 
282 dcomplex dotc(const Field& y, const Field& x);
283 
285 dcomplex dotc(const Field& y, const int exy, const Field& x, const int nexx);
286 
288 void axpy(Field& y, const double a, const Field& x);
289 
291 void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
292 
295 void axpy(Field& y, const dcomplex a, const Field& x);
296 
298 void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
299 
301 void scal(Field& x, const double a);
302 
304 void scal(Field& x, const int exx, const double a);
305 
308 void scal(Field& x, const dcomplex a);
309 
311 void scal(Field& x, const int exx, const dcomplex a);
312 
314 void copy(Field& y, const Field& x);
315 
317 void copy(Field& y, const int exy, const Field& x, const int exx);
318 
320 void aypx(const double a, Field& y, const Field& x);
321 
324 void aypx(const dcomplex a, Field& y, const Field& x);
325 
326 //----------------------------------------------------------------
327 
328 inline int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
329 {
330  return Communicator::exchange(count, recv_buf->ptr(0), send_buf->ptr(0), idir, ipm, tag);
331 }
332 
333 
334 inline int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
335 {
336  return Communicator::send_1to1(count, recv_buf->ptr(0), send_buf->ptr(0), p_to, p_from, tag);
337 }
338 
339 
340 //----------------------------------------------------------------
341 // utility: report statistics.
342 void report_field_stat(const Bridge::VerboseLevel vl, const std::string& msg, const Field& f);
343 
344 //----------------------------------------------------------------
345 #endif
int m_Nin
Definition: field.h:45
void report_field_stat(const Bridge::VerboseLevel vl, const std::string &msg, const Field &f)
Definition: field.cpp:552
void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:282
friend void scal(Field &x, const double a)
scal(x, a): x = a * x
Definition: field.cpp:282
void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:168
int m_Nex
Definition: field.h:47
const double * ptr(const int jin, const int site, const int jex) const
Definition: field.h:142
double norm2() const
Definition: field.cpp:441
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:164
const double * ptr(const int i) const
Definition: field.h:153
int m_Nvol
Definition: field.h:46
double * ptr(const int i)
Definition: field.h:159
double * ptr(const int jin, const int site, const int jex)
Definition: field.h:148
Container of Field-type object.
Definition: field.h:39
Bridge::VerboseLevel m_vl
Definition: field.h:59
friend void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:381
int nvol() const
Definition: field.h:116
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:132
element_type
Definition: field.h:42
friend void axpy(Field &y, const double a, const Field &x)
axpy(y, a, x): y := a * x + y
Definition: field.cpp:168
Field & operator=(const Field &v)
Definition: field.h:102
bool check_size(const int nin, const int nvol, const int nex) const
checking size parameters. [23 May 2016 H.Matsufuru]
Definition: field.h:124
Field()
Definition: field.h:63
void addpart_ex(int ex, const Field &w, int exw)
Definition: field.h:193
static int send_1to1(int count, double *recv_buf, double *send_buf, int p_to, int p_from, int tag)
send array of double from rank p_from to rank p_to. communication distinguished by tag...
friend double dot(const Field &y, const Field &x)
Definition: field.cpp:46
void addpart_ex(int ex, const Field &w, int exw, double prf)
Definition: field.h:200
element_type m_element_type
Definition: field.h:48
int nin() const
Definition: field.h:115
void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:461
void set(const int i, double v)
Definition: field.h:169
friend dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:92
void reset(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:84
double norm() const
Definition: field.h:211
Field clone() const
Definition: field.h:79
dcomplex dotc(const Field &y, const Field &x)
Definition: field.cpp:92
static int exchange(int count, double *recv_buf, double *send_buf, int idir, int ipm, int tag)
receive array of double from upstream specified by idir and ipm, and send array to downstream...
void check()
Definition: field.cpp:39
int nex() const
Definition: field.h:117
Common parameter class: provides parameters as singleton.
double dot(const Field &y, const Field &x)
Definition: field.cpp:46
std::valarray< double > field
Definition: field.h:51
void add(const int i, double v)
Definition: field.h:181
int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
Definition: field.h:334
friend void aypx(const double a, Field &y, const Field &x)
aypx(y, a, x): y := a * y + x
Definition: field.cpp:461
Bridge::VerboseLevel vl
Definition: checker.cpp:18
int ntot() const
Definition: field.h:120
VerboseLevel
Definition: bridgeIO.h:42
Field(const int Nin, const int Nvol, const int Nex, const element_type cmpl=COMPLEX)
Definition: field.h:71
element_type field_element_type() const
Definition: field.h:118
void add(const int jin, const int site, const int jex, double v)
Definition: field.h:176
void stat(double &Fave, double &Fmax, double &Fdev) const
determines the statistics of the field. average, maximum value, and deviation is determined over glob...
Definition: field.cpp:516
void copy(Field &y, const Field &x)
copy(y, x): y = x
Definition: field.cpp:381
void setpart_ex(int ex, const Field &w, int exw)
Definition: field.h:186
int myindex(const int jin, const int site, const int jex) const
Definition: field.h:54
int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
Definition: field.h:328
double cmp(const int i) const
Definition: field.h:137
int size() const
Definition: field.h:121