Rheolef  7.2
an efficient C++ finite element environment
diststream.cc
Go to the documentation of this file.
1 
22 #include "rheolef/diststream.h"
23 #include "rheolef/environment.h"
24 #include "rheolef/rheostream.h" // scatch()
25 using namespace std;
26 namespace rheolef {
27 // ----------------------------------------------------------------------------
28 // global variables
29 // ----------------------------------------------------------------------------
30 
32 idiststream din (cin);
34 odiststream dout (cout);
36 odiststream dlog (clog);
38 odiststream derr (cerr);
39 
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
43 bool
44 dis_scatch (idiststream& ips, const communicator& comm, std::string ch)
45 {
46  // TODO: comm is in ips.comm()
47  typedef std::size_t size_type;
48  size_type io_proc = idiststream::io_proc();
49  size_type my_proc = comm.rank();
50  bool status = false;
51  if (my_proc == io_proc) {
52  status = scatch(ips.is(),ch);
53  }
54 #ifdef _RHEOLEF_HAVE_MPI
55  mpi::broadcast (comm, status, io_proc);
56 #endif // _RHEOLEF_HAVE_MPI
57  return status;
58 }
59 // --------------------------------------------------------------
60 // intput
61 // --------------------------------------------------------------
64 idiststream::io_proc() {
65 #ifndef _RHEOLEF_HAVE_MPI
66  return 0;
67 #else // _RHEOLEF_HAVE_MPI
68  boost::optional<int> opt_io_proc = environment::io_rank();
69  check_macro (opt_io_proc, "no process can perform i/o");
70  int io_proc = opt_io_proc.get();
71  if (io_proc == mpi::any_source) {
73  io_proc = 0;
74  }
75  return size_type(io_proc);
76 #endif // _RHEOLEF_HAVE_MPI
77 }
79 odiststream::io_proc()
80 {
81  return idiststream::io_proc();
82 }
84 void
85 idiststream::open (
86  std::string filename,
87  std::string suffix,
88  const communicator& comm)
89 {
90  close();
91  if (_use_alloc && _ptr_is != 0) {
92  delete_macro (_ptr_is);
93  _use_alloc = false;
94  _ptr_is = 0;
95  }
96  if (size_type(comm.rank()) == idiststream::io_proc()) {
97  _ptr_is = new_macro (irheostream(filename, suffix));
98  } else {
99  _ptr_is = new_macro (irheostream);
100  }
101  _comm = comm;
102  _use_alloc = true;
103 }
104 void
105 idiststream::close ()
106 {
107  if (_use_alloc && _ptr_is != 0) {
108  if (size_type(_comm.rank()) == idiststream::io_proc()) {
109  irheostream* ptr_irs = (irheostream*)(_ptr_is);
110  (*ptr_irs).close();
111  }
112  }
113 }
114 idiststream::~idiststream ()
115 {
116  close();
117  if (_use_alloc && _ptr_is != 0) {
118  delete_macro (_ptr_is);
119  _use_alloc = false;
120  _ptr_is = 0;
121  }
122 }
123 bool
124 idiststream::good() const
125 {
126  bool status;
127  if (size_type(comm().rank()) != idiststream::io_proc()) {
128  status = true;
129  } else if (_ptr_is == 0) {
130  status = false;
131  } else {
132  status = (*_ptr_is).good();
133  }
134 #ifdef _RHEOLEF_HAVE_MPI
135  mpi::broadcast(comm(), status, 0);
136 #endif // _RHEOLEF_HAVE_MPI
137  return status;
138 }
139 // --------------------------------------------------------------
140 // output
141 // --------------------------------------------------------------
143 void
144 odiststream::open (
145  std::string filename,
146  std::string suffix,
147  io::mode_type mode,
148  const communicator& comm)
149 {
150  close();
151  if (_use_alloc && _ptr_os != 0) {
152  delete_macro (_ptr_os);
153  _use_alloc = false;
154  _ptr_os = 0;
155  }
156  if (size_type(comm.rank()) == odiststream::io_proc()) {
157  _ptr_os = new_macro (orheostream(filename, suffix, mode));
158  } else {
159  _ptr_os = new_macro (orheostream);
160  }
161  _comm = comm;
162  _use_alloc = true;
163 }
164 void
165 odiststream::close ()
166 {
167  if (_use_alloc && _ptr_os != 0) {
168  if (size_type(_comm.rank()) == odiststream::io_proc()) {
169  orheostream* ptr_ors = (orheostream*)(_ptr_os);
170  (*ptr_ors).close();
171  }
172  }
173 }
174 void
175 odiststream::flush()
176 {
177  if (size_type(_comm.rank()) != odiststream::io_proc()) return;
178  if (_use_alloc && _ptr_os != 0) {
179  orheostream* ptr_ors = (orheostream*)(_ptr_os);
180  (*ptr_ors).flush();
181  } else if (_ptr_os != 0) {
182  (*_ptr_os).flush(); // call usual ostream::flush(), i.e. not gziped output
183  }
184 }
185 odiststream::~odiststream ()
186 {
187  close();
188  if (_use_alloc && _ptr_os != 0) {
189  delete_macro (_ptr_os);
190  _use_alloc = false;
191  _ptr_os = 0;
192  }
193 }
194 bool
195 odiststream::good() const
196 {
197  bool status;
198  if (size_type(comm().rank()) != idiststream::io_proc()) {
199  status = true;
200  } else if (_ptr_os == 0) {
201  status = false;
202  } else {
203  status = (*_ptr_os).good();
204  }
205 #ifdef _RHEOLEF_HAVE_MPI
206  mpi::broadcast(comm(), status, 0);
207 #endif // _RHEOLEF_HAVE_MPI
208  return status;
209 }
210 // -----------------------------------------
211 // system utilities
212 // -----------------------------------------
213 int
214 dis_system (const std::string& command, const communicator& comm)
215 {
217  size_type io_proc = odiststream::io_proc();
218  size_type my_proc = comm.rank();
219  int status = 0;
220 #ifdef _RHEOLEF_HAVE_MPI
221  mpi::communicator().barrier();
222 #endif // _RHEOLEF_HAVE_MPI
223  if (my_proc == io_proc) {
224  status = std::system (command.c_str());
225  }
226 #ifdef _RHEOLEF_HAVE_MPI
227  mpi::communicator().barrier();
228  mpi::broadcast (mpi::communicator(), status, io_proc);
229 #endif // _RHEOLEF_HAVE_MPI
230  return status;
231 }
232 bool
233 dis_file_exists (const std::string& filename, const communicator& comm)
234 {
236  size_type io_proc = odiststream::io_proc();
237  size_type my_proc = comm.rank();
238  bool status = false;
239  if (my_proc == io_proc) {
240  status = file_exists (filename);
241  }
242 #ifdef _RHEOLEF_HAVE_MPI
243  mpi::broadcast (mpi::communicator(), status, io_proc);
244 #endif // _RHEOLEF_HAVE_MPI
245  return status;
246 }
247 
248 } // namespace rheolef
field::size_type size_type
Definition: branch.cc:430
idiststream: see the diststream page for the full documentation
Definition: diststream.h:336
std::istream & is()
Definition: diststream.h:400
std::size_t size_type
Definition: diststream.h:139
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
string command
Definition: mkgeo_ball.sh:136
This file is part of Rheolef.
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:44
int dis_system(const std::string &command, const communicator &comm)
Definition: diststream.cc:214
bool dis_scatch(idiststream &ips, const communicator &comm, std::string ch)
distributed version of scatch(istream&,string)
Definition: diststream.cc:44
bool dis_file_exists(const std::string &filename, const communicator &comm)
Definition: diststream.cc:233
bool file_exists(const std::string &filename)
file_exists: see the rheostream page for the full documentation
Definition: scatch.icc:34