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>
40 #include <boost/filesystem.hpp>
50 template<
typename TemplateElementType >
111 bool reset(
unsigned int lines,
unsigned int columns );
123 bool reset(
unsigned int lines,
unsigned int columns,
138 bool reset(
unsigned int lines,
unsigned int columns,
139 MemoryPolicy memoryPolicy,
unsigned char maxMemPercentUsage );
154 bool reset(
unsigned int lines,
unsigned int columns,
155 MemoryPolicy memoryPolicy,
unsigned long int maxTmpFileSize,
156 unsigned char maxMemPercentUsage );
204 const unsigned int& line,
205 const unsigned int& column )
221 const unsigned int& line,
222 const unsigned int& column )
const
242 const unsigned int& line )
259 const unsigned int& line )
const
405 const unsigned int& line )
const;
415 std::string& fullFileName )
const;
418 template<
typename TemplateElementType >
425 template<
typename TemplateElementType >
430 template<
typename TemplateElementType >
436 template<
typename TemplateElementType >
441 remove( m_fullFileName.c_str() );
446 template<
typename TemplateElementType >
458 template<
typename TemplateElementType >
464 template<
typename TemplateElementType >
473 template<
typename TemplateElementType >
479 template<
typename TemplateElementType >
485 template<
typename TemplateElementType >
491 template<
typename TemplateElementType >
493 unsigned int columns )
499 template<
typename TemplateElementType >
507 template<
typename TemplateElementType >
510 unsigned char maxMemPercentUsage )
513 maxMemPercentUsage );
516 template<
typename TemplateElementType >
519 unsigned long int maxTmpFileSize,
520 unsigned char maxMemPercentUsage )
532 if( ( lines > 0 ) && ( columns > 0 ) )
541 const unsigned int lineSizeBytes = (
unsigned int)(
553 TemplateElementType* newLinePtr = 0;
555 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
577 unsigned int maxRAMLines = 1;
589 const double freeVMem =
MIN( totalPhysMem,
590 ( totalVMem - usedVMem ) );
595 maxRAMLines = (
unsigned int)
MAX( 1.0, linesMem /
596 ((
double)lineSizeBytes) );
601 const unsigned int ramLinesIndexesVecSize =
MIN( maxRAMLines,
607 TemplateElementType* newLinePtr = 0;
609 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
610 ramLinesIndexesVecSize ; ++allLinesPtrsVecIdx )
641 template<
typename TemplateElementType >
644 TemplateElementType* linePtr = 0;
645 for(
unsigned int memoryblocksHandlerIdx = 0 ; memoryblocksHandlerIdx <
652 delete[] ( linePtr );
670 template<
typename TemplateElementType >
677 template<
typename TemplateElementType >
684 template<
typename TemplateElementType >
690 template<
typename TemplateElementType >
697 "Unable to initiate the matrix object" );
699 unsigned int column = 0;;
700 TemplateElementType
const* inLinePtr = 0;
701 TemplateElementType* outLinePtr = 0;
703 for(
unsigned int line = 0 ; line <
m_totalLines ; ++line )
709 outLinePtr[ column ] = inLinePtr[ column ];
716 template<
typename TemplateElementType >
722 template<
typename TemplateElementType >
728 template<
typename TemplateElementType >
734 template<
typename TemplateElementType >
737 const unsigned long int diskLinesNmb =
m_totalLines - startingLineIdx;
741 const unsigned long int lineSizeBytes = (
unsigned long int)(
744 const unsigned long int maxLinesPerFile = (
unsigned long int )
747 const unsigned long int maxFileSize = (
unsigned long int)
748 ( maxLinesPerFile * lineSizeBytes );
750 const unsigned int openDiskFilesNumber = (
unsigned int)ceil( ((
double)diskLinesNmb)
751 / ((
double)maxLinesPerFile) );
757 unsigned int remainingLinesNmb = diskLinesNmb;
758 unsigned int fileSize = 0;
759 unsigned int fileLinesNumber = 0;
760 unsigned int diskLinesVecIdx = startingLineIdx;
762 for(
unsigned int fileIdx = 0 ; fileIdx < openDiskFilesNumber ;
767 fileSize = maxFileSize;
768 fileLinesNumber = maxLinesPerFile;
770 if( remainingLinesNmb < maxLinesPerFile )
772 fileSize = (
unsigned long int)( lineSizeBytes * remainingLinesNmb );
773 fileLinesNumber = remainingLinesNmb;
776 remainingLinesNmb -= fileLinesNumber;
780 FILE* newFilePtr = 0;
781 std::string newFullFileName;
785 TERP_LOGERR(
"Unable to create temporary disk file" );
794 for(
unsigned int lineIdx = 0; lineIdx < fileLinesNumber ; ++lineIdx )
809 template<
typename TemplateElementType >
840 (
long)( inLineData.m_fileOff ), SEEK_SET ),
845 (
size_t)(
sizeof( TemplateElementType ) *
m_totalColumns ), 1, inLineData.m_filePtr ),
851 (
long)( inLineData.m_fileOff ), SEEK_SET ),
858 (
size_t)(
sizeof( TemplateElementType ) * m_totalColumns ), 1,
859 inLineData.m_filePtr ),
"File write error" )
873 outLineData.m_filePtr = inLineData.m_filePtr;
874 outLineData.m_fileOff = inLineData.m_fileOff;
876 inLineData.m_filePtr = 0;
877 inLineData.m_fileOff = 0;
890 template<
typename TemplateElementType >
892 FILE** fileptr, std::string& fullFileName )
const
894 fullFileName = boost::filesystem::unique_path(
895 boost::filesystem::temp_directory_path() /=
896 boost::filesystem::path(
"TerralibRPMatrix_%%%%-%%%%-%%%%-%%%%" ) ).string();
897 if( fullFileName.empty() )
902 (*fileptr) = fopen( fullFileName.c_str(),
"wb+" );
905 long seekoff = (long)( size - 1 );
907 if( 0 != fseek( (*fileptr), seekoff, SEEK_SET ) )
909 fclose( (*fileptr) );
910 remove( fullFileName.c_str() );
915 unsigned char c =
'\0';
916 if( 1 != fwrite( &c, 1, 1, (*fileptr) ) )
918 fclose( (*fileptr) );
919 remove( fullFileName.c_str() );
928 #endif //__TERRALIB_RP_INTERNAL_MATRIX_H
void clear()
Clear all allocated resources and go back to the initial default parameters.
unsigned int m_fileOff
The offset within the file.
FILE * m_filePtr
The file pointer.
unsigned char m_maxMemPercentUsage
the max amount of free memory to use when necessary.
unsigned char getMaxMemPercentUsage() const
Returns the max amount of free memory to use when necessary.
std::vector< DiskLineInfo > m_diskLinesInfoVec
Info of all lines, used when a line is on disk.
#define MIN(a, b)
Macro that returns min between two values.
#define TERP_LOGERR(message)
Logs an error message.
const TemplateElementType & operator()(const unsigned int &line, const unsigned int &column) const
Operator () overload.
TemplateElementType & operator()(const unsigned int &line, const unsigned int &column)
Operator () overload.
bool allocateDiskLines(unsigned int startingLineIdx)
Allocate disk lines.
#define MAX(a, b)
Macro that returns max between two values.
void init()
Reset the internal variables to the initial state.
MemoryPolicy m_memoryPolicy
The used memory policy (default:RAMMemPol).
MemoryPolicy getMemPolicy() const
Returns the current memory policy.
#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 long int m_maxTmpFileSize
Max bytes per temp file (for swapped tiles only, default: 2GB).
std::vector< unsigned int > m_ramLinesIndexesVec
The indexes of all lines loaded into RAM.
MemoryPolicy
Memory polycy.
std::vector< TemplateElementType * > m_allLinesPtrsVec
A vector with pointers to all lines.
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
unsigned int getColumnsNumber() const
The number of current matrix columns.
Openend disk files info node.
unsigned int m_totalColumns
The total columns number (default:0).
const Matrix< TemplateElementType > & operator=(const Matrix< TemplateElementType > &external)
Operator = overload.
unsigned long int getMaxTmpFileSize() const
Returns the current maximum temporary disk file size.
std::vector< OpenDiskFileHandler > m_openDiskFilesHandler
A vector with open disk files handler.
void reset()
Reset (clear) the active instance data.
TemplateElementType * getScanLine(const unsigned int &line) const
Returns a pointer to the required line.
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
TemplateElementType const * operator[](const unsigned int &line) const
Operator [] overload.
std::vector< TemplateElementType * > m_memoryblocksHandler
A generic template matrix.
TemplateElementType ElementTypeT
Public matrix element type definition.
FILE * m_filePtr
The file pointer.
TemplateElementType * m_currentSwapTilePtr
A pointer to the current swap tile.
unsigned int m_nextSwapLineRamLinesIndexesVecIdx
The index inside ramLinesIndexesVec_ of the next RAM line index that will be swapped to disk when a d...
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
bool createNewDiskFile(unsigned long int size, FILE **fileptr, std::string &fullFileName) const
Create a new disk file.
#define TERP_DEBUG_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
std::string m_fullFileName
The full created file name.
bool isEmpty() const
Empty Matrix verification.
unsigned int m_totalLines
The total lines number (default:0).
unsigned int getLinesNumber() const
The number of current matrix lines.
TemplateElementType * operator[](const unsigned int &line)
Operator [] overload.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
boost::scoped_array< TemplateElementType > m_swapMemoryBlockHandler
An auxiliar line used when swapping data to/from disk.
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...