Loading...
Searching...
No Matches
CachedBandBlocksManager.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/memory/CachedBandBlocksManager.h
22
23 \brief RAM cached and tiled raster band blocks manager.
24*/
25
26#ifndef __TERRALIB_MEMORY_INTERNAL_CACHEDBANDBLOCKSMANAGER_H
27#define __TERRALIB_MEMORY_INTERNAL_CACHEDBANDBLOCKSMANAGER_H
28
29// TerraLib
30#include "../raster/Raster.h"
31#include "Config.h"
32
33// STL
34#include <memory>
35#include <vector>
36
37// Boost
38#include <boost/noncopyable.hpp>
39#include <boost/thread.hpp>
40#include <boost/scoped_array.hpp>
41
42namespace te
43{
44 namespace mem
45 {
46 /*!
47 \class CachedBandBlocksManager
48
49 \brief RAM cached and tiled raster band blocks manager.
50 */
51 class TEMEMORYEXPORT CachedBandBlocksManager : public boost::noncopyable
52 {
53 public:
54
56
58
59 /*!
60 \brief Initialize this instance to an initial state.
61
62 \param externalRaster The external raster where the data will be read/written.
63
64 \param maxMemPercentUsed The maximum free memory percentual to use valid range: [1:100].
65
66 \param dataPrefetchThreshold The read-ahead data prefetch threshold (0-will disable prefetch, 1-data always prefetched, higher values will do prefetch when necessary).
67
68 \return true if OK, false on errors.
69 */
70 bool initialize( const te::rst::Raster& externalRaster,
71 const unsigned char maxMemPercentUsed,
72 const unsigned int dataPrefetchThreshold );
73
74 /*!
75 \brief Initialize this instance to an initial state.
76
77 \param externalRaster The external raster where the data will be read/written.
78
79 \param maxNumberOfCacheBlocks The maximum number of cache blocks.
80
81 \param dataPrefetchThreshold The read-ahead data prefetch threshold (0-will disable prefetch, 1-data always prefetched, higher values will do prefetch when necessary).
82
83 \return true if OK, false on errors.
84 */
85 bool initialize( const unsigned int maxNumberOfCacheBlocks,
86 const te::rst::Raster& externalRaster,
87 const unsigned int dataPrefetchThreshold );
88
89 /*!
90 \brief Returns true if this instance is initialized.
91
92 \return true if this instance is initialized.
93 */
94 bool isInitialized() const
95 {
96 return m_rasterPtr ? true : false;
97 };
98
99 /*!
100 \note Free all allocated internal resources and go back to the initial state.
101 */
102 void free();
103
104 /*!
105 \brief Returns a pointer to the required data block.
106
107 \param band The band index.
108 \param x The block-id in x (or x-offset).
109 \param y The block-id in y (or y-offset).
110
111 \return Pointer to the required data block.
112 */
113 void* getBlockPointer(unsigned int band, unsigned int x, unsigned int y );
114
115
116 /*! \brief Returns the associated raster. */
118 {
119 return m_rasterPtr;
120 };
121
122 /*! \brief The maximum number of cache blocks. */
123 unsigned int getMaxNumberOfCacheBlocks() const
124 {
125 return m_maxNumberOfCacheBlocks;
126 };
127
128 /*! \brief The read-ahead data prefetch threshold. */
129 unsigned int getDataPrefetchThreshold() const
130 {
131 return m_dataPrefetchThreshold;
132 };
133
134 protected :
135
136 /*!
137 \class BlockIndex
138
139 \brief Internal blocks indexes.
140 */
142 {
143 public :
144
145 unsigned int m_b; //!< Block band index
146 unsigned int m_y; //!< Block index over the Y axis.
147 unsigned int m_x; //!< Block index over the X axis.
148
150 : m_b( 0 ), m_y( 0 ), m_x( 0 )
151 {
152 }
153
155 {
156 }
157 };
158
159 /*!
160 \class ThreadParameters
161
162 \brief Internal read/write thread execution parameters.
163 */
165 {
166 public :
167
169 {
170 InvalidTaskT = 0,
171 ReadTaskT = 1, //!< m_exchangeBlockPtr must point to a valid exchange block (it will be keept inside the thread for further use), m_blockPtr must be zero and will be updated to point to the read block.
172 WriteTaskT = 2, //!< m_blockPtr must point to the block to be written, m_exchangeBlockPtr must be zero.
173 SuicideTastT = 3 //!< The thread will finish the main loop and exit.
174 };
175
176 te::rst::Raster* m_rasterPtr; //!< External raster pointer.
177
178 unsigned int m_dataPrefetchThreshold; //!< The user defined read-ahead threshold.
179
180 bool m_taskFinished; //!< true when the thread has finished the required task.
181
182 TaskType m_task; //!< The required task to be performed (read/write/exit).
183
184 unsigned char* m_blockPtr; //!< Input block pointer.
185
186 unsigned char* m_exchangeBlockPtr; //!< Exchange block pointer.
187
188 unsigned int m_blockB; //!< Raster block band index.
189
190 unsigned int m_blockX; //!< Raster block X index
191
192 unsigned int m_blockY; //!< Raster block Y index
193
194 boost::mutex m_doTaskMutex; //!< Used when wakenning the thread to perform some task.
195
196 boost::mutex m_taskFinishedMutex; //!< used by the thread to inform a task finishment.
197
198 boost::condition_variable m_doTaskCondVar; //!< Used by the thread when awakenning to perform some task.
199
200 boost::condition_variable m_taskFinishedCondVar; //!< Used to wait for the required task finishment.
201
202 boost::scoped_array< unsigned char > m_threadDataBlockHandler; //!< A extra block used in exchange when a read-ahead task is performed.
203
205 : m_rasterPtr( 0 ),
206 m_dataPrefetchThreshold( 0 ),
207 m_taskFinished( false ),
208 m_task( InvalidTaskT ),
209 m_blockPtr( 0 ),
210 m_exchangeBlockPtr( 0 ),
211 m_blockB( 0 ),
212 m_blockX( 0 ),
213 m_blockY( 0 )
214 {
215 };
216
218 };
219
220 te::rst::Raster* m_rasterPtr; //!< External raster pointer.
221
222 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).
223
224 unsigned int m_globalBlocksNumberX; //!< The maximum number of blocks (X axis) for all bands.
225
226 unsigned int m_globalBlocksNumberY; //!< The maximum number of blocks (Y axis) for all bands.
227
228 unsigned int m_globalBlockSizeBytes; //!< The maximum block size for all bands.
229
230 unsigned int m_maxNumberOfCacheBlocks; //!< The maximum number of cache blocks.
231
232 unsigned int m_blocksFifoNextSwapBlockIndex; //!< The next block swapp index over m_blocksFifo.
233
234// variables used by internal methods
236
237 std::vector< std::vector< std::vector< unsigned char* > > > m_blocksPointers; //!< 3D Matrix of block pointers indexed as [band][blockYIndex][blockXIndex].
238
239 std::vector< unsigned char* > m_blocksHandler; //!< Cache blocks handler.
240
241 std::vector< BlockIndex > m_blocksFifo; //!< blocks swap FIFO.
242
243 ThreadParameters m_threadParams; //!< The internal thread execution parameters.
244
245 std::unique_ptr< boost::thread > m_threadHandler; //!< The internal thread handler.
246
247 /*!
248 \brief Thread entry.
249
250 \param paramsPtr A pointer to the thread parameters.
251 */
252 static void threadEntry(ThreadParameters* paramsPtr);
253
254 private :
255
256 /*! \brief Initialize this instance to an initial state. */
257 void initState();
258 };
259
260 } // end namespace mem
261} // end namespace te
262
263#endif // __TERRALIB_MEMORY_INTERNAL_CACHEDBANDBLOCKSMANAGER_H
unsigned int m_y
Block index over the Y axis.
unsigned int m_x
Block index over the X axis.
Internal read/write thread execution parameters.
unsigned char * m_exchangeBlockPtr
Exchange block pointer.
unsigned int m_dataPrefetchThreshold
The user defined read-ahead threshold.
boost::condition_variable m_doTaskCondVar
Used by the thread when awakenning to perform some task.
te::rst::Raster * m_rasterPtr
External raster pointer.
boost::mutex m_doTaskMutex
Used when wakenning the thread to perform some task.
bool m_taskFinished
true when the thread has finished the required task.
boost::scoped_array< unsigned char > m_threadDataBlockHandler
A extra block used in exchange when a read-ahead task is performed.
boost::condition_variable m_taskFinishedCondVar
Used to wait for the required task finishment.
TaskType m_task
The required task to be performed (read/write/exit).
boost::mutex m_taskFinishedMutex
used by the thread to inform a task finishment.
RAM cached and tiled raster band blocks manager.
unsigned int m_globalBlockSizeBytes
The maximum block size for all bands.
std::unique_ptr< boost::thread > m_threadHandler
The internal thread handler.
std::vector< unsigned char * > m_blocksHandler
Cache blocks handler.
void * getBlockPointer(unsigned int band, unsigned int x, unsigned int y)
Returns a pointer to the required data block.
static void threadEntry(ThreadParameters *paramsPtr)
Thread entry.
bool initialize(const unsigned int maxNumberOfCacheBlocks, const te::rst::Raster &externalRaster, const unsigned int dataPrefetchThreshold)
Initialize this instance to an initial state.
unsigned int getDataPrefetchThreshold() const
The read-ahead data prefetch threshold.
unsigned int m_globalBlocksNumberX
The maximum number of blocks (X axis) for all bands.
bool isInitialized() const
Returns true if this instance is initialized.
unsigned int m_blocksFifoNextSwapBlockIndex
The next block swapp index over m_blocksFifo.
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].
unsigned int m_dataPrefetchThreshold
The read-ahead data prefetch threshold (0-will disable prefetch, 1-data always prefetched,...
te::rst::Raster * m_rasterPtr
External raster pointer.
ThreadParameters m_threadParams
The internal thread execution parameters.
void initState()
Initialize this instance to an initial state.
te::rst::Raster * getRaster() const
Returns the associated raster.
bool initialize(const te::rst::Raster &externalRaster, const unsigned char maxMemPercentUsed, const unsigned int dataPrefetchThreshold)
Initialize this instance to an initial state.
std::vector< BlockIndex > m_blocksFifo
blocks swap FIFO.
unsigned int m_maxNumberOfCacheBlocks
The maximum number of cache blocks.
unsigned int getMaxNumberOfCacheBlocks() const
The maximum number of cache blocks.
An abstract class for raster data strucutures.
Definition: Raster.h:72
TerraLib.
#define TEMEMORYEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:84
Proxy configuration file for TerraView (see terraview_config.h).