Bridge++  Version 1.5.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  const int nin = vlocal->nin();
33  const int nvol = vlocal->nvol();
34  const int nex = vlocal->nex();
35 
36  const int Lx = CommonParameters::Lx();
37  const int Ly = CommonParameters::Ly();
38  const int Lz = CommonParameters::Lz();
39  const int Lt = CommonParameters::Lt();
40  const long_t Lvol = CommonParameters::Lvol();
41 
42  const int Nx = CommonParameters::Nx();
43  const int Ny = CommonParameters::Ny();
44  const int Nz = CommonParameters::Nz();
45  const int Nt = CommonParameters::Nt();
46  const int Nvol = CommonParameters::Nvol();
47 
48  const int NPEx = CommonParameters::NPEx();
49  const int NPEy = CommonParameters::NPEy();
50  const int NPEz = CommonParameters::NPEz();
51  const int NPEt = CommonParameters::NPEt();
52  const 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 
80  for (int z = 0; z < Nz; ++z) {
81  int z2 = z + Nz * ipz;
82 
83  for (int y = 0; y < Ny; ++y) {
84  int y2 = y + Ny * ipy;
85 
86  for (int x = 0; x < Nx; ++x) {
87  int x2 = x + Nx * ipx;
88 
89  int lsite = idx.site(x, y, z, t);
90  int gsite = gindex.site(x2, y2, z2, t2);
91 
92  for (int i = 0; i < nin; ++i) {
93  vtmp.set(i, lsite, j, vglobal->cmp(i, gsite, j));
94  }
95  }
96  }
97  }
98  }
99  }
100  }
101 
102  const int size = nin * nvol * nex;
103 
104  int ipe;
105  int coord[4] = { ipx, ipy, ipz, ipt, };
106 
107  Communicator::grid_rank(&ipe, coord);
108 
109  send_1to1(size, vlocal, &vtmp, ipe, 0, ipe);
110 
112  }
113 
115 }
116 
117 
118 //====================================================================
119 void FieldIO::gather(Field *vglobal, Field *vlocal)
120 {
121  const int nin = vlocal->nin();
122  const int nvol = vlocal->nvol();
123  const int nex = vlocal->nex();
124 
125  const int Lx = CommonParameters::Lx();
126  const int Ly = CommonParameters::Ly();
127  const int Lz = CommonParameters::Lz();
128  const int Lt = CommonParameters::Lt();
129  const long_t Lvol = CommonParameters::Lvol();
130 
131  const int Nx = CommonParameters::Nx();
132  const int Ny = CommonParameters::Ny();
133  const int Nz = CommonParameters::Nz();
134  const int Nt = CommonParameters::Nt();
135  const int Nvol = CommonParameters::Nvol();
136 
137  const int NPEx = CommonParameters::NPEx();
138  const int NPEy = CommonParameters::NPEy();
139  const int NPEz = CommonParameters::NPEz();
140  const int NPEt = CommonParameters::NPEt();
141  const int NPE = CommonParameters::NPE();
142 
143  if (Communicator::is_primary()) {
144  if ((nin != vglobal->nin()) ||
145  (nex != vglobal->nex()) ||
146  (Lvol != vglobal->nvol())) {
147  vout.crucial(m_vl, "%s: %s: size mismatch.\n", class_name.c_str(), __func__);
148  exit(EXIT_FAILURE);
149  }
150  }
151 
152  Index_lex gindex(Lx, Ly, Lz, Lt);
153 
154  Field vtmp(nin, nvol, nex);
155 
157 
158  for (int iblock = 0; iblock < NPE; ++iblock) {
159  int ipx = (iblock) % NPEx;
160  int ipy = (iblock / NPEx) % NPEy;
161  int ipz = (iblock / NPEx / NPEy) % NPEz;
162  int ipt = (iblock / NPEx / NPEy / NPEz) % NPEt;
163 
164  int ipe;
165  int coord[4] = { ipx, ipy, ipz, ipt, };
166 
167  Communicator::grid_rank(&ipe, coord);
168 
169  const int size = nin * nvol * nex;
170 
171  send_1to1(size, &vtmp, vlocal, 0, ipe, ipe);
172 
174 
175  if (Communicator::is_primary()) {
176  for (int j = 0; j < nex; ++j) {
177  for (int t = 0; t < Nt; ++t) {
178  int t2 = t + Nt * ipt;
179  for (int z = 0; z < Nz; ++z) {
180  int z2 = z + Nz * ipz;
181  for (int y = 0; y < Ny; ++y) {
182  int y2 = y + Ny * ipy;
183  for (int x = 0; x < Nx; ++x) {
184  int x2 = x + Nx * ipx;
185 
186  int site = idx.site(x, y, z, t);
187  int gsite = gindex.site(x2, y2, z2, t2);
188 
189  for (int i = 0; i < nin; ++i) {
190  vglobal->set(i, gsite, j, vtmp.cmp(i, site, j));
191  }
192  }
193  }
194  }
195  }
196  }
197  }
198  }
199 
201 }
202 
203 
204 //====================================================================
205 
207 {
208 #if defined(__BYTE_ORDER)
209  return __BYTE_ORDER == __BIG_ENDIAN;
210 #else
211  union
212  {
213  int l;
214  char c[sizeof(int)];
215  }
216  u;
217 
218  u.l = 1;
219 
220  return (u.c[sizeof(int) - 1] == 1) ? true : false;
221 #endif
222 }
223 
224 
225 //====================================================================
226 
227 void FieldIO::convert_endian(void *ptr, size_t size, size_t nmemb)
228 {
229  switch (size)
230  {
231  case 1: // bytes: do nothing.
232  break;
233 
234  case 2:
235  { // uint16_t (short)
236  uint16_t *p = (uint16_t *)ptr;
237 
238  for (unsigned int i = 0; i < nmemb; ++i) {
239  uint16_t v = p[i];
240  uint16_t w;
241 
242  w = v >> 8 & 0x00ff;
243  w |= v << 8 & 0xff00;
244 
245  p[i] = w;
246  }
247 
248  break;
249  }
250 
251  case 4:
252  { // uint32_t
253  uint32_t *p = (uint32_t *)ptr;
254 
255  for (unsigned int i = 0; i < nmemb; ++i) {
256  uint32_t v = p[i];
257  uint32_t w;
258 
259  w = v >> 24 & 0x000000ff;
260  w |= v >> 8 & 0x0000ff00;
261  w |= v << 8 & 0x00ff0000;
262  w |= v << 24 & 0xff000000;
263 
264  p[i] = w;
265  }
266 
267  break;
268  }
269 
270  case 8:
271  { // uint64_t
272  uint32_t *p = (uint32_t *)ptr;
273 
274  for (unsigned int i = 0; i < nmemb; ++i) {
275  uint32_t v1 = *p;
276  uint32_t v2 = *(p + 1);
277  uint32_t w1, w2;
278 
279  w1 = v1 >> 24 & 0x000000ff;
280  w1 |= v1 >> 8 & 0x0000ff00;
281  w1 |= v1 << 8 & 0x00ff0000;
282  w1 |= v1 << 24 & 0xff000000;
283 
284  w2 = v2 >> 24 & 0x000000ff;
285  w2 |= v2 >> 8 & 0x0000ff00;
286  w2 |= v2 << 8 & 0x00ff0000;
287  w2 |= v2 << 24 & 0xff000000;
288 
289  *p = w2;
290  *(p + 1) = w1;
291 
292  p += 2;
293  }
294 
295  break;
296  }
297 
298  default:
299 // return EINVAL;
300  vout.crucial("%s: %s: unsupported word size.\n", class_name.c_str(), __func__);
301  exit(EXIT_FAILURE);
302  }
303 
304 // return 0;
305 }
306 
307 
308 //====================================================================
309 // default implementation for collection of fields.
310 void FieldIO::read_file(std::vector<Field *>& vv, const std::string& filename_base)
311 {
312  for (int i = 0, n = vv.size(); i < n; ++i) {
313  std::string filename = FileUtils::generate_filename("%s.%d", filename_base.c_str(), i);
314  read_file(vv[i], filename);
315  }
316 }
317 
318 
319 //====================================================================
320 // default implementation for collection of fields.
321 void FieldIO::write_file(std::vector<Field *>& vv, const std::string& filename_base)
322 {
323  for (int i = 0, n = vv.size(); i < n; ++i) {
324  std::string filename = FileUtils::generate_filename("%s.%d", filename_base.c_str(), i);
325  write_file(vv[i], filename);
326  }
327 }
328 
329 
330 //====================================================================
331 //============================================================END=====
Index_lex idx
Definition: fieldIO.h:59
BridgeIO vout
Definition: bridgeIO.cpp:503
static const std::string class_name
Definition: fieldIO.h:56
static int NPEy()
void set(const int jin, const int site, const int jex, double v)
Definition: field.h:175
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:45
int nvol() const
Definition: field.h:127
double cmp(const int jin, const int site, const int jex) const
Definition: field.h:143
static bool is_bigendian()
Definition: fieldIO.cpp:206
int nin() const
Definition: field.h:126
std::string generate_filename(const char *fmt,...)
Definition: file_utils.cpp:17
int nex() const
Definition: field.h:128
Lexical site index.
Definition: index_lex.h:34
long long_t
definition of long for Bridge++
Definition: bridge_long.h:46
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:359
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:227
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:119
static long_t Lvol()
Bridge::VerboseLevel m_vl
Definition: fieldIO.h:64