OpenLB 1.7
Loading...
Searching...
No Matches
parallelIO.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2007 Jonas Latt
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program 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
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef PARALLEL_IO_H
25#define PARALLEL_IO_H
26
27#include <streambuf>
28#include <istream>
29#include <ostream>
30#include <fstream>
31#include <iostream>
32
34
35namespace olb {
36
37class ParBuf : public std::streambuf {
38public:
39 typedef enum {normal, serial} Modes;
40 // There are two modes for proceeding of parallel i/o:
41 // - normal: the default mode. all nodes write data, and all
42 // nodes receive data; this mode requests broadcasting.
43 // - serial: only node 0 writes and receives data.
44public:
45 ParBuf(std::streambuf* _originalBuf);
46 Modes getMode() const;
47 void setMode(Modes _mode);
48protected:
49 int_type overflow (int_type c) override;
50 std::streamsize xsputn(const char* s, std::streamsize num) override;
51
52 int_type uflow() override;
53 int_type underflow() override;
54 std::streamsize xsgetn (char* s, std::streamsize num) override;
55private:
56 std::streambuf* originalBuf;
57 Modes mode;
58};
59
60class olb_ofstream : public std::ostream {
61public:
63 explicit olb_ofstream(const char * filename,
64 openmode mode = out | trunc );
65 ~olb_ofstream() override;
66
67 std::streambuf* rdbuf() const;
68 bool is_open();
69 void open(const char* filename, openmode mode = out | trunc);
70 void close();
71private:
72 std::filebuf fbuf;
73 ParBuf mybuf;
74};
75
76class olb_ifstream : public std::istream {
77public:
79 explicit olb_ifstream(const char * filename,
80 openmode mode = in );
81 ~olb_ifstream() override;
82
83 std::streambuf* rdbuf() const;
84 bool is_open();
85 void open(const char* filename, openmode mode = in);
86 void close();
87private:
88 std::filebuf fbuf;
89 ParBuf mybuf;
90};
91
92class olb_fstream : public std::iostream {
93public:
95 explicit olb_fstream(const char * filename,
96 openmode mode = in | out );
97 ~olb_fstream() override;
98
99 std::streambuf* rdbuf() const;
100 bool is_open();
101 void open(const char* filename, openmode mode = in | out);
102 void close();
103private:
104 std::filebuf fbuf;
105 ParBuf mybuf;
106};
107
109// Class ParBuf
111
112ParBuf::ParBuf(std::streambuf* _originalBuf)
113 : originalBuf(_originalBuf), mode(normal)
114{ }
115
116std::streambuf::int_type
117ParBuf::overflow (std::streambuf::int_type c)
118{
119 int_type returnVal = c;
120 if (c != EOF) {
121#ifdef PARALLEL_MODE_MPI
122 if (singleton::mpi().isMainProcessor()) {
123#endif
124 returnVal = originalBuf->sputc((char)c);
125#ifdef PARALLEL_MODE_MPI
126 }
127 if (mode==normal) {
128 singleton::mpi().bCast(&returnVal, 1);
129 }
130#endif
131 }
132 return returnVal;
133}
134
137{
138 return mode;
139}
140
141void
143{
144 mode = _mode;
145}
146
147std::streamsize
148ParBuf::xsputn(const char* s, std::streamsize num)
149{
150#ifdef PARALLEL_MODE_MPI
151 if (singleton::mpi().isMainProcessor()) {
152#endif
153 return originalBuf->sputn(s,num);
154#ifdef PARALLEL_MODE_MPI
155 }
156 else {
157 return num;
158 }
159#endif
160}
161
162std::streambuf::int_type
164{
165 int_type value;
166#ifdef PARALLEL_MODE_MPI
167 if (singleton::mpi().isMainProcessor()) {
168#endif
169 value = originalBuf->sbumpc();
170#ifdef PARALLEL_MODE_MPI
171 }
172 if (mode==normal) {
173 singleton::mpi().bCast(&value, 1);
174 }
175#endif
176 return value;
177}
178
179std::streambuf::int_type
181{
182 int_type value;
183#ifdef PARALLEL_MODE_MPI
184 if (singleton::mpi().isMainProcessor()) {
185#endif
186 value = originalBuf->sgetc();
187#ifdef PARALLEL_MODE_MPI
188 }
189 if (mode==normal) {
190 singleton::mpi().bCast(&value, 1);
191 }
192#endif
193 return value;
194}
195
196std::streamsize
197ParBuf::xsgetn (char* s, std::streamsize num)
198{
199 std::streamsize sizeRead=0;
200#ifdef PARALLEL_MODE_MPI
201 if (singleton::mpi().isMainProcessor()) {
202#endif
203 sizeRead = originalBuf->sgetn(s, num);
204#ifdef PARALLEL_MODE_MPI
205 }
206 if (mode==normal) {
207 int intSizeRead = (int) sizeRead;
208 singleton::mpi().bCast(&intSizeRead, 1);
209 singleton::mpi().bCast(s, intSizeRead);
210 sizeRead = (std::streamsize) intSizeRead;
211 }
212#endif
213 return sizeRead;
214}
215
217// Class olb_ofstream
219
220olb_ofstream::olb_ofstream() : std::ostream(nullptr), fbuf(), mybuf(&fbuf)
221{
222 this->init(&mybuf);
223}
224
225olb_ofstream::olb_ofstream(const char * filename, openmode mode)
226 : std::ostream(nullptr), fbuf(), mybuf(&fbuf)
227{
228 init(&mybuf);
229 open(filename, mode);
230}
231
234
235std::streambuf*
237{
238 return const_cast<ParBuf*>(&mybuf);
239}
240
241bool
243{
244 return fbuf.is_open();
245}
246
247void
248olb_ofstream::open(const char* filename, openmode mode)
249{
250 int ok;
251#ifdef PARALLEL_MODE_MPI
252 if (singleton::mpi().isMainProcessor()) {
253#endif
254 ok = (bool) fbuf.open(filename, mode | ios_base::out);
255#ifdef PARALLEL_MODE_MPI
256 }
257 singleton::mpi().bCast(&ok, 1);
258#endif
259 if (!ok) {
260 this->setstate(ios_base::failbit);
261 }
262}
263
264void
266{
267 int ok;
268#ifdef PARALLEL_MODE_MPI
269 if (singleton::mpi().isMainProcessor()) {
270#endif
271 ok = (bool) fbuf.close();
272#ifdef PARALLEL_MODE_MPI
273 }
274 singleton::mpi().bCast(&ok, 1);
275#endif
276 if (!ok) {
277 setstate(ios_base::failbit);
278 }
279}
280
281
282
284// Class olb_ifstream
286
287olb_ifstream::olb_ifstream() : std::istream(nullptr), fbuf(), mybuf(&fbuf)
288{
289 init(&mybuf);
290}
291
292olb_ifstream::olb_ifstream(const char * filename, openmode mode)
293 : std::istream(nullptr), fbuf(), mybuf(&fbuf)
294{
295 init(&mybuf);
296 open(filename, mode);
297}
298
301
302std::streambuf*
304{
305 return const_cast<ParBuf*>(&mybuf);
306}
307
308bool
310{
311 return fbuf.is_open();
312}
313
314
315void
316olb_ifstream::open(const char* filename, openmode mode)
317{
318 int ok;
319#ifdef PARALLEL_MODE_MPI
320 if (singleton::mpi().isMainProcessor()) {
321#endif
322 ok = (bool) fbuf.open(filename, mode | ios_base::in);
323#ifdef PARALLEL_MODE_MPI
324 }
325 singleton::mpi().bCast(&ok, 1);
326#endif
327 if (!ok) {
328 this->setstate(ios_base::failbit);
329 }
330}
331
332void
334{
335 int ok;
336#ifdef PARALLEL_MODE_MPI
337 if (singleton::mpi().isMainProcessor()) {
338#endif
339 ok = (bool) fbuf.close();
340#ifdef PARALLEL_MODE_MPI
341 }
342 singleton::mpi().bCast(&ok, 1);
343#endif
344 if (!ok) {
345 setstate(ios_base::failbit);
346 }
347}
348
349
351// Class olb_fstream
353
354olb_fstream::olb_fstream() : std::iostream(nullptr), fbuf(), mybuf(&fbuf)
355{
356 this->init(&mybuf);
357}
358
359olb_fstream::olb_fstream(const char * filename, openmode mode)
360 : std::iostream(nullptr), fbuf(), mybuf(&fbuf)
361{
362 init(&mybuf);
363 open(filename, mode);
364}
365
368
369std::streambuf*
371{
372 return const_cast<ParBuf*>(&mybuf);
373}
374
375bool
377{
378 return fbuf.is_open();
379}
380
381void
382olb_fstream::open(const char* filename, openmode mode)
383{
384 int ok;
385#ifdef PARALLEL_MODE_MPI
386 if (singleton::mpi().isMainProcessor()) {
387#endif
388 ok = (bool) fbuf.open(filename, mode);
389#ifdef PARALLEL_MODE_MPI
390 }
391 singleton::mpi().bCast(&ok, 1);
392#endif
393 if (!ok) {
394 this->setstate(ios_base::failbit);
395 }
396}
397
398void
400{
401 int ok;
402#ifdef PARALLEL_MODE_MPI
403 if (singleton::mpi().isMainProcessor()) {
404#endif
405 ok = (bool) fbuf.close();
406#ifdef PARALLEL_MODE_MPI
407 }
408 singleton::mpi().bCast(&ok, 1);
409#endif
410 if (!ok) {
411 setstate(ios_base::failbit);
412 }
413}
414
415} // namespace olb
416
417#endif
void setMode(Modes _mode)
Definition parallelIO.h:142
int_type uflow() override
Definition parallelIO.h:163
ParBuf(std::streambuf *_originalBuf)
Definition parallelIO.h:112
int_type underflow() override
Definition parallelIO.h:180
std::streamsize xsgetn(char *s, std::streamsize num) override
Definition parallelIO.h:197
int_type overflow(int_type c) override
Definition parallelIO.h:117
Modes getMode() const
Definition parallelIO.h:136
std::streamsize xsputn(const char *s, std::streamsize num) override
Definition parallelIO.h:148
void open(const char *filename, openmode mode=in|out)
Definition parallelIO.h:382
std::streambuf * rdbuf() const
Definition parallelIO.h:370
~olb_fstream() override
Definition parallelIO.h:366
void open(const char *filename, openmode mode=in)
Definition parallelIO.h:316
~olb_ifstream() override
Definition parallelIO.h:299
std::streambuf * rdbuf() const
Definition parallelIO.h:303
~olb_ofstream() override
Definition parallelIO.h:232
void open(const char *filename, openmode mode=out|trunc)
Definition parallelIO.h:248
std::streambuf * rdbuf() const
Definition parallelIO.h:236
void bCast(T *sendBuf, int sendCount, int root=0, MPI_Comm comm=MPI_COMM_WORLD)
Broadcast data from one processor to multiple processors.
Wrapper functions that simplify the use of MPI.
MpiManager & mpi()
Top level namespace for all of OpenLB.