SourceXtractorPlusPlus  0.13
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 
116 
119 
120  TileKey key {std::static_pointer_cast<const ImageSource>(source), x, y};
121  auto it = m_tile_map.find(key);
122  if (it != m_tile_map.end()) {
123 #ifndef NDEBUG
124  //m_tile_logger.debug() << "Cache hit " << key;
125 #endif
126  return std::dynamic_pointer_cast<ImageTile>(it->second);
127  } else {
128  auto tile = source->getImageTile(x, y,
129  std::min(m_tile_width, source->getWidth()-x), std::min(m_tile_height, source->getHeight()-y));
130  addTile(key, std::static_pointer_cast<ImageTile>(tile));
132  return tile;
133  }
134  }
135 
137  if (s_instance == nullptr) {
138  s_instance = std::make_shared<TileManager>();
139  }
140  return s_instance;
141  }
142 
143  void saveAllTiles() {
145 
146  for (auto tile_key : m_tile_list) {
147  m_tile_map.at(tile_key)->saveIfModified();
148  }
149  }
150 
151  int getTileWidth() const {
152  return m_tile_width;
153  }
154 
155  int getTileHeight() const {
156  return m_tile_height;
157  }
158 
159 private:
160 
161  void removeTile(TileKey tile_key) {
162 #ifndef NDEBUG
163  //m_tile_logger.debug() << "Cache eviction " << tile_key;
164 #endif
165 
166  auto& tile = m_tile_map.at(tile_key);
167 
168  tile->saveIfModified();
169  m_total_memory_used -= tile->getTileMemorySize();
170 
171  m_tile_map.erase(tile_key);
172  }
173 
176  assert(m_tile_list.size() > 0);
177  auto tile_to_remove = m_tile_list.back();
178  removeTile(tile_to_remove);
179  m_tile_list.pop_back();
180  }
181  }
182 
184 #ifndef NDEBUG
185  //m_tile_logger.debug() << "Cache miss " << key;
186 #endif
187 
188  m_tile_map[key] = tile;
189  m_tile_list.push_front(key);
190  m_total_memory_used += tile->getTileMemorySize();
191  }
192 
196 
199 
201 
203 
205 };
206 
207 }
208 
209 
210 #endif /* _SEFRAMEWORK_IMAGE_TILEMANAGER_H_ */
SourceXtractor::TileManager::removeExtraTiles
void removeExtraTiles()
Definition: TileManager.h:174
SourceXtractor::TileManager::getInstance
static std::shared_ptr< TileManager > getInstance()
Definition: TileManager.h:136
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:198
SourceXtractor::TileManager::m_tile_width
int m_tile_width
Definition: TileManager.h:193
SourceXtractor::TileKey::operator==
bool operator==(const TileKey &other) const
Definition: TileManager.h:46
SourceXtractor::TileKey
Definition: TileManager.h:42
Elements::Logging
SourceXtractor::TileKey::m_source
std::shared_ptr< const ImageSource > m_source
Definition: TileManager.h:43
std::recursive_mutex
STL class.
std::lock_guard< std::recursive_mutex >
SourceXtractor::TileManager::s_instance
static std::shared_ptr< TileManager > s_instance
Definition: TileManager.h:204
SourceXtractor::TileManager::m_mutex
std::recursive_mutex m_mutex
Definition: TileManager.h:200
SourceXtractor::TileManager::saveAllTiles
void saveAllTiles()
Definition: TileManager.h:143
SourceXtractor::TileManager::m_tile_logger
Elements::Logging m_tile_logger
Definition: TileManager.h:202
SourceXtractor::TileKey::getRepr
std::string getRepr() const
Definition: TileManager.h:50
SourceXtractor::TileManager::m_max_memory
long m_max_memory
Definition: TileManager.h:194
SourceXtractor
Definition: Aperture.h:30
SourceXtractor::TileManager::getTileForPixel
std::shared_ptr< ImageTile > getTileForPixel(int x, int y, std::shared_ptr< const ImageSource > source)
Definition: TileManager.h:114
SourceXtractor::TileManager::removeTile
void removeTile(TileKey tile_key)
Definition: TileManager.h:161
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:151
std::min
T min(T... args)
std::ostringstream
STL class.
SourceXtractor::TileManager::addTile
void addTile(TileKey key, std::shared_ptr< ImageTile > tile)
Definition: TileManager.h:183
SourceXtractor::TileManager::m_tile_map
std::unordered_map< TileKey, std::shared_ptr< ImageTile > > m_tile_map
Definition: TileManager.h:197
x
std::shared_ptr< DependentParameter< std::shared_ptr< EngineParameter > > > x
Definition: MoffatModelFittingTask.cpp:94
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:195
SourceXtractor::TileManager::m_tile_height
int m_tile_height
Definition: TileManager.h:193
ImageSource.h
std::ostringstream::str
T str(T... args)
std::size_t
Logging.h
SourceXtractor::TileManager::getTileHeight
int getTileHeight() const
Definition: TileManager.h:155
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:94
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