Rheolef  7.1
an efficient C++ finite element environment
diststream.h
Go to the documentation of this file.
1 # ifndef _RHEOLEF_DISTSTREAM_H
2 # define _RHEOLEF_DISTSTREAM_H
3 //
4 // This file is part of Rheolef.
5 //
6 // Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7 //
8 // Rheolef is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // Rheolef is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with Rheolef; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 //
22 // =========================================================================
23 // AUTHORS: Pierre.Saramito@imag.fr
24 // DATE: 31 october 1997 ; 13 nov 1998
25 
26 namespace rheolef {
116 } // namespace rheolef
117 
118 #include "rheolef/distributed.h"
119 #include "rheolef/dis_macros.h"
120 #include "rheolef/catchmark.h"
121 
122 namespace rheolef {
123 
125 // [verbatim_odiststream]
126 class odiststream {
127 public:
128  typedef std::size_t size_type;
129 
130 // allocators/deallocators:
131 
132  odiststream();
133  odiststream (std::string filename, std::string suffix = "",
134  io::mode_type mode = io::out, const communicator& comm = communicator());
135  odiststream (std::string filename,
136  io::mode_type mode, const communicator& comm = communicator());
137  odiststream (std::string filename, std::string suffix, const communicator& comm);
138  odiststream (std::string filename, const communicator& comm);
139  odiststream(std::ostream& os, const communicator& comm = communicator());
140  ~odiststream();
141 
142 // modifiers:
143 
144  void open (std::string filename, std::string suffix = "",
145  io::mode_type mode = io::out, const communicator& comm = communicator());
146  void open (std::string filename,
147  io::mode_type mode, const communicator& comm = communicator());
148  void open (std::string filename, std::string suffix,
149  const communicator& comm);
150  void open (std::string filename, const communicator& comm);
151  void flush();
152  void close();
153 
154 // accessors:
155 
156  const communicator& comm() const { return _comm; }
157  bool good() const;
158  operator bool() const { return good(); }
159  static size_type io_proc();
160 // [verbatim_odiststream]
161 
162 // internals:
163 
164  std::ostream& os();
165  bool nop();
166 
167 protected:
168 // data:
169  std::ostream* _ptr_os;
171  communicator _comm;
172 private:
173  odiststream(const odiststream&);
174  odiststream& operator= (const odiststream&);
175 // [verbatim_odiststream_cont]
176 };
177 // [verbatim_odiststream_cont]
178 
179 // ------------------------------------------------------------------
180 // inlined
181 // ------------------------------------------------------------------
182 inline
184  : _ptr_os(0), _use_alloc(false), _comm()
185 {
186 }
187 inline
188 odiststream::odiststream (std::string filename, std::string suffix, io::mode_type mode, const communicator& comm)
189  : _ptr_os(0), _use_alloc(false), _comm()
190 {
191  open (filename, suffix, mode, comm);
192 }
193 inline
194 odiststream::odiststream (std::string filename, io::mode_type mode, const communicator& comm)
195  : _ptr_os(0), _use_alloc(false), _comm()
196 {
197  open (filename, mode, comm);
198 }
199 inline
200 odiststream::odiststream (std::string filename, std::string suffix, const communicator& comm)
201  : _ptr_os(0), _use_alloc(false), _comm()
202 {
203  open (filename, suffix, comm);
204 }
205 inline
206 odiststream::odiststream (std::string filename, const communicator& comm)
207  : _ptr_os(0), _use_alloc(false), _comm()
208 {
209  open (filename, comm);
210 }
211 inline
212 odiststream::odiststream(std::ostream& os, const communicator& comm)
213  : _ptr_os(&os), _use_alloc(false), _comm(comm)
214 {
215 }
216 inline
217 void
218 odiststream::open (std::string filename, io::mode_type mode, const communicator& comm)
219 {
220  open (filename, std::string(""), mode, comm);
221 }
222 inline
223 void
224 odiststream::open (std::string filename, std::string suffix, const communicator& comm)
225 {
226  open (filename, suffix, io::out, comm);
227 }
228 inline
229 void
230 odiststream::open (std::string filename, const communicator& comm)
231 {
232  open (filename, std::string(""), io::out, comm);
233 }
234 inline
235 std::ostream&
237  check_macro (_ptr_os != 0, "try to use an uninitialized odiststream");
238  return *_ptr_os;
239 }
240 #ifndef _RHEOLEF_HAVE_MPI
241 inline bool odiststream::nop() { return false; }
242 #else
243 inline bool odiststream::nop() { return size_type(_comm.rank()) != io_proc(); }
244 #endif //_RHEOLEF_HAVE_MPI
245 
246 // ------------------------------------------------------------------
247 // standard i/o:
248 // ------------------------------------------------------------------
249 # define define_sequential_odiststream_raw_macro(arg) \
250  inline \
251  odiststream& \
252  operator << (odiststream& s, arg) { \
253  if (s.nop()) return s; s.os() << x; return s; \
254  }
255 # define define_sequential_odiststream_macro(T) \
256  define_sequential_odiststream_raw_macro(const T& x)
257 
258 # define define_distributed_odiststream_macro(T) \
259  inline \
260  odiststream& \
261  operator << (odiststream& s, const T& x) { \
262  s.os() << x; return s; \
263  }
264 
265 template <class T>
266 inline
269 {
270  if (s.nop()) return s;
271  s.os() << x;
272  return s;
273 }
278 define_sequential_odiststream_macro(long unsigned int)
284 #ifdef _RHEOLEF_HAVE_FLOAT128
285 define_sequential_odiststream_macro(boost::multiprecision::float128)
286 #endif // _RHEOLEF_HAVE_FLOAT128
288 define_sequential_odiststream_raw_macro(std::ostream& (*x)(std::ostream&))
289 
290 // [verbatim_idiststream]
292 class idiststream {
293 public:
294  typedef std::size_t size_type;
295 
296 // allocators/deallocators:
297 
298  idiststream();
299  idiststream (std::istream& is, const communicator& comm = communicator());
300  idiststream (std::string filename, std::string suffix = "",
301  const communicator& comm = communicator());
302  ~idiststream();
303 
304 // modifiers:
305 
306  void open (std::string filename, std::string suffix = "",
307  const communicator& comm = communicator());
308  void close();
309 
310 // accessors:
311 
312  const communicator& comm() const { return _comm; }
313  bool good() const;
314  operator bool() const { return good(); }
315  static size_type io_proc();
316 // [verbatim_idiststream]
317 
318 // internals:
319 
320  std::istream& is();
321  bool nop();
322  bool do_load();
323 
324 protected:
325 // data:
326  std::istream* _ptr_is;
327  bool _use_alloc;
328  communicator _comm;
329 private:
330  idiststream(const idiststream&);
331  idiststream& operator= (const idiststream&);
332 // [verbatim_idiststream_cont]
333 };
334 // [verbatim_idiststream_cont]
335 // ------------------------------------------------------------------
336 // inlined
337 // ------------------------------------------------------------------
338 inline
339 idiststream::idiststream()
340  : _ptr_is(0), _use_alloc(false), _comm()
341 {
342 }
343 inline
344 idiststream::idiststream (std::istream& is, const communicator& comm)
345  : _ptr_is(&is), _use_alloc(false), _comm(comm)
346 {
347 }
348 inline
349 idiststream::idiststream (std::string filename, std::string suffix, const communicator& comm)
350  : _ptr_is(0), _use_alloc(false), _comm()
351 {
352  open (filename, suffix, comm);
353 }
354 inline
355 std::istream&
356 idiststream::is()
357 {
358  check_macro (_ptr_is != 0, "try to use an uninitialized idiststream");
359  return *_ptr_is;
360 }
361 #ifndef _RHEOLEF_HAVE_MPI
362 inline bool idiststream::nop() { return false; }
363 inline bool idiststream::do_load() { return true; }
364 #else
365 inline bool idiststream::nop() { return size_type(_comm.rank()) != io_proc(); }
366 inline bool idiststream::do_load() { return size_type(_comm.rank()) == io_proc(); }
367 #endif //_RHEOLEF_HAVE_MPI
368 
369 // ------------------------------------------------------------------
370 // standard i/o:
371 // ------------------------------------------------------------------
372 #ifdef _RHEOLEF_HAVE_MPI
373 # define define_sequential_idiststream_macro(T) \
374 inline \
375 idiststream& \
376 operator >> (idiststream& s, T& x) \
377 { \
378  if (s.do_load()) { (s.is()) >> x; } \
379  mpi::broadcast (mpi::communicator(), x, s.io_proc()); \
380  return s; \
381 }
382 #else // _RHEOLEF_HAVE_MPI
383 # define define_sequential_idiststream_macro(T) \
384 inline \
385 idiststream& \
386 operator >> (idiststream& s, T& x) \
387 { \
388  (s.is()) >> x; \
389  return s; \
390 }
391 #endif // _RHEOLEF_HAVE_MPI
392 # define define_distributed_raw_idiststream_macro(T) \
393 inline \
394 idiststream& \
395 operator >> (idiststream& s, T_x) \
396 { \
397  s.is() >> x; \
398  return s; \
399 }
400 # define define_distributed_idiststream_macro(T) \
401  define_distributed_raw_idiststream_macro(T& x)
402 
407 define_sequential_idiststream_macro(long unsigned int)
412 #ifdef _RHEOLEF_HAVE_FLOAT128
413 define_sequential_idiststream_macro(boost::multiprecision::float128)
414 #endif // _RHEOLEF_HAVE_FLOAT128
415 // iomanips such as: ids << noverbose
416 inline
417 idiststream&
418 operator >> (idiststream& s, std::istream& (*x)(std::istream&))
419 {
420  s.is() >> x;
421  return s;
422 }
423 
424 // predefined distributed streams
427 extern idiststream din;
430 extern odiststream dout;
433 extern odiststream dlog;
436 extern odiststream derr;
437 
438 bool dis_scatch (idiststream& ips, const communicator& comm, std::string ch);
439 
440 inline
441 bool dis_scatch (idiststream& ips, std::string ch)
442 {
443  return dis_scatch (ips, ips.comm(), ch);
444 }
445 inline
446 idiststream&
447 operator>> (idiststream& ids, const catchmark& m)
448 {
449  if (ids.nop()) return ids;
450  ids.is() >> setmark(m.mark());
451  std::string label = "#" + m.mark();
452  if (!scatch(ids.is(),label)) {
453  bool verbose = iorheo::getverbose(ids.is());
454  if (verbose) warning_macro ("catchmark: label `"<< label <<"' not found on input");
455  }
456  return ids;
457 }
458 inline
460 operator<< (odiststream& ods, const catchmark& m)
461 {
462  if (ods.nop()) return ods;
463  ods.os() << setmark(m.mark())
464  << "#" << m.mark() << std::endl;
465  return ods;
466 }
467 // system utilities:
468 int dis_system (const std::string& command, const communicator& comm = communicator());
469 bool dis_file_exists (const std::string& filename, const communicator& comm = communicator());
470 
471 
472 } // namespace rheolef
473 # endif // _RHEOLEF_DISTSTREAM_H
rheolef::odiststream::open
void open(std::string filename, std::string suffix="", io::mode_type mode=io::out, const communicator &comm=communicator())
This routine opens a physical output file.
Definition: diststream.cc:143
rheolef::odiststream::flush
void flush()
Definition: diststream.cc:174
rheolef::io::out
@ out
Definition: rheostream.h:167
warning_macro
#define warning_macro(message)
Definition: dis_macros.h:53
check_macro
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
rheolef::catchmark
see the catchmark page for the full documentation
Definition: catchmark.h:67
rheolef::dis_scatch
bool dis_scatch(idiststream &ips, std::string ch)
Definition: diststream.h:441
rheolef::io::mode_type
mode_type
Definition: rheostream.h:166
rheolef::odiststream::_use_alloc
bool _use_alloc
Definition: diststream.h:170
rheolef::odiststream::odiststream
odiststream()
Definition: diststream.h:183
define_sequential_odiststream_raw_macro
#define define_sequential_odiststream_raw_macro(arg)
Definition: diststream.h:249
rheolef::odiststream::os
std::ostream & os()
Definition: diststream.h:236
rheolef::size_type
size_t size_type
Definition: basis_get.cc:76
rheolef::odiststream::_ptr_os
std::ostream * _ptr_os
Definition: diststream.h:169
rheolef::odiststream::comm
const communicator & comm() const
Definition: diststream.h:156
rheolef::odiststream::nop
bool nop()
Definition: diststream.h:243
rheolef::operator>>
idiststream & operator>>(idiststream &ids, const catchmark &m)
Definition: diststream.h:447
rheolef::odiststream::io_proc
static size_type io_proc()
Definition: diststream.cc:78
mkgeo_ball.command
string command
Definition: mkgeo_ball.sh:136
operator<<
ostream & operator<<(ostream &os, const tiny_element &K)
Definition: tiny_element.cc:27
rheolef::din
idiststream din
see the diststream page for the full documentation
Definition: diststream.h:427
rheolef::scatch
bool scatch(std::istream &in, const std::string &ch, bool full_match=true)
scatch: see the rheostream page for the full documentation
Definition: scatch.icc:52
rheolef::dis_file_exists
bool dis_file_exists(const std::string &filename, const communicator &comm)
Definition: diststream.cc:232
rheolef::odiststream::close
void close()
Definition: diststream.cc:164
rheolef::odiststream::~odiststream
~odiststream()
Definition: diststream.cc:184
mkgeo_sector.m
m
Definition: mkgeo_sector.sh:118
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::dis_system
int dis_system(const std::string &command, const communicator &comm)
Definition: diststream.cc:213
define_sequential_idiststream_macro
#define define_sequential_idiststream_macro(T)
Definition: diststream.h:373
rheolef::odiststream
odiststream: see the diststream page for the full documentation
Definition: diststream.h:126
mkgeo_ball.verbose
verbose
Definition: mkgeo_ball.sh:133
define_sequential_odiststream_macro
#define define_sequential_odiststream_macro(T)
Definition: diststream.h:255
size_type
field::size_type size_type
Definition: branch.cc:425
rheolef::odiststream::_comm
communicator _comm
Definition: diststream.h:171
rheolef::operator<<
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition: catchmark.h:99
rheolef::odiststream::size_type
std::size_t size_type
Definition: diststream.h:128
rheolef::odiststream::good
bool good() const
Definition: diststream.cc:194
T
Expr1::float_type T
Definition: field_expr.h:261