25 #ifndef __TERRALIB_RP_INTERNAL_MATRIX_H
26 #define __TERRALIB_RP_INTERNAL_MATRIX_H
30 #include "../common/PlatformUtils.h"
39 #include <boost/scoped_array.hpp>
49 template<
typename TemplateElementType >
110 bool reset(
unsigned int lines,
unsigned int columns );
122 bool reset(
unsigned int lines,
unsigned int columns,
137 bool reset(
unsigned int lines,
unsigned int columns,
138 MemoryPolicy memoryPolicy,
unsigned char maxMemPercentUsage );
153 bool reset(
unsigned int lines,
unsigned int columns,
154 MemoryPolicy memoryPolicy,
unsigned long int maxTmpFileSize,
155 unsigned char maxMemPercentUsage );
203 const unsigned int& line,
204 const unsigned int& column )
220 const unsigned int& line,
221 const unsigned int& column )
const
241 const unsigned int& line )
258 const unsigned int& line )
const
402 const unsigned int& line )
const;
413 template<
typename TemplateElementType >
420 template<
typename TemplateElementType >
425 template<
typename TemplateElementType >
431 template<
typename TemplateElementType >
440 template<
typename TemplateElementType >
452 template<
typename TemplateElementType >
458 template<
typename TemplateElementType >
467 template<
typename TemplateElementType >
473 template<
typename TemplateElementType >
479 template<
typename TemplateElementType >
485 template<
typename TemplateElementType >
487 unsigned int columns )
493 template<
typename TemplateElementType >
501 template<
typename TemplateElementType >
504 unsigned char maxMemPercentUsage )
507 maxMemPercentUsage );
510 template<
typename TemplateElementType >
513 unsigned long int maxTmpFileSize,
514 unsigned char maxMemPercentUsage )
526 if( ( lines > 0 ) && ( columns > 0 ) )
535 const unsigned int lineSizeBytes = (
unsigned int)(
547 TemplateElementType* newLinePtr = 0;
549 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
571 unsigned int maxRAMLines = 1;
583 const double freeVMem =
MIN( totalPhysMem,
584 ( totalVMem - usedVMem ) );
589 maxRAMLines = (
unsigned int)
MAX( 1.0, linesMem /
590 ((
double)lineSizeBytes) );
595 const unsigned int ramLinesIndexesVecSize =
MIN( maxRAMLines,
601 TemplateElementType* newLinePtr = 0;
603 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
604 ramLinesIndexesVecSize ; ++allLinesPtrsVecIdx )
635 template<
typename TemplateElementType >
638 TemplateElementType* linePtr = 0;
639 for(
unsigned int memoryblocksHandlerIdx = 0 ; memoryblocksHandlerIdx <
646 delete[] ( linePtr );
664 template<
typename TemplateElementType >
671 template<
typename TemplateElementType >
678 template<
typename TemplateElementType >
684 template<
typename TemplateElementType >
691 "Unable to initiate the matrix object" );
693 unsigned int column = 0;;
694 TemplateElementType
const* inLinePtr = 0;
695 TemplateElementType* outLinePtr = 0;
697 for(
unsigned int line = 0 ; line <
m_totalLines ; ++line )
703 outLinePtr[ column ] = inLinePtr[ column ];
710 template<
typename TemplateElementType >
716 template<
typename TemplateElementType >
722 template<
typename TemplateElementType >
728 template<
typename TemplateElementType >
731 const unsigned long int diskLinesNmb =
m_totalLines - startingLineIdx;
735 const unsigned long int lineSizeBytes = (
unsigned long int)(
738 const unsigned long int maxLinesPerFile = (
unsigned long int )
741 const unsigned long int maxFileSize = (
unsigned long int)
742 ( maxLinesPerFile * lineSizeBytes );
744 const unsigned int openDiskFilesNumber = (
unsigned int)ceil( ((
double)diskLinesNmb)
745 / ((
double)maxLinesPerFile) );
751 unsigned int remainingLinesNmb = diskLinesNmb;
752 unsigned int fileSize = 0;
753 unsigned int fileLinesNumber = 0;
754 unsigned int diskLinesVecIdx = startingLineIdx;
756 for(
unsigned int fileIdx = 0 ; fileIdx < openDiskFilesNumber ;
761 fileSize = maxFileSize;
762 fileLinesNumber = maxLinesPerFile;
764 if( remainingLinesNmb < maxLinesPerFile )
766 fileSize = (
unsigned long int)( lineSizeBytes * remainingLinesNmb );
767 fileLinesNumber = remainingLinesNmb;
770 remainingLinesNmb -= fileLinesNumber;
774 FILE* newFilePtr = 0;
778 TERP_LOGERR(
"Unable to create temporary disk file" );
786 for(
unsigned int lineIdx = 0; lineIdx < fileLinesNumber ; ++lineIdx )
801 template<
typename TemplateElementType >
832 (
long)( inLineData.m_fileOff ), SEEK_SET ),
837 (
size_t)(
sizeof( TemplateElementType ) *
m_totalColumns ), 1, inLineData.m_filePtr ),
843 (
long)( inLineData.m_fileOff ), SEEK_SET ),
850 (
size_t)(
sizeof( TemplateElementType ) * m_totalColumns ), 1,
851 inLineData.m_filePtr ),
"File write error" )
865 outLineData.m_filePtr = inLineData.m_filePtr;
866 outLineData.m_fileOff = inLineData.m_fileOff;
868 inLineData.m_filePtr = 0;
869 inLineData.m_fileOff = 0;
882 template<
typename TemplateElementType >
884 FILE** fileptr )
const
887 (*fileptr) = tmpfile();
890 long seekoff = (long)( size - 1 );
892 if( 0 != fseek( (*fileptr), seekoff, SEEK_SET ) )
894 fclose( (*fileptr) );
899 unsigned char c =
'\0';
900 if( 1 != fwrite( &c, 1, 1, (*fileptr) ) )
902 fclose( (*fileptr) );
911 #endif //__TERRALIB_RP_INTERNAL_MATRIX_H
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
unsigned int m_totalColumns
The total columns number (default:0).
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
MemoryPolicy
Memory polycy.
void clear()
Clear all allocated resources and go back to the initial default parameters.
std::vector< TemplateElementType * > m_memoryblocksHandler
std::vector< unsigned int > m_ramLinesIndexesVec
The indexes of all lines loaded into RAM.
unsigned char getMaxMemPercentUsage() const
Returns the max amount of free memory to use when necessary.
unsigned char m_maxMemPercentUsage
the max amount of free memory to use when necessary.
const TemplateElementType & operator()(const unsigned int &line, const unsigned int &column) const
Operator () overload.
bool allocateDiskLines(unsigned int startingLineIdx)
Allocate disk lines.
unsigned int getLinesNumber() const
The number of current matrix lines.
TemplateElementType * operator[](const unsigned int &line)
Operator [] overload.
const Matrix< TemplateElementType > & operator=(const Matrix< TemplateElementType > &external)
Operator = overload.
TemplateElementType const * operator[](const unsigned int &line) const
Operator [] overload.
#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...
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< OpenDiskFileHandler > m_openDiskFilesHandler
A vector with open disk files handler.
#define TERP_LOGERR(message)
Logs an error message.
unsigned long int m_maxTmpFileSize
Max bytes per temp file (for swapped tiles only, default: 2GB).
unsigned int m_totalLines
The total lines number (default:0).
FILE * m_filePtr
The file pointer.
TemplateElementType ElementTypeT
Public matrix element type definition.
FILE * m_filePtr
The file pointer.
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.
bool isEmpty() const
Empty Matrix verification.
#define MIN(a, b)
Macro that returns min between two values.
A generic template matrix.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
unsigned int m_fileOff
The offset within the file.
unsigned int getColumnsNumber() const
The number of current matrix columns.
TemplateElementType * m_currentSwapTilePtr
A pointer to the current swap tile.
TemplateElementType * getScanLine(const unsigned int &line) const
Returns a pointer to the required line.
bool createNewDiskFile(unsigned long int size, FILE **fileptr) const
Create a new disk file.
void reset()
Reset (clear) the active instance data.
std::vector< TemplateElementType * > m_allLinesPtrsVec
A vector with pointers to all lines.
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.
boost::scoped_array< TemplateElementType > m_swapMemoryBlockHandler
An auxiliar line used when swapping data to/from disk.
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).
TemplateElementType & operator()(const unsigned int &line, const unsigned int &column)
Operator () overload.
Openend disk files info node.
MemoryPolicy getMemPolicy() const
Returns the current memory policy.
unsigned long int getMaxTmpFileSize() const
Returns the current maximum temporary disk file size.
#define MAX(a, b)
Macro that returns max between two values.