25 #ifndef __TERRALIB_RP_INTERNAL_MATRIX_H 
   26 #define __TERRALIB_RP_INTERNAL_MATRIX_H 
   30   #include "../common/PlatformUtils.h" 
   31   #include "../core/filesystem/FileSystem.h" 
   32   #include "../core/encoding/CharEncoding.h" 
   33   #include "../core/utils/Platform.h" 
   43   #include <boost/scoped_array.hpp> 
   44   #include <boost/filesystem.hpp> 
   54       template< 
typename TemplateElementType >
 
  115           bool reset( 
unsigned int lines, 
unsigned int columns );
 
  128           bool reset( 
unsigned int lines, 
unsigned int columns,
 
  144           bool reset( 
unsigned int lines, 
unsigned int columns,
 
  145             MemoryPolicy memoryPolicy, 
unsigned char maxMemPercentUsage );
 
  160           bool reset( 
unsigned int lines, 
unsigned int columns,
 
  161             MemoryPolicy memoryPolicy, 
unsigned long int maxTmpFileSize,
 
  162             unsigned char maxMemPercentUsage );
 
  173             unsigned int columns, 
unsigned int maxRAMLines );
 
  184             unsigned int columns, 
unsigned long int maxTmpFileSize,
 
  185             unsigned int maxRAMLines );
 
  228             const unsigned int& line,
 
  229             const unsigned int& column )
 
  245             const unsigned int& line,
 
  246             const unsigned int& column )
 const 
  266             const unsigned int& line )
 
  283             const unsigned int& line )
 const 
  427             const unsigned int& line ) 
const;
 
  430       template< 
typename TemplateElementType >
 
  437       template< 
typename TemplateElementType >
 
  442       template< 
typename TemplateElementType >
 
  448       template< 
typename TemplateElementType >
 
  453           std::fclose( m_filePtr );
 
  454           remove( m_fullFileName.c_str() );
 
  458       template< 
typename TemplateElementType >
 
  470       template< 
typename TemplateElementType >
 
  476       template< 
typename TemplateElementType >
 
  485       template< 
typename TemplateElementType >
 
  491       template< 
typename TemplateElementType >
 
  497       template< 
typename TemplateElementType >
 
  504       template< 
typename TemplateElementType >
 
  506         unsigned int columns )
 
  511       template< 
typename TemplateElementType >
 
  518       template< 
typename TemplateElementType >
 
  521         unsigned char maxMemPercentUsage )
 
  524           maxMemPercentUsage );
 
  527       template< 
typename TemplateElementType >
 
  530         unsigned long int maxTmpFileSize,
 
  531         unsigned char maxMemPercentUsage )
 
  535         unsigned int maxRAMLines = std::numeric_limits< unsigned int >::max();
 
  541           const unsigned int lineSizeBytes = (
unsigned int)(
 
  542             sizeof( TemplateElementType ) * columns );
 
  550           const double freeVMem = std::min( totalPhysMem,
 
  551             ( totalVMem - usedVMem ) );
 
  553           const double linesMem = ( ((double)maxMemPercentUsage) / 100.0 ) *
 
  556           maxRAMLines = (
unsigned int)std::max( 1.0, linesMem /
 
  557             ((
double)lineSizeBytes) );
 
  560         return reset( memoryPolicy, lines, columns, maxTmpFileSize, maxRAMLines );
 
  563       template< 
typename TemplateElementType >
 
  565         unsigned int lines, 
unsigned int columns, 
unsigned int maxRAMLines)
 
  571       template< 
typename TemplateElementType >
 
  573         unsigned int lines, 
