2
/// This file is part of Rheolef.
4
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
6
/// Rheolef is free software; you can redistribute it and/or modify
7
/// it under the terms of the GNU General Public License as published by
8
/// the Free Software Foundation; either version 2 of the License, or
9
/// (at your option) any later version.
11
/// Rheolef is distributed in the hope that it will be useful,
12
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
/// GNU General Public License for more details.
16
/// You should have received a copy of the GNU General Public License
17
/// along with Rheolef; if not, write to the Free Software
18
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
/// =========================================================================
22
#include "rheolef/parstream.h"
23
#include "rheolef/rheostream.h" // scatch()
26
// ----------------------------------------------------------------------------
28
// ----------------------------------------------------------------------------
30
iparstream pcin (cin);
31
oparstream pcout (cout);
32
oparstream pcerr (cerr);
34
// ----------------------------------------------------------------------------
35
/// @brief parallel version of scatch(istream&,string)
36
// ----------------------------------------------------------------------------
38
par_scatch (iparstream& ips, const communicator& comm, std::string ch)
40
// TODO: comm is in ips.comm()
41
typedef std::size_t size_type;
42
size_type io_proc = iparstream::io_proc();
43
size_type my_proc = comm.rank();
45
if (my_proc == io_proc) {
46
status = scatch(ips.is(),ch);
48
#ifdef _RHEOLEF_HAVE_MPI
49
mpi::broadcast (comm, status, io_proc);
50
#endif // _RHEOLEF_HAVE_MPI
53
// --------------------------------------------------------------
55
// --------------------------------------------------------------
56
/// @brief This routine returns the rank of a process that can perform i/o
58
iparstream::io_proc() {
59
#ifndef _RHEOLEF_HAVE_MPI
61
#else // _RHEOLEF_HAVE_MPI
62
boost::optional<int> opt_io_proc = environment::io_rank();
63
check_macro (opt_io_proc, "no process can perform i/o");
64
int io_proc = opt_io_proc.get();
65
if (io_proc == mpi::any_source) {
66
/// every process can perform I/O using the standard facilities:
69
return size_type(io_proc);
70
#endif // _RHEOLEF_HAVE_MPI
75
return iparstream::io_proc();
77
/// @brief This routine opens a physical input file
82
const communicator& comm)
84
warning_macro ("iparstream::open("<<filename<<","<<suffix<<")...");
86
if (_use_alloc && _ptr_is != 0) {
87
delete_macro (_ptr_is);
91
if (size_type(comm.rank()) == iparstream::io_proc()) {
92
_ptr_is = new_macro (irheostream(filename, suffix));
94
_ptr_is = new_macro (irheostream);
98
warning_macro ("iparstream::open("<<filename<<","<<suffix<<") done");
103
if (_use_alloc && _ptr_is != 0) {
104
if (size_type(_comm.rank()) == iparstream::io_proc()) {
105
irheostream* ptr_irs = (irheostream*)(_ptr_is);
110
iparstream::~iparstream ()
113
if (_use_alloc && _ptr_is != 0) {
114
delete_macro (_ptr_is);
120
iparstream::good() const
123
if (size_type(comm().rank()) != iparstream::io_proc()) {
125
} else if (_ptr_is == 0) {
128
status = (*_ptr_is).good();
130
mpi::broadcast(comm(), status, 0);
133
// --------------------------------------------------------------
135
// --------------------------------------------------------------
136
/// @brief This routine opens a physical output file
139
std::string filename,
141
const communicator& comm)
144
if (_use_alloc && _ptr_os != 0) {
145
delete_macro (_ptr_os);
149
if (size_type(comm.rank()) == oparstream::io_proc()) {
150
_ptr_os = new_macro (orheostream(filename, suffix));
152
_ptr_os = new_macro (orheostream);
160
if (_use_alloc && _ptr_os != 0) {
161
if (size_type(_comm.rank()) == oparstream::io_proc()) {
162
orheostream* ptr_ors = (orheostream*)(_ptr_os);
167
oparstream::~oparstream ()
170
if (_use_alloc && _ptr_os != 0) {
171
delete_macro (_ptr_os);
177
oparstream::good() const
180
if (size_type(comm().rank()) != iparstream::io_proc()) {
182
} else if (_ptr_os == 0) {
185
status = (*_ptr_os).good();
187
mpi::broadcast(comm(), status, 0);
191
} // namespace rheolef