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

« back to all changes in this revision

Viewing changes to skit/plib2/parstream.cc

  • 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
 
///
2
 
/// This file is part of Rheolef.
3
 
///
4
 
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
5
 
///
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.
10
 
///
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.
15
 
///
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
19
 
/// 
20
 
/// =========================================================================
21
 
 
22
 
#include "rheolef/parstream.h"
23
 
#include "rheolef/rheostream.h" // scatch()
24
 
using namespace std;
25
 
namespace rheolef {
26
 
// ----------------------------------------------------------------------------
27
 
// global variables
28
 
// ----------------------------------------------------------------------------
29
 
 
30
 
iparstream pcin  (cin);
31
 
oparstream pcout (cout);
32
 
oparstream pcerr (cerr);
33
 
 
34
 
// ----------------------------------------------------------------------------
35
 
/// @brief parallel version of scatch(istream&,string)
36
 
// ----------------------------------------------------------------------------
37
 
bool
38
 
par_scatch (iparstream& ips, const communicator& comm, std::string ch)
39
 
{
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();
44
 
  bool status;
45
 
  if (my_proc == io_proc) {
46
 
    status = scatch(ips.is(),ch);
47
 
  }
48
 
#ifdef _RHEOLEF_HAVE_MPI
49
 
  mpi::broadcast (comm, status, io_proc);
50
 
#endif // _RHEOLEF_HAVE_MPI
51
 
  return status;
52
 
}
53
 
// --------------------------------------------------------------
54
 
// intput
55
 
// --------------------------------------------------------------
56
 
/// @brief This routine returns the rank of a process that can perform i/o
57
 
iparstream::size_type
58
 
iparstream::io_proc() {
59
 
#ifndef _RHEOLEF_HAVE_MPI
60
 
    return 0;
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:
67
 
        io_proc = 0;
68
 
    }
69
 
    return size_type(io_proc);
70
 
#endif // _RHEOLEF_HAVE_MPI
71
 
}
72
 
oparstream::size_type
73
 
oparstream::io_proc() 
74
 
{
75
 
  return iparstream::io_proc(); 
76
 
}
77
 
/// @brief This routine opens a physical input file
78
 
void
79
 
iparstream::open (
80
 
  std::string filename, 
81
 
  std::string suffix,
82
 
  const communicator& comm)
83
 
{
84
 
  warning_macro ("iparstream::open("<<filename<<","<<suffix<<")...");
85
 
  close();
86
 
  if (_use_alloc && _ptr_is != 0) {
87
 
    delete_macro (_ptr_is);
88
 
    _use_alloc = false;
89
 
    _ptr_is = 0;
90
 
  }
91
 
  if (size_type(comm.rank()) == iparstream::io_proc()) {
92
 
    _ptr_is = new_macro (irheostream(filename, suffix));
93
 
  } else {
94
 
    _ptr_is = new_macro (irheostream);
95
 
  }
96
 
  _comm = comm;
97
 
  _use_alloc = true;
98
 
  warning_macro ("iparstream::open("<<filename<<","<<suffix<<") done");
99
 
}
100
 
void
101
 
iparstream::close ()
102
 
{
103
 
  if (_use_alloc && _ptr_is != 0) {
104
 
    if (size_type(_comm.rank()) == iparstream::io_proc()) {
105
 
      irheostream* ptr_irs = (irheostream*)(_ptr_is);
106
 
      (*ptr_irs).close();
107
 
    }
108
 
  }
109
 
}
110
 
iparstream::~iparstream ()
111
 
{
112
 
  close();
113
 
  if (_use_alloc && _ptr_is != 0) {
114
 
    delete_macro (_ptr_is);
115
 
    _use_alloc = false;
116
 
    _ptr_is = 0;
117
 
  }
118
 
}
119
 
bool
120
 
iparstream::good() const
121
 
{
122
 
    bool status;
123
 
    if (size_type(comm().rank()) != iparstream::io_proc()) {
124
 
        status = true;
125
 
    } else if (_ptr_is == 0) {
126
 
        status = false;
127
 
    } else {
128
 
        status = (*_ptr_is).good();
129
 
    }
130
 
    mpi::broadcast(comm(), status, 0);
131
 
    return status;
132
 
}
133
 
// --------------------------------------------------------------
134
 
// output
135
 
// --------------------------------------------------------------
136
 
/// @brief This routine opens a physical output file
137
 
void
138
 
oparstream::open (
139
 
  std::string filename, 
140
 
  std::string suffix,
141
 
  const communicator& comm)
142
 
{
143
 
  close();
144
 
  if (_use_alloc && _ptr_os != 0) {
145
 
    delete_macro (_ptr_os);
146
 
    _use_alloc = false;
147
 
    _ptr_os = 0;
148
 
  }
149
 
  if (size_type(comm.rank()) == oparstream::io_proc()) {
150
 
    _ptr_os = new_macro (orheostream(filename, suffix));
151
 
  } else {
152
 
    _ptr_os = new_macro (orheostream);
153
 
  }
154
 
  _comm = comm;
155
 
  _use_alloc = true;
156
 
}
157
 
void
158
 
oparstream::close ()
159
 
{
160
 
  if (_use_alloc && _ptr_os != 0) {
161
 
    if (size_type(_comm.rank()) == oparstream::io_proc()) {
162
 
      orheostream* ptr_ors = (orheostream*)(_ptr_os);
163
 
      (*ptr_ors).close();
164
 
    }
165
 
  }
166
 
}
167
 
oparstream::~oparstream ()
168
 
{
169
 
  close();
170
 
  if (_use_alloc && _ptr_os != 0) {
171
 
    delete_macro (_ptr_os);
172
 
    _use_alloc = false;
173
 
    _ptr_os = 0;
174
 
  }
175
 
}
176
 
bool
177
 
oparstream::good() const
178
 
{
179
 
    bool status;
180
 
    if (size_type(comm().rank()) != iparstream::io_proc()) {
181
 
        status = true;
182
 
    } else if (_ptr_os == 0) {
183
 
        status = false;
184
 
    } else {
185
 
        status = (*_ptr_os).good();
186
 
    }
187
 
    mpi::broadcast(comm(), status, 0);
188
 
    return status;
189
 
}
190
 
 
191
 
} // namespace rheolef