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