rheolef  6.3
diststream.cc
Go to the documentation of this file.
1 
2 #include "rheolef/diststream.h"
3 #include "rheolef/rheostream.h" // scatch()
4 using namespace std;
5 namespace rheolef {
6 
7 idiststream din (cin);
8 odiststream dout (cout);
9 odiststream dclog (clog);
10 odiststream derr (cerr);
11 
12 bool
13 dis_scatch (idiststream& ips, const communicator& comm, std::string ch)
14 {
15  typedef std::size_t size_type;
16  size_type io_proc = idiststream::io_proc();
17  size_type my_proc = comm.rank();
18  bool status = false;
19  if (my_proc == io_proc) {
20  status = scatch(ips.is(),ch);
21  }
22 #ifdef _RHEOLEF_HAVE_MPI
23  mpi::broadcast (comm, status, io_proc);
24 #endif // _RHEOLEF_HAVE_MPI
25  return status;
26 }
28 idiststream::io_proc() {
29 #ifndef _RHEOLEF_HAVE_MPI
30  return 0;
31 #else // _RHEOLEF_HAVE_MPI
32  boost::optional<int> opt_io_proc = environment::io_rank();
33  check_macro (opt_io_proc, "no process can perform i/o");
34  int io_proc = opt_io_proc.get();
35  if (io_proc == mpi::any_source) {
37  io_proc = 0;
38  }
39  return size_type(io_proc);
40 #endif // _RHEOLEF_HAVE_MPI
41 }
43 odiststream::io_proc()
44 {
45  return idiststream::io_proc();
46 }
47 void
48 idiststream::open (
49  std::string filename,
50  std::string suffix,
51  const communicator& comm)
52 {
53  close();
54  if (_use_alloc && _ptr_is != 0) {
55  delete_macro (_ptr_is);
56  _use_alloc = false;
57  _ptr_is = 0;
58  }
59  if (size_type(comm.rank()) == idiststream::io_proc()) {
60  _ptr_is = new_macro (irheostream(filename, suffix));
61  } else {
62  _ptr_is = new_macro (irheostream);
63  }
64  _comm = comm;
65  _use_alloc = true;
66 }
67 void
68 idiststream::close ()
69 {
70  if (_use_alloc && _ptr_is != 0) {
71  if (size_type(_comm.rank()) == idiststream::io_proc()) {
72  irheostream* ptr_irs = (irheostream*)(_ptr_is);
73  (*ptr_irs).close();
74  }
75  }
76 }
77 idiststream::~idiststream ()
78 {
79  close();
80  if (_use_alloc && _ptr_is != 0) {
81  delete_macro (_ptr_is);
82  _use_alloc = false;
83  _ptr_is = 0;
84  }
85 }
86 bool
87 idiststream::good() const
88 {
89  bool status;
90  if (size_type(comm().rank()) != idiststream::io_proc()) {
91  status = true;
92  } else if (_ptr_is == 0) {
93  status = false;
94  } else {
95  status = (*_ptr_is).good();
96  }
97 #ifdef _RHEOLEF_HAVE_MPI
98  mpi::broadcast(comm(), status, 0);
99 #endif // _RHEOLEF_HAVE_MPI
100  return status;
101 }
102 void
103 odiststream::open (
104  std::string filename,
105  std::string suffix,
106  bool use_gzip,
107  const communicator& comm)
108 {
109  close();
110  if (_use_alloc && _ptr_os != 0) {
111  delete_macro (_ptr_os);
112  _use_alloc = false;
113  _ptr_os = 0;
114  }
115  if (size_type(comm.rank()) == odiststream::io_proc()) {
116  _ptr_os = new_macro (orheostream(filename, suffix, use_gzip));
117  } else {
118  _ptr_os = new_macro (orheostream);
119  }
120  _comm = comm;
121  _use_alloc = true;
122 }
123 void
124 odiststream::close ()
125 {
126  if (_use_alloc && _ptr_os != 0) {
127  if (size_type(_comm.rank()) == odiststream::io_proc()) {
128  orheostream* ptr_ors = (orheostream*)(_ptr_os);
129  (*ptr_ors).close();
130  }
131  }
132 }
133 odiststream::~odiststream ()
134 {
135  close();
136  if (_use_alloc && _ptr_os != 0) {
137  delete_macro (_ptr_os);
138  _use_alloc = false;
139  _ptr_os = 0;
140  }
141 }
142 bool
143 odiststream::good() const
144 {
145  bool status;
146  if (size_type(comm().rank()) != idiststream::io_proc()) {
147  status = true;
148  } else if (_ptr_os == 0) {
149  status = false;
150  } else {
151  status = (*_ptr_os).good();
152  }
153 #ifdef _RHEOLEF_HAVE_MPI
154  mpi::broadcast(comm(), status, 0);
155 #endif // _RHEOLEF_HAVE_MPI
156  return status;
157 }
158 int
159 dis_system (const std::string& command, const communicator& comm)
160 {
162  size_type io_proc = odiststream::io_proc();
163  size_type my_proc = comm.rank();
164  int status = 0;
165  if (my_proc == io_proc) {
166  status = std::system (command.c_str());
167  }
168 #ifdef _RHEOLEF_HAVE_MPI
169  mpi::broadcast (mpi::communicator(), status, io_proc);
170 #endif // _RHEOLEF_HAVE_MPI
171  return status;
172 }
173 bool
174 dis_file_exists (const std::string& filename, const communicator& comm)
175 {
177  size_type io_proc = odiststream::io_proc();
178  size_type my_proc = comm.rank();
179  bool status = false;
180  if (my_proc == io_proc) {
181  status = file_exists (filename);
182  }
183 #ifdef _RHEOLEF_HAVE_MPI
184  mpi::broadcast (mpi::communicator(), status, io_proc);
185 #endif // _RHEOLEF_HAVE_MPI
186  return status;
187 }
188 
189 } // namespace rheolef
190