unsigned int columns, 
unsigned long int maxTmpFileSize,
 
  574         unsigned int maxRAMLines )
 
  598         if( ( lines > 0 ) && ( columns > 0 ) )
 
  617               TemplateElementType* newLinePtr = 0;
 
  619               for( 
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
 
  641               const unsigned int ramLinesIndexesVecSize = std::min( 
m_maxRAMLines,
 
  647               TemplateElementType* newLinePtr = 0;
 
  649               for( 
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
 
  650                 ramLinesIndexesVecSize ; ++allLinesPtrsVecIdx )
 
  663               const unsigned long int diskLinesNmb = 
m_totalLines - ramLinesIndexesVecSize;
 
  667                 const unsigned long int lineSizeBytes = (
unsigned long int)(
 
  670                 const unsigned long int maxLinesPerFile = ( 
unsigned long int )
 
  673                 const unsigned long int maxFileSize = (
unsigned long int)
 
  674                   ( maxLinesPerFile * lineSizeBytes );
 
  676                 const unsigned int openDiskFilesNumber = (
unsigned int)ceil( ((
double)diskLinesNmb)
 
  677                   / ((
double)maxLinesPerFile) );
 
  684                 unsigned int remainingLinesNmb = diskLinesNmb;
 
  685                 unsigned int fileSize = 0;
 
  686                 unsigned int fileLinesNumber = 0;
 
  687                 unsigned int diskLinesVecIdx = ramLinesIndexesVecSize;
 
  689                 for( 
unsigned int fileIdx = 0 ; fileIdx < openDiskFilesNumber ;
 
  694                   fileSize = maxFileSize;
 
  695                   fileLinesNumber = maxLinesPerFile;
 
  697                   if( remainingLinesNmb < maxLinesPerFile )
 
  699                     fileSize = (
unsigned long int)( lineSizeBytes * remainingLinesNmb );
 
  700                     fileLinesNumber = remainingLinesNmb;
 
  703                   remainingLinesNmb -= fileLinesNumber;
 
  707                   std::FILE* newFilePtr = 0;
 
  708                   std::string newFullFileName;
 
  711                     &( newFilePtr ), newFullFileName ) )
 
  722                     for( 
unsigned int fileLineIdx = 0; fileLineIdx < fileLinesNumber ; ++fileLineIdx )
 
  746       template< 
typename TemplateElementType >
 
  749         TemplateElementType* linePtr = 0;
 
  750         for( 
unsigned int memoryblocksHandlerIdx = 0 ; memoryblocksHandlerIdx <
 
  757             delete[] ( linePtr );
 
  781       template< 
typename TemplateElementType >
 
  788       template< 
typename TemplateElementType >
 
  795       template< 
typename TemplateElementType >
 
  801       template< 
typename TemplateElementType >
 
  808           "Unable to initiate the matrix object" );
 
  810         unsigned int column = 0;;
 
  811         TemplateElementType 
const* inLinePtr = 0;
 
  812         TemplateElementType* outLinePtr = 0;
 
  814         for( 
unsigned int line = 0 ; line < 
m_totalLines ; ++line )
 
  820             outLinePtr[ column ] = inLinePtr[ column ];
 
  827       template< 
typename TemplateElementType >
 
  833       template< 
typename TemplateElementType >
 
  839       template< 
typename TemplateElementType >
 
  845       template< 
typename TemplateElementType >
 
  876             (
long)( inLineData.
m_fileOff ), SEEK_SET ),
 
  887             (
long)( inLineData.
m_fileOff ), SEEK_SET ),
 
  895             inLineData.
m_filePtr ), 
"File write error" )
 
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
 
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
 
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
 
static bool allocateTmpDiskFile(unsigned long int size, FILE **fileptr, std::string &fullFileName)
Create and allocate a new temporary disk file.
 
unsigned int m_fileOff
The offset within the file.
 
std::FILE * m_filePtr
The file pointer.
 
Openend disk files info node.
 
std::string m_fullFileName
The full created file name.
 
std::FILE * m_filePtr
The file pointer.
 
A generic template matrix.
 
std::vector< unsigned int > m_ramLinesIndexesVec
The indexes of all lines loaded into RAM.
 
std::vector< TemplateElementType * > m_allLinesPtrsVec
A vector with pointers to all lines.
 
