~ubuntu-branches/ubuntu/saucy/rheolef/saucy

« back to all changes in this revision

Viewing changes to skit/plib2/diststream.h

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Saramito
  • Date: 2011-03-23 11:14:26 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20110323111426-cjvhey7lxt6077ty
Tags: 5.93-1
* New upstream release (minor changes):
  - some extra warning message deleted in heap_allocator
  - graphic output with mayavi2 fixed
  - add doc refman in .info and .pdf format

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ifndef _RHEOLEF_DISTSTREAM_H
 
2
# define _RHEOLEF_DISTSTREAM_H
 
3
///
 
4
/// This file is part of Rheolef.
 
5
///
 
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
 
7
///
 
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.
 
12
///
 
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.
 
17
///
 
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
 
21
/// 
 
22
/// =========================================================================
 
23
//
 
24
// distributed i/o
 
25
//
 
26
// author: Pierre.Saramito@imag.fr
 
27
//
 
28
// date: 13 nov 1998
 
29
//
 
30
#include "rheolef/distributed.h"
 
31
#include "rheolef/dis_macros.h"
 
32
namespace rheolef {
 
33
 
 
34
class odiststream {
 
35
public:
 
36
     typedef std::size_t size_type;
 
37
 
 
38
// allocators/deallocators:
 
39
 
 
40
     odiststream()
 
41
       : _ptr_os(0), _use_alloc(false), _comm() {}
 
42
 
 
43
     odiststream(std::ostream& os, const communicator& comm = communicator())
 
44
       : _ptr_os(&os), _use_alloc(false), _comm(comm) {}
 
45
 
 
46
     ~odiststream();
 
47
 
 
48
// modifiers:
 
49
 
 
50
     void open (std::string filename, std::string suffix,
 
51
             const communicator& comm = communicator());
 
52
 
 
53
     void close();
 
54
 
 
55
// accessors:
 
56
 
 
57
     const communicator& comm() const { return _comm; }
 
58
     bool good() const;
 
59
     operator bool() const { return good(); }
 
60
     static size_type io_proc();
 
61
 
 
62
// internal accesses:
 
63
public:
 
64
     std::ostream& os() {
 
65
        check_macro (_ptr_os != 0, "try to use an uninitialized odiststream");
 
66
        return *_ptr_os;
 
67
     }
 
68
#ifndef _RHEOLEF_HAVE_MPI
 
69
     bool nop() { return false; }
 
70
#else
 
71
     bool nop() { return size_type(_comm.rank()) != io_proc(); }
 
72
#endif //_RHEOLEF_HAVE_MPI
 
73
 
 
74
// data:
 
75
protected:
 
76
     std::ostream* _ptr_os;
 
77
     bool          _use_alloc;
 
78
     communicator  _comm;
 
79
};
 
80
 
 
81
// standard i/o
 
82
 
 
83
# define define_sequential_odiststream_raw_macro(arg)   \
 
84
    inline                                              \
 
85
    odiststream&                                                \
 
86
    operator << (odiststream& s, arg) {                 \
 
87
        if (s.nop()) return s; s.os() << x; return s;   \
 
88
    }
 
89
 
 
90
# define define_sequential_odiststream_macro(T)                 \
 
91
    define_sequential_odiststream_raw_macro(const T& x)
 
92
 
 
93
# define define_distributed_odiststream_macro(T)        \
 
94
    inline                                              \
 
95
    odiststream&                                                \
 
96
    operator << (odiststream& s, const T& x) {          \
 
97
        s.os() << x; return s;                          \
 
98
    }
 
99
 
 
100
template <class T>
 
101
inline
 
102
odiststream&
 
103
operator << (odiststream& s, T x) {
 
104
    if (s.nop()) return s; s.os() << x; return s;
 
105
}
 
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&))
 
121
 
 
122
class idiststream {
 
123
public:
 
124
     typedef std::size_t size_type;
 
125
 
 
126
// allocators/deallocators:
 
127
     
 
128
     idiststream()
 
129
       : _ptr_is(0), _use_alloc(false), _comm() {}
 
130
 
 
131
     idiststream (std::istream& is, const communicator& comm = communicator())
 
132
       : _ptr_is(&is), _use_alloc(false), _comm(comm) {}
 
133
 
 
134
      ~idiststream();
 
135
 
 
136
// modifiers:
 
137
 
 
138
     void open (std::string filename, std::string suffix,
 
139
             const communicator& comm = communicator());
 
140
 
 
141
     void close();
 
142
 
 
143
// accessors:
 
144
 
 
145
     const communicator& comm() const { return _comm; }
 
146
     bool good() const;
 
147
     operator bool() const { return good(); }
 
148
     static size_type io_proc();
 
149
 
 
150
// internal accesses:
 
151
public:
 
152
     std::istream& is() {
 
153
        check_macro (_ptr_is != 0, "try to use an uninitialized idiststream");
 
154
        return *_ptr_is;
 
155
     }
 
156
#ifndef _RHEOLEF_HAVE_MPI
 
157
     bool do_load() { return true; }
 
158
#else
 
159
     bool do_load() { return size_type(_comm.rank()) == io_proc(); }
 
160
#endif // _RHEOLEF_HAVE_MPI
 
161
 
 
162
// data:
 
163
protected:
 
164
     std::istream* _ptr_is;
 
165
     bool          _use_alloc;
 
166
     communicator  _comm;
 
167
};
 
168
 
 
169
#ifdef _RHEOLEF_HAVE_MPI
 
170
# define define_sequential_idiststream_macro(T)         \
 
171
inline                                                  \
 
172
idiststream&                                            \
 
173
operator >> (idiststream& s, T& x)                      \
 
174
{                                                       \
 
175
  if (s.do_load()) { (s.is()) >> x; }                   \
 
176
  mpi::broadcast(mpi::communicator(), x, 0);            \
 
177
  return s;                                             \
 
178
}
 
179
#else // _RHEOLEF_HAVE_MPI
 
180
# define define_sequential_idiststream_macro(T)         \
 
181
inline                                                  \
 
182
idiststream&                                            \
 
183
operator >> (idiststream& s, T& x)                      \
 
184
{                                                       \
 
185
  (s.is()) >> x;                                        \
 
186
  return s;                                             \
 
187
}
 
188
#endif // _RHEOLEF_HAVE_MPI
 
189
# define define_distributed_idiststream_macro(T)        \
 
190
inline                                                  \
 
191
idiststream&                                            \
 
192
operator >> (idiststream& s, T& x)                      \
 
193
{                                                       \
 
194
  s.is() >> x;                                          \
 
195
  return s;                                             \
 
196
}
 
197
 
 
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
 
210
 
 
211
// predefined distributed streams
 
212
extern idiststream dcin;
 
213
extern odiststream dcout;
 
214
extern odiststream dclog;
 
215
extern odiststream dcerr;
 
216
 
 
217
bool dis_scatch (idiststream& ips, const communicator& comm, std::string ch);
 
218
 
 
219
} // namespace rheolef
 
220
# endif // _RHEOLEF_DISTSTREAM_H