NeoPZ
TPZFileEqnStorage.cpp
Go to the documentation of this file.
1 
6 #include "TPZFileEqnStorage.h"
7 #include <stdlib.h>
8 
9 #ifdef WIN32
10 //#include <dir.h>
11 #include <io.h>
12 #endif
13 #include "pzlog.h"
14 
15 #ifdef LOG4CXX
16 static LoggerPtr logger(Logger::getLogger("pz.frontal.tpzfileeqnstorage"));
17 #endif
18 #include <fstream>
19 using namespace std;
20 
21 template<class TVar>
27  fNumBlocks++;
28  TPZVec<int64_t> Position(fNumHeaders,0);
33  if(fCurrentBlock==0) {
34  int64_t basepos = ftell(fIOStream);
35  fBlockPos.Push(basepos);
36 #ifdef LOG4CXX
37  if (logger->isDebugEnabled())
38  {
39  std::stringstream sout;
40  sout << "basepos = " << basepos;
41  LOGPZ_DEBUG(logger,sout.str())
42  }
43 #endif
44  }else{
45  int64_t tempaddress = ftell(fIOStream);
46  fBlockPos.Push(tempaddress);
47  }
48 
49  int64_t firstpos = ftell(fIOStream);
50  //if (fCurrentBlock) firstpos =
51 
52 #ifdef LOG4CXX
53  if (logger->isDebugEnabled())
54  {
55  std::stringstream sout;
56  sout << "Writing the position of the headers, numheaders " << fNumHeaders << " position ";
57  for(int i=0; i<fNumHeaders; i++) sout << Position[i] << ' ';
58  LOGPZ_DEBUG(logger,sout.str())
59  }
60 #endif
62  fwrite(Position.begin(),sizeof(int64_t),fNumHeaders,fIOStream);
63 
65  int64_t firstaddress = ftell(fIOStream);
66 
68  fseek(fIOStream,firstpos,SEEK_SET);
69  fwrite(&firstaddress,sizeof(int64_t),1,fIOStream);
70 #ifdef LOG4CXX
71  if (logger->isDebugEnabled())
72  {
73  std::stringstream sout;
74  sout << "At position " << firstpos << " writing the first address " << firstaddress;
75  LOGPZ_DEBUG(logger,sout.str())
76  }
77 #endif
79  fCurBlockPosition = firstaddress;
80 
82 #ifdef LOG4CXX
83  if (logger->isDebugEnabled())
84  {
85  std::stringstream sout;
86  sout << "Setting the file position at " << firstaddress;
87  LOGPZ_DEBUG(logger,sout.str())
88  }
89 #endif
90  fseek(fIOStream,firstaddress,SEEK_SET);
91 
92 }
93 
94 template<class TVar>
95 void TPZFileEqnStorage<TVar>::Store(int ieq, int jeq, const char *name){
96  //Initial tests with C input output files !
97  int loop_limit=100;// = jeq-ieq;
98  int i;
99  FILE *out_file = fopen(name,"wb");
100  double number=2.1;
101  double val = 0;
102  int64_t fPos[5] = {0};
103  int64_t firstpos = ftell(out_file);
104  fwrite(fPos,sizeof(int64_t),5,out_file);
105  double readvec[4][100];
106  int iblock =0;
107  int64_t sizereturn;
108  sizereturn = 0;
109  for(iblock=0; iblock<4; iblock++) {
110  int64_t currentpos = ftell(out_file);
111  fseek(out_file,firstpos+iblock*sizeof(int64_t),SEEK_SET);
112  fwrite(&currentpos,sizeof(int64_t),1,out_file);
113  fseek(out_file,currentpos,SEEK_SET);
114  for(i=0;i<loop_limit;i++){
115  val=number*i*(iblock+1);
116  fwrite(&val, sizeof(double), 1, out_file);
117  }
118  }
119  fclose(out_file);
120  out_file = fopen(name,"rb");
121  sizereturn = fread(fPos,sizeof(int64_t),5,out_file);
122 #ifdef PZDEBUG
123  if (sizereturn != 5) DebugStop();
124 #endif
125  for(iblock = 0; iblock<4; iblock++) {
126  fseek(out_file,fPos[iblock],SEEK_SET);
127  sizereturn = fread(readvec[iblock],sizeof(double),loop_limit,out_file);
128 #ifdef PZDEBUG
129  if (sizereturn != loop_limit) DebugStop();
130 #endif
131  }
132 }
133 
134 template<class TVar>
136 {
137  TPZEqnArray<TVar> REqnArray;
138  for(int64_t i=0;i<fBlockPos.NElements();i++) {
139  if (fBlockPos[i]) {
140  if(fseek(fIOStream,fBlockPos[i],SEEK_SET)){
141  cout << "fseek fail on Element " << i << " Position " << fBlockPos[i] << endl;
142  cout.flush();
143  }
144  int64_t position;
145  if(!fread(&position,sizeof(int64_t),1,fIOStream)){
146  cout << "fread fail on Element " << i << " Position " << position << endl;
147  cout << "EOF " << feof(fIOStream) << endl;
148  cout << "Error Number " << ferror(fIOStream) << endl;
149  cout.flush();
150  }
151  if(fseek(fIOStream,position,SEEK_SET)){
152  cout << "fseek fail on Element " << i << " Position " << position << endl;
153  cout.flush();
154  }
155 
156  REqnArray.Read(fIOStream);
157  REqnArray.EqnForward(f,dec);
158  }
159  }
160 }
161 
162 template<class TVar>
164 {
165  TPZEqnArray<TVar> REqnArray;
166  int i;
167  int64_t sizereturn;
168  sizereturn = 0;
169  for(i=fBlockPos.NElements()-1;i>=0;i--){
170  if (fBlockPos[i]) {
171  fseek(fIOStream,fBlockPos[i],SEEK_SET);
172  int64_t position;
173  sizereturn = fread(&position,sizeof(int64_t),1,fIOStream);
174 #ifdef PZDEBUG
175  if (sizereturn != 1) DebugStop();
176 #endif
177  fseek(fIOStream,position,SEEK_SET);
178  REqnArray.Read(fIOStream);
179  REqnArray.EqnBackward(f,dec);
180  }
181  }
182 }
183 
184 template<class TVar>
186 {
187 
188 }
189 
190 template<class TVar>
191 void TPZFileEqnStorage<TVar>::Print(const char *name, std::ostream& out) const {
192  int i;
193  TPZEqnArray<TVar> REqnArray;
194  out << "Number of entries on File "<< fBlockPos.NElements() << endl;
195  for(i=0;i<fBlockPos.NElements();i++) {
196  if (fBlockPos[i]) {
197  fseek(fIOStream,fBlockPos[i],SEEK_SET);
198  REqnArray.Read(fIOStream);
199  REqnArray.Print(name, out);
200  }
201  }
202 }
203 
204 // Redefine TPZFileEqnStorage so it can handle parallel writeing!!!
205 template<class TVar>
207 {
208 #ifdef LOG4CXX
209  if (logger->isDebugEnabled())
210  {
211  std::stringstream sout;
212  sout << "fCurrentBlock "<< fCurrentBlock << " fNumHeaders " << fNumHeaders;
213  LOGPZ_DEBUG(logger,sout.str())
214  }
215 #endif
216 
217  if(fCurrentBlock%(fNumHeaders-1)==0) {
218 #ifdef LOG4CXX
219  if (logger->isDebugEnabled()) {
220  LOGPZ_DEBUG(logger,"writing headers")
221  }
222 #endif
223  WriteHeaders();
224  // fBlockPos.Push(fBlockPos[fCurrentBlock-1]+sizeof(int64_t));
225 
226  }else if(fCurrentBlock!=0){
227 #ifdef LOG4CXX
228  if (logger->isDebugEnabled())
229  {
230  std::stringstream sout;
231  sout << "fCurrentBlock " << fCurrentBlock << " fBlockPos[fCurrentBlock-1] "<< fBlockPos[fCurrentBlock-1] << fBlockPos;
232  LOGPZ_DEBUG(logger,sout.str())
233  }
234 #endif
235  fBlockPos.Push(fBlockPos[fCurrentBlock-1]+sizeof(int64_t));
236  }
237 
238  EqnArray->Write(fIOStream);
239 
241  int64_t nextaddress=ftell(fIOStream);
242 
247  //fseek(fIOStream,fCurBlockPosition+sizeof(int64_t),SEEK_SET);
248  fseek(fIOStream,fBlockPos[fCurrentBlock]+sizeof(int64_t),SEEK_SET);
249  fCurBlockPosition=ftell(fIOStream);
250  fwrite(&nextaddress,sizeof(int64_t),1,fIOStream);
251 #ifdef LOG4CXX
252  if (logger->isDebugEnabled())
253  {
254  std::stringstream sout;
255  sout << "nextaddress " << nextaddress << " fCurBlockPosition " << fCurBlockPosition;
256  LOGPZ_DEBUG(logger,sout.str())
257  }
258 #endif
259 
260  fseek(fIOStream,nextaddress,SEEK_SET);
261 
262  fCurrentBlock++;
263 }
264 
265 template<class TVar>
266 TPZFileEqnStorage<TVar>::TPZFileEqnStorage(char option, const std::string & name)
267 : TPZRegisterClassId(&TPZFileEqnStorage<TVar>::ClassId)
268 {
269  fCurBlockPosition = -1;
270  fNumBlocks=0;
271  fCurrentBlock=0;
272  fNumHeaders=20;
273  fFileName = name;
274  if(option=='r'){
275  fIOStream = fopen(fFileName.c_str(),"rb"); //open for reading
280  int64_t sizereturn;
281  sizereturn = 0;
282  sizereturn = fread(&fNumHeaders,sizeof(int),1,fIOStream);
283 #ifdef PZDEBUG
284  if (sizereturn != 1) DebugStop();
285 #endif
286  sizereturn = fread(&fNumBlocks,sizeof(int),1,fIOStream);
287 #ifdef PZDEBUG
288  if (sizereturn != 1) DebugStop();
289 #endif
291  }else if(option=='w'){
292  fIOStream = fopen(fFileName.c_str(),"wb"); //open for writing
297  int zero = 0;
298  fwrite(&fNumHeaders,sizeof(int),1,fIOStream);
299  fwrite(&zero,sizeof(int),1,fIOStream);
300  //fCurBlockPosition = ftell(fIOStream);
301  }
302 }
303 
304 template<class TVar>
306 {
307  remove(fFileName.c_str());
308 }
309 
310 template<class TVar>
312 {
313  int Loop_Limit=24;
314  //cout << "Loop_Limit <";
315  //cin >> Loop_Limit;
316  std::string filename;
317  filename = "testbinary.txt\0";
318  TPZFileEqnStorage FileStoreW('w',filename);
319  // FileStoreW.SetBlockSize(10);
320  TPZEqnArray<TVar> EqnArray;
321 
322  ifstream input("MatrizInversa.txt");
323  TVar aux;
324  int i, j;
325  TPZFMatrix<TVar> DecMat(Loop_Limit,Loop_Limit);
326  for(i=0;i<Loop_Limit;i++){
327  for(j=0;j<Loop_Limit;j++){
328  input >> aux;
329  DecMat(i,j) = aux;
330  }
331  }
332 
333  for(i=0;i<Loop_Limit;i++){
334  EqnArray.BeginEquation(i);
335  for(j=i;j<Loop_Limit;j++){
336  // aux = DecMat(i,j);
337  EqnArray.AddTerm(j,DecMat(i,j));
338  }
339  EqnArray.EndEquation();
340  }
341 
342  FileStoreW.AddEqnArray(&EqnArray);
343 
344  FileStoreW.FinishWriting();
345 
346  FileStoreW.ReOpen();
347 
348  ofstream output("testeFileBin.txt");
349 
350  FileStoreW.Print("Teste",output);
351 
352  TPZFMatrix<TVar> f(Loop_Limit,1);
353 
354  for(i=0;i<Loop_Limit;i++) {
355  f(i,0) = (TVar)(float)((i+1.)*2.1/23.);
356  }
357 
358  f.Print("Teste");
359 
360  FileStoreW.Forward(f,ECholesky);
361 
362 }
363 
364 static char filenamestorage[256];
365 
366 template<class TVar>
369 {
370  strcpy(filenamestorage, "/tmp/binary_frontalXXXXXX");
371  int fdtmp = -1;
372 #ifdef WIN32
373  _mktemp(filenamestorage);
374 #else
375  fdtmp = mkstemp(filenamestorage); //returns file description for tmp file
376 #endif
377 
378  fCurBlockPosition = -1;
379  fNumBlocks=0;
380  fCurrentBlock=0;
381  fNumHeaders=20;
382 
384 #ifdef WIN32
385  fIOStream = fopen(filenamestorage,"wb"); //open for writing
386 #else
387  fIOStream = fdopen(fdtmp,"wb"); //open for writing
388 #endif
389 
393  int zero = 0;
394  fwrite(&fNumHeaders,sizeof(int),1,fIOStream);
395  //cout << ftell(fIOStream) << endl;
396  fwrite(&zero,sizeof(int),1,fIOStream);
397 }
398 
399 template<class TVar>
402 {
403  strcpy(filenamestorage, "/tmp/binary_frontalXXXXXX");
404 #ifdef WIN32
405  _mktemp(filenamestorage);
406 #else
407  int fdtmp = -1;
408  fdtmp = mkstemp(filenamestorage); //returns file description for tmp file
409 #endif
410 
411  fCurBlockPosition = -1;
412  fNumBlocks=0;
413  fCurrentBlock=0;
414  fNumHeaders=20;
415 
417 #ifdef WIN32
418  fIOStream = fopen(filenamestorage,"wb"); //open for writing
419 #else
420  fIOStream = fdopen(fdtmp,"wb"); //open for writing
421 #endif
422 
426  int zero = 0;
427  fwrite(&fNumHeaders,sizeof(int),1,fIOStream);
428  //cout << ftell(fIOStream) << endl;
429  fwrite(&zero,sizeof(int),1,fIOStream);
430 }
431 
432 template<class TVar>
434 {
435  if(fIOStream) FinishWriting();
436  remove(fFileName.c_str());
437 
438  strcpy(filenamestorage, "/tmp/binary_frontalXXXXXX");
439 
440  int fdtmp = -1;
441 #ifdef WIN32
442  _mktemp(filenamestorage);
443 #else
444  fdtmp = mkstemp(filenamestorage); //returns file description for tmp file
445 #endif
446  cout << "Temporary file name " << filenamestorage << endl;
447  cout.flush();
448 
449  fBlockPos.Resize(0);
450  fCurBlockPosition = -1;
451  fNumBlocks=0;
452  fCurrentBlock=0;
453  fNumHeaders=20;
454 
456 #ifdef WIN32
457  fIOStream = fopen(filenamestorage,"wb"); //open for writing
458 #else
459  fIOStream = fdopen(fdtmp,"wb"); //open for writing
460 #endif
461 
465  int zero = 0;
466  fwrite(&fNumHeaders,sizeof(int),1,fIOStream);
467  fwrite(&zero,sizeof(int),1,fIOStream);
468 }
469 
470 template<class TVar>
472 {
473 #ifdef LOG4CXX
474  if (logger->isDebugEnabled()) LOGPZ_DEBUG(logger,"reopening the file")
475 #endif
476  fIOStream = fopen(fFileName.c_str(),"rb"); //open for reading
481  int64_t sizereturn;
482  sizereturn = 0;
483  sizereturn = fread(&fNumHeaders,sizeof(int),1,fIOStream);
484 #ifdef PZDEBUG
485  if (sizereturn != 1) DebugStop();
486 #endif
487  sizereturn = fread(&fNumBlocks,sizeof(int),1,fIOStream);
488 #ifdef PZDEBUG
489  if (sizereturn != 1) DebugStop();
490 #endif
491 }
492 
493 template<class TVar>
494 void TPZFileEqnStorage<TVar>::OpenGeneric(char option, const char * name)
495 {
496  fCurBlockPosition = -1;
497  fNumBlocks=0;
498  fCurrentBlock=0;
499  fNumHeaders=11;
500 
501  fFileName = name;
502 
503  if(option=='r'){
504  fIOStream = fopen(fFileName.c_str(),"rb"); //open for reading
509  int64_t sizereturn;
510  sizereturn = 0;
511  sizereturn = fread(&fNumHeaders,sizeof(int),1,fIOStream);
512 #ifdef PZDEBUG
513  if (sizereturn != 1) DebugStop();
514 #endif
515  sizereturn = fread(&fNumBlocks,sizeof(int),1,fIOStream);
516 #ifdef PZDEBUG
517  if (sizereturn != 1) DebugStop();
518 #endif
520  }else if(option=='w'){
521  fIOStream = fopen(fFileName.c_str(),"wb"); //open for writing
526  int zero = 0;
527  fwrite(&fNumHeaders,sizeof(int),1,fIOStream);
528  fwrite(&zero,sizeof(int),1,fIOStream);
529  }
530 }
531 
532 template<class TVar>
534 {
535 #ifdef LOG4CXX
536  if(logger->isDebugEnabled()) LOGPZ_DEBUG(logger,"Closing the binary file")
537 #endif
538  fseek(fIOStream,sizeof(int),SEEK_SET);
539  //cout << "Second fseek " << ftell(fIOStream) << endl;
540  //fwrite(&fNumBlocks,sizeof(int),1,fIOStream);
541  fwrite(&fNumBlocks,sizeof(int),1,fIOStream);
542  fclose(fIOStream);
543  fIOStream = 0;
544 }
545 
546 template<class TVar>
548 {
549 #ifdef LOG4CXX
550  if(logger->isDebugEnabled() ) LOGPZ_DEBUG(logger,"Reading block positions")
551 #endif
552  int aux = fNumBlocks * (fNumHeaders-1);
553  fBlockPos.Resize(aux);
554  int i, ibl = 0;
555  cout << "Reading Block Positions\n";
556  cout.flush();
557  int64_t sizereturn;
558  sizereturn = 0;
559  for(i=0;i<fNumBlocks;i++) {
560  cout << "*";
561  cout.flush();
562  if(!(i%20)){
563  cout << 100*i/fNumBlocks << "% Read\n";
564  cout.flush();
565  }
566  sizereturn = fread(&fBlockPos[ibl],sizeof(int64_t),fNumHeaders-1,fIOStream);
567 #ifdef PZDEBUG
568  if (sizereturn != fNumHeaders-1) DebugStop();
569 #endif
570  ibl+=fNumHeaders-1;
571  int64_t nextpos;
572  sizereturn = fread(&nextpos,sizeof(int64_t),fNumHeaders-1,fIOStream);
573 #ifdef PZDEBUG
574  if (sizereturn != fNumHeaders-1) DebugStop();
575 #endif
576  fseek(fIOStream,nextpos,SEEK_SET);
577  }
578 }
579 
580 template<class TVar>
582  return "File Storage";
583 }
584 
585 template class TPZFileEqnStorage<float>;
587 
588 template class TPZFileEqnStorage<double>;
590 
591 template class TPZFileEqnStorage<long double>;
Has the same porpouse of EqnStack but stores the EqnArrays in a different form (binary files)...
void EqnForward(TPZFMatrix< TVar > &F, DecomposeType dec)
Forward substitution on equations stored in EqnArray.
filename
Definition: stats.py:82
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
void ReadBlockPositions()
Method used for binary input/output.
void Forward(TPZFMatrix< TVar > &f, DecomposeType dec) const
Executes a Forward substitution.
It is an equation array, generally in its decomposed form. Frontal.
Definition: tpzeqnarray.h:36
void Backward(TPZFMatrix< TVar > &f, DecomposeType dec) const
Executes a Backward substitution.
int fNumBlocks
Used with binary input/output aritimethics.
void Write(char *outputfile)
Writes on disk.
Definition: tpzeqnarray.cpp:65
clarg::argString input("-if", "input file", "cube1.txt")
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
void OpenGeneric(char option, const char *name)
Sets file name and if it is for input or output, the second term can be either &#39;r&#39; for input and &#39;w&#39; ...
void BeginEquation(int eq)
It starts an equation storage.
Definition: tpzeqnarray.cpp:75
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
Definition: pzmanvector.h:426
void Print(const char *name, std::ostream &out)
It prints all terms stored in TPZEqnArray.
Definition: tpzeqnarray.cpp:42
void Read(char *inputfile)
Reads from disk.
Definition: tpzeqnarray.cpp:67
std::string fFileName
file name containing binary data
f
Definition: test.py:287
std::string GetStorage()
Type of Storage.
~TPZFileEqnStorage()
Simple destructor.
TPZFileEqnStorage()
Simple constructor.
TPZStack< int64_t > fBlockPos
Stack containing block positions.
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
void EqnBackward(TPZFMatrix< TVar > &U, DecomposeType dec)
Backward substitution on equations stored in EqnArray.
Definition: tpzeqnarray.cpp:93
#define LOGPZ_DEBUG(A, B)
Define log for debug info.
Definition: pzlog.h:87
void AddTerm(int col, TVar val)
Add a term to the current equation.
Definition: tpzeqnarray.h:83
T * begin() const
Casting operator. Returns The fStore pointer.
Definition: pzvec.h:450
void Print(const char *name, std::ostream &out) const
It prints TPZEqnStorage data.
Full matrix class. Matrix.
Definition: pzfmatrix.h:32
Contains the TPZFileEqnStorage class which implements an equation array and stores the EqnArrays...
void Reset()
Resets data.
int fNumHeaders
Indicates the number of headers for the object.
void EndEquation()
Ends the current equation.
Definition: tpzeqnarray.cpp:70
void AddEqnArray(TPZEqnArray< TVar > *EqnArray)
Adds an EqnArray.
static char filenamestorage[256]
FILE * fIOStream
binary file itself
void Store(int ieq, int jeq, const char *name)
Stores from ieq to jeq equations on a binary file.
int fCurrentBlock
Used with binary input/output aritimethics.
virtual void Print(std::ostream &out) const
Definition: pzmatrix.h:253
int ClassId() const override
Define the class id associated with the class.
void WriteHeaders()
Writes the header of the binary file.
void ReOpen()
Reopens an binary file with its current fFileName.
void Zero()
Reinitialize the object.
int fCurBlockPosition
Used with binary input/output aritimethics.
void FinishWriting()
Method used for binary input/output.
DecomposeType
Defines decomposition type for any matrix classes.
Definition: pzmatrix.h:52