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