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;
437 std::string& fullFileName )
const;
440 template<
typename TemplateElementType >
447 template<
typename TemplateElementType >
452 template<
typename TemplateElementType >
458 template<
typename TemplateElementType >
463 std::fclose( m_filePtr );
464 remove( m_fullFileName.c_str() );
468 template<
typename TemplateElementType >
480 template<
typename TemplateElementType >
486 template<
typename TemplateElementType >
495 template<
typename TemplateElementType >
501 template<
typename TemplateElementType >
507 template<
typename TemplateElementType >
514 template<
typename TemplateElementType >
516 unsigned int columns )
521 template<
typename TemplateElementType >
528 template<
typename TemplateElementType >
531 unsigned char maxMemPercentUsage )
534 maxMemPercentUsage );
537 template<
typename TemplateElementType >
540 unsigned long int maxTmpFileSize,
541 unsigned char maxMemPercentUsage )
545 unsigned int maxRAMLines = std::numeric_limits< unsigned int >::max();
551 const unsigned int lineSizeBytes = (
unsigned int)(
552 sizeof( TemplateElementType ) * columns );
560 const double freeVMem = std::min( totalPhysMem,
561 ( totalVMem - usedVMem ) );
563 const double linesMem = ( ((double)maxMemPercentUsage) / 100.0 ) *
566 maxRAMLines = (
unsigned int)std::max( 1.0, linesMem /
567 ((
double)lineSizeBytes) );
570 return reset( memoryPolicy, lines, columns, maxTmpFileSize, maxRAMLines );
573 template<
typename TemplateElementType >
575 unsigned int lines,
unsigned int columns,
unsigned int maxRAMLines)
581 template<
typename TemplateElementType >
583 unsigned int lines,
unsigned int columns,
unsigned long int maxTmpFileSize,
584 unsigned int maxRAMLines )
608 if( ( lines > 0 ) && ( columns > 0 ) )
627 TemplateElementType* newLinePtr = 0;
629 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
651 const unsigned int ramLinesIndexesVecSize = std::min(
m_maxRAMLines,
657 TemplateElementType* newLinePtr = 0;
659 for(
unsigned int allLinesPtrsVecIdx = 0 ; allLinesPtrsVecIdx <
660 ramLinesIndexesVecSize ; ++allLinesPtrsVecIdx )
673 const unsigned long int diskLinesNmb =
m_totalLines - ramLinesIndexesVecSize;
677 const unsigned long int lineSizeBytes = (
unsigned long int)(
680 const unsigned long int maxLinesPerFile = (
unsigned long int )
683 const unsigned long int maxFileSize = (
unsigned long int)
684 ( maxLinesPerFile * lineSizeBytes );
686 const unsigned int openDiskFilesNumber = (
unsigned int)ceil( ((
double)diskLinesNmb)
687 / ((
double)maxLinesPerFile) );
694 unsigned int remainingLinesNmb = diskLinesNmb;
695 unsigned int fileSize = 0;
696 unsigned int fileLinesNumber = 0;
697 unsigned int diskLinesVecIdx = ramLinesIndexesVecSize;
699 for(
unsigned int fileIdx = 0 ; fileIdx < openDiskFilesNumber ;
704 fileSize = maxFileSize;
705 fileLinesNumber = maxLinesPerFile;
707 if( remainingLinesNmb < maxLinesPerFile )
709 fileSize = (
unsigned long int)( lineSizeBytes * remainingLinesNmb );
710 fileLinesNumber = remainingLinesNmb;
713 remainingLinesNmb -= fileLinesNumber;
717 std::FILE* newFilePtr = 0;
718 std::string newFullFileName;
731 for(
unsigned int fileLineIdx = 0; fileLineIdx < fileLinesNumber ; ++fileLineIdx )
755 template<
typename TemplateElementType >
758 TemplateElementType* linePtr = 0;
759 for(
unsigned int memoryblocksHandlerIdx = 0 ; memoryblocksHandlerIdx <
766 delete[] ( linePtr );
790 template<
typename TemplateElementType >
797 template<
typename TemplateElementType >
804 template<
typename TemplateElementType >
810 template<
typename TemplateElementType >
817 "Unable to initiate the matrix object" );
819 unsigned int column = 0;;
820 TemplateElementType
const* inLinePtr = 0;
821 TemplateElementType* outLinePtr = 0;
823 for(
unsigned int line = 0 ; line <
m_totalLines ; ++line )
829 outLinePtr[ column ] = inLinePtr[ column ];
836 template<
typename TemplateElementType >
842 template<
typename TemplateElementType >
848 template<
typename TemplateElementType >
854 template<
typename TemplateElementType >
885 (
long)( inLineData.
m_fileOff ), SEEK_SET ),
896 (
long)( inLineData.
m_fileOff ), SEEK_SET ),
904 inLineData.
m_filePtr ),
"File write error" )
935 template<
typename TemplateElementType >
937 FILE** fileptr, std::string& fullFileName )
const
941 bool returnValue =
true;
942 long seekoff = (long)size;
943 const boost::filesystem::path randShortFileNamePath(
944 "TerralibRPMatrix_%%%%-%%%%-%%%%-%%%%" );
945 const unsigned char c =
'\0';
949 randShortFileNamePath).
string());
951 if( fullFileName.empty() )
960 if( (*fileptr) == 0 )
966 if( std::fseek( (*fileptr), seekoff, SEEK_SET ) != 0 )
968 std::fclose( (*fileptr) );
969 std::remove( fullFileName.c_str() );
974 if( 1 != std::fwrite( &c, 1, 1, (*fileptr) ) )
976 std::fclose( (*fileptr) );
977 std::remove( fullFileName.c_str() );
987 std::fclose( (*fileptr) );
990 std::remove( fullFileName.c_str() );
1001 randShortFileNamePath).
string());
1003 if( fullFileName.empty() )
1005 returnValue =
false;
1012 if( (*fileptr) == 0 )
1014 returnValue =
false;
1018 if( std::fseek( (*fileptr), seekoff, SEEK_SET ) != 0 )
1020 std::fclose( (*fileptr) );
1021 std::remove( fullFileName.c_str() );
1022 returnValue =
false;
1026 if( 1 != std::fwrite( &c, 1, 1, (*fileptr) ) )
1028 std::fclose( (*fileptr) );
1029 std::remove( fullFileName.c_str() );
1030 returnValue =
false;
1039 std::fclose( (*fileptr) );
1042 std::remove( fullFileName.c_str() );
1043 returnValue =
false;
1052 #endif //__TERRALIB_RP_INTERNAL_MATRIX_H