$treeview $search $mathjax
Palabos  Version 1.1
$projectbrief
$projectbrief
$searchbox

parallelIO.h

Go to the documentation of this file.
00001 /* This file is part of the Palabos library.
00002  *
00003  * Copyright (C) 2011 FlowKit Sarl
00004  * Avenue de Chailly 23
00005  * 1012 Lausanne, Switzerland
00006  * E-mail contact: contact@flowkit.com
00007  *
00008  * The most recent release of Palabos can be downloaded at 
00009  * <http://www.palabos.org/>
00010  *
00011  * The library Palabos is free software: you can redistribute it and/or
00012  * modify it under the terms of the GNU Affero General Public License as
00013  * published by the Free Software Foundation, either version 3 of the
00014  * License, or (at your option) any later version.
00015  *
00016  * The library is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU Affero General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Affero General Public License
00022  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #ifndef PARALLEL_IO_H
00026 #define PARALLEL_IO_H
00027 
00028 #include "core/globalDefs.h"
00029 #include "parallelism/mpiManager.h"
00030 #include <streambuf>
00031 #include <istream>
00032 #include <ostream>
00033 #include <fstream>
00034 #include <iostream>
00035 #include <cstdio>
00036 
00037 namespace plb {
00038 
00040 
00044 class DevNullBuffer : public std::streambuf {
00045 protected:
00046     virtual int_type overflow(int_type c) {
00047         return EOF;
00048     }
00049     virtual int_type underflow() {
00050         return EOF;
00051     }
00052 };
00053 
00054 // Parallel Output Streams.
00055 
00056 struct Parallel_ostream {
00057     virtual ~Parallel_ostream() { }
00058     virtual std::ostream& getOriginalStream() =0;
00059 };
00060 
00061 class Parallel_referring_ostream : public Parallel_ostream {
00062 public:
00063     Parallel_referring_ostream(std::ostream& original_ostream_)
00064         : devNullStream(&devNullBuffer),
00065           original_ostream(original_ostream_)
00066     { }
00067     virtual std::ostream& getOriginalStream() {
00068         if (global::mpi().isMainProcessor()) {
00069             return original_ostream;
00070         }
00071         else {
00072             return devNullStream;
00073         }
00074     }
00075 private:
00076     DevNullBuffer devNullBuffer;
00077     std::ostream  devNullStream;
00078     std::ostream& original_ostream;
00079 };
00080 
00081 template<typename Value>
00082 Parallel_ostream& operator<< (Parallel_ostream& lhs, Value const& rhs) {
00083     lhs.getOriginalStream() << rhs;
00084     return lhs;
00085 }
00086 
00087 inline Parallel_ostream& operator<< (Parallel_ostream& lhs, std::ostream& (*op)(std::ostream&)) {
00088     lhs.getOriginalStream() << op;
00089     return lhs;
00090 }
00091 
00092 class plb_ofstream : public Parallel_ostream {
00093 public:
00094     plb_ofstream();
00095     explicit plb_ofstream(const char* filename,
00096                           std::ostream::openmode mode = std::ostream::out | std::ostream::trunc );
00097     ~plb_ofstream();
00098     virtual std::ostream& getOriginalStream();
00099 
00100     bool is_open();
00101     void open(const char* filename, std::ostream::openmode mode = std::ostream::out | std::ostream::trunc);
00102     void close();
00103 private:
00104     plb_ofstream(plb_ofstream const& rhs);
00105     plb_ofstream& operator=(plb_ofstream const& rhs);
00106 private:
00107     DevNullBuffer devNullBuffer;
00108     std::ostream  devNullStream;
00109     std::ofstream *original;
00110 };
00111 
00112 extern Parallel_referring_ostream pcout;
00113 extern Parallel_referring_ostream pcerr;
00114 extern Parallel_referring_ostream pclog;
00115 
00116 
00117 // Parallel Input Streams.
00118 
00119 struct Parallel_istream {
00120     virtual ~Parallel_istream() { }
00121     virtual std::istream& getOriginalStream() =0;
00122 };
00123 
00124 class Parallel_referring_istream : public Parallel_istream {
00125 public:
00126     Parallel_referring_istream(std::istream& original_istream_)
00127         : original_istream(original_istream_)
00128     { }
00129     virtual std::istream& getOriginalStream() {
00130         return original_istream;
00131     }
00132 private:
00133     std::istream& original_istream;
00134 };
00135 
00136 template<typename Value>
00137 Parallel_istream& operator>> (Parallel_istream& lhs, Value& rhs) {
00138     lhs.getOriginalStream() >> rhs;
00139     return lhs;
00140 }
00141 
00142 inline Parallel_istream& operator>> (Parallel_istream& lhs, std::istream& (*op)(std::istream&)) {
00143     lhs.getOriginalStream() >> op;
00144     return lhs;
00145 }
00146 
00147 class plb_ifstream : public Parallel_istream {
00148 public:
00149     plb_ifstream();
00150     explicit plb_ifstream(const char* filename,
00151                           std::istream::openmode mode = std::ostream::in );
00152     ~plb_ifstream();
00153     virtual std::istream& getOriginalStream();
00154 
00155     bool is_open();
00156     void open(const char* filename, std::istream::openmode mode = std::ostream::in);
00157     void close();
00158 private:
00159     plb_ifstream(plb_ifstream const& rhs);
00160     plb_ifstream& operator=(plb_ifstream const& rhs);
00161 private:
00162     DevNullBuffer devNullBuffer;
00163     std::istream  devNullStream;
00164     std::ifstream *original;
00165 };
00166 
00167 }  // namespace plb
00168 
00169 #endif