SourceXtractorPlusPlus  0.11
Please provide a description of the project.
TileManager.h
Go to the documentation of this file.
1 
17 /*
18  * TileManager.h
19  *
20  * Created on: Feb 23, 2018
21  * Author: mschefer
22  */
23 
24 #ifndef _SEFRAMEWORK_IMAGE_TILEMANAGER_H_
25 #define _SEFRAMEWORK_IMAGE_TILEMANAGER_H_
26 
27 #include <iostream>
28 #include <thread>
29 #include <mutex>
30 
31 #include <list>
32 #include <unordered_map>
33 
34 #include <ElementsKernel/Logging.h>
35 
38 
39 namespace SourceXtractor {
40 
41 
42 struct TileKey {
45 
46  bool operator==(const TileKey& other) const {
47  return m_source == other.m_source && m_tile_x == other.m_tile_x && m_tile_y == other.m_tile_y;
48  }
49 
50  std::string getRepr() const {
52  str << m_source.get() << "[" << m_source->getRepr() << "] " << m_tile_x << "," << m_tile_y;
53  return str.str();
54  }
55 };
56 
57 inline std::ostream& operator << (std::ostream &out, const TileKey &tk) {
58  out << tk.getRepr();
59  return out;
60 }
61 
62 }
63 
64 namespace std {
65 
66 template <>
67 struct hash<SourceXtractor::TileKey>
68 {
70  std::size_t hash = 0;
71  boost::hash_combine(hash, key.m_source);
72  boost::hash_combine(hash, key.m_tile_x);
73  boost::hash_combine(hash, key.m_tile_y);
74  return hash;
75  }
76 };
77 
78 }
79 
80 namespace SourceXtractor {
81 
82 class TileManager {
83 public:
84 
85  TileManager() : m_tile_width(256), m_tile_height(256), m_max_memory(100 * 1024L * 1024L), m_total_memory_used(0),
86  m_tile_logger(Elements::Logging::getLogger("TileManager")) {
87  }
88 
89  virtual ~TileManager() {
90  saveAllTiles();
91  }
92 
93  // Actually not thread safe, call before starting the multi-threading
94  void setOptions(int tile_width, int tile_height, int max_memory) {
96 
97  flush();
98 
99  m_tile_width = tile_width;
100  m_tile_height = tile_height;
101  m_max_memory = max_memory*1024L*1024L;
102  }
103 
104  void flush() {
106 
107  // empty anything still stored in cache
108  saveAllTiles();
109  m_tile_list.clear();
110  m_tile_map.clear();
112  }
113 
114  template <typename T>
117 
120 
121  TileKey key {std::static_pointer_cast<const ImageSourceBase>(source), x, y};
122  auto it = m_tile_map.find(key);
123  if (it != m_tile_map.end()) {
124 #ifndef NDEBUG
125  m_tile_logger.debug() << "Cache hit " << key;
126 #endif
127  return std::dynamic_pointer_cast<ImageTile<T>>(it->second);
128  } else {
129  auto tile = source->getImageTile(x, y,
130  std::min(m_tile_width, source->getWidth()-x), std::min(m_tile_height, source->getHeight()-y));
131  addTile(key, std::static_pointer_cast<ImageTileBase>(tile));
133  return tile;
134  }
135  }
136 
138  if (s_instance == nullptr) {
139  s_instance = std::make_shared<TileManager>();
140  }
141  return s_instance;
142  }
143 
144  void saveAllTiles() {
146 
147  for (auto tile_key : m_tile_list) {
148  m_tile_map.at(tile_key)->saveIfModified();
149  }
150  }
151 
152  int getTileWidth() const {
153  return m_tile_width;
154  }
155 
156  int getTileHeight() const {
157  return m_tile_height;
158  }
159 
160 private:
161 
162  void removeTile(TileKey tile_key) {
163 #ifndef NDEBUG
164  m_tile_logger.debug() << "Cache eviction " << tile_key;
165 #endif
166 
167  auto& tile = m_tile_map.at(tile_key);
168 
169  tile->saveIfModified();
170  m_total_memory_used -= tile->getTileSize();
171 
172  m_tile_map.erase(tile_key);
173  }
174 
177  assert(m_tile_list.size() > 0);
178  auto tile_to_remove = m_tile_list.back();
179  removeTile(tile_to_remove);
180  m_tile_list.pop_back();
181  }
182  }
183 
185 #ifndef NDEBUG
186  m_tile_logger.debug() << "Cache miss " << key;
187 #endif
188 
189  m_tile_map[key] = tile;
190  m_tile_list.push_front(key);
191  m_total_memory_used += tile->getTileSize();
192  }
193 
197 
200 
202 
204 
206 };
207 
208 }
209 
210 
211 #endif /* _SEFRAMEWORK_IMAGE_TILEMANAGER_H_ */
SourceXtractor::TileKey::m_source
std::shared_ptr< const ImageSourceBase > m_source
Definition: TileManager.h:43
SourceXtractor::TileManager::removeExtraTiles
void removeExtraTiles()
Definition: TileManager.h:175
SourceXtractor::TileManager::getInstance
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:137
std::lock
T lock(T... args)
SourceXtractor::TileManager::flush
void flush()
Definition: TileManager.h:104
std::string
STL class.
std::shared_ptr
STL class.
std::list
STL class.
SourceXtractor::TileManager::m_tile_list
std::list< TileKey > m_tile_list
Definition: TileManager.h:199
SourceXtractor::TileManager::m_tile_map
std::unordered_map< TileKey, std::shared_ptr< ImageTileBase > > m_tile_map
Definition: TileManager.h:198
SourceXtractor::TileManager::m_tile_width
int m_tile_width
Definition: TileManager.h:194
SourceXtractor::TileKey::operator==
bool operator==(const TileKey &other) const
Definition: TileManager.h:46
SourceXtractor::TileKey
Definition: TileManager.h:42
Elements::Logging
SourceXtractor::TileManager::addTile
void addTile(TileKey key, std::shared_ptr< ImageTileBase > tile)
Definition: TileManager.h:184
std::recursive_mutex
STL class.
std::lock_guard
STL class.
SourceXtractor::TileManager::s_instance
static std::shared_ptr< TileManager > s_instance
Definition: TileManager.h:205
SourceXtractor::TileManager::m_mutex
std::recursive_mutex m_mutex
Definition: TileManager.h:201
SourceXtractor::TileManager::saveAllTiles
void saveAllTiles()
Definition: TileManager.h:144
SourceXtractor::TileManager::m_tile_logger
Elements::Logging m_tile_logger
Definition: TileManager.h:203
SourceXtractor::TileKey::getRepr
std::string getRepr() const
Definition: TileManager.h:50
SourceXtractor::TileManager::m_max_memory
long m_max_memory
Definition: TileManager.h:195
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::TileManager::removeTile
void removeTile(TileKey tile_key)
Definition: TileManager.h:162
std::ostream
STL class.
SourceXtractor::TileManager::TileManager
TileManager()
Definition: TileManager.h:85
ImageTile.h
SourceXtractor::TileManager::~TileManager
virtual ~TileManager()
Definition: TileManager.h:89
SourceXtractor::TileManager::getTileWidth
int getTileWidth() const
Definition: TileManager.h:152
Elements::Logging::debug
void debug(const std::string &logMessage)
SourceXtractor::ImageSource
Definition: ImageSource.h:41
std::min
T min(T... args)
std::ostringstream
STL class.
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:93
SourceXtractor::operator<<
std::ostream & operator<<(std::ostream &out, const TileKey &tk)
Definition: TileManager.h:57
std
STL namespace.
SourceXtractor::TileManager::m_total_memory_used
long m_total_memory_used
Definition: TileManager.h:196
SourceXtractor::TileManager::m_tile_height
int m_tile_height
Definition: TileManager.h:194
SourceXtractor::TileManager::getTileForPixel
std::shared_ptr< ImageTile< T > > getTileForPixel(int x, int y, std::shared_ptr< const ImageSource< T >> source)
Definition: TileManager.h:115
ImageSource.h
std::ostringstream::str
T str(T... args)
std::size_t
Logging.h
SourceXtractor::TileManager::getTileHeight
int getTileHeight() const
Definition: TileManager.h:156
std::hash< SourceXtractor::TileKey >::operator()
std::size_t operator()(const SourceXtractor::TileKey &key) const
Definition: TileManager.h:69
y
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > y
Definition: MoffatModelFittingTask.cpp:93
std::unordered_map
STL class.
SourceXtractor::TileKey::m_tile_y
int m_tile_y
Definition: TileManager.h:44
SourceXtractor::TileManager::setOptions
void setOptions(int tile_width, int tile_height, int max_memory)
Definition: TileManager.h:94
SourceXtractor::TileKey::m_tile_x
int m_tile_x
Definition: TileManager.h:44
std::hash
Elements
SourceXtractor::TileManager
Definition: TileManager.h:82