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