27 #include "../common/PlatformUtils.h" 28 #include "../raster/Band.h" 29 #include "../raster/BandProperty.h" 58 const te::rst::Raster& externalRaster,
const unsigned char maxMemPercentUsed,
59 const unsigned int dataPrefetchThreshold)
65 unsigned int maxBlockSizeBytes = 0;
67 for(
unsigned int bandIdx = 0 ; bandIdx < externalRaster.
getNumberOfBands() ;
77 const double freeVMem = ( ((double)maxMemPercentUsed) / 100.0 ) *
78 std::min( totalPhysMem, ( totalVMem - usedVMem ) );
79 const unsigned int maxNumberOfCacheBlocks = (
unsigned int)
80 std::max( 1.0, std::ceil( freeVMem / ((
double)maxBlockSizeBytes) ) );
82 return initialize( maxNumberOfCacheBlocks, externalRaster, dataPrefetchThreshold );
86 const unsigned int maxNumberOfCacheBlocks,
88 const unsigned int dataPrefetchThreshold)
94 unsigned int numberOfRasterBlocks = 0;
96 for(
unsigned int bandIdx = 0 ; bandIdx < externalRaster.
getNumberOfBands() ;
108 numberOfRasterBlocks +=
121 unsigned int blockBIdx = 0;
176 unsigned int blockBIdx = 0;
177 unsigned int blockYIdx = 0;
178 unsigned int blockXIdx = 0;
179 void* blockPtr =
nullptr;
203 unsigned char* blockPtr =
nullptr;
204 for( std::vector< unsigned char* >::size_type blocksHandlerIdx = 0 ;
210 delete[]( blockPtr );
223 unsigned int x,
unsigned int y )
226 assert( band < m_rasterPtr->getNumberOfBands() );
234 if( m_getBlockPointer_BlkPtr ==
nullptr )
245 newBlockFifoIndex.
m_y = y;
246 newBlockFifoIndex.
m_x = x;
255 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ];
256 assert( m_getBlockPointer_BlkPtr );
258 choosedSwapBlockIndex.
m_y ][ choosedSwapBlockIndex.
m_x ] =
nullptr;
283 boost::unique_lock<boost::mutex> lock(
295 choosedSwapBlockIndex.
m_x, choosedSwapBlockIndex.
m_y,
296 m_getBlockPointer_BlkPtr );
301 choosedSwapBlockIndex.
m_b =
band;
302 choosedSwapBlockIndex.
m_y = y;
303 choosedSwapBlockIndex.
m_x = x;
328 boost::unique_lock<boost::mutex> lock(
363 assert( internalExchangeBlockPtr );
365 int globalReadAheadDirectionVectorX = 0;
366 int globalReadAheadDirectionVectorY = 0;
367 int globalReadAheadDirectionVectorB = 0;
368 bool globalReadAheadBlockReady =
false;
369 int globalReadAheadBlockB = 0;
370 int globalReadAheadBlockX = 0;
371 int globalReadAheadBlockY = 0;
372 int lastReadBlockX = 0;
373 int lastReadBlockY = 0;
374 int lastReadBlockB = 0;
376 int currentReadDirectionVectorX = 0;
377 int currentReadDirectionVectorY = 0;
378 int currentReadDirectionVectorB = 0;
379 int nextReadAheadBlkB = 0;
380 int nextReadAheadBlkX = 0;
381 int nextReadAheadBlkY = 0;
382 bool doReadAhead =
false;
387 boost::unique_lock<boost::mutex> lock( paramsPtr->
m_doTaskMutex );
400 if( globalReadAheadBlockReady
401 && ( globalReadAheadBlockB == (
int)paramsPtr->
m_blockB )
402 && ( globalReadAheadBlockX == (
int)paramsPtr->
m_blockX )
403 && ( globalReadAheadBlockY == (
int)paramsPtr->
m_blockY ) )
408 paramsPtr->
m_blockPtr = internalExchangeBlockPtr;
409 globalReadAheadBlockReady =
false;
441 currentReadDirectionVectorX = ( ((
int)paramsPtr->
m_blockX) - lastReadBlockX );
442 currentReadDirectionVectorY = ( ((
int)paramsPtr->
m_blockY) - lastReadBlockY );
443 currentReadDirectionVectorB = ( ((
int)paramsPtr->
m_blockB) - lastReadBlockB );
447 if( ( std::abs( currentReadDirectionVectorX ) < 2 ) &&
448 ( std::abs( currentReadDirectionVectorY ) < 2 )
449 && ( std::abs( currentReadDirectionVectorB ) < 2 ) )
451 globalReadAheadDirectionVectorX += currentReadDirectionVectorX;
452 globalReadAheadDirectionVectorY += currentReadDirectionVectorY;
453 globalReadAheadDirectionVectorB += currentReadDirectionVectorB;
455 if( ((
unsigned int)std::abs( globalReadAheadDirectionVectorB )) >
458 if( globalReadAheadDirectionVectorB > 0 )
460 nextReadAheadBlkB = ((int)paramsPtr->
m_blockB) + 1;
461 --globalReadAheadDirectionVectorB;
465 nextReadAheadBlkB = ((int)paramsPtr->
m_blockB) - 1;
466 ++globalReadAheadDirectionVectorB;
469 if( ( nextReadAheadBlkB >= 0 ) &&
473 nextReadAheadBlkB = ((int)paramsPtr->
m_blockB);
476 if( ((
unsigned int)std::abs( globalReadAheadDirectionVectorX )) >
479 if( globalReadAheadDirectionVectorX > 0 )
481 nextReadAheadBlkX = ((int)paramsPtr->
m_blockX) + 1;
482 --globalReadAheadDirectionVectorX;
486 nextReadAheadBlkX = ((int)paramsPtr->
m_blockX) - 1;
487 ++globalReadAheadDirectionVectorX;
490 if( ( nextReadAheadBlkX >= 0 ) &&
491 ( nextReadAheadBlkX <
495 nextReadAheadBlkX = ((int)paramsPtr->
m_blockX);
498 if( ((
unsigned int)std::abs( globalReadAheadDirectionVectorY )) >
501 if( globalReadAheadDirectionVectorY > 0 )
503 nextReadAheadBlkY = ((int)paramsPtr->
m_blockY) + 1;
504 --globalReadAheadDirectionVectorY;
508 nextReadAheadBlkY = ((int)paramsPtr->
m_blockY) - 1;
509 ++globalReadAheadDirectionVectorY;
512 if( ( nextReadAheadBlkY >= 0 ) &&
513 ( nextReadAheadBlkY <
517 nextReadAheadBlkY = ((int)paramsPtr->
m_blockY);
522 globalReadAheadDirectionVectorX = 0;
523 globalReadAheadDirectionVectorY = 0;
524 globalReadAheadDirectionVectorB = 0;
532 assert( nextReadAheadBlkX <
534 assert( nextReadAheadBlkY <
538 nextReadAheadBlkY, internalExchangeBlockPtr );
540 globalReadAheadBlockX = nextReadAheadBlkX;
541 globalReadAheadBlockY = nextReadAheadBlkY;
542 globalReadAheadBlockB = nextReadAheadBlkB;
543 globalReadAheadBlockReady =
true;
550 lastReadBlockY = (
int)paramsPtr->
m_blockY;
boost::mutex m_taskFinishedMutex
used by the thread to inform a task finishment.
TECOMMONEXPORT unsigned long long int GetTotalVirtualMemory()
Returns the amount of total virtual memory (bytes) that can be claimed by the current process (physic...
unsigned int m_globalBlocksNumberX
The maximum number of blocks (X axis) for all bands.
Internal read/write thread execution parameters.
void * getBlockPointer(unsigned int band, unsigned int x, unsigned int y)
Returns a pointer to the required data block.
unsigned int m_globalBlockSizeBytes
The maximum block size for all bands.
bool m_taskFinished
true when the thread has finished the required task.
int m_nblocksx
The number of blocks in x.
unsigned int m_b
Block band index.
int m_nblocksy
The number of blocks in y.
unsigned int m_dataPrefetchThreshold
The user defined read-ahead threshold.
boost::scoped_array< unsigned char > m_threadDataBlockHandler
A extra block used in exchange when a read-ahead task is performed.
unsigned int m_x
Block index over the X axis.
unsigned int m_y
Block index over the Y axis.
void initState()
Initialize this instance to an initial state.
RAM cached and tiled raster band blocks manager.
te::common::AccessPolicy getAccessPolicy() const
Returns the raster access policy.
unsigned int m_blockB
Raster block band index.
CachedBandBlocksManager()
unsigned int m_blockY
Raster block Y index.
te::rst::Raster * m_rasterPtr
External raster pointer.
An abstract class for raster data strucutures.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
BandProperty * getProperty()
Returns the band property.
ThreadParameters m_threadParams
The internal thread execution parameters.
unsigned int m_maxNumberOfCacheBlocks
The maximum number of cache blocks.
virtual void write(int x, int y, void *buffer)=0
It writes a data block from the specified buffer.
bool initialize(const te::rst::Raster &externalRaster, const unsigned char maxMemPercentUsed, const unsigned int dataPrefetchThreshold)
Initialize this instance to an initial state.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
unsigned int m_globalBlocksNumberY
The maximum number of blocks (Y axis) for all bands.
std::vector< std::vector< std::vector< unsigned char * > > > m_blocksPointers
3D Matrix of block pointers indexed as [band][blockYIndex][blockXIndex].
te::rst::Raster * m_rasterPtr
External raster pointer.
unsigned int m_blockX
Raster block X index.
m_blockPtr must point to the block to be written, m_exchangeBlockPtr must be zero.
TECOMMONEXPORT unsigned long long int GetTotalPhysicalMemory()
Returns the amount of total physical memory (bytes).
unsigned int m_dataPrefetchThreshold
The read-ahead data prefetch threshold (0-will disable prefetch, 1-data always prefetched, higher values will do prefetch when necessary).
virtual void read(int x, int y, void *buffer) const =0
It reads a data block to the specified buffer.
unsigned int m_blocksFifoNextSwapBlockIndex
The next block swapp index over m_blocksFifo.
m_exchangeBlockPtr must point to a valid exchange block (it will be keept inside the thread for furth...
static void threadEntry(ThreadParameters *paramsPtr)
Thread entry.
unsigned char * m_blockPtr
Input block pointer.
std::unique_ptr< boost::thread > m_threadHandler
The internal thread handler.
unsigned char * m_getBlockPointer_BlkPtr
unsigned char * m_exchangeBlockPtr
Exchange block pointer.
boost::condition_variable m_taskFinishedCondVar
Used to wait for the required task finishment.
std::vector< BlockIndex > m_blocksFifo
blocks swap FIFO.
std::vector< unsigned char * > m_blocksHandler
Cache blocks handler.
TECOMMONEXPORT unsigned long long int GetUsedVirtualMemory()
Returns the amount of used virtual memory (bytes) for the current process (physical + swapped)...
~CachedBandBlocksManager()
virtual int getBlockSize() const
It returns the number of bytes ocuppied by a data block.
The thread will finish the main loop and exit.
TaskType m_task
The required task to be performed (read/write/exit).
boost::mutex m_doTaskMutex
Used when wakenning the thread to perform some task.
boost::condition_variable m_doTaskCondVar
Used by the thread when awakenning to perform some task.