28 #include "../raster/Band.h"
29 #include "../raster/BandProperty.h"
32 #include "../common/PlatformUtils.h"
33 #include "../common/Translator.h"
67 unsigned int maxBlockSizeBytes = 0;
68 unsigned int maxNumberOfCacheBlocks = 1;
84 const double freeVMem = ( ((double)maxMemPercentUsed) / 100.0 ) *
85 std::min( totalPhysMem, ( totalVMem - usedVMem ) );
86 maxNumberOfCacheBlocks = (
unsigned int)
87 std::max( 1.0, std::ceil( freeVMem / ((
double)maxBlockSizeBytes) ) );
90 return initialize( maxNumberOfCacheBlocks, sync );
94 const unsigned int maxNumberOfCacheBlocks,
105 unsigned int numberOfRasterBlocks = 0;
107 for(
unsigned int bandIdx = 0 ; bandIdx < m_syncPtr->m_raster.getNumberOfBands() ;
110 if( m_globalBlocksNumberX < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksx )
113 if( m_globalBlocksNumberY < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy )
114 m_globalBlocksNumberY = (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy;
116 if( m_globalBlockSizeBytes < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getBlockSize() )
117 m_globalBlockSizeBytes = (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getBlockSize();
119 numberOfRasterBlocks +=
120 ( m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksx *
121 m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy );
128 m_maxNumberOfCacheBlocks = std::min( numberOfRasterBlocks, 1u );
132 m_maxNumberOfCacheBlocks = std::min( maxNumberOfCacheBlocks, numberOfRasterBlocks );
133 m_maxNumberOfCacheBlocks = std::max( m_maxNumberOfCacheBlocks, 1u );
136 unsigned int blockBIdx = 0;
137 m_blocksPointers.resize( m_syncPtr->m_raster.getNumberOfBands() );
139 for( blockBIdx = 0 ; blockBIdx < m_blocksPointers.size() ; ++blockBIdx )
141 m_blocksPointers[ blockBIdx ].resize( m_globalBlocksNumberY );
143 for(
unsigned int blockYIdx = 0 ; blockYIdx < m_globalBlocksNumberY ;
146 m_blocksPointers[ blockBIdx ][ blockYIdx ].resize( m_globalBlocksNumberX, 0 );
161 unsigned int blockBIdx = 0;
162 unsigned int blockYIdx = 0;
163 unsigned int blockXIdx = 0;
166 for( blockBIdx = 0 ; blockBIdx < m_blocksPointers.size() ; ++blockBIdx )
168 for( blockYIdx = 0 ; blockYIdx < m_globalBlocksNumberY ;
171 for( blockXIdx = 0 ; blockXIdx < m_globalBlocksNumberX ;
174 blockPtr = m_blocksPointers[ blockBIdx ][ blockYIdx ][ blockXIdx ];
178 if( !m_syncPtr->releaseBlock( blockBIdx, blockXIdx, blockYIdx, blockPtr ) )
180 throw Exception(
TE_TR(
"Block release error") );
188 m_blocksPointers.clear();
190 unsigned char* blockPtr = 0;
191 for( std::vector< unsigned char* >::size_type blocksHandlerIdx = 0 ;
192 blocksHandlerIdx < m_blocksHandler.size() ; ++blocksHandlerIdx )
194 blockPtr = m_blocksHandler[ blocksHandlerIdx ];
197 delete[]( blockPtr );
200 m_blocksHandler.clear();
202 m_blocksFifo.clear();
208 unsigned int x,
unsigned int y )
211 assert( band < m_syncPtr->m_raster.getNumberOfBands() );
212 assert( x < m_globalBlocksNumberX );
213 assert( y < m_globalBlocksNumberY );
214 assert( x < (
unsigned int)m_syncPtr->m_raster.getBand( band )->getProperty()->m_nblocksx );
215 assert( y < (
unsigned int)m_syncPtr->m_raster.getBand( band )->getProperty()->m_nblocksy );
217 m_getBlockPointer_BlkPtr = m_blocksPointers[ band ][ y ][ x ];
219 if( m_getBlockPointer_BlkPtr == 0 )
222 if( m_blocksHandler.size() < m_maxNumberOfCacheBlocks )
224 m_getBlockPointer_BlkPtr =
new unsigned char[ m_globalBlockSizeBytes ];
225 m_blocksHandler.push_back( m_getBlockPointer_BlkPtr );
229 newBlockFifoIndex.
m_b = band;
230 newBlockFifoIndex.
m_y = y;
231 newBlockFifoIndex.
m_x = x;
232 m_blocksFifo.push_back( newBlockFifoIndex );
236 BlockIndex& choosedSwapBlockIndex = m_blocksFifo[
237 m_blocksFifoNextSwapBlockIndex ];
239 m_getBlockPointer_BlkPtr = m_blocksPointers[ choosedSwapBlockIndex.
m_b ][
240 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ];
241 assert( m_getBlockPointer_BlkPtr );
242 m_blocksPointers[ choosedSwapBlockIndex.
m_b ][
243 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ] = 0;
247 if( !m_syncPtr->releaseBlock( choosedSwapBlockIndex.
m_b,
248 choosedSwapBlockIndex.
m_x, choosedSwapBlockIndex.
m_y, m_getBlockPointer_BlkPtr ) )
250 throw Exception(
TE_TR(
"Block release error") );
254 choosedSwapBlockIndex.
m_b = band;
255 choosedSwapBlockIndex.
m_y = y;
256 choosedSwapBlockIndex.
m_x = x;
257 m_blocksFifoNextSwapBlockIndex = ( m_blocksFifoNextSwapBlockIndex + 1 ) %
258 ((
unsigned int)m_blocksFifo.size());
263 if( !m_syncPtr->acquireBlock( band, x, y, m_getBlockPointer_BlkPtr ) )
265 throw Exception(
TE_TR(
"Block release error") );
268 m_blocksPointers[ band ][ y ][ x ] = m_getBlockPointer_BlkPtr;
272 return m_getBlockPointer_BlkPtr;
279 return &(m_syncPtr->m_raster);
SynchronizedBandBlocksManager()
RasterSynchronizer * m_syncPtr
A pointer to the synchronizer used by this instance, of null if not initialized.
int m_nblocksx
The number of blocks in x.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
~SynchronizedBandBlocksManager()
An access synchronizer to be used in SynchronizedRaster raster instances.
#define TE_TR(message)
It marks a string in order to get translated.
unsigned int m_blocksFifoNextSwapBlockIndex
The next block swapp index over m_blocksFifo.
boost::mutex m_mutex
General sync mutex;.
void initState()
Initialize this instance to an initial state.
unsigned int m_maxNumberOfCacheBlocks
The maximum number of cache blocks.
An exception class for the Raster module.
Raster & m_raster
The input raster.
TECOMMONEXPORT unsigned long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
An abstract class for raster data strucutures.
unsigned int m_y
Block index over the Y axis.
BandProperty * getProperty()
Returns the band property.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
te::rst::Raster * getRaster() const
Returns the associated raster.
unsigned char * m_getBlockPointer_BlkPtr
unsigned int m_globalBlockSizeBytes
The maximum block size for all bands.
bool initialize(RasterSynchronizer &sync, const unsigned char maxMemPercentUsed)
Initialize this instance to an initial state.
Synchronized raster raster band blocks manager.
TECOMMONEXPORT unsigned long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
te::common::AccessPolicy m_policy
The access policy used on the given input raster.
unsigned int m_b
Block band index.
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
unsigned int m_globalBlocksNumberY
The maximum number of blocks (Y axis) for all bands.
void * getBlockPointer(unsigned int band, unsigned int x, unsigned int y)
Returns a pointer to the required data block.
unsigned int m_globalBlocksNumberX
The maximum number of blocks (X axis) for all bands.
unsigned int m_x
Block index over the X axis.
TECOMMONEXPORT unsigned long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...