Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2019 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "OFile.h"
23 : #include "Exception.h"
24 : #include "core/Action.h"
25 : #include "core/PlumedMain.h"
26 : #include "core/Value.h"
27 : #include "Communicator.h"
28 : #include "Tools.h"
29 : #include <cstdarg>
30 : #include <cstring>
31 :
32 : #include <iostream>
33 : #include <string>
34 : #include <cstdlib>
35 : #include <cerrno>
36 :
37 : #include <memory>
38 : #include <utility>
39 :
40 : #ifdef __PLUMED_HAS_ZLIB
41 : #include <zlib.h>
42 : #endif
43 :
44 : namespace PLMD {
45 :
46 4198478 : size_t OFile::llwrite(const char*ptr,size_t s) {
47 : size_t r;
48 4198478 : if(linked) return linked->llwrite(ptr,s);
49 4198461 : if(! (comm && comm->Get_rank()>0)) {
50 3511563 : if(!fp) plumed_merror("writing on uninitilized File");
51 3511563 : if(gzfp) {
52 : #ifdef __PLUMED_HAS_ZLIB
53 1263 : r=gzwrite(gzFile(gzfp),ptr,s);
54 : #else
55 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
56 : #endif
57 : } else {
58 3510300 : r=fwrite(ptr,1,s,fp);
59 : }
60 : }
61 : // This barrier is apparently useless since it comes
62 : // just before a Bcast.
63 : //
64 : // Anyway, it looks like it is solving an issue that appeared on
65 : // TRAVIS (at least on my laptop) so I add it here.
66 : // GB
67 4198461 : if(comm) comm->Barrier();
68 :
69 :
70 4198461 : if(comm) comm->Bcast(r,0);
71 4198461 : return r;
72 : }
73 :
74 6788 : OFile::OFile():
75 : linked(NULL),
76 : fieldChanged(false),
77 : backstring("bck"),
78 : enforceRestart_(false),
79 13576 : enforceBackup_(false)
80 : {
81 6788 : fmtField();
82 6788 : buflen=1;
83 6788 : actual_buffer_length=0;
84 6788 : buffer.reset(new char [buflen]);
85 : // these are set to zero to avoid valgrind errors
86 13576 : for(int i=0; i<buflen; ++i) buffer[i]=0;
87 6788 : buffer_string.reset(new char [1000]);
88 : // these are set to zero to avoid valgrind errors
89 13576000 : for(unsigned i=0; i<1000; ++i) buffer_string[i]=0;
90 6788 : }
91 :
92 4 : OFile& OFile::link(OFile&l) {
93 4 : fp=NULL;
94 4 : gzfp=NULL;
95 4 : linked=&l;
96 4 : return *this;
97 : }
98 :
99 2726 : OFile& OFile::setLinePrefix(const std::string&l) {
100 2726 : linePrefix=l;
101 2726 : return *this;
102 : }
103 :
104 20540533 : int OFile::printf(const char*fmt,...) {
105 : va_list arg;
106 20540533 : va_start(arg, fmt);
107 20540533 : int r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
108 20540533 : va_end(arg);
109 20540533 : if(r>=buflen-actual_buffer_length) {
110 : int newlen=buflen;
111 27224 : while(newlen<=r+actual_buffer_length) newlen*=2;
112 13456 : std::unique_ptr<char[]> newbuf{new char [newlen]};
113 13456 : memmove(newbuf.get(),buffer.get(),buflen);
114 2137943 : for(int k=buflen; k<newlen; k++) newbuf[k]=0;
115 : buffer=std::move(newbuf);
116 13456 : buflen=newlen;
117 : va_list arg;
118 13456 : va_start(arg, fmt);
119 13456 : r=std::vsnprintf(&buffer[actual_buffer_length],buflen-actual_buffer_length,fmt,arg);
120 13456 : va_end(arg);
121 : }
122 20540533 : plumed_massert(r>-1 && r<buflen-actual_buffer_length,"error using fmt string " + std::string(fmt));
123 :
124 : // Line is buffered until newline, then written with a PLUMED: prefix
125 : char*p1=buffer.get();
126 : char*p2;
127 : // newline is only searched in the just added portion:
128 20540533 : char*psearch=p1+actual_buffer_length;
129 20540533 : actual_buffer_length+=r;
130 45121574 : while((p2=strchr(psearch,'\n'))) {
131 4040508 : if(linePrefix.length()>0) llwrite(linePrefix.c_str(),linePrefix.length());
132 4040508 : llwrite(p1,p2-p1+1);
133 4040508 : actual_buffer_length-=(p2-p1)+1;
134 4040508 : p1=p2+1;
135 : psearch=p1;
136 : };
137 20540533 : if(buffer.get()!=p1) memmove(buffer.get(),p1,actual_buffer_length);
138 20540533 : return r;
139 : }
140 :
141 26870 : OFile& OFile::addConstantField(const std::string&name) {
142 : Field f;
143 : f.name=name;
144 26870 : const_fields.push_back(f);
145 26870 : return *this;
146 : }
147 :
148 :
149 3265 : OFile& OFile::clearFields() {
150 : fields.clear();
151 : const_fields.clear();
152 : previous_fields.clear();
153 3265 : return *this;
154 : }
155 :
156 13182398 : OFile& OFile::fmtField(const std::string&fmt) {
157 13182398 : this->fieldFmt=fmt;
158 13182398 : return *this;
159 : }
160 :
161 12740 : OFile& OFile::fmtField() {
162 12740 : this->fieldFmt="%23.16lg";
163 12740 : return *this;
164 : }
165 :
166 14171434 : OFile& OFile::printField(const std::string&name,double v) {
167 : // When one tries to print -nan we print nan instead.
168 : // The distinction between +nan and -nan is not well defined
169 : // Always printing nan simplifies some regtest (special functions computed our of range).
170 14171434 : if(std::isnan(v)) v=std::numeric_limits<double>::quiet_NaN();
171 : sprintf(buffer_string.get(),fieldFmt.c_str(),v);
172 28342868 : printField(name,buffer_string.get());
173 14171434 : return *this;
174 : }
175 :
176 5905340 : OFile& OFile::printField(const std::string&name,int v) {
177 : sprintf(buffer_string.get()," %d",v);
178 11810680 : printField(name,buffer_string.get());
179 5905340 : return *this;
180 : }
181 :
182 35744803 : OFile& OFile::printField(const std::string&name,const std::string & v) {
183 : unsigned i;
184 397846651 : for(i=0; i<const_fields.size(); i++) if(const_fields[i].name==name) break;
185 35744803 : if(i>=const_fields.size()) {
186 : Field field;
187 : field.name=name;
188 : field.value=v;
189 15231686 : fields.push_back(field);
190 : } else {
191 41026234 : if(const_fields[i].value!=v) fieldChanged=true;
192 : const_fields[i].value=v;
193 : }
194 35744803 : return *this;
195 : }
196 :
197 5119 : OFile& OFile::setupPrintValue( Value *val ) {
198 5119 : if( val->isPeriodic() ) {
199 536 : addConstantField("min_" + val->getName() );
200 536 : addConstantField("max_" + val->getName() );
201 : }
202 5119 : return *this;
203 : }
204 :
205 175407 : OFile& OFile::printField( Value* val, const double& v ) {
206 175407 : printField( val->getName(), v );
207 175407 : if( val->isPeriodic() ) {
208 11026 : std::string min, max; val->getDomain( min, max );
209 22052 : printField( "min_" + val->getName(), min );
210 22052 : printField("max_" + val->getName(), max );
211 : }
212 175407 : return *this;
213 : }
214 :
215 3614256 : OFile& OFile::printField() {
216 : bool reprint=false;
217 7223730 : if(fieldChanged || fields.size()!=previous_fields.size()) {
218 : reprint=true;
219 33993568 : } else for(unsigned i=0; i<fields.size(); i++) {
220 60770208 : if( previous_fields[i].name!=fields[i].name ||
221 0 : (fields[i].constant && fields[i].value!=previous_fields[i].value) ) {
222 : reprint=true;
223 : break;
224 : }
225 : }
226 3614256 : if(reprint) {
227 5790 : printf("#! FIELDS");
228 89856 : for(unsigned i=0; i<fields.size(); i++) printf(" %s",fields[i].name.c_str());
229 5790 : printf("\n");
230 65208 : for(unsigned i=0; i<const_fields.size(); i++) {
231 26814 : printf("#! SET %s %s",const_fields[i].name.c_str(),const_fields[i].value.c_str());
232 26814 : printf("\n");
233 : }
234 : }
235 34077628 : for(unsigned i=0; i<fields.size(); i++) printf("%s",fields[i].value.c_str());
236 3614256 : printf("\n");
237 3614256 : previous_fields=fields;
238 : fields.clear();
239 3614256 : fieldChanged=false;
240 3614256 : return *this;
241 : }
242 :
243 119 : void OFile::setBackupString( const std::string& str ) {
244 119 : backstring=str;
245 119 : }
246 :
247 36 : void OFile::backupAllFiles( const std::string& str ) {
248 36 : if(str=="/dev/null") return;
249 72 : plumed_assert( backstring!="bck" && !checkRestart());
250 36 : size_t found=str.find_last_of("/\\");
251 72 : std::string filename = appendSuffix(str,getSuffix());
252 36 : std::string directory=filename.substr(0,found+1);
253 36 : std::string file=filename.substr(found+1);
254 36 : if( FileExist(filename) ) backupFile("bck", filename);
255 0 : for(int i=0;; i++) {
256 36 : std::string num; Tools::convert(i,num);
257 180 : std::string filestr = directory + backstring + "." + num + "." + file;
258 36 : if( !FileExist(filestr) ) break;
259 0 : backupFile( "bck", filestr);
260 0 : }
261 : }
262 :
263 2996 : void OFile::backupFile( const std::string& bstring, const std::string& fname ) {
264 3218 : if(fname=="/dev/null") return;
265 2774 : int maxbackup=100;
266 2774 : if(std::getenv("PLUMED_MAXBACKUP")) Tools::convert(std::getenv("PLUMED_MAXBACKUP"),maxbackup);
267 2774 : if(maxbackup>0 && (!comm || comm->Get_rank()==0)) {
268 2350 : FILE* ff=std::fopen(const_cast<char*>(fname.c_str()),"r");
269 2350 : if(ff) {
270 58 : std::fclose(ff);
271 : std::string backup;
272 58 : size_t found=fname.find_last_of("/\\");
273 58 : std::string directory=fname.substr(0,found+1);
274 58 : std::string file=fname.substr(found+1);
275 1 : for(int i=0;; i++) {
276 : std::string num;
277 59 : Tools::convert(i,num);
278 59 : if(i>maxbackup) plumed_merror("cannot backup file "+file+" maximum number of backup is "+num+"\n");
279 354 : backup=directory+bstring +"."+num+"."+file;
280 59 : FILE* fff=std::fopen(backup.c_str(),"r");
281 59 : if(!fff) break;
282 1 : else std::fclose(fff);
283 1 : }
284 58 : int check=rename(fname.c_str(),backup.c_str());
285 58 : plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for reason: "+strerror(errno));
286 : }
287 : }
288 : }
289 :
290 3265 : OFile& OFile::open(const std::string&path) {
291 3265 : plumed_assert(!cloned);
292 3265 : eof=false;
293 3265 : err=false;
294 3265 : fp=NULL;
295 3265 : gzfp=NULL;
296 3265 : this->path=path;
297 9795 : this->path=appendSuffix(path,getSuffix());
298 3265 : if(checkRestart()) {
299 538 : fp=std::fopen(const_cast<char*>(this->path.c_str()),"a");
300 269 : mode="a";
301 538 : if(Tools::extension(this->path)=="gz") {
302 : #ifdef __PLUMED_HAS_ZLIB
303 48 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"a9");
304 : #else
305 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
306 : #endif
307 : }
308 : } else {
309 2996 : backupFile( backstring, this->path );
310 2996 : if(comm)comm->Barrier();
311 5992 : fp=std::fopen(const_cast<char*>(this->path.c_str()),"w");
312 2996 : mode="w";
313 5992 : if(Tools::extension(this->path)=="gz") {
314 : #ifdef __PLUMED_HAS_ZLIB
315 2 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"w9");
316 : #else
317 : plumed_merror("file " + getPath() + ": trying to use a gz file without zlib being linked");
318 : #endif
319 : }
320 : }
321 3265 : if(plumed) plumed->insertFile(*this);
322 3265 : return *this;
323 : }
324 :
325 114 : OFile& OFile::rewind() {
326 : // we use here "hard" rewind, which means close/reopen
327 : // the reason is that normal rewind does not work when in append mode
328 : // moreover, we can take a backup of the file
329 114 : plumed_assert(fp);
330 114 : clearFields();
331 114 : if(gzfp) {
332 : #ifdef __PLUMED_HAS_ZLIB
333 15 : gzclose((gzFile)gzfp);
334 : #endif
335 99 : } else fclose(fp);
336 114 : if(!comm || comm->Get_rank()==0) {
337 92 : std::string fname=this->path;
338 92 : size_t found=fname.find_last_of("/\\");
339 92 : std::string directory=fname.substr(0,found+1);
340 92 : std::string file=fname.substr(found+1);
341 276 : std::string backup=directory+backstring +".last."+file;
342 92 : int check=rename(fname.c_str(),backup.c_str());
343 92 : plumed_massert(check==0,"renaming "+fname+" into "+backup+" failed for reason: "+strerror(errno));
344 : }
345 114 : if(gzfp) {
346 : #ifdef __PLUMED_HAS_ZLIB
347 15 : gzfp=(void*)gzopen(const_cast<char*>(this->path.c_str()),"w9");
348 : #endif
349 99 : } else fp=std::fopen(const_cast<char*>(path.c_str()),"w");
350 114 : return *this;
351 : }
352 :
353 7928 : FileBase& OFile::flush() {
354 7928 : if(heavyFlush) {
355 3836 : if(gzfp) {
356 : #ifdef __PLUMED_HAS_ZLIB
357 9 : gzclose(gzFile(gzfp));
358 18 : gzfp=(void*)gzopen(const_cast<char*>(path.c_str()),"a");
359 : #endif
360 : } else {
361 3827 : fclose(fp);
362 7654 : fp=std::fopen(const_cast<char*>(path.c_str()),"a");
363 : }
364 : } else {
365 4092 : FileBase::flush();
366 : // if(gzfp) gzflush(gzFile(gzfp),Z_FINISH);
367 : // for some reason flushing with Z_FINISH has problems on linux
368 : // I thus use this (incomplete) flush
369 : #ifdef __PLUMED_HAS_ZLIB
370 4092 : if(gzfp) gzflush(gzFile(gzfp),Z_FULL_FLUSH);
371 : #endif
372 : }
373 7928 : return *this;
374 : }
375 :
376 3301 : bool OFile::checkRestart()const {
377 3301 : if(enforceRestart_) return true;
378 3300 : else if(enforceBackup_) return false;
379 3992 : else if(action) return action->getRestart();
380 186 : else if(plumed) return plumed->getRestart();
381 : else return false;
382 : }
383 :
384 1 : OFile& OFile::enforceRestart() {
385 1 : enforceRestart_=true;
386 1 : enforceBackup_=false;
387 1 : return *this;
388 : }
389 :
390 1211 : OFile& OFile::enforceBackup() {
391 1211 : enforceBackup_=true;
392 1211 : enforceRestart_=false;
393 1211 : return *this;
394 : }
395 :
396 :
397 5874 : }
|