| 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).           | 
| 38 | 
> | 
 * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008).           | 
| 39 | 
  | 
 * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 | 
  | 
 * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | 
  | 
 */ | 
| 42 | 
  | 
  | 
| 43 | 
  | 
/** | 
| 44 | 
< | 
 * @file basic_ifstrstream.cpp | 
| 44 | 
> | 
 * @file ifstrstream.cpp | 
| 45 | 
  | 
 * @author Teng Lin | 
| 46 | 
  | 
 * @date 10/14/2004 | 
| 47 | 
  | 
 * @version 1.0 | 
| 48 | 
  | 
 */ | 
| 49 | 
  | 
 | 
| 50 | 
– | 
#include "io/ifstrstream.hpp" | 
| 51 | 
– | 
 | 
| 50 | 
  | 
#ifdef IS_MPI | 
| 51 | 
  | 
#include <mpi.h> | 
| 52 | 
  | 
#endif | 
| 53 | 
  | 
 | 
| 54 | 
+ | 
#include "io/ifstrstream.hpp" | 
| 55 | 
+ | 
 | 
| 56 | 
  | 
namespace OpenMD { | 
| 57 | 
  | 
   | 
| 58 | 
  | 
  /**  Constructs an object of class ifstream.  */ | 
| 59 | 
  | 
#ifdef IS_MPI | 
| 60 | 
  | 
  ifstrstream::ifstrstream()  | 
| 61 | 
  | 
    :  std::basic_istream<char, std::char_traits<char> >(0), | 
| 62 | 
< | 
       internalStringBuf_(), isRead(false)  {     | 
| 62 | 
> | 
    internalStringBuf_(), isRead(false)  {    | 
| 63 | 
  | 
    this->init(&internalStringBuf_); | 
| 64 | 
  | 
  } | 
| 65 | 
  | 
#else | 
| 66 | 
  | 
  ifstrstream::ifstrstream()  | 
| 67 | 
  | 
    :  std::basic_istream<char, std::char_traits<char> >(0), | 
| 68 | 
< | 
       internalFileBuf_(), isRead(false)  {     | 
| 68 | 
> | 
    internalFileBuf_(), isRead(false)  {      | 
| 69 | 
  | 
    this->init(&internalFileBuf_); | 
| 70 | 
  | 
  } | 
| 71 | 
  | 
#endif | 
| 72 | 
  | 
   | 
| 73 | 
  | 
  /** | 
| 74 | 
  | 
   * Explicit constructor | 
| 75 | 
< | 
   * @filename String containing the name of the file to be opened | 
| 76 | 
< | 
   * @mode Flags describing the requested i/o mode for the file, default value is ios_base::in       | 
| 77 | 
< | 
   * @checkFilename Flags indicating checking the file name in parallel | 
| 75 | 
> | 
   * @param filename String containing the name of the file to be opened | 
| 76 | 
> | 
   * @param mode Flags describing the requested i/o mode for the file, | 
| 77 | 
> | 
   * default value is ios_base::in | 
| 78 | 
> | 
   * @param checkFilename Flags indicating checking the file name in parallel | 
| 79 | 
  | 
   */ | 
| 80 | 
  | 
#ifdef IS_MPI | 
| 81 | 
  | 
  ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) | 
| 82 | 
  | 
    :  std::basic_istream<char, std::char_traits<char> >(0), | 
| 83 | 
< | 
       internalStringBuf_(), isRead(false) { | 
| 83 | 
> | 
    internalStringBuf_(), isRead(false) { | 
| 84 | 
  | 
    this->init(&internalStringBuf_); | 
| 85 | 
  | 
    isRead =  internalOpen(filename,  mode | std::ios_base::in, checkFilename); | 
| 86 | 
  | 
  } | 
| 87 | 
  | 
#else | 
| 88 | 
  | 
  ifstrstream::ifstrstream(const char* filename, std::ios_base::openmode mode, bool checkFilename) | 
| 89 | 
  | 
    :  std::basic_istream<char, std::char_traits<char> >(0), | 
| 90 | 
< | 
       internalFileBuf_(), isRead(false) { | 
| 90 | 
> | 
    internalFileBuf_(), isRead(false) { | 
| 91 | 
  | 
     | 
| 92 | 
  | 
    this->init(&internalFileBuf_); | 
| 93 | 
  | 
    isRead =  internalOpen(filename,  mode | std::ios_base::in, checkFilename); | 
| 94 | 
  | 
  } | 
| 95 | 
  | 
#endif   | 
| 96 | 
  | 
  /** | 
| 97 | 
< | 
   * virtual destructor will close the file(in single mode) and clear the stream buffer | 
| 97 | 
> | 
   * virtual destructor will close the file (in single mode) and clear | 
| 98 | 
> | 
   * the stream buffer | 
| 99 | 
  | 
   */ | 
| 100 | 
< | 
  ifstrstream::~ifstrstream(){ | 
| 99 | 
< | 
    close(); | 
| 100 | 
> | 
  ifstrstream::~ifstrstream(){    close(); | 
| 101 | 
  | 
  } | 
| 102 | 
  | 
   | 
| 103 | 
  | 
  /** | 
| 104 | 
< | 
   * Opens a file and associats a buffer with the specified file to perform the i/o operations  | 
| 105 | 
< | 
   * (single mode). Master reads a file and brocasts its content to the other slave nodes. After | 
| 106 | 
< | 
   * brocasting, every nodes fall back to stringstream (parallel mode). | 
| 107 | 
< | 
   * @filename String containing the name of the file to be opened | 
| 108 | 
< | 
   * @mode Flags describing the requested i/o mode for the file | 
| 109 | 
< | 
   * @checkFilename Flags indicating checking the file name in parallel | 
| 104 | 
> | 
   * Opens a file and associates a buffer with the specified file to | 
| 105 | 
> | 
   * perform the i/o operations (single mode). The master node reads a | 
| 106 | 
> | 
   * file and broadcasts its content to the other slave nodes. After | 
| 107 | 
> | 
   * broadcasting, all nodes fall back to stringstream (parallel | 
| 108 | 
> | 
   * mode). | 
| 109 | 
> | 
   * @param filename String containing the name of the file to be opened | 
| 110 | 
> | 
   * @param mode Flags describing the requested i/o mode for the file | 
| 111 | 
> | 
   * @param checkFilename Flags indicating checking the file name in parallel | 
| 112 | 
  | 
   */ | 
| 113 | 
  | 
  void ifstrstream::open(const char* filename, std::ios_base::openmode mode, bool checkFilename){ | 
| 114 | 
  | 
     | 
| 118 | 
  | 
  } | 
| 119 | 
  | 
 | 
| 120 | 
  | 
  /** | 
| 121 | 
< | 
   * Tests if the stream is currently associated with a valid  buffer. | 
| 122 | 
< | 
   * @return true if a file has successfully been opened (single mode) or the whole file is read  | 
| 123 | 
< | 
   * and spreaded among all of the processors (parallel mode),  otherwise false is returned | 
| 121 | 
> | 
   * Tests if the stream is currently associated with a valid buffer. | 
| 122 | 
> | 
   * @return true if a file has successfully been opened (single mode) | 
| 123 | 
> | 
   * or the whole file has been read and spread among all of the | 
| 124 | 
> | 
   * processors (parallel mode), otherwise false is returned | 
| 125 | 
  | 
   */ | 
| 126 | 
  | 
  bool ifstrstream::is_open ( ) { | 
| 127 | 
  | 
#ifdef IS_MPI             | 
| 135 | 
  | 
  /** | 
| 136 | 
  | 
   * In single mode, closes a file. The stream's file buffer is | 
| 137 | 
  | 
   * released from its association with the currently open file. In | 
| 138 | 
< | 
   * parallel mode, clean the | 
| 138 | 
> | 
   * parallel mode, cleans up. | 
| 139 | 
  | 
   */ | 
| 140 | 
  | 
  void ifstrstream::close() { | 
| 141 | 
  | 
#ifndef IS_MPI             | 
| 161 | 
  | 
  }   | 
| 162 | 
  | 
   | 
| 163 | 
  | 
  /** | 
| 164 | 
< | 
     * Internal function used to open the file | 
| 165 | 
< | 
     * @return true if succesfully opens a file (single mode) or gets the file content (parallel mode) | 
| 166 | 
< | 
     * otherwise return false | 
| 167 | 
< | 
     * @filename String containing the name of the file to be opened | 
| 168 | 
< | 
     * @mode Flags describing the requested i/o mode for the file | 
| 169 | 
< | 
     * @todo use try - catch syntax to make the program more readable | 
| 170 | 
< | 
     */ | 
| 164 | 
> | 
   * Internal function used to open the file  | 
| 165 | 
> | 
   * @return true if succesfully opens a file (single mode) or gets | 
| 166 | 
> | 
   * the file content (parallel mode) otherwise return false | 
| 167 | 
> | 
   * @param filename String containing the name of the file to be opened | 
| 168 | 
> | 
   * @param mode Flags describing the requested i/o mode for the file | 
| 169 | 
> | 
   * @param checkFilename Flags indicating checking the file name in parallel | 
| 170 | 
> | 
   * @todo use try - catch syntax to make the program more readable | 
| 171 | 
> | 
   */ | 
| 172 | 
  | 
 | 
| 173 | 
  | 
  bool ifstrstream::internalOpen(const char* filename, std::ios_base::openmode mode, bool checkFilename){ | 
| 174 | 
  | 
     | 
| 182 | 
  | 
    int myRank; | 
| 183 | 
  | 
    int masterNode; | 
| 184 | 
  | 
     | 
| 185 | 
< | 
    myRank =  MPI::COMM_WORLD.Get_rank(); | 
| 185 | 
> | 
    MPI_Comm_rank( MPI_COMM_WORLD, &myRank); | 
| 186 | 
> | 
       | 
| 187 | 
  | 
    masterNode = 0; | 
| 188 | 
  | 
     | 
| 189 | 
  | 
    if (myRank == masterNode) { | 
| 192 | 
  | 
         | 
| 193 | 
  | 
        //check the filename is the same | 
| 194 | 
  | 
        filenameLen = strlen(filename); | 
| 195 | 
< | 
        MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); | 
| 196 | 
< | 
        MPI::COMM_WORLD.Bcast((void*)filename, filenameLen, MPI::CHAR,  | 
| 197 | 
< | 
                              masterNode); | 
| 195 | 
> | 
        MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); | 
| 196 | 
> | 
        MPI_Bcast((void*)filename, filenameLen, MPI_CHAR,  | 
| 197 | 
> | 
                  masterNode, MPI_COMM_WORLD); | 
| 198 | 
  | 
         | 
| 199 | 
  | 
        diffFilename = 0; | 
| 200 | 
< | 
        MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); | 
| 201 | 
< | 
 | 
| 200 | 
> | 
        MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM,  | 
| 201 | 
> | 
                      MPI_COMM_WORLD); | 
| 202 | 
> | 
         | 
| 203 | 
  | 
        //if file names are different just return false | 
| 204 | 
  | 
        if (error > 0) | 
| 205 | 
  | 
          return false;    | 
| 224 | 
  | 
          fileSize = FileIOError; | 
| 225 | 
  | 
         | 
| 226 | 
  | 
        //broadcast the file size | 
| 227 | 
< | 
        MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); | 
| 227 | 
> | 
        MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); | 
| 228 | 
  | 
         | 
| 229 | 
  | 
        if (fileSize < 0) { | 
| 230 | 
  | 
          fin.close();                     | 
| 231 | 
< | 
          delete fbuf; | 
| 231 | 
> | 
          delete[] fbuf; | 
| 232 | 
  | 
           | 
| 233 | 
  | 
          return false; | 
| 234 | 
  | 
        } | 
| 235 | 
  | 
         | 
| 236 | 
  | 
        // make a c-style  std::string and broadcast it | 
| 237 | 
  | 
        fbuf[fileSize] = '\0'; | 
| 238 | 
< | 
        MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); | 
| 238 | 
> | 
        MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); | 
