1
# ifndef _RHEOLEF_DISTSTREAM_H
2
# define _RHEOLEF_DISTSTREAM_H
4
/// This file is part of Rheolef.
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
8
/// Rheolef is free software; you can redistribute it and/or modify
9
/// it under the terms of the GNU General Public License as published by
10
/// the Free Software Foundation; either version 2 of the License, or
11
/// (at your option) any later version.
13
/// Rheolef 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.
18
/// You should have received a copy of the GNU General Public License
19
/// along with Rheolef; if not, write to the Free Software
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
/// =========================================================================
26
// author: Pierre.Saramito@imag.fr
30
#include "rheolef/distributed.h"
31
#include "rheolef/dis_macros.h"
36
typedef std::size_t size_type;
38
// allocators/deallocators:
41
: _ptr_os(0), _use_alloc(false), _comm() {}
43
odiststream(std::ostream& os, const communicator& comm = communicator())
44
: _ptr_os(&os), _use_alloc(false), _comm(comm) {}
50
void open (std::string filename, std::string suffix,
51
const communicator& comm = communicator());
57
const communicator& comm() const { return _comm; }
59
operator bool() const { return good(); }
60
static size_type io_proc();
65
check_macro (_ptr_os != 0, "try to use an uninitialized odiststream");
68
#ifndef _RHEOLEF_HAVE_MPI
69
bool nop() { return false; }
71
bool nop() { return size_type(_comm.rank()) != io_proc(); }
72
#endif //_RHEOLEF_HAVE_MPI
76
std::ostream* _ptr_os;
83
# define define_sequential_odiststream_raw_macro(arg) \
86
operator << (odiststream& s, arg) { \
87
if (s.nop()) return s; s.os() << x; return s; \
90
# define define_sequential_odiststream_macro(T) \
91
define_sequential_odiststream_raw_macro(const T& x)
93
# define define_distributed_odiststream_macro(T) \
96
operator << (odiststream& s, const T& x) { \
97
s.os() << x; return s; \
103
operator << (odiststream& s, T x) {
104
if (s.nop()) return s; s.os() << x; return s;
106
define_sequential_odiststream_macro(char)
107
define_sequential_odiststream_macro(int)
108
define_sequential_odiststream_macro(unsigned int)
109
define_sequential_odiststream_macro(long int)
110
define_sequential_odiststream_macro(long unsigned int)
111
define_sequential_odiststream_macro(float)
112
define_sequential_odiststream_macro(double)
113
define_sequential_odiststream_macro(long double)
114
define_sequential_odiststream_macro(char*const)
115
define_sequential_odiststream_macro(std::string)
116
#ifdef _RHEOLEF_HAVE_DOUBLEDOUBLE
117
define_sequential_odiststream_macro(doubledouble)
118
#endif // _RHEOLEF_HAVE_DOUBLEDOUBLE
119
define_sequential_odiststream_raw_macro(char *x)
120
define_sequential_odiststream_raw_macro(std::ostream& (*x)(std::ostream&))
124
typedef std::size_t size_type;
126
// allocators/deallocators:
129
: _ptr_is(0), _use_alloc(false), _comm() {}
131
idiststream (std::istream& is, const communicator& comm = communicator())
132
: _ptr_is(&is), _use_alloc(false), _comm(comm) {}
138
void open (std::string filename, std::string suffix,
139
const communicator& comm = communicator());
145
const communicator& comm() const { return _comm; }
147
operator bool() const { return good(); }
148
static size_type io_proc();
150
// internal accesses:
153
check_macro (_ptr_is != 0, "try to use an uninitialized idiststream");
156
#ifndef _RHEOLEF_HAVE_MPI
157
bool do_load() { return true; }
159
bool do_load() { return size_type(_comm.rank()) == io_proc(); }
160
#endif // _RHEOLEF_HAVE_MPI
164
std::istream* _ptr_is;
169
#ifdef _RHEOLEF_HAVE_MPI
170
# define define_sequential_idiststream_macro(T) \
173
operator >> (idiststream& s, T& x) \
175
if (s.do_load()) { (s.is()) >> x; } \
176
mpi::broadcast(mpi::communicator(), x, 0); \
179
#else // _RHEOLEF_HAVE_MPI
180
# define define_sequential_idiststream_macro(T) \
183
operator >> (idiststream& s, T& x) \
188
#endif // _RHEOLEF_HAVE_MPI
189
# define define_distributed_idiststream_macro(T) \
192
operator >> (idiststream& s, T& x) \
198
define_sequential_idiststream_macro(char)
199
define_sequential_idiststream_macro(int)
200
define_sequential_idiststream_macro(long int)
201
define_sequential_idiststream_macro(unsigned int)
202
define_sequential_idiststream_macro(long unsigned int)
203
define_sequential_idiststream_macro(float)
204
define_sequential_idiststream_macro(double)
205
define_sequential_idiststream_macro(long double)
206
define_sequential_idiststream_macro(std::string)
207
#ifdef _RHEOLEF_HAVE_DOUBLEDOUBLE
208
define_sequential_idiststream_macro(doubledouble)
209
#endif // _RHEOLEF_HAVE_DOUBLEDOUBLE
211
// predefined distributed streams
212
extern idiststream dcin;
213
extern odiststream dcout;
214
extern odiststream dclog;
215
extern odiststream dcerr;
217
bool dis_scatch (idiststream& ips, const communicator& comm, std::string ch);
219
} // namespace rheolef
220
# endif // _RHEOLEF_DISTSTREAM_H