Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
fieldIO.cpp
Go to the documentation of this file.
1 
14 #include "fieldIO.h"
15 
17 #include "bridgeIO.h"
18 using Bridge::vout;
19 
20 #include "Tools/file_utils.h"
21 
22 //#include <errno.h>
23 #ifdef HAVE_ENDIAN
24 #include <endian.h>
25 #endif
26 
27 const std::string FieldIO::class_name = "FieldIO";
28 
29 //====================================================================
30 void FieldIO::deliver(Field *vlocal, Field *vglobal)
31 {
32  int nin = vlocal->nin();
33  int nvol = vlocal->nvol();
34  int nex = vlocal->nex();
35 
36  int Lx = CommonParameters::Lx();
37  int Ly = CommonParameters::Ly();
38  int Lz = CommonParameters::Lz();
39  int Lt = CommonParameters::Lt();
40  int Lvol = CommonParameters::Lvol();
41 
42  int Nx = CommonParameters::Nx();
43  int Ny = CommonParameters::Ny();
44  int Nz = CommonParameters::Nz();
45  int Nt = CommonParameters::Nt();
46  int Nvol = CommonParameters::Nvol();
47 
48  int NPEx = CommonParameters::NPEx();
49  int NPEy = CommonParameters::NPEy();
50  int NPEz = CommonParameters::NPEz();
51  int NPEt = CommonParameters::NPEt();
52  int NPE = CommonParameters::NPE();
53 
55  if ((nin != vglobal->nin()) ||
56  (nex != vglobal->nex()) ||
57  (Lvol != vglobal->nvol())) {
58  vout.crucial(m_vl, "%s: %s: size mismatch.\n", class_name.c_str(), __func__);
59  exit(EXIT_FAILURE);
60  }
61  }
62 
63  Index_lex gindex(Lx, Ly, Lz, Lt);
64 
65  Field vtmp(nin, nvol, nex);
66 
68 
69  for (int iblock = 0; iblock < NPE; ++iblock) {
70  int ipx = (iblock) % NPEx;
71  int ipy = (iblock / NPEx) % NPEy;
72  int ipz = (iblock / NPEx / NPEy) % NPEz;
73  int ipt = (iblock / NPEx / NPEy / NPEz) % NPEt;
74 
76  for (int j = 0; j < nex; ++j) {
77  for (int t = 0; t < Nt; ++t) {
78  int t2 = t + Nt * ipt;
79  for (int z = 0; z < Nz; ++z) {
80  int z2 = z + Nz * ipz;
81  for (int y = 0; y < Ny; ++y) {
82  int y2 = y + Ny * ipy;
83  for (int x = 0; x < Nx; ++x) {
84  int x2 = x + Nx * ipx;
85 
86  int lsite = idx.site(x, y, z, t);
87  int gsite = gindex.site(x2, y2, z2, t2);
88 
89  for (int i = 0; i < nin; ++i) {
90  vtmp.set(i, lsite, j, vglobal->cmp(i, gsite, j));
91  }
92  }
93  }
94  }
95  }
96  }
97  }
98 
99  int size = nin * nvol * nex;
100 
101  int ipe;
102  int coord[4] = { ipx, ipy, ipz, ipt, };
103 
104  Communicator::grid_rank(&ipe, coord);
105 
106  send_1to1(size, vlocal, &vtmp, ipe, 0, ipe);
107 
109  }
110 
112 }
113 
114 
115 //====================================================================
116 void FieldIO::gather(Field *vglobal, Field *vlocal)
117 {
118  int nin = vlocal->nin();
119  int nvol = vlocal->nvol();
120  int nex = vlocal->nex();
121 
122  int Lx = CommonParameters::Lx();
123  int Ly = CommonParameters::Ly();
124  int Lz = CommonParameters::Lz();
125  int Lt = CommonParameters::Lt();
126  int Lvol = CommonParameters::Lvol();
127 
128  int Nx = CommonParameters::Nx();
129  int Ny = CommonParameters::Ny();
130  int Nz = CommonParameters::Nz();
131  int Nt = CommonParameters::Nt();
132  int Nvol = CommonParameters::Nvol();
133 
134  int NPEx = CommonParameters::NPEx();
135  int NPEy = CommonParameters::NPEy();
136  int NPEz = CommonParameters::NPEz();
137  int NPEt = CommonParameters::NPEt();
138  int NPE = CommonParameters::NPE();
139 
140  if (Communicator::is_primary()) {
141  if ((nin != vglobal->nin()) ||
142  (nex != vglobal->nex()) ||
143  (Lvol != vglobal->nvol())) {
144  vout.crucial(m_vl, "%s: %s: size mismatch.\n", class_name.c_str(), __func__);
145  exit(EXIT_FAILURE);
146  }
147  }
148 
149  Index_lex gindex(Lx, Ly, Lz, Lt);
150 
151  Field vtmp(nin, nvol, nex);
152 
154 
155  for (int iblock = 0; iblock < NPE; ++iblock) {
156  int ipx = (iblock) % NPEx;
157  int ipy = (iblock / NPEx) % NPEy;
158  int ipz = (iblock / NPEx / NPEy) % NPEz;
159  int ipt = (iblock / NPEx / NPEy / NPEz) % NPEt;
160 
161  int ipe;
162  int coord[4] = { ipx, ipy, ipz, ipt, };
163 
164  Communicator::grid_rank(&ipe, coord);
165 
166  int size = nin * nvol * nex;
167 
168  send_1to1(size, &vtmp, vlocal, 0, ipe, ipe);
169 
171 
172  if (Communicator::is_primary()) {
173  for (int j = 0; j < nex; ++j) {
174  for (int t = 0; t < Nt; ++t) {
175  int t2 = t + Nt * ipt;
176  for (int z = 0; z < Nz; ++z) {
177  int z2 = z + Nz * ipz;
178  for (int y = 0; y < Ny; ++y) {
179  int y2 = y + Ny * ipy;
180  for (int x = 0; x < Nx; ++x) {
181  int x2 = x + Nx * ipx;
182 
183  int site = idx.site(x, y, z, t);
184  int gsite = gindex.site(x2, y2, z2, t2);
185 
186  for (int i = 0; i < nin; ++i) {
187  vglobal->set(i, gsite, j, vtmp.cmp(i, site, j));
188  }
189  }
190  }
191  }
192  }
193  }
194  }
195  }
196 
198 }
199 
200 
201 //====================================================================
202 
204 {
205 #if defined(__BYTE_ORDER)
206  return __BYTE_ORDER == __BIG_ENDIAN;
207 
208 #else
209  union
210  {
211  int l;
212  char c[sizeof(int)];
213  }
214  u;
215 
216  u.l = 1;
217 
218  return (u.c[sizeof(int) - 1] == 1) ? true : false;
219 #endif
220 }
221 
222 
223 //====================================================================
224 
225 void FieldIO::convert_endian(void *ptr, size_t size, size_t nmemb)
226 {
227  switch (size)
228  {
229  case 1: // bytes: do nothing.
230  break;
231 
232  case 2:
233  { // uint16_t (short)
234  uint16_t *p = (uint16_t *)ptr;
235 
236  for (unsigned int i = 0; i < nmemb; ++i) {
237  uint16_t v = p[i];
238  uint16_t w;
239 
240  w = v >> 8 & 0x00ff;
241  w |= v << 8 & 0xff00;
242 
243  p[i] = w;
244  }
245 
246  break;
247  }
248 
249  case 4:
250  { // uint32_t
251  uint32_t *p = (uint32_t *)ptr;
252 
253  for (unsigned int i = 0; i < nmemb; ++i) {
254  uint32_t v = p[i];
255  uint32_t w;
256 
257  w = v >> 24 & 0x000000ff;
258  w |= v >> 8 & 0x0000ff00;
259  w |= v << 8 & 0x00ff0000;
260  w |= v << 24 & 0xff000000;
261 
262  p[i] = w;
263  }
264 
265  break;
266  }
267 
268  case 8:
269  { // uint64_t
270  uint32_t *p = (uint32_t *)ptr;
271 
272  for (unsigned int i = 0; i < nmemb; ++i) {
273  uint32_t v1 = *p;
274  uint32_t v2 = *(p + 1);
275  uint32_t w1, w2;
276 
277  w1 = v1 >> 24 & 0x000000ff;
278  w1 |= v1 >> 8 & 0x0000ff00;
279  w1 |= v1 << 8 & 0x00ff0000;
280  w1 |= v1 << 24 & 0xff000000;
281 
282  w2 = v2 >> 24 & 0x000000ff;
283  w2 |= v2 >> 8 & 0x0000ff00;
284  w2 |= v2 << 8 & 0x00ff0000;
285  w2 |= v2 << 24 & 0xff000000;
286 
287  *p++ = w2;
288  *p++ = w1;
289  }
290 
291  break;
292  }
293 
294  default:
295 // return EINVAL;
296  vout.crucial("%s: %s: unsupported word size.\n", class_name.c_str(), __func__);
297  exit(EXIT_FAILURE);
298  }
299 
300 // return 0;
301 }
302 
303 
304 //====================================================================
305 // default implementation for collection of fields.
306 void FieldIO::read_file(std::vector<Field *>& vv, const std::string& filename_base)
307 {
308  for (int i = 0, n = vv.size(); i < n; ++i) {
309  std::string filename = FileUtils::generate_filename("%s.%d", filename_base.c_str(), i);
310  read_file(vv[i], filename);
311  }
312 }
313 
314 
315 //====================================================================
316 // default implementation for collection of fields.
317 void FieldIO::write_file(std::vector<Field *>& vv, const std::string& filename_base)
318 {
319  for (int i = 0, n = vv.size(); i < n; ++i) {
320  std::string filename = FileUtils::generate_filename("%s.%d", filename_base.c_str(), i);
321  write_file(vv[i], filename);
322  }
323 }
324 
325 
326 //====================================================================
327 //============================================================END=====
Index_lex idx
Definition: fieldIO.h:59
BridgeIO vout
Definition: bridgeIO.cpp:495
static const std::string class_name
Definition: fieldIO.h:56
static int NPEy()
static int NPEt()
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:164
int site(const int &x, const int &y, const int &z, const int &t) const
Definition: index_lex.h:53
void deliver(Field *vlocal, Field *vglobal)
distribute data on primary node over parallel nodes.
Definition: fieldIO.cpp:30
Container of Field-type object.
Definition: field.h:39
int nvol() const
Definition: field.h:116
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:132
static int Lvol()
static bool is_bigendian()
Definition: fieldIO.cpp:203
int nin() const
Definition: field.h:115
std::string generate_filename(const char *fmt,...)
Definition: file_utils.cpp:17
int nex() const
Definition: field.h:117
Lexical site index.
Definition: index_lex.h:34
static int grid_rank(int *rank, const int *grid_coord)
find rank number from grid coordinate.
static int NPEx()
void crucial(const char *format,...)
Definition: bridgeIO.cpp:178
int send_1to1(int count, Field *recv_buf, Field *send_buf, int p_to, int p_from, int tag)
Definition: field.h:334
static int NPEz()
virtual void read_file(Field *v, std::string)=0
read data from file.
virtual void write_file(Field *v, std::string)=0
write data to file.
static int sync()
synchronize within small world.
static void convert_endian(void *buf, size_t size, size_t nmemb)
check if machine byte order is big-endian.
Definition: fieldIO.cpp:225
static bool is_primary()
check if the present node is primary in small communicator.
void gather(Field *vglobal, Field *vlocal)
gather data on parallel nodes to primary node.
Definition: fieldIO.cpp:116
static int NPE()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64