Loading...
Searching...
No Matches
diststream.cc
Go to the documentation of this file.
#include "rheolef/diststream.h"
#include "rheolef/environment.h"
#include "rheolef/rheostream.h" // scatch()
using namespace std;
namespace rheolef {
// ----------------------------------------------------------------------------
// global variables
// ----------------------------------------------------------------------------
idiststream din (cin);
odiststream dout (cout);
odiststream dlog (clog);
odiststream derr (cerr);
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
bool
dis_scatch (idiststream& ips, const communicator& comm, std::string ch)
{
// TODO: comm is in ips.comm()
typedef std::size_t size_type;
size_type io_proc = idiststream::io_proc();
size_type my_proc = comm.rank();
bool status = false;
if (my_proc == io_proc) {
status = scatch(ips.is(),ch);
}
#ifdef _RHEOLEF_HAVE_MPI
mpi::broadcast (comm, status, io_proc);
#endif // _RHEOLEF_HAVE_MPI
return status;
}
// --------------------------------------------------------------
// intput
// --------------------------------------------------------------
idiststream::size_type
idiststream::io_proc() {
#ifndef _RHEOLEF_HAVE_MPI
return 0;
#else // _RHEOLEF_HAVE_MPI
boost::optional<int> opt_io_proc = environment::io_rank();
check_macro (opt_io_proc, "no process can perform i/o");
int io_proc = opt_io_proc.get();
if (io_proc == mpi::any_source) {
io_proc = 0;
}
return size_type(io_proc);
#endif // _RHEOLEF_HAVE_MPI
}
odiststream::size_type
odiststream::io_proc()
{
return idiststream::io_proc();
}
void
idiststream::open (
std::string filename,
std::string suffix,
const communicator& comm)
{
close();
if (_use_alloc && _ptr_is != 0) {
delete_macro (_ptr_is);
_use_alloc = false;
_ptr_is = 0;
}
if (size_type(comm.rank()) == idiststream::io_proc()) {
_ptr_is = new_macro (irheostream(filename, suffix));
} else {
_ptr_is = new_macro (irheostream);
}
_comm = comm;
_use_alloc = true;
}
void
idiststream::close ()
{
if (_use_alloc && _ptr_is != 0) {
if (size_type(_comm.rank()) == idiststream::io_proc()) {
irheostream* ptr_irs = (irheostream*)(_ptr_is);
(*ptr_irs).close();
}
}
}
idiststream::~idiststream ()
{
close();
if (_use_alloc && _ptr_is != 0) {
delete_macro (_ptr_is);
_use_alloc = false;
_ptr_is = 0;
}
}
bool
idiststream::good() const
{
bool status;
if (size_type(comm().rank()) != idiststream::io_proc()) {
status = true;
} else if (_ptr_is == 0) {
status = false;
} else {
status = (*_ptr_is).good();
}
#ifdef _RHEOLEF_HAVE_MPI
mpi::broadcast(comm(), status, 0);
#endif // _RHEOLEF_HAVE_MPI
return status;
}
// --------------------------------------------------------------
// output
// --------------------------------------------------------------
void
odiststream::open (
std::string filename,
std::string suffix,
io::mode_type mode,
const communicator& comm)
{
close();
if (_use_alloc && _ptr_os != 0) {
delete_macro (_ptr_os);
_use_alloc = false;
_ptr_os = 0;
}
if (size_type(comm.rank()) == odiststream::io_proc()) {
_ptr_os = new_macro (orheostream(filename, suffix, mode));
} else {
_ptr_os = new_macro (orheostream);
}
_comm = comm;
_use_alloc = true;
}
void
odiststream::close ()
{
if (_use_alloc && _ptr_os != 0) {
if (size_type(_comm.rank()) == odiststream::io_proc()) {
orheostream* ptr_ors = (orheostream*)(_ptr_os);
(*ptr_ors).close();
}
}
}
void
odiststream::flush()
{
if (size_type(_comm.rank()) != odiststream::io_proc()) return;
if (_use_alloc && _ptr_os != 0) {
orheostream* ptr_ors = (orheostream*)(_ptr_os);
(*ptr_ors).flush();
} else if (_ptr_os != 0) {
(*_ptr_os).flush(); // call usual ostream::flush(), i.e. not gziped output
}
}
odiststream::~odiststream ()
{
close();
if (_use_alloc && _ptr_os != 0) {
delete_macro (_ptr_os);
_use_alloc = false;
_ptr_os = 0;
}
}
bool
odiststream::good() const
{
bool status;
if (size_type(comm().rank()) != idiststream::io_proc()) {
status = true;
} else if (_ptr_os == 0) {
status = false;
} else {
status = (*_ptr_os).good();
}
#ifdef _RHEOLEF_HAVE_MPI
mpi::broadcast(comm(), status, 0);
#endif // _RHEOLEF_HAVE_MPI
return status;
}
// -----------------------------------------
// system utilities
// -----------------------------------------
int
dis_system (const std::string& command, const communicator& comm)
{
typedef odiststream::size_type size_type;
size_type io_proc = odiststream::io_proc();
size_type my_proc = comm.rank();
int status = 0;
#ifdef _RHEOLEF_HAVE_MPI
mpi::communicator().barrier();
#endif // _RHEOLEF_HAVE_MPI
if (my_proc == io_proc) {
status = std::system (command.c_str());
}
#ifdef _RHEOLEF_HAVE_MPI
mpi::communicator().barrier();
mpi::broadcast (mpi::communicator(), status, io_proc);
#endif // _RHEOLEF_HAVE_MPI
return status;
}
bool
dis_file_exists (const std::string& filename, const communicator& comm)
{
typedef odiststream::size_type size_type;
size_type io_proc = odiststream::io_proc();
size_type my_proc = comm.rank();
bool status = false;
if (my_proc == io_proc) {
status = file_exists (filename);
}
#ifdef _RHEOLEF_HAVE_MPI
mpi::broadcast (mpi::communicator(), status, io_proc);
#endif // _RHEOLEF_HAVE_MPI
return status;
}
} // namespace rheolef
size_type
field::size_type size_type
Definition branch.cc:430
rheolef::environment::io_rank
static boost::optional< int > io_rank()
Definition environment.cc:123
rheolef::idiststream
idiststream: see the diststream page for the full documentation
Definition diststream.h:336
rheolef::idiststream::io_proc
static size_type io_proc()
This routine returns the rank of a process that can perform i/o.
Definition diststream.cc:64
rheolef::idiststream::~idiststream
~idiststream()
Definition diststream.cc:114
rheolef::idiststream::close
void close()
Definition diststream.cc:105
rheolef::idiststream::_comm
communicator _comm
Definition diststream.h:372
rheolef::idiststream::is
std::istream & is()
Definition diststream.h:400
rheolef::idiststream::size_type
std::size_t size_type
Definition diststream.h:338
rheolef::idiststream::open
void open(std::string filename, std::string suffix="", const communicator &comm=communicator())
This routine opens a physical input file.
Definition diststream.cc:85
rheolef::idiststream::_ptr_is
std::istream * _ptr_is
Definition diststream.h:370
rheolef::idiststream::good
bool good() const
Definition diststream.cc:124
rheolef::idiststream::_use_alloc
bool _use_alloc
Definition diststream.h:371
rheolef::idiststream::comm
const communicator & comm() const
Definition diststream.h:356
rheolef::irheostream
Definition rheostream.h:179
rheolef::irheostream::close
void close()
Definition rheostream.cc:192
rheolef::odiststream::_ptr_os
std::ostream * _ptr_os
Definition diststream.h:180
rheolef::odiststream::~odiststream
~odiststream()
Definition diststream.cc:185
rheolef::odiststream::open
void open(std::string filename, std::string suffix="", io::mode_type mode=io::out, const communicator &comm=communicator())
This routine opens a physical output file.
Definition diststream.cc:144
rheolef::odiststream::close
void close()
Definition diststream.cc:165
rheolef::odiststream::_comm
communicator _comm
Definition diststream.h:182
rheolef::odiststream::size_type
std::size_t size_type
Definition diststream.h:139
rheolef::odiststream::good
bool good() const
Definition diststream.cc:195
rheolef::odiststream::flush
void flush()
Definition diststream.cc:175
rheolef::odiststream::io_proc
static size_type io_proc()
Definition diststream.cc:79
rheolef::odiststream::_use_alloc
bool _use_alloc
Definition diststream.h:181
rheolef::odiststream::comm
const communicator & comm() const
Definition diststream.h:167
rheolef::orheostream
Definition rheostream.h:194
rheolef::orheostream::close
void close()
Definition rheostream.cc:143
rheolef::orheostream::flush
void flush()
Definition rheostream.cc:150
rheolef::din
idiststream din(cin)
see the diststream page for the full documentation
Definition diststream.h:464
rheolef::dlog
odiststream dlog(clog)
see the diststream page for the full documentation
Definition diststream.h:470
rheolef::dout
odiststream dout(cout)
see the diststream page for the full documentation
Definition diststream.h:467
rheolef::derr
odiststream derr(cerr)
see the diststream page for the full documentation
Definition diststream.h:473
check_macro
check_macro(expr1.have_homogeneous_space(Xh1), "dual(expr1,expr2); expr1 should have homogeneous space. HINT: use dual(interpolate(Xh, expr1),expr2)")
rheolef::io::mode_type
mode_type
Definition rheostream.h:171
rheolef
This file is part of Rheolef.
Definition compiler_eigen.h:39
rheolef::scatch
bool scatch(std::istream &in, const std::string &ch, bool full_match=true)
scatch: see the rheostream page for the full documentation
Definition scatch.icc:44
rheolef::dis_scatch
bool dis_scatch(idiststream &ips, const communicator &comm, std::string ch)
distributed version of scatch(istream&,string)
Definition diststream.cc:44
rheolef::dis_system
int dis_system(const std::string &command, const communicator &comm)
Definition diststream.cc:214
rheolef::dis_file_exists
bool dis_file_exists(const std::string &filename, const communicator &comm)
Definition diststream.cc:233
rheolef::file_exists
bool file_exists(const std::string &filename)
file_exists: see the rheostream page for the full documentation
Definition scatch.icc:34
std
STL namespace.