Bridge++  Ver. 1.3.x
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 
23 #include "commonParameters.h"
24 #include "communicator.h"
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() :
64  m_Nin(0), m_Nvol(0), m_Nex(0), m_element_type(COMPLEX),
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  {
81  return Field(m_Nin, m_Nvol, m_Nex, m_element_type);
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 
123  double cmp(const int jin, const int site, const int jex) const
124  {
125  return field[myindex(jin, site, jex)];
126  }
127 
128  double cmp(const int i) const
129  {
130  return field[i];
131  }
132 
133  const double *ptr(const int jin, const int site, const int jex) const
134  {
135  // when using c++03, const_cast is necessary.
136  return &(const_cast<std::valarray<double>&>(field)[myindex(jin, site, jex)]);
137  }
138 
139  double *ptr(const int jin, const int site, const int jex)
140  {
141  return &field[myindex(jin, site, jex)];
142  }
143 
144  const double *ptr(const int i) const
145  {
146  // when using c++03, const_cast is necessary.
147  return &(const_cast<std::valarray<double>&>(field)[i]);
148  }
149 
150  double *ptr(const int i)
151  {
152  return &field[i];
153  }
154 
155  void set(const int jin, const int site, const int jex, double v)
156  {
157  field[myindex(jin, site, jex)] = v;
158  }
159 
160  void set(const int i, double v)
161  {
162  field[i] = v;
163  }
164 
165  void set(double a);
166 
167  void add(const int jin, const int site, const int jex, double v)
168  {
169  field[myindex(jin, site, jex)] += v;
170  }
171 
172  void add(const int i, double v)
173  {
174  field[i] += v;
175  }
176 
177  void setpart_ex(int ex, const Field& w, int exw)
178  {
179  assert(ex < nex());
180  assert(exw < w.nex());
181 // for (int site = 0; site < m_Nvol; ++site) {
182 // for (int jin = 0; jin < m_Nin; ++jin) {
183 // field[myindex(jin, site, ex)] = w.field[myindex(jin, site, exw)];
184 // }
185 // }
186  return copy(*this, ex, w, exw);
187  }
188 
189  void addpart_ex(int ex, const Field& w, int exw)
190  {
191  assert(ex < nex());
192  assert(exw < w.nex());
193 // for (int site = 0; site < m_Nvol; ++site) {
194 // for (int jin = 0; jin < m_Nin; ++jin) {
195 // field[myindex(jin, site, ex)] += w.field[myindex(jin, site, exw)];
196 // }
197 // }
198  return axpy(*this, ex, 1.0, w, exw);
199  }
200 
201  void addpart_ex(int ex, const Field& w, int exw, double prf)
202  {
203  assert(ex < nex());
204  assert(exw < w.nex());
205 // for (int site = 0; site < m_Nvol; ++site) {
206 // for (int jin = 0; jin < m_Nin; ++jin) {
207 // field[myindex(jin, site, ex)]
208 // += prf * w.field[myindex(jin, site, exw)];
209 // }
210 // }
211  return axpy(*this, ex, prf, w, exw);
212  }
213 
214  // linear algebra operations
215  // Field& operator-() { field *= -1; return *this; }
216 
217  /*
218  Field& operator+=(const Field& v) { field += v.field; return *this; }
219  Field& operator-=(const Field& v) { field -= v.field; return *this; }
220  Field& operator*=(const double a) { field *= a; return *this; }
221  Field& operator/=(const double a) { field *= 1.0 / a; return *this; }
222  Field& operator*=(const dcomplex a)
223  {
224  if (imag(a) == 0.0) {
225  field *= real(a);
226  } else {
227  scal(*this, a);
228  }
229  return *this;
230  }
231  */
232 
233  // XXX
234  //double operator*(const Field& rhs) { return dot(*this, rhs); }
235 
236  // BLAS-like routine
237 
238  double norm2() const;
239 
240  double norm() const { return sqrt(norm2()); }
241 
242  friend
243  double dot(const Field& y, const Field& x);
244 
245  friend
246  double dot(const Field& y, const int exy, const Field& x, const int exx);
247 
248  friend
249  dcomplex dotc(const Field& y, const Field& x);
250 
251  friend
252  dcomplex dotc(const Field& y, const int exy, const Field& x, const int exx);
253 
254  friend
255  void axpy(Field& y, const double a, const Field& x);
256 
257  friend
258  void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
259 
260  friend
261  void axpy(Field& y, const dcomplex a, const Field& x);
262 
263  friend
264  void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
265 
266  friend
267  void scal(Field& x, const double a);
268 
269  friend
270  void scal(Field& x, const int exx, const double a);
271 
272  friend
273  void scal(Field& x, const dcomplex a);
274 
275  friend
276  void scal(Field& x, const int exx, const dcomplex a);
277 
278  friend
279  void copy(Field& y, const Field& x);
280 
281  friend
282  void copy(Field& y, const int exy, const Field& x, const int exx);
283 
284  friend
285  void aypx(const double a, Field& y, const Field& x);
286 
287  friend
288  void aypx(const dcomplex a, Field& y, const Field& x);
289 
290 
296  void stat(double& Fave, double& Fmax, double& Fdev) const;
297 };
298 
299 //----------------------------------------------------------------
300 
301 /*
302 inline Field operator*(const Field& v, const double s)
303 {
304  Field w(v);
305 
306  w *= s;
307  return w;
308 }
309 
310 
311 inline Field operator*(const double s, const Field& v)
312 {
313  return operator*(v, s);
314 }
315 
316 
317 inline Field operator+(const Field& lhs, const Field& rhs)
318 {
319  Field w(lhs);
320 
321  w += rhs;
322  return w;
323 }
324 
325 
326 inline Field operator-(const Field& lhs, const Field& rhs)
327 {
328  Field w(lhs);
329 
330  w -= rhs;
331  return w;
332 }
333 */
334 
335 //----------------------------------------------------------------
336 // BLAS-like routines
337 
340 double dot(const Field& y, const Field& x);
341 
343 double dot(const Field& y, const int exy, const Field& x, const int exx);
344 
347 dcomplex dotc(const Field& y, const Field& x);
348 
350 dcomplex dotc(const Field& y, const int exy, const Field& x, const int nexx);
351 
353 void axpy(Field& y, const double a, const Field& x);
354 
356 void axpy(Field& y, const int exy, const double a, const Field& x, const int exx);
357 
360 void axpy(Field& y, const dcomplex a, const Field& x);
361 
363 void axpy(Field& y, const int exy, const dcomplex a, const Field& x, const int exx);
364 
366 void scal(Field& x, const double a);
367 
369 void scal(Field& x, const int exx, const double a);
370 
373 void scal(Field& x, const dcomplex a);
374 
376 void scal(Field& x, const int exx, const dcomplex a);
377 
379 void copy(Field& y, const Field& x);
380 
382 void copy(Field& y, const int exy, const Field& x, const int exx);
383 
385 void aypx(const double a, Field& y, const Field& x);
386 
389 void aypx(const dcomplex a, Field& y, const Field& x);
390 
391 //----------------------------------------------------------------
392 
393 inline int exchange(int count, Field *recv_buf, Field *send_buf, int idir, int ipm, int tag)
394 {
395  return Communicator::exchange(count, recv_buf->ptr(0), send_buf->ptr(0), idir, ipm, tag);
396 }
397 
398 
399 inline int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
400 {
401  return Communicator::send_1to1(count, recv_buf->ptr(0), send_buf->ptr(0), p_to, p_from, tag);
402 }
403 
404 
405 //----------------------------------------------------------------
406 // utility: report statistics.
407 void report_field_stat(const Bridge::VerboseLevel vl, const std::string& msg, const Field& f);
408 
409 //----------------------------------------------------------------
410 #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:133
double norm2() const
Definition: field.cpp:441
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:155
const double * ptr(const int i) const
Definition: field.h:144
int m_Nvol
Definition: field.h:46
double * ptr(const int i)
Definition: field.h:150
double * ptr(const int jin, const int site, const int jex)
Definition: field.h:139
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:123
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
Field()
Definition: field.h:63
void addpart_ex(int ex, const Field &w, int exw)
Definition: field.h:189
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:201
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:160
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:240
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:172
int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
Definition: field.h:399
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:39
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:167
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:177
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:393
double cmp(const int i) const
Definition: field.h:128
int size() const
Definition: field.h:121