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