Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
diststream.cc
Go to the documentation of this file.
1
21
22#include "rheolef/diststream.h"
23#include "rheolef/environment.h"
24#include "rheolef/rheostream.h" // scatch()
25using namespace std;
26namespace rheolef {
27// ----------------------------------------------------------------------------
28// global variables
29// ----------------------------------------------------------------------------
30
32idiststream din (cin);
34odiststream dout (cout);
36odiststream dlog (clog);
38odiststream derr (cerr);
39
40// ----------------------------------------------------------------------------
42// ----------------------------------------------------------------------------
43bool
44dis_scatch (idiststream& ips, const communicator& comm, std::string ch)
45{
46 // TODO: comm is in ips.comm()
47 typedef std::size_t size_type;
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// --------------------------------------------------------------
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}
84void
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}
104void
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}
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}
123bool
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// --------------------------------------------------------------
143void
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}
164void
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}
174void
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}
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}
194bool
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// -----------------------------------------
213int
214dis_system (const std::string& command, const communicator& comm)
215{
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}
232bool
233dis_file_exists (const std::string& filename, const communicator& comm)
234{
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
static boost::optional< int > io_rank()
idiststream: see the diststream page for the full documentation
Definition diststream.h:336
static size_type io_proc()
This routine returns the rank of a process that can perform i/o.
Definition diststream.cc:64
communicator _comm
Definition diststream.h:372
std::istream & is()
Definition diststream.h:400
std::size_t size_type
Definition diststream.h:338
void open(std::string filename, std::string suffix="", const communicator &comm=communicator())
This routine opens a physical input file.
Definition diststream.cc:85
std::istream * _ptr_is
Definition diststream.h:370
const communicator & comm() const
Definition diststream.h:356
std::ostream * _ptr_os
Definition diststream.h:180
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.
communicator _comm
Definition diststream.h:182
std::size_t size_type
Definition diststream.h:139
static size_type io_proc()
Definition diststream.cc:79
const communicator & comm() const
Definition diststream.h:167
idiststream din(cin)
see the diststream page for the full documentation
Definition diststream.h:464
odiststream dlog(clog)
see the diststream page for the full documentation
Definition diststream.h:470
odiststream dout(cout)
see the diststream page for the full documentation
Definition diststream.h:467
odiststream derr(cerr)
see the diststream page for the full documentation
Definition diststream.h:473
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
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
bool dis_scatch(idiststream &ips, const communicator &comm, std::string ch)
distributed version of scatch(istream&,string)
Definition diststream.cc:44
int dis_system(const std::string &command, const communicator &comm)
bool dis_file_exists(const std::string &filename, const communicator &comm)
bool file_exists(const std::string &filename)
file_exists: see the rheostream page for the full documentation
Definition scatch.icc:34
STL namespace.