⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parallelio.cpp

📁 open lattice boltzmann project www.openlb.org
💻 CPP
字号:
/*  This file is part of the OpenLB library * *  Copyright (C) 2007 Jonas Latt *  Address: Rue General Dufour 24,  1211 Geneva 4, Switzerland  *  E-mail: jonas.latt@gmail.com * *  This program is free software; you can redistribute it and/or *  modify it under the terms of the GNU General Public License *  as published by the Free Software Foundation; either version 2 *  of the License, or (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public  *  License along with this program; if not, write to the Free  *  Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *  Boston, MA  02110-1301, USA.*/#include "complexGrids/mpiManager/mpiManager.h"#include "parallelIO.h"namespace olb {///////////////////////////////////////////////////////////////////// Class ParBuf///////////////////////////////////////////////////////////////////ParBuf::ParBuf(std::streambuf* _originalBuf)  : originalBuf(_originalBuf), mode(normal){ }std::streambuf::int_typeParBuf::overflow (std::streambuf::int_type c) {  int_type returnVal = c;  if (c != EOF) {#ifdef PARALLEL_MODE_MPI    if (singleton::mpi().isMainProcessor()) {#endif      returnVal = originalBuf->sputc((char)c);#ifdef PARALLEL_MODE_MPI    }    if (mode==normal) {      singleton::mpi().bCast(&returnVal, 1);    }#endif  }  return returnVal;}ParBuf::ModesParBuf::getMode() const {  return mode;}voidParBuf::setMode(ParBuf::Modes _mode) {  mode = _mode;}std::streamsizeParBuf::xsputn(const char* s, std::streamsize num) {#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    return originalBuf->sputn(s,num);#ifdef PARALLEL_MODE_MPI  }  else {    return num;  }#endif}std::streambuf::int_typeParBuf::uflow() {  int_type value;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    value = originalBuf->sbumpc();#ifdef PARALLEL_MODE_MPI  }  if (mode==normal) {      singleton::mpi().bCast(&value, 1);  }#endif  return value;}std::streambuf::int_typeParBuf::underflow() {  int_type value;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    value = originalBuf->sgetc();#ifdef PARALLEL_MODE_MPI  }  if (mode==normal) {    singleton::mpi().bCast(&value, 1);  }#endif  return value;}std::streamsizeParBuf::xsgetn (char* s, std::streamsize num) {    std::streamsize sizeRead=0;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    sizeRead = originalBuf->sgetn(s, num);#ifdef PARALLEL_MODE_MPI  }  if (mode==normal) {      int intSizeRead = (int) sizeRead;      singleton::mpi().bCast(&intSizeRead, 1);      singleton::mpi().bCast(s, intSizeRead);      sizeRead = (std::streamsize) intSizeRead;  }#endif  return sizeRead;}///////////////////////////////////////////////////////////////////// Class olb_ofstream///////////////////////////////////////////////////////////////////olb_ofstream::olb_ofstream() : std::ostream(NULL), fbuf(), mybuf(&fbuf) {  this->init(&mybuf);}olb_ofstream::olb_ofstream(const char * filename, openmode mode)  : std::ostream(NULL), fbuf(), mybuf(&fbuf){   init(&mybuf);  open(filename, mode);}olb_ofstream::~olb_ofstream(){ }std::streambuf*olb_ofstream::rdbuf() const {  return const_cast<ParBuf*>(&mybuf);}boololb_ofstream::is_open() {  return fbuf.is_open();}voidolb_ofstream::open(const char* filename, openmode mode) {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.open(filename, mode | ios_base::out);#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    this->setstate(ios_base::failbit);  }}voidolb_ofstream::close() {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.close();#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    setstate(ios_base::failbit);  }}///////////////////////////////////////////////////////////////////// Class olb_ifstream///////////////////////////////////////////////////////////////////olb_ifstream::olb_ifstream() : std::istream(NULL), fbuf(), mybuf(&fbuf) {  init(&mybuf);}olb_ifstream::olb_ifstream(const char * filename, openmode mode)  : std::istream(NULL), fbuf(), mybuf(&fbuf){   init(&mybuf);  open(filename, mode);}olb_ifstream::~olb_ifstream(){ }std::streambuf*olb_ifstream::rdbuf() const {  return const_cast<ParBuf*>(&mybuf);}boololb_ifstream::is_open() {  return fbuf.is_open();}voidolb_ifstream::open(const char* filename, openmode mode) {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.open(filename, mode | ios_base::in);#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    this->setstate(ios_base::failbit);  }}voidolb_ifstream::close() {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.close();#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    setstate(ios_base::failbit);  }}///////////////////////////////////////////////////////////////////// Class olb_fstream///////////////////////////////////////////////////////////////////olb_fstream::olb_fstream() : std::iostream(NULL), fbuf(), mybuf(&fbuf) {  this->init(&mybuf);}olb_fstream::olb_fstream(const char * filename, openmode mode)  : std::iostream(NULL), fbuf(), mybuf(&fbuf){   init(&mybuf);  open(filename, mode);}olb_fstream::~olb_fstream(){ }std::streambuf*olb_fstream::rdbuf() const {  return const_cast<ParBuf*>(&mybuf);}boololb_fstream::is_open() {  return fbuf.is_open();}voidolb_fstream::open(const char* filename, openmode mode) {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.open(filename, mode);#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    this->setstate(ios_base::failbit);  }}voidolb_fstream::close() {  int ok;#ifdef PARALLEL_MODE_MPI  if (singleton::mpi().isMainProcessor()) {#endif    ok = (bool) fbuf.close();#ifdef PARALLEL_MODE_MPI  }  singleton::mpi().bCast(&ok, 1);#endif  if (!ok) {    setstate(ios_base::failbit);  }}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -