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;
81 const double freeVMem = ( ((double)maxMemPercentUsed) / 100.0 ) *
82 std::min( totalPhysMem, ( totalVMem - usedVMem ) );
83 unsigned int maxNumberOfCacheBlocks = (
unsigned int)
84 std::max( 1.0, std::ceil( freeVMem / ((
double)maxBlockSizeBytes) ) );
86 return initialize( maxNumberOfCacheBlocks, sync );
90 const unsigned int maxNumberOfCacheBlocks,
101 unsigned int numberOfRasterBlocks = 0;
103 for(
unsigned int bandIdx = 0 ; bandIdx < m_syncPtr->m_raster.getNumberOfBands() ;
106 if( m_globalBlocksNumberX < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksx )
109 if( m_globalBlocksNumberY < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy )
110 m_globalBlocksNumberY = (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy;
112 if( m_globalBlockSizeBytes < (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getBlockSize() )
113 m_globalBlockSizeBytes = (
unsigned int)m_syncPtr->m_raster.getBand( bandIdx )->getBlockSize();
115 numberOfRasterBlocks +=
116 ( m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksx *
117 m_syncPtr->m_raster.getBand( bandIdx )->getProperty()->m_nblocksy );
122 m_maxNumberOfCacheBlocks = std::min( maxNumberOfCacheBlocks, numberOfRasterBlocks );
123 m_maxNumberOfCacheBlocks = std::max( m_maxNumberOfCacheBlocks, 1u );
125 unsigned int blockBIdx = 0;
126 m_blocksPointers.resize( m_syncPtr->m_raster.getNumberOfBands() );
128 for( blockBIdx = 0 ; blockBIdx < m_blocksPointers.size() ; ++blockBIdx )
130 m_blocksPointers[ blockBIdx ].resize( m_globalBlocksNumberY );
132 for(
unsigned int blockYIdx = 0 ; blockYIdx < m_globalBlocksNumberY ;
135 m_blocksPointers[ blockBIdx ][ blockYIdx ].resize( m_globalBlocksNumberX, 0 );
150 unsigned int blockBIdx = 0;
151 unsigned int blockYIdx = 0;
152 unsigned int blockXIdx = 0;
155 for( blockBIdx = 0 ; blockBIdx < m_blocksPointers.size() ; ++blockBIdx )
157 for( blockYIdx = 0 ; blockYIdx < m_globalBlocksNumberY ;
160 for( blockXIdx = 0 ; blockXIdx < m_globalBlocksNumberX ;
163 blockPtr = m_blocksPointers[ blockBIdx ][ blockYIdx ][ blockXIdx ];
167 if( !m_syncPtr->releaseBlock( blockBIdx, blockXIdx, blockYIdx, blockPtr ) )
169 throw Exception(
TE_TR(
"Block release error") );
177 m_blocksPointers.clear();
179 unsigned char* blockPtr = 0;
180 for( std::vector< unsigned char* >::size_type blocksHandlerIdx = 0 ;
181 blocksHandlerIdx < m_blocksHandler.size() ; ++blocksHandlerIdx )
183 blockPtr = m_blocksHandler[ blocksHandlerIdx ];
186 delete[]( blockPtr );
189 m_blocksHandler.clear();
191 m_blocksFifo.clear();
197 unsigned int x,
unsigned int y )
200 assert( band < m_syncPtr->m_raster.getNumberOfBands() );
201 assert( x < m_globalBlocksNumberX );
202 assert( y < m_globalBlocksNumberY );
203 assert( x < (
unsigned int)m_syncPtr->m_raster.getBand( band )->getProperty()->m_nblocksx );
204 assert( y < (
unsigned int)m_syncPtr->m_raster.getBand( band )->getProperty()->m_nblocksy );
206 m_getBlockPointer_BlkPtr = m_blocksPointers[ band ][ y ][ x ];
208 if( m_getBlockPointer_BlkPtr == 0 )
211 if( m_blocksHandler.size() < m_maxNumberOfCacheBlocks )
213 m_getBlockPointer_BlkPtr =
new unsigned char[ m_globalBlockSizeBytes ];
214 m_blocksHandler.push_back( m_getBlockPointer_BlkPtr );
218 newBlockFifoIndex.
m_b = band;
219 newBlockFifoIndex.
m_y = y;
220 newBlockFifoIndex.
m_x = x;
221 m_blocksFifo.push_back( newBlockFifoIndex );
225 BlockIndex& choosedSwapBlockIndex = m_blocksFifo[
226 m_blocksFifoNextSwapBlockIndex ];
228 m_getBlockPointer_BlkPtr = m_blocksPointers[ choosedSwapBlockIndex.
m_b ][
229 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ];
230 assert( m_getBlockPointer_BlkPtr );
231 m_blocksPointers[ choosedSwapBlockIndex.
m_b ][
232 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ] = 0;
236 if( !m_syncPtr->releaseBlock( choosedSwapBlockIndex.
m_b,
237 choosedSwapBlockIndex.
m_x, choosedSwapBlockIndex.
m_y, m_getBlockPointer_BlkPtr ) )
239 throw Exception(
TE_TR(
"Block release error") );
243 choosedSwapBlockIndex.
m_b = band;
244 choosedSwapBlockIndex.
m_y = y;
245 choosedSwapBlockIndex.
m_x = x;
246 m_blocksFifoNextSwapBlockIndex = ( m_blocksFifoNextSwapBlockIndex + 1 ) %
247 ((
unsigned int)m_blocksFifo.size());
252 if( !m_syncPtr->acquireBlock( band, x, y, m_getBlockPointer_BlkPtr ) )
254 throw Exception(
TE_TR(
"Block release error") );
257 m_blocksPointers[ band ][ y ][ x ] = m_getBlockPointer_BlkPtr;
261 return m_getBlockPointer_BlkPtr;
268 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).
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...