| 239 | 
  | 
         | 
| 240 | 
  | 
        //close the file and delete the buffer | 
| 241 | 
  | 
        fin.close();       | 
| 242 | 
  | 
        internalStringBuf_.str(fbuf); | 
| 243 | 
< | 
        delete [] fbuf; | 
| 243 | 
> | 
        delete[] fbuf; | 
| 244 | 
  | 
      }else{ | 
| 245 | 
  | 
        fileSize = FileNotExists; | 
| 246 | 
< | 
        MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); | 
| 246 | 
> | 
        MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); | 
| 247 | 
  | 
        return false; | 
| 248 | 
  | 
      } | 
| 249 | 
  | 
       | 
| 251 | 
  | 
       | 
| 252 | 
  | 
      //check file name | 
| 253 | 
  | 
      if (checkFilename) { | 
| 254 | 
< | 
        MPI::COMM_WORLD.Bcast(&filenameLen, 1, MPI::INT, masterNode); | 
| 254 | 
> | 
        MPI_Bcast(&filenameLen, 1, MPI_INT, masterNode, MPI_COMM_WORLD); | 
| 255 | 
  | 
         | 
| 256 | 
  | 
        char * masterFilename = new char[filenameLen]; | 
| 257 | 
< | 
        MPI::COMM_WORLD.Bcast(masterFilename, filenameLen, MPI::CHAR,  | 
| 258 | 
< | 
                              masterNode); | 
| 257 | 
> | 
        MPI_Bcast(masterFilename, filenameLen, MPI_CHAR,  | 
| 258 | 
> | 
                  masterNode, MPI_COMM_WORLD); | 
| 259 | 
  | 
         | 
| 260 | 
  | 
        if( strcmp(masterFilename, filename) == 0) | 
| 261 | 
  | 
          diffFilename = 0; | 
| 262 | 
  | 
        else | 
| 263 | 
  | 
          diffFilename = 1; | 
| 264 | 
  | 
         | 
| 265 | 
< | 
        delete masterFilename; | 
| 265 | 
> | 
        delete[] masterFilename; | 
| 266 | 
  | 
         | 
| 267 | 
< | 
        MPI::COMM_WORLD.Allreduce(&diffFilename, &error, 1, MPI::INT, MPI::SUM); | 
| 267 | 
> | 
        MPI_Allreduce(&diffFilename, &error, 1, MPI_INT, MPI_SUM,  | 
| 268 | 
> | 
                      MPI_COMM_WORLD); | 
| 269 | 
  | 
         | 
| 270 | 
  | 
        if (error > 0) | 
| 271 | 
  | 
          return false;                         | 
| 272 | 
  | 
      } | 
| 273 | 
  | 
      //get file size | 
| 274 | 
< | 
      MPI::COMM_WORLD.Bcast(&fileSize, 1, MPI::LONG, masterNode); | 
| 274 | 
> | 
      MPI_Bcast(&fileSize, 1, MPI_LONG, masterNode, MPI_COMM_WORLD); | 
| 275 | 
  | 
       | 
| 276 | 
  | 
      if (fileSize >= 0 ) { | 
| 277 | 
  | 
        fbuf = new char[fileSize+1]; | 
| 278 | 
  | 
        assert(fbuf); | 
| 279 | 
  | 
         | 
| 280 | 
  | 
        //receive file content | 
| 281 | 
< | 
        MPI::COMM_WORLD.Bcast(fbuf, fileSize + 1, MPI::CHAR, masterNode); | 
| 281 | 
> | 
        MPI_Bcast(fbuf, fileSize + 1, MPI_CHAR, masterNode, MPI_COMM_WORLD); | 
| 282 | 
  | 
         | 
| 283 | 
  | 
        internalStringBuf_.str(fbuf); | 
| 284 | 
  | 
        delete [] fbuf; |