ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/io/ifstrstream.cpp
(Generate patch)

Comparing trunk/src/io/ifstrstream.cpp (file contents):
Revision 1879 by gezelter, Sun Jun 16 15:15:42 2013 UTC vs.
Revision 1969 by gezelter, Wed Feb 26 14:14:50 2014 UTC

# Line 47 | Line 47
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
# Line 73 | Line 73 | namespace OpenMD {
73    /**
74     * Explicit constructor
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, default value is ios_base::in      
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).
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
# Line 115 | Line 118 | namespace OpenMD {
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            
# Line 131 | Line 135 | namespace OpenMD {
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            
# Line 157 | Line 161 | namespace OpenMD {
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 <     * @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 <     */
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      
# Line 178 | Line 182 | namespace OpenMD {
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) {
# Line 187 | Line 192 | namespace OpenMD {
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;  
# Line 218 | Line 224 | namespace OpenMD {
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();                    
# Line 229 | Line 235 | namespace OpenMD {
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();      
# Line 237 | Line 243 | namespace OpenMD {
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        
# Line 245 | Line 251 | namespace OpenMD {
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;
# Line 258 | Line 264 | namespace OpenMD {
264          
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines