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