unsigned int m_totalColumns
The total columns number (default:0).
 
const TemplateElementType & operator()(const unsigned int &line, const unsigned int &column) const
Operator () overload.
 
void reset(MemoryPolicy memoryPolicy)
Reset (clear) the active instance data and update its internal parameters folowing the the new suppli...
 
bool reset(unsigned int lines, unsigned int columns, MemoryPolicy memoryPolicy, unsigned char maxMemPercentUsage)
Reset (clear) the active instance data and update its internal parameters folowing the the new suppli...
 
unsigned int getLinesNumber() const
The number of current matrix lines.
 
bool reset(unsigned int lines, unsigned int columns)
Reset (clear) the active instance data and update its internal parameters folowing the the new suppli...
 
void clear()
Clear all allocated resources but keep the current parameters.
 
boost::scoped_array< TemplateElementType > m_swapMemoryBlockHandler
An auxiliar line used when swapping data to/from disk.
 
unsigned int getColumnsNumber() const
The number of current matrix columns.
 
unsigned int m_maxRAMLines
the max amount of RAM lines.
 
TemplateElementType * getScanLine(const unsigned int &line) const
Returns a pointer to the required line.
 
MemoryPolicy getMemPolicy() const
Returns the current memory policy.
 
bool reset(MemoryPolicy memoryPolicy, unsigned int lines, unsigned int columns, unsigned int maxRAMLines)
 
const Matrix< TemplateElementType > & operator=(const Matrix< TemplateElementType > &external)
Operator = overload.
 
bool reset(MemoryPolicy memoryPolicy, unsigned int lines, unsigned int columns, unsigned long int maxTmpFileSize, unsigned int maxRAMLines)
 
unsigned int m_totalLines
The total lines number (default:0).
 
TemplateElementType const  * operator[](const unsigned int &line) const
Operator [] overload.
 
std::vector< TemplateElementType * > m_memoryblocksHandler
 
unsigned int getMaxRAMLines() const
Returns the allowed maximum amount of RAM lines.
 
TemplateElementType * m_currentSwapTilePtr
A pointer to the current swap tile.
 
TemplateElementType * operator[](const unsigned int &line)
Operator [] overload.
 
Matrix(const Matrix< TemplateElementType > &external)
 
void init()
Reset the internal variables to the default state.
 
bool reset(unsigned int lines, unsigned int columns, MemoryPolicy memoryPolicy, unsigned long int maxTmpFileSize, unsigned char maxMemPercentUsage)
Reset (clear) the active instance data and update its internal parameters folowing the the new suppli...
 
TemplateElementType & operator()(const unsigned int &line, const unsigned int &column)
Operator () overload.
 
bool isEmpty() const
Empty Matrix verification.
 
std::vector< OpenDiskFileHandler > m_openDiskFilesHandler
A vector with open disk files handler.
 
unsigned long int m_maxTmpFileSize
Max bytes per temp file (for swapped tiles only, default: 2GB).
 
MemoryPolicy m_memoryPolicy
The used memory policy (default:RAMMemPol).
 
MemoryPolicy
Memory polycy.
 
unsigned long int getMaxTmpFileSize() const
Returns the current maximum temporary disk file size.
 
void reset()
Reset (clear) the active instance data.
 
TemplateElementType ElementTypeT
Public matrix element type definition.
 
unsigned int m_nextSwapLineRamLinesIndexesVecIdx
The index inside ramLinesIndexesVec_ of the next RAM line index that will be swapped to disk when a d...
 
std::vector< DiskLineInfo > m_diskLinesInfoVec
Info of all lines, used when a line is on disk.
 
bool reset(unsigned int lines, unsigned int columns, MemoryPolicy memoryPolicy)
Reset (clear) the active instance data and update its internal parameters folowing the the new suppli...
 
TECOMMONEXPORT unsigned long long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
 
TECOMMONEXPORT unsigned long long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped).
 
TECOMMONEXPORT unsigned long long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...