| 1 |
|
/* |
| 2 |
< |
* Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project |
| 3 |
< |
* |
| 4 |
< |
* Contact: oopse@oopse.org |
| 5 |
< |
* |
| 6 |
< |
* This program is free software; you can redistribute it and/or |
| 7 |
< |
* modify it under the terms of the GNU Lesser General Public License |
| 8 |
< |
* as published by the Free Software Foundation; either version 2.1 |
| 9 |
< |
* of the License, or (at your option) any later version. |
| 10 |
< |
* All we ask is that proper credit is given for our work, which includes |
| 11 |
< |
* - but is not limited to - adding the above copyright notice to the beginning |
| 12 |
< |
* of your source code files, and to any copyright notice that you may distribute |
| 13 |
< |
* with programs based on this work. |
| 14 |
< |
* |
| 15 |
< |
* This program is distributed in the hope that it will be useful, |
| 16 |
< |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
< |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
< |
* GNU Lesser General Public License for more details. |
| 19 |
< |
* |
| 20 |
< |
* You should have received a copy of the GNU Lesser General Public License |
| 21 |
< |
* along with this program; if not, write to the Free Software |
| 22 |
< |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 2 |
> |
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
| 3 |
|
* |
| 4 |
+ |
* The University of Notre Dame grants you ("Licensee") a |
| 5 |
+ |
* non-exclusive, royalty free, license to use, modify and |
| 6 |
+ |
* redistribute this software in source and binary code form, provided |
| 7 |
+ |
* that the following conditions are met: |
| 8 |
+ |
* |
| 9 |
+ |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
+ |
* notice, this list of conditions and the following disclaimer. |
| 11 |
+ |
* |
| 12 |
+ |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
+ |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
+ |
* documentation and/or other materials provided with the |
| 15 |
+ |
* distribution. |
| 16 |
+ |
* |
| 17 |
+ |
* This software is provided "AS IS," without a warranty of any |
| 18 |
+ |
* kind. All express or implied conditions, representations and |
| 19 |
+ |
* warranties, including any implied warranty of merchantability, |
| 20 |
+ |
* fitness for a particular purpose or non-infringement, are hereby |
| 21 |
+ |
* excluded. The University of Notre Dame and its licensors shall not |
| 22 |
+ |
* be liable for any damages suffered by licensee as a result of |
| 23 |
+ |
* using, modifying or distributing the software or its |
| 24 |
+ |
* derivatives. In no event will the University of Notre Dame or its |
| 25 |
+ |
* licensors be liable for any lost revenue, profit or data, or for |
| 26 |
+ |
* direct, indirect, special, consequential, incidental or punitive |
| 27 |
+ |
* damages, however caused and regardless of the theory of liability, |
| 28 |
+ |
* arising out of the use of or inability to use software, even if the |
| 29 |
+ |
* University of Notre Dame has been advised of the possibility of |
| 30 |
+ |
* such damages. |
| 31 |
+ |
* |
| 32 |
+ |
* SUPPORT OPEN SCIENCE! If you use OpenMD or its source code in your |
| 33 |
+ |
* research, please cite the appropriate papers when you publish your |
| 34 |
+ |
* work. Good starting points are: |
| 35 |
+ |
* |
| 36 |
+ |
* [1] Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). |
| 37 |
+ |
* [2] Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). |
| 38 |
+ |
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
| 39 |
+ |
* [4] Vardeman & Gezelter, in progress (2009). |
| 40 |
|
*/ |
| 41 |
< |
|
| 41 |
> |
|
| 42 |
|
/** |
| 43 |
|
* @file basic_ifstrstream.hpp |
| 44 |
|
* @author Teng Lin |
| 49 |
|
#ifndef IO_IFSTRSTREAM_HPP |
| 50 |
|
#define IO_IFSTRSTREAM_HPP |
| 51 |
|
|
| 52 |
+ |
#include <cassert> |
| 53 |
|
#include <cstring> |
| 54 |
|
#include <fstream> |
| 55 |
|
#include <sstream> |
| 56 |
|
|
| 57 |
|
#ifdef IS_MPI |
| 58 |
< |
#include <mpi.hpp> |
| 58 |
> |
#include <mpi.h> |
| 59 |
|
#endif |
| 60 |
|
|
| 61 |
< |
namespace oopse { |
| 45 |
< |
using namespace std; |
| 46 |
< |
/** |
| 47 |
< |
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
| 48 |
< |
* @brief class provides a stream interface to read data from files. |
| 49 |
< |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
| 50 |
< |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
| 51 |
< |
* After brocasting, every node will fall back to stringstream.</p> |
| 52 |
< |
* |
| 53 |
< |
* @code |
| 54 |
< |
* const int MAXLEN = 1024; |
| 55 |
< |
* char buffer[MAXLEN]; |
| 56 |
< |
* ifstrstream in; |
| 57 |
< |
* in.open("Shapes.frc"); |
| 58 |
< |
* if (in.is_open()) { |
| 59 |
< |
* in.getline(buffer, MAXLEN); |
| 60 |
< |
* } |
| 61 |
< |
* in.close(); |
| 62 |
< |
* @endcode |
| 63 |
< |
*/ |
| 64 |
< |
template <class _CharT, class _Traits, class _Alloc> |
| 65 |
< |
class basic_ifstrstream : public basic_istream<_CharT, _Traits> { |
| 66 |
< |
public: |
| 67 |
< |
//traits |
| 68 |
< |
typedef _CharT char_type; |
| 69 |
< |
typedef typename _Traits::int_type int_type; |
| 70 |
< |
typedef typename _Traits::pos_type pos_type; |
| 71 |
< |
typedef typename _Traits::off_type off_type; |
| 72 |
< |
typedef _Traits traits_type; |
| 61 |
> |
namespace OpenMD { |
| 62 |
|
|
| 63 |
< |
typedef basic_ios<_CharT, _Traits> _Basic_ios; |
| 64 |
< |
typedef basic_istream<_CharT, _Traits> _Base; |
| 63 |
> |
/** |
| 64 |
> |
* @class basic_ifstrstream basic_ifstrstream.hpp "io/basic_ifstrstream.hpp" |
| 65 |
> |
* @brief basic_ifstrstream class provides a stream interface to read data from files. |
| 66 |
> |
* <p>In single mode, it falls back to ifstream. Don't need to read the whole file into memory. |
| 67 |
> |
* In parallel mode, the master node will read the whole file and brocast it to other slave nodes. |
| 68 |
> |
* After brocasting, every node will fall back to stringstream.</p> |
| 69 |
> |
* |
| 70 |
> |
* @code |
| 71 |
> |
* const int MAXLEN = 1024; |
| 72 |
> |
* char buffer[MAXLEN]; |
| 73 |
> |
* ifstrstream in; |
| 74 |
> |
* in.open("Shapes.frc"); |
| 75 |
> |
* if (in.is_open()) { |
| 76 |
> |
* in.getline(buffer, MAXLEN); |
| 77 |
> |
* } |
| 78 |
> |
* in.close(); |
| 79 |
> |
* @endcode |
| 80 |
> |
*/ |
| 81 |
> |
template <class _CharT, class _Traits, class _Alloc> |
| 82 |
> |
class basic_ifstrstream : public std::basic_istream<_CharT, _Traits> { |
| 83 |
> |
public: |
| 84 |
> |
//traits |
| 85 |
> |
typedef _CharT char_type; |
| 86 |
> |
typedef typename _Traits::int_type int_type; |
| 87 |
> |
typedef typename _Traits::pos_type pos_type; |
| 88 |
> |
typedef typename _Traits::off_type off_type; |
| 89 |
> |
typedef _Traits traits_type; |
| 90 |
|
|
| 91 |
+ |
typedef std::basic_ios<_CharT, _Traits> _Basic_ios; |
| 92 |
+ |
typedef std::basic_istream<_CharT, _Traits> _Base; |
| 93 |
+ |
|
| 94 |
|
#ifdef IS_MPI |
| 95 |
< |
typedef basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
| 95 |
> |
typedef std::basic_stringbuf<_CharT, _Traits, _Alloc> _Buf; |
| 96 |
|
#else |
| 97 |
< |
typedef basic_filebuf<_CharT, _Traits> _Buf; |
| 97 |
> |
typedef std::basic_filebuf<_CharT, _Traits> _Buf; |
| 98 |
|
#endif |
| 99 |
|
|
| 100 |
< |
static const int FileNoExists = -1; |
| 101 |
< |
static const int FileIOError = -2; |
| 100 |
> |
static const int FileNotExists = -1; |
| 101 |
> |
static const int FileIOError = -2; |
| 102 |
|
|
| 103 |
< |
public: |
| 103 |
> |
public: |
| 104 |
|
|
| 105 |
< |
/** Constructs an object of class ifstream. */ |
| 106 |
< |
basic_ifstrstream() |
| 107 |
< |
: basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), |
| 108 |
< |
internalBuf_(NULL), isRead(false) { |
| 109 |
< |
|
| 110 |
< |
#ifdef IS_MPI |
| 111 |
< |
//in parallel mode, fall back to istringstream |
| 95 |
< |
basic_stringbuf<_CharT, _Traits, _Alloc>* stringBuffer = new basic_stringbuf<_CharT, _Traits, _Alloc>(); |
| 96 |
< |
internalBuf_ = stringBuffer; |
| 97 |
< |
#else |
| 98 |
< |
//in single version, fall back to ifstream |
| 99 |
< |
basic_filebuf<_CharT, _Traits>* fileBuffer = new basic_filebuf<_CharT, _Traits>(); |
| 100 |
< |
internalBuf_ = fileBuffer; |
| 101 |
< |
#endif |
| 102 |
< |
|
| 103 |
< |
this->init(internalBuf_); |
| 104 |
< |
isRead = false; |
| 105 |
< |
} |
| 105 |
> |
/** Constructs an object of class ifstream. */ |
| 106 |
> |
basic_ifstrstream() |
| 107 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
| 108 |
> |
internalBuf_(), isRead(false) { |
| 109 |
> |
|
| 110 |
> |
this->init(&internalBuf_); |
| 111 |
> |
} |
| 112 |
|
|
| 113 |
< |
/** |
| 114 |
< |
* Explicit constructor |
| 115 |
< |
* @filename String containing the name of the file to be opened |
| 116 |
< |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
| 117 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
| 118 |
< |
*/ |
| 119 |
< |
explicit basic_ifstrstream(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false) |
| 120 |
< |
: basic_ios<_CharT, _Traits>(), basic_istream<_CharT, _Traits>(0), |
| 121 |
< |
internalBuf_(NULL), isRead(false) { |
| 113 |
> |
/** |
| 114 |
> |
* Explicit constructor |
| 115 |
> |
* @filename String containing the name of the file to be opened |
| 116 |
> |
* @mode Flags describing the requested i/o mode for the file, default value is ios_base::in |
| 117 |
> |
* @checkFilename Flags indicating checking the file name in parallel |
| 118 |
> |
*/ |
| 119 |
> |
explicit basic_ifstrstream(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false) |
| 120 |
> |
: std::basic_istream<_CharT, _Traits>(0), |
| 121 |
> |
internalBuf_(), isRead(false) { |
| 122 |
|
|
| 123 |
< |
isRead = internalOpen(filename, mode | ios_base::in, checkFilename); |
| 124 |
< |
} |
| 123 |
> |
this->init(&internalBuf_); |
| 124 |
> |
isRead = internalOpen(filename, mode | std::ios_base::in, checkFilename); |
| 125 |
> |
} |
| 126 |
|
|
| 127 |
< |
/** |
| 128 |
< |
* |
| 129 |
< |
*/ |
| 130 |
< |
~basic_ifstrstream(){ |
| 131 |
< |
close(); |
| 132 |
< |
delete internalBuf_; |
| 126 |
< |
internalBuf_ = NULL; |
| 127 |
< |
} |
| 127 |
> |
/** |
| 128 |
> |
* virtual destructor will close the file(in single mode) and clear the stream buffer |
| 129 |
> |
*/ |
| 130 |
> |
~basic_ifstrstream(){ |
| 131 |
> |
close(); |
| 132 |
> |
} |
| 133 |
|
|
| 134 |
< |
/** |
| 135 |
< |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
| 136 |
< |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
| 137 |
< |
* brocasting, every nodes fall back to stringstream (parallel mode). |
| 138 |
< |
* @filename String containing the name of the file to be opened |
| 139 |
< |
* @mode Flags describing the requested i/o mode for the file |
| 140 |
< |
* @checkFilename Flags indicating checking the file name in parallel |
| 141 |
< |
*/ |
| 142 |
< |
void open(const char* filename, ios_base::openmode mode = ios_base::in, bool checkFilename = false){ |
| 134 |
> |
/** |
| 135 |
> |
* Opens a file and associats a buffer with the specified file to perform the i/o operations |
| 136 |
> |
* (single mode). Master reads a file and brocasts its content to the other slave nodes. After |
| 137 |
> |
* brocasting, every nodes fall back to stringstream (parallel mode). |
| 138 |
> |
* @filename String containing the name of the file to be opened |
| 139 |
> |
* @mode Flags describing the requested i/o mode for the file |
| 140 |
> |
* @checkFilename Flags indicating checking the file name in parallel |
| 141 |
> |
*/ |
| 142 |
> |
void open(const char* filename, std::ios_base::openmode mode = std::ios_base::in, bool checkFilename = false){ |
| 143 |
|
|
| 144 |
< |
if (!isRead ) { |
| 145 |
< |
isRead = internalOpen(filename, mode, checkFilename); |
| 146 |
< |
} |
| 147 |
< |
} |
| 144 |
> |
if (!isRead ) { |
| 145 |
> |
isRead = internalOpen(filename, mode, checkFilename); |
| 146 |
> |
} |
| 147 |
> |
} |
| 148 |
|
|
| 149 |
< |
/** |
| 150 |
< |
* Tests if the stream is currently associated with a valid buffer. |
| 151 |
< |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
| 152 |
< |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
| 153 |
< |
*/ |
| 154 |
< |
bool is_open ( ) { |
| 149 |
> |
/** |
| 150 |
> |
* Tests if the stream is currently associated with a valid buffer. |
| 151 |
> |
* @return true if a file has successfully been opened (single mode) or the whole file is read |
| 152 |
> |
* and spreaded among all of the processors (parallel mode), otherwise false is returned |
| 153 |
> |
*/ |
| 154 |
> |
bool is_open ( ) { |
| 155 |
|
#ifdef IS_MPI |
| 156 |
< |
return isRead; |
| 156 |
> |
return isRead; |
| 157 |
|
#else |
| 158 |
< |
//single version fall back to ifstream |
| 159 |
< |
return this->rdbuf()->is_open(); |
| 158 |
> |
//single version fall back to ifstream |
| 159 |
> |
return internalBuf_.is_open(); |
| 160 |
|
#endif |
| 161 |
< |
} |
| 161 |
> |
} |
| 162 |
|
|
| 163 |
< |
/** |
| 164 |
< |
* In single mode, closes a file. The stream's file buffer is released from its association with |
| 165 |
< |
* the currently open file. In parallel mode, clean the |
| 166 |
< |
*/ |
| 167 |
< |
void close() { |
| 163 |
> |
/** |
| 164 |
> |
* In single mode, closes a file. The stream's file buffer is released from its association with |
| 165 |
> |
* the currently open file. In parallel mode, clean the |
| 166 |
> |
*/ |
| 167 |
> |
void close() { |
| 168 |
|
#ifndef IS_MPI |
| 169 |
< |
//single version fall back to ifstream |
| 170 |
< |
if (!this->rdbuf()->close()) |
| 171 |
< |
this->setstate(ios_base::failbit); |
| 169 |
> |
//single version fall back to ifstream |
| 170 |
> |
if (!internalBuf_.close()) |
| 171 |
> |
this->setstate(std::ios_base::failbit); |
| 172 |
|
#endif |
| 173 |
|
|
| 174 |
< |
isRead = false; |
| 175 |
< |
} |
| 174 |
> |
isRead = false; |
| 175 |
> |
} |
| 176 |
|
|
| 177 |
< |
/** |
| 178 |
< |
* Gets the stream buffer object associated with the stream |
| 179 |
< |
* @return A pointer to the stream buffe object(filebuf in single mode, stringbuf in |
| 180 |
< |
* parallel mode) associated with the stream. |
| 181 |
< |
*/ |
| 182 |
< |
_Buf* rdbuf() const{ |
| 183 |
< |
return const_cast<_Buf*>(internalBuf_); |
| 184 |
< |
} |
| 180 |
< |
|
| 181 |
< |
private: |
| 182 |
< |
|
| 183 |
< |
/** |
| 184 |
< |
* Internal function used to open the file |
| 185 |
< |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
| 186 |
< |
* otherwise return false |
| 187 |
< |
* @filename String containing the name of the file to be opened |
| 188 |
< |
* @mode Flags describing the requested i/o mode for the file |
| 189 |
< |
* @todo use try - catch syntax to make the program more readable |
| 190 |
< |
*/ |
| 191 |
< |
bool internalOpen(const char* filename, ios_base::openmode mode, bool checkFilename){ |
| 177 |
> |
/** |
| 178 |
> |
* Gets the stream buffer object associated with the stream |
| 179 |
> |
* @return A pointer to the stream buffe object(filebuf in single mode, stringbuf in |
| 180 |
> |
* parallel mode) associated with the stream. |
| 181 |
> |
*/ |
| 182 |
> |
_Buf* rdbuf() const{ |
| 183 |
> |
return static_cast<_Buf*>(&internalBuf_); |
| 184 |
> |
} |
| 185 |
|
|
| 186 |
+ |
private: |
| 187 |
+ |
|
| 188 |
+ |
/** |
| 189 |
+ |
* Internal function used to open the file |
| 190 |
+ |
* @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) |
| 191 |
+ |
* otherwise return false |
| 192 |
+ |
* @filename String containing the name of the file to be opened |
| 193 |
+ |
* @mode Flags describing the requested i/o mode for the file |
| 194 |
+ |
* @todo use try - catch syntax to make the program more readable |
| 195 |
+ |
*/ |
| 196 |
+ |
bool internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ |
| 197 |
+ |
|
| 198 |
|
#ifdef IS_MPI |
| 199 |
< |
int commStatus; |
| 200 |
< |
long fileSize; |
| 201 |
< |
char* fbuf; |
| 202 |
< |
int filenameLen; |
| 203 |
< |
int diffFilename; |
| 204 |
< |
int error; |
| 205 |
< |
int myRank; |
| 206 |
< |
int masterNode; |
| 199 |
> |
int commStatus; |
| 200 |
> |
long fileSize; |
| 201 |
> |
char* fbuf; |
| 202 |
> |
int filenameLen; |
| 203 |
> |
int diffFilename; |
| 204 |
> |
int error; |
| 205 |
> |
int myRank; |
| 206 |
> |
int masterNode; |
| 207 |
|
|
| 208 |
< |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
| 209 |
< |
masterNode = 0; |
| 208 |
> |
commStatus = MPI_Comm_rank(MPI_COMM_WORLD, &myRank); |
| 209 |
> |
masterNode = 0; |
| 210 |
|
|
| 211 |
< |
if (myRank == masterNode) { |
| 211 |
> |
if (myRank == masterNode) { |
| 212 |
|
|
| 213 |
< |
if (checkFilename) { |
| 213 |
> |
if (checkFilename) { |
| 214 |
|
|
| 215 |
< |
//check the filename is the same |
| 216 |
< |
filenameLen = strlen(filename); |
| 217 |
< |
commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 218 |
< |
commStatus = MPI_Bcast(filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 215 |
> |
//check the filename is the same |
| 216 |
> |
filenameLen = strlen(filename); |
| 217 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 218 |
> |
commStatus = MPI_Bcast((void*)filename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 219 |
|
|
| 220 |
< |
diffFilename = 0; |
| 221 |
< |
commStatus = MPI_Allreduce(diffFilename, error, 1, MPI_INT, MPI_COMM_WORLD); |
| 220 |
> |
diffFilename = 0; |
| 221 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 222 |
|
|
| 223 |
< |
//if file names are different just return false |
| 224 |
< |
if (error > 0) |
| 225 |
< |
return false; |
| 226 |
< |
} |
| 223 |
> |
//if file names are different just return false |
| 224 |
> |
if (error > 0) |
| 225 |
> |
return false; |
| 226 |
> |
} |
| 227 |
|
|
| 228 |
< |
ifstream fin(filename, mode); |
| 224 |
< |
basic_stringbuf<_CharT, _Traits, _Alloc>* sbuf; |
| 228 |
> |
std::ifstream fin(filename, mode); |
| 229 |
|
|
| 230 |
< |
if (fin.is_open()) { |
| 230 |
> |
if (fin.is_open()) { |
| 231 |
|
|
| 232 |
< |
fin.in.seekg(0, ios::end); |
| 233 |
< |
fileSize = fin.tellg(); |
| 234 |
< |
|
| 231 |
< |
// '\0' need one more char |
| 232 |
< |
fbuf = new char[fileSize+1]; |
| 232 |
> |
fin.seekg(0, std::ios::end); |
| 233 |
> |
fileSize = fin.tellg(); |
| 234 |
> |
fin.seekg(0, std::ios::beg); |
| 235 |
|
|
| 236 |
< |
assert(fbuf != 0); |
| 236 |
> |
// '\0' need one more char |
| 237 |
> |
fbuf = new char[fileSize+1]; |
| 238 |
> |
|
| 239 |
> |
assert(fbuf != 0); |
| 240 |
|
|
| 241 |
< |
fin.read(fbuf, fileSize); |
| 241 |
> |
fin.read(fbuf, fileSize); |
| 242 |
|
|
| 243 |
< |
if (fin.fail()) |
| 244 |
< |
fileSize = FileIOError; |
| 243 |
> |
if (fin.fail()) |
| 244 |
> |
fileSize = FileIOError; |
| 245 |
|
|
| 246 |
< |
//brocasting the file size |
| 247 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 246 |
> |
//brocasting the file size |
| 247 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 248 |
|
|
| 249 |
< |
if (fileSize < 0) { |
| 250 |
< |
fin.close(); |
| 251 |
< |
delete fbuf; |
| 249 |
> |
if (fileSize < 0) { |
| 250 |
> |
fin.close(); |
| 251 |
> |
delete fbuf; |
| 252 |
|
|
| 253 |
< |
return false; |
| 254 |
< |
} |
| 253 |
> |
return false; |
| 254 |
> |
} |
| 255 |
|
|
| 256 |
< |
// make a c-style string and brocasting it |
| 257 |
< |
fbuf[fileSize] = '\0'; |
| 258 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 256 |
> |
// make a c-style std::string and brocasting it |
| 257 |
> |
fbuf[fileSize] = '\0'; |
| 258 |
> |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 259 |
|
|
| 260 |
< |
//it is safe to delete null pointer |
| 261 |
< |
delete internalBuf_; |
| 262 |
< |
|
| 263 |
< |
//initilaize istream |
| 264 |
< |
internalBuf_ = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
| 265 |
< |
assert(internalBuf_); |
| 266 |
< |
this->init(internalBuf_); |
| 267 |
< |
|
| 268 |
< |
//close the file and delete the buffer |
| 264 |
< |
fin.close(); |
| 265 |
< |
delete fbuf; |
| 266 |
< |
}else{ |
| 267 |
< |
fileSize = FileNoExists; |
| 268 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 269 |
< |
return false; |
| 270 |
< |
} |
| 260 |
> |
//close the file and delete the buffer |
| 261 |
> |
fin.close(); |
| 262 |
> |
internalBuf_.str(fbuf); |
| 263 |
> |
delete [] fbuf; |
| 264 |
> |
}else{ |
| 265 |
> |
fileSize = FileNotExists; |
| 266 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 267 |
> |
return false; |
| 268 |
> |
} |
| 269 |
|
|
| 270 |
< |
} else{ |
| 273 |
< |
//check file name |
| 274 |
< |
if (checkFilename) { |
| 275 |
< |
commStatus = MPI_Bcast(filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 270 |
> |
} else{ //slave nodes |
| 271 |
|
|
| 272 |
< |
char * masterFilename = new char[filenameLen]; |
| 273 |
< |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 272 |
> |
//check file name |
| 273 |
> |
if (checkFilename) { |
| 274 |
> |
commStatus = MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); |
| 275 |
> |
|
| 276 |
> |
char * masterFilename = new char[filenameLen]; |
| 277 |
> |
commStatus = MPI_Bcast(masterFilename, filenameLen, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 278 |
|
|
| 279 |
< |
if( strcmp(masterFilename, filename) == 0) |
| 280 |
< |
diffFilename = 0; |
| 281 |
< |
else |
| 282 |
< |
diffFilename = 1; |
| 279 |
> |
if( strcmp(masterFilename, filename) == 0) |
| 280 |
> |
diffFilename = 0; |
| 281 |
> |
else |
| 282 |
> |
diffFilename = 1; |
| 283 |
|
|
| 284 |
< |
delete masterFilename; |
| 284 |
> |
delete masterFilename; |
| 285 |
|
|
| 286 |
< |
commStatus = MPI_Allreduce(diffFilename, error, 1, MPI_INT, MPI_COMM_WORLD); |
| 286 |
> |
commStatus = MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); |
| 287 |
|
|
| 288 |
< |
if (error > 0) |
| 289 |
< |
return false; |
| 290 |
< |
} |
| 291 |
< |
//get file size |
| 292 |
< |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 288 |
> |
if (error > 0) |
| 289 |
> |
return false; |
| 290 |
> |
} |
| 291 |
> |
//get file size |
| 292 |
> |
commStatus = MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); |
| 293 |
|
|
| 294 |
< |
if (fileSize >= 0 ) { |
| 295 |
< |
fbuf = new char[fileSize+1]; |
| 296 |
< |
assert(fbuf); |
| 294 |
> |
if (fileSize >= 0 ) { |
| 295 |
> |
fbuf = new char[fileSize+1]; |
| 296 |
> |
assert(fbuf); |
| 297 |
|
|
| 298 |
< |
//receive file content |
| 299 |
< |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 298 |
> |
//receive file content |
| 299 |
> |
commStatus = MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); |
| 300 |
|
|
| 301 |
< |
//it is safe to delete null pointer |
| 302 |
< |
delete internalBuf_; |
| 301 |
> |
internalBuf_.str(fbuf); |
| 302 |
> |
delete [] fbuf; |
| 303 |
|
|
| 304 |
< |
//initilaize istream |
| 305 |
< |
internalBuf_ = new basic_stringbuf<_CharT, _Traits, _Alloc>(fbuf, mode); |
| 307 |
< |
assert(internalBuf_); |
| 308 |
< |
this->init(internalBuf_); |
| 304 |
> |
} else if (fileSize == FileNotExists ) { |
| 305 |
> |
return false; |
| 306 |
|
|
| 307 |
< |
delete fbuf; |
| 307 |
> |
} else if (fileSize == FileIOError ) { |
| 308 |
> |
return false; |
| 309 |
> |
} |
| 310 |
> |
} |
| 311 |
|
|
| 312 |
– |
} else if (fileSize == FileNoExists ) { |
| 313 |
– |
return false; |
| 314 |
– |
|
| 315 |
– |
} else if (fileSize == FileIOError ) { |
| 316 |
– |
return false; |
| 317 |
– |
} |
| 318 |
– |
} |
| 319 |
– |
|
| 312 |
|
#else |
| 313 |
< |
//in single version, fall back to ifstream |
| 314 |
< |
basic_filebuf<_CharT, _Traits>* fileBuffer = new basic_filebuf<_CharT, _Traits>(); |
| 313 |
> |
//in single version, fall back to ifstream |
| 314 |
> |
if (!internalBuf_.open(filename, mode)) { |
| 315 |
> |
this->setstate(std::ios_base::failbit); |
| 316 |
> |
return false; |
| 317 |
> |
} |
| 318 |
|
|
| 324 |
– |
this->init(fileBuffer); |
| 325 |
– |
if (!fileBuffer->open(filename, mode)) |
| 326 |
– |
this->setstate(ios_base::failbit); |
| 327 |
– |
|
| 328 |
– |
//it is safe to delete null pointer |
| 329 |
– |
delete internalBuf_; |
| 330 |
– |
|
| 331 |
– |
internalBuf_ = fileBuffer; |
| 319 |
|
#endif |
| 320 |
< |
|
| 321 |
< |
return true; |
| 322 |
< |
} |
| 320 |
> |
this->clear(); |
| 321 |
> |
return true; |
| 322 |
> |
} |
| 323 |
|
|
| 324 |
< |
basic_streambuf<_CharT, _Traits>* internalBuf_; /** internal stream buffer */ |
| 325 |
< |
bool isRead; /** file opened flag */ |
| 326 |
< |
}; |
| 324 |
> |
_Buf internalBuf_; /** internal stream buffer */ |
| 325 |
> |
bool isRead; /** file opened flag */ |
| 326 |
> |
}; |
| 327 |
|
|
| 328 |
< |
typedef basic_istringstream<char, char_traits<char>, allocator<char> > ifstringstream; |
| 329 |
< |
}//namespace oopse |
| 328 |
> |
typedef basic_ifstrstream<char, std::char_traits<char>, std::allocator<char> > ifstrstream; |
| 329 |
> |
}//namespace OpenMD |
| 330 |
|
#endif //IO_IFSTRSTREAM_HPP |