Matrix.h
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/rp/Matrix.h
22  \brief Generic template matrix.
23  */
24 
25 #ifndef __TERRALIB_RP_INTERNAL_MATRIX_H
26 #define __TERRALIB_RP_INTERNAL_MATRIX_H
27 
28  #include "Macros.h"
29 
30  #include "../common/PlatformUtils.h"
31 
32  #include <cstring>
33  #include <cmath>
34  #include <cstdio>
35  #include <string>
36  #include <vector>
37  #include <memory>
38 
39  #include <boost/scoped_array.hpp>
40  #include <boost/filesystem.hpp>
41 
42  namespace te
43  {
44  namespace rp
45  {
46  /*!
47  \class Matrix
48  \brief A generic template matrix
49  */
50  template< typename TemplateElementType >
51  class Matrix {
52  public :
53 
54  /*!
55  \brief Public matrix element type definition.
56  */
57  typedef TemplateElementType ElementTypeT;
58 
59  /*!
60  \brief Memory polycy.
61  */
63  {
64  /*!
65  \details Automatic memory policy ( Try to use RAM or DISK, if there is no
66  avaliable RAM ) DO NOT USE AutoMemPol FOR COMPLEX DATA TYPES.
67  */
69  /*!
70  \details RAM memory policy.
71  */
73  /*!
74  \details Disk memory policy ( virtual mapped memory ) -
75  DO NOT USE DiskMemPol FOR COMPLEX DATA TYPES.
76  */
78  };
79 
80  Matrix();
81 
82  Matrix( const Matrix< TemplateElementType >& external );
83 
84  virtual ~Matrix();
85 
86  /*!
87  \brief Reset (clear) the active instance data.
88  \note Other parameters will not be changed.
89 
90  \param memoryPolicy Memory policy.
91  */
92  void reset();
93 
94  /*!
95  \brief Reset (clear) the active instance data and update its
96  internal parameters folowing the the new supplied parameters.
97  \note Other parameters will not be changed.
98 
99  \param memoryPolicy Memory policy.
100  */
101  void reset( MemoryPolicy memoryPolicy );
102 
103  /*!
104  \brief Reset (clear) the active instance data and update its
105  internal parameters folowing the the new supplied parameters.
106  \note Other parameters will not be changed.
107  \param lines Number of lines.
108  \param columns Number of columns.
109  \return true if OK, false on error.
110  */
111  bool reset( unsigned int lines, unsigned int columns );
112 
113  /*!
114  \brief Reset (clear) the active instance data and update its
115  internal parameters folowing the the new supplied parameters.
116  \note Other parameters will not be changed.
117 
118  \param lines Number of lines.
119  \param columns Number of columns.
120  \param memoryPolicy Memory policy.
121  \return true if OK, false on error.
122  */
123  bool reset( unsigned int lines, unsigned int columns,
124  MemoryPolicy memoryPolicy );
125 
126  /*!
127  \brief Reset (clear) the active instance data and update its
128  internal parameters folowing the the new supplied parameters.
129  \note Other parameters will not be changed.
130 
131  \param lines Number of lines.
132  \param columns Number of columns.
133  \param memoryPolicy Memory policy.
134  \param maxMemPercentUsage The max amount of free memory to use
135  when necessary (suggested default:40).
136  \return true if OK, false on error.
137  */
138  bool reset( unsigned int lines, unsigned int columns,
139  MemoryPolicy memoryPolicy, unsigned char maxMemPercentUsage );
140 
141  /*!
142  \brief Reset (clear) the active instance data and update its
143  internal parameters folowing the the new supplied parameters.
144  \note Other parameters will not be changed.
145 
146  \param lines Number of lines.
147  \param columns Number of columns.
148  \param memoryPolicy Memory policy.
149  \param maxTmpFileSize Max temp file size.
150  \param maxMemPercentUsage The max amount of free memory to use
151  when necessary (suggested default:40).
152  \return true if OK, false on error.
153  */
154  bool reset( unsigned int lines, unsigned int columns,
155  MemoryPolicy memoryPolicy, unsigned long int maxTmpFileSize,
156  unsigned char maxMemPercentUsage );
157 
158  /*!
159  \brief Clear all allocated resources and go back to the initial
160  default parameters.
161  */
162  void clear();
163 
164  /*!
165  \brief The number of current matrix lines.
166 
167  \return The number of current matrix lines.
168  */
169  unsigned int getLinesNumber() const;
170 
171  /*!
172  \brief The number of current matrix columns.
173 
174  \return The number of current matrix columns
175  */
176  unsigned int getColumnsNumber() const;
177 
178  /*!
179  \brief Empty Matrix verification.
180 
181  \return true if the matrix is empty.
182  */
183  bool isEmpty() const;
184 
185  /*!
186  \brief Operator = overload.
187 
188  \note The external memory policy will be used as reference.
189 
190  \param external External instance reference.
191  \return A reference to the current matrix.
192  */
194  const Matrix< TemplateElementType >& external );
195 
196  /*!
197  \brief Operator () overload.
198 
199  \param line Line number.
200  \param column Column number.
201  \return A reference to the required element.
202  */
203  inline TemplateElementType& operator()(
204  const unsigned int& line,
205  const unsigned int& column )
206  {
208  "Invalid columns" )
209 
210  return getScanLine( line )[ column ];
211  };
212 
213  /*!
214  \brief Operator () overload.
215 
216  \param line Line number.
217  \param column Column number.
218  \return A const reference to the required element.
219  */
220  inline const TemplateElementType& operator()(
221  const unsigned int& line,
222  const unsigned int& column ) const
223  {
225  "Invalid columns" )
226 
227  return getScanLine( line )[ column ] ;
228  };
229 
230  /*!
231  \brief Operator [] overload.
232 
233  \param line Line number.
234  \param column Column number.
235  \return A pointer to the required line.
236  \note The returned pointer is garanteed to
237  be valid until an acess to another line occurs.
238  \note Concurrent thread access to this method is guaranteed
239  if RAMMemPol policy method is used.
240  */
241  inline TemplateElementType* operator[](
242  const unsigned int& line )
243  {
244  return getScanLine( line );
245  };
246 
247  /*!
248  \brief Operator [] overload.
249 
250  \param line Line number.
251  \param column Column number.
252  \return A pointer to the required line.
253  \note The returned pointer is garanteed to
254  be valid until an acess to another line occurs.
255  \note Concurrent thread access to this method is guaranteed
256  if RAMMemPol policy method is used.
257  */
258  inline TemplateElementType const* operator[](
259  const unsigned int& line ) const
260  {
261  return getScanLine( line );
262  };
263 
264  /*!
265  \brief Returns the current memory policy.
266  \return The current memory policy.
267  */
268  MemoryPolicy getMemPolicy() const;
269 
270  /*!
271  \brief Returns the current maximum temporary disk file size.
272  \return Returns the current maximum temporary disk file size.
273  */
274  unsigned long int getMaxTmpFileSize() const;
275 
276  /*!
277  \brief Returns the max amount of free memory to use when necessary.
278  \return Returns the max amount of free memory to use when necessary.
279  */
280  unsigned char getMaxMemPercentUsage() const;
281 
282  protected :
283 
284  /*!
285  \brief Disk lines info.
286  */
288  {
289  public :
290 
291  FILE* m_filePtr; //!< The file pointer.
292 
293  unsigned int m_fileOff; //!< The offset within the file.
294 
295  DiskLineInfo();
296 
297  ~DiskLineInfo();
298  };
299 
300  /*!
301  \brief Openend disk files info node.
302  */
304  {
305  public :
306 
307  FILE* m_filePtr; //!< The file pointer.
308 
309  std::string m_fullFileName; //!< The full created file name.
310 
312 
314  };
315 
316  /*!
317  \brief Max bytes per temp file (for swapped tiles only, default: 2GB).
318  */
319  unsigned long int m_maxTmpFileSize;
320 
321  /*!
322  \brief the max amount of free memory to use when necessary.
323  */
324  unsigned char m_maxMemPercentUsage;
325 
326  /*!
327  \brief The total lines number (default:0).
328  */
329  unsigned int m_totalLines;
330 
331  /*!
332  \brief The total columns number (default:0).
333  */
334  unsigned int m_totalColumns;
335 
336  /*!
337  \brief The used memory policy (default:RAMMemPol).
338  */
340 
341  /*!
342  \brief The index inside ramLinesIndexesVec_ of the next RAM
343  line index that will be swapped to disk when a disk line
344  is required.
345  */
346  mutable unsigned int m_nextSwapLineRamLinesIndexesVecIdx;
347 
348  /*!
349  \brief A pointer to the current swap tile.
350  */
351  mutable TemplateElementType* m_currentSwapTilePtr;
352 
353  std::vector< TemplateElementType* > m_memoryblocksHandler;
354 
355  /*!
356  \brief A vector with pointers to all lines.
357  */
358  mutable std::vector< TemplateElementType* > m_allLinesPtrsVec;
359 
360  /*!
361  \brief A vector with open disk files handler.
362  */
363  std::vector< OpenDiskFileHandler > m_openDiskFilesHandler;
364 
365  /*!
366  \brief Info of all lines, used when a line is on disk.
367  */
368  mutable std::vector< DiskLineInfo > m_diskLinesInfoVec;
369 
370  /*!
371  \brief The indexes of all lines loaded into RAM.
372  */
373  mutable std::vector< unsigned int > m_ramLinesIndexesVec;
374 
375  /*!
376  \brief An auxiliar line used when swapping
377  data to/from disk.
378  */
379  mutable boost::scoped_array< TemplateElementType > m_swapMemoryBlockHandler;
380 
381  /*!
382  \brief Reset the internal variables to the initial state.
383  */
384  void init();
385 
386  /*!
387  \brief Allocate disk lines.
388  \param startingLineIdx Starting line index.
389  \return true if OK, false on errors.
390  */
391  bool allocateDiskLines( unsigned int startingLineIdx );
392 
393  /*!
394  \brief Returns a pointer to the required line.
395 
396  \param line Line number.
397  \param column Column number.
398  \return A pointer to the required line.
399  \note The returned pointer is garanteed to
400  be valid until an acess to another line occurs.
401  \note Concurrent thread access to this method is guaranteed
402  if RAMMemPol policy method is used.
403  */
404  TemplateElementType* getScanLine(
405  const unsigned int& line ) const;
406 
407  /*!
408  \brief Create a new disk file.
409  \param size The file size.
410  \param fileptr The file pointer.
411  \param fullFileName The created full file name.
412  \return true if OK. false on errors.
413  */
414  bool createNewDiskFile( unsigned long int size, FILE** fileptr,
415  std::string& fullFileName ) const;
416  };
417 
418  template< typename TemplateElementType >
420  {
421  m_filePtr = 0;
422  m_fileOff = 0;
423  }
424 
425  template< typename TemplateElementType >
427  {
428  }
429 
430  template< typename TemplateElementType >
432  {
433  m_filePtr = 0;
434  }
435 
436  template< typename TemplateElementType >
438  {
439  if( m_filePtr )
440  {
441  remove( m_fullFileName.c_str() );
442  fclose( m_filePtr );
443  }
444  }
445 
446  template< typename TemplateElementType >
448  {
449  m_maxTmpFileSize = 2ul * 1024ul * 1024ul * 1024ul;;
451  m_totalLines = 0;
452  m_totalColumns = 0;
456  }
457 
458  template< typename TemplateElementType >
460  {
461  init();
462  }
463 
464  template< typename TemplateElementType >
466  const Matrix< TemplateElementType >& external )
467  {
468  init();
469 
470  operator=( external );
471  }
472 
473  template< typename TemplateElementType >
475  {
476  clear();
477  }
478 
479  template< typename TemplateElementType >
481  {
482  clear();
483  }
484 
485  template< typename TemplateElementType >
487  {
488  reset( 0, 0, memoryPolicy, m_maxTmpFileSize, m_maxMemPercentUsage );
489  }
490 
491  template< typename TemplateElementType >
492  bool Matrix< TemplateElementType >::reset( unsigned int lines,
493  unsigned int columns )
494  {
495  return reset( lines, columns, m_memoryPolicy, m_maxTmpFileSize,
497  }
498 
499  template< typename TemplateElementType >
500  bool Matrix< TemplateElementType >::reset( unsigned int lines,
501  unsigned int columns, MemoryPolicy memoryPolicy )
502  {
503  return reset( lines, columns, memoryPolicy, m_maxTmpFileSize,
505  }
506 
507  template< typename TemplateElementType >
508  bool Matrix< TemplateElementType >::reset( unsigned int lines,
509  unsigned int columns, MemoryPolicy memoryPolicy,
510  unsigned char maxMemPercentUsage )
511  {
512  return reset( lines, columns, memoryPolicy, m_maxTmpFileSize,
513  maxMemPercentUsage );
514  }
515 
516  template< typename TemplateElementType >
517  bool Matrix< TemplateElementType >::reset( unsigned int lines,
518  unsigned int columns, MemoryPolicy memoryPolicy,
519  unsigned long int maxTmpFileSize,
520  unsigned char maxMemPercentUsage )
521  {
522  clear();
523 
524  // Updating the global vars
525 
526  m_maxTmpFileSize = maxTmpFileSize;
527  m_maxMemPercentUsage = maxMemPercentUsage;
528  m_memoryPolicy = memoryPolicy;
529 
530  /* Update the old buffer if necessary */
531 
532  if( ( lines > 0 ) && ( columns > 0 ) )
533  {
534  try
535  {
536  // Updating the global vars
537 
538  m_totalLines = lines;
539  m_totalColumns = columns;
540 
541  const unsigned int lineSizeBytes = (unsigned int)(
542  sizeof( TemplateElementType ) * m_totalColumns );
543 
544  // Allocating the lines pointers vectpr
545 
546  m_allLinesPtrsVec.resize( m_totalLines, 0 );
547 
548  if( m_memoryPolicy == RAMMemPol )
549  {
550  m_ramLinesIndexesVec.resize( m_totalLines, 0 );
552 
553  TemplateElementType* newLinePtr = 0;
554 
555  for( unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
556  m_totalLines ; ++allLinesPtrsVecIdx )
557  {
558  newLinePtr = new TemplateElementType[ m_totalColumns ];
559 
560  m_memoryblocksHandler[ allLinesPtrsVecIdx ] = newLinePtr;
561 
562  m_allLinesPtrsVec[ allLinesPtrsVecIdx ] = newLinePtr;
563 
564  m_ramLinesIndexesVec[ allLinesPtrsVecIdx ] = allLinesPtrsVecIdx;
565  }
566  }
567  else
568  { // AutoMemPol, DiskMemPol
569 
570  // Allocating the swap line pointer
571 
572  m_swapMemoryBlockHandler.reset( new TemplateElementType[ m_totalColumns ] );
574 
575  // Defining the number of max RAM lines
576 
577  unsigned int maxRAMLines = 1;
578 
579  if( m_memoryPolicy == AutoMemPol )
580  {
581  // Defining the max number of RAM tiles
582 
583  const double totalPhysMem = (double)te::common::GetTotalPhysicalMemory();
584 
585  const double usedVMem = (double)te::common::GetUsedVirtualMemory();
586 
587  const double totalVMem = (double)te::common::GetTotalVirtualMemory();
588 
589  const double freeVMem = MIN( totalPhysMem,
590  ( totalVMem - usedVMem ) );
591 
592  const double linesMem = ( ((double)m_maxMemPercentUsage) / 100.0 ) *
593  freeVMem;
594 
595  maxRAMLines = (unsigned int)MAX( 1.0, linesMem /
596  ((double)lineSizeBytes) );
597  }
598 
599  // Allocating RAM lines
600 
601  const unsigned int ramLinesIndexesVecSize = MIN( maxRAMLines,
602  m_totalLines );
603 
604  m_ramLinesIndexesVec.resize( ramLinesIndexesVecSize, 0 );
605  m_memoryblocksHandler.resize( ramLinesIndexesVecSize );
606 
607  TemplateElementType* newLinePtr = 0;
608 
609  for( unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
610  ramLinesIndexesVecSize ; ++allLinesPtrsVecIdx )
611  {
612  newLinePtr = new TemplateElementType[ m_totalColumns ];
613 
614  m_memoryblocksHandler[ allLinesPtrsVecIdx ] = newLinePtr;
615 
616  m_allLinesPtrsVec[ allLinesPtrsVecIdx ] = newLinePtr;
617 
618  m_ramLinesIndexesVec[ allLinesPtrsVecIdx ] = allLinesPtrsVecIdx;
619  }
620 
621  // Allocating Disk lines
622 
623  if( ! allocateDiskLines( ramLinesIndexesVecSize ) )
624  {
625  clear();
626  TERP_LOG_AND_RETURN_FALSE( "Tiles allocation error" )
627  }
628  }
629  }
630  catch(...)
631  {
632  clear();
633 
634  TERP_LOG_AND_RETURN_FALSE( "Tiles allocation error" )
635  }
636  }
637 
638  return true;
639  }
640 
641  template< typename TemplateElementType >
643  {
644  TemplateElementType* linePtr = 0;
645  for( unsigned int memoryblocksHandlerIdx = 0 ; memoryblocksHandlerIdx <
646  m_memoryblocksHandler.size() ; ++memoryblocksHandlerIdx )
647  {
648  linePtr = m_memoryblocksHandler[ memoryblocksHandlerIdx ];
649 
650  if( linePtr )
651  {
652  delete[] ( linePtr );
653  }
654  }
655  m_memoryblocksHandler.clear();
656 
657  m_allLinesPtrsVec.clear();
658 
659  m_openDiskFilesHandler.clear();
660 
661  m_diskLinesInfoVec.clear();
662 
663  m_ramLinesIndexesVec.clear();
664 
665  m_swapMemoryBlockHandler.reset();
666 
667  init();
668  }
669 
670  template< typename TemplateElementType >
672  {
673  return m_totalLines;
674  }
675 
676 
677  template< typename TemplateElementType >
679  {
680  return m_totalColumns;
681  }
682 
683 
684  template< typename TemplateElementType >
686  {
687  return ( m_totalLines == 0 ) ? true : false;
688  }
689 
690  template< typename TemplateElementType >
692  const Matrix< TemplateElementType >& external )
693  {
695  reset( external.m_totalLines, external.m_totalColumns,
697  "Unable to initiate the matrix object" );
698 
699  unsigned int column = 0;;
700  TemplateElementType const* inLinePtr = 0;
701  TemplateElementType* outLinePtr = 0;
702 
703  for( unsigned int line = 0 ; line < m_totalLines ; ++line )
704  {
705  inLinePtr = external.getScanLine( line );
706  outLinePtr = getScanLine( line );
707 
708  for( column = 0 ; column < m_totalColumns ; ++column ) {
709  outLinePtr[ column ] = inLinePtr[ column ];
710  }
711  }
712 
713  return *this;
714  }
715 
716  template< typename TemplateElementType >
718  {
719  return m_memoryPolicy;
720  }
721 
722  template< typename TemplateElementType >
724  {
725  return m_maxTmpFileSize;
726  }
727 
728  template< typename TemplateElementType >
730  {
731  return m_maxMemPercentUsage;
732  }
733 
734  template< typename TemplateElementType >
735  bool Matrix< TemplateElementType >::allocateDiskLines( unsigned int startingLineIdx )
736  {
737  const unsigned long int diskLinesNmb = m_totalLines - startingLineIdx;
738 
739  if( diskLinesNmb )
740  {
741  const unsigned long int lineSizeBytes = (unsigned long int)(
742  sizeof( TemplateElementType ) * m_totalColumns );
743 
744  const unsigned long int maxLinesPerFile = ( unsigned long int )
745  floor( ( (double)m_maxTmpFileSize ) / ( (double) lineSizeBytes ) );
746 
747  const unsigned long int maxFileSize = (unsigned long int)
748  ( maxLinesPerFile * lineSizeBytes );
749 
750  const unsigned int openDiskFilesNumber = (unsigned int)ceil( ((double)diskLinesNmb)
751  / ((double)maxLinesPerFile) );
752 
753  // Allocating each file
754 
756 
757  unsigned int remainingLinesNmb = diskLinesNmb;
758  unsigned int fileSize = 0;
759  unsigned int fileLinesNumber = 0;
760  unsigned int diskLinesVecIdx = startingLineIdx;
761 
762  for( unsigned int fileIdx = 0 ; fileIdx < openDiskFilesNumber ;
763  ++fileIdx )
764  {
765  // Defining the current file size
766 
767  fileSize = maxFileSize;
768  fileLinesNumber = maxLinesPerFile;
769 
770  if( remainingLinesNmb < maxLinesPerFile )
771  {
772  fileSize = (unsigned long int)( lineSizeBytes * remainingLinesNmb );
773  fileLinesNumber = remainingLinesNmb;
774  }
775 
776  remainingLinesNmb -= fileLinesNumber;
777 
778  // allocating the temporary file
779 
780  FILE* newFilePtr = 0;
781  std::string newFullFileName;
782 
783  if( ! createNewDiskFile( fileSize, &( newFilePtr ), newFullFileName ) )
784  {
785  TERP_LOGERR( "Unable to create temporary disk file" );
786  return false;
787  }
788  else
789  {
791  m_openDiskFilesHandler.back().m_filePtr = newFilePtr;
792  m_openDiskFilesHandler.back().m_fullFileName = newFullFileName;
793 
794  for( unsigned int lineIdx = 0; lineIdx < fileLinesNumber ; ++lineIdx )
795  {
796  m_diskLinesInfoVec[ diskLinesVecIdx ].m_filePtr = newFilePtr;
797  m_diskLinesInfoVec[ diskLinesVecIdx ].m_fileOff = lineIdx *
798  lineSizeBytes;
799 
800  ++diskLinesVecIdx;
801  }
802  }
803  }
804  }
805 
806  return true;
807  }
808 
809  template< typename TemplateElementType >
810  TemplateElementType*
811  Matrix< TemplateElementType >::getScanLine( const unsigned int& line ) const
812  {
813  TERP_DEBUG_TRUE_OR_THROW( line < m_totalLines, "Invalid tile index" );
814 
815  if( m_allLinesPtrsVec[ line ] )
816  {
817  return m_allLinesPtrsVec[ line ];
818  }
819  else
820  {
821  // Finding the swap line index
822 
824  m_ramLinesIndexesVec.size(), "Internal error" );
825  const unsigned int swapLineIdx = m_ramLinesIndexesVec[
827 
828  TERP_DEBUG_TRUE_OR_THROW( line < m_diskLinesInfoVec.size(), "Internal error" );
829  DiskLineInfo& inLineData = m_diskLinesInfoVec[ line ];
830 
831  TERP_DEBUG_TRUE_OR_THROW( swapLineIdx < m_diskLinesInfoVec.size(),
832  "Internal error" )
833  DiskLineInfo& outLineData = m_diskLinesInfoVec[
834  swapLineIdx ];
835 
836  /* Reading the required tile into RAM */
837 
838  TERP_DEBUG_TRUE_OR_THROW( inLineData.m_filePtr, "Internal error" );
839  TERP_TRUE_OR_THROW( 0 == fseek( inLineData.m_filePtr,
840  (long)( inLineData.m_fileOff ), SEEK_SET ),
841  "File seek error" )
842 
843  TERP_DEBUG_TRUE_OR_THROW( m_currentSwapTilePtr, "Internal error" );
844  TERP_TRUE_OR_THROW( 1 == fread( (void*)m_currentSwapTilePtr,
845  (size_t)( sizeof( TemplateElementType ) * m_totalColumns ), 1, inLineData.m_filePtr ),
846  "File read error" )
847 
848  /* Flushing the choosed tile to disk */
849 
850  TERP_TRUE_OR_THROW( 0 == fseek( inLineData.m_filePtr,
851  (long)( inLineData.m_fileOff ), SEEK_SET ),
852  "File seek error" );
853 
854  TERP_DEBUG_TRUE_OR_THROW( swapLineIdx < m_allLinesPtrsVec.size(),
855  "Internal error" );
856  TERP_TRUE_OR_THROW( 1 == fwrite( (void*)m_allLinesPtrsVec[
857  swapLineIdx ],
858  (size_t)( sizeof( TemplateElementType ) * m_totalColumns ), 1,
859  inLineData.m_filePtr ), "File write error" )
860 
861  // Updating the tile pointers
862 
863  TemplateElementType* linePtr = m_allLinesPtrsVec[ swapLineIdx ];
864 
865  m_allLinesPtrsVec[ swapLineIdx ] = 0;
866 
868 
869  m_currentSwapTilePtr = linePtr;
870 
871  /* Updating the info vectors */
872 
873  outLineData.m_filePtr = inLineData.m_filePtr;
874  outLineData.m_fileOff = inLineData.m_fileOff;
875 
876  inLineData.m_filePtr = 0;
877  inLineData.m_fileOff = 0;
878 
882  m_ramLinesIndexesVec.size() );
884  m_ramLinesIndexesVec.size(), "Internal error" );
885 
886  return m_allLinesPtrsVec[ line ];
887  }
888  }
889 
890  template< typename TemplateElementType >
892  FILE** fileptr, std::string& fullFileName ) const
893  {
894  fullFileName = boost::filesystem::unique_path(
895  boost::filesystem::temp_directory_path() /=
896  boost::filesystem::path( "TerralibRPMatrix_%%%%-%%%%-%%%%-%%%%" ) ).string();
897  if( fullFileName.empty() )
898  {
899  return false;
900  }
901 
902  (*fileptr) = fopen( fullFileName.c_str(), "wb+" );
903  TERP_TRUE_OR_RETURN_FALSE( (*fileptr) != 0, "Invalid file pointer" )
904 
905  long seekoff = (long)( size - 1 );
906 
907  if( 0 != fseek( (*fileptr), seekoff, SEEK_SET ) )
908  {
909  fclose( (*fileptr) );
910  remove( fullFileName.c_str() );
911  TERP_LOGERR( "File seek error" );
912  return false;
913  }
914 
915  unsigned char c = '\0';
916  if( 1 != fwrite( &c, 1, 1, (*fileptr) ) )
917  {
918  fclose( (*fileptr) );
919  remove( fullFileName.c_str() );
920  TERP_LOGERR( "File write error" );
921  return false;
922  }
923 
924  return true;
925  }
926  } //namespace rp
927  } // namespace te
928 #endif //__TERRALIB_RP_INTERNAL_MATRIX_H
929 
void clear()
Clear all allocated resources and go back to the initial default parameters.
Definition: Matrix.h:642
unsigned int m_fileOff
The offset within the file.
Definition: Matrix.h:293
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
FILE * m_filePtr
The file pointer.
Definition: Matrix.h:291
unsigned char m_maxMemPercentUsage
the max amount of free memory to use when necessary.
Definition: Matrix.h:324
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
unsigned char getMaxMemPercentUsage() const
Returns the max amount of free memory to use when necessary.
Definition: Matrix.h:729
std::vector< DiskLineInfo > m_diskLinesInfoVec
Info of all lines, used when a line is on disk.
Definition: Matrix.h:368
#define MIN(a, b)
Macro that returns min between two values.
#define TERP_LOGERR(message)
Logs an error message.
Definition: Macros.h:116
const TemplateElementType & operator()(const unsigned int &line, const unsigned int &column) const
Operator () overload.
Definition: Matrix.h:220
TemplateElementType & operator()(const unsigned int &line, const unsigned int &column)
Operator () overload.
Definition: Matrix.h:203
bool allocateDiskLines(unsigned int startingLineIdx)
Allocate disk lines.
Definition: Matrix.h:735
Disk lines info.
Definition: Matrix.h:287
#define MAX(a, b)
Macro that returns max between two values.
void init()
Reset the internal variables to the initial state.
Definition: Matrix.h:447
MemoryPolicy m_memoryPolicy
The used memory policy (default:RAMMemPol).
Definition: Matrix.h:339
MemoryPolicy getMemPolicy() const
Returns the current memory policy.
Definition: Matrix.h:717
#define TERP_TRUE_OR_RETURN_FALSE(value, message)
Checks if value is true. For false values a warning message will be logged and a return of context wi...
Definition: Macros.h:183
unsigned long int m_maxTmpFileSize
Max bytes per temp file (for swapped tiles only, default: 2GB).
Definition: Matrix.h:319
std::vector< unsigned int > m_ramLinesIndexesVec
The indexes of all lines loaded into RAM.
Definition: Matrix.h:373
MemoryPolicy
Memory polycy.
Definition: Matrix.h:62
std::vector< TemplateElementType * > m_allLinesPtrsVec
A vector with pointers to all lines.
Definition: Matrix.h:358
unsigned int getColumnsNumber() const
The number of current matrix columns.
Definition: Matrix.h:678
Openend disk files info node.
Definition: Matrix.h:303
unsigned int m_totalColumns
The total columns number (default:0).
Definition: Matrix.h:334
const Matrix< TemplateElementType > & operator=(const Matrix< TemplateElementType > &external)
Operator = overload.
Definition: Matrix.h:691
URI C++ Library.
unsigned long int getMaxTmpFileSize() const
Returns the current maximum temporary disk file size.
Definition: Matrix.h:723
std::vector< OpenDiskFileHandler > m_openDiskFilesHandler
A vector with open disk files handler.
Definition: Matrix.h:363
void reset()
Reset (clear) the active instance data.
Definition: Matrix.h:480
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...
TemplateElementType * getScanLine(const unsigned int &line) const
Returns a pointer to the required line.
Definition: Matrix.h:811
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
Definition: Macros.h:236
TemplateElementType const * operator[](const unsigned int &line) const
Operator [] overload.
Definition: Matrix.h:258
std::vector< TemplateElementType * > m_memoryblocksHandler
Definition: Matrix.h:353
A generic template matrix.
Definition: Matrix.h:51
TemplateElementType ElementTypeT
Public matrix element type definition.
Definition: Matrix.h:57
FILE * m_filePtr
The file pointer.
Definition: Matrix.h:307
TemplateElementType * m_currentSwapTilePtr
A pointer to the current swap tile.
Definition: Matrix.h:351
unsigned int m_nextSwapLineRamLinesIndexesVecIdx
The index inside ramLinesIndexesVec_ of the next RAM line index that will be swapped to disk when a d...
Definition: Matrix.h:346
bool createNewDiskFile(unsigned long int size, FILE **fileptr, std::string &fullFileName) const
Create a new disk file.
Definition: Matrix.h:891
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:356
std::string m_fullFileName
The full created file name.
Definition: Matrix.h:309
bool isEmpty() const
Empty Matrix verification.
Definition: Matrix.h:685
unsigned int m_totalLines
The total lines number (default:0).
Definition: Matrix.h:329
virtual ~Matrix()
Definition: Matrix.h:474
unsigned int getLinesNumber() const
The number of current matrix lines.
Definition: Matrix.h:671
TemplateElementType * operator[](const unsigned int &line)
Operator [] overload.
Definition: Matrix.h:241
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:149
boost::scoped_array< TemplateElementType > m_swapMemoryBlockHandler
An auxiliar line used when swapping data to/from disk.
Definition: Matrix.h:379