CMS 3D CMS Logo

Pool.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_src_Pool_h
2 #define RecoTracker_MkFitCore_src_Pool_h
3 
4 #include "Matriplex/Memory.h"
5 #include "oneapi/tbb/concurrent_queue.h"
6 
7 namespace mkfit {
8 
12  template <typename TT>
13  class Pool {
14  public:
15  Pool() = default;
16 
17  ~Pool() { clear(); }
18 
19  void clear() {
20  TT *x = nullptr;
21  while (m_stack.try_pop(x)) {
22  destroy(x);
23  }
24  }
25 
26  size_t size() const { return m_stack.unsafe_size(); }
27 
28  void populate(int threads = Config::numThreadsFinder) {
29  for (int i = 0; i < threads; ++i) {
30  m_stack.push(create());
31  }
32  }
33 
34  auto makeOrGet() {
35  TT *x = nullptr;
36  if (not m_stack.try_pop(x)) {
37  x = create();
38  }
39  auto deleter = [this](TT *ptr) { this->addBack(ptr); };
40  return std::unique_ptr<TT, decltype(deleter)>(x, std::move(deleter));
41  }
42 
43  private:
44  TT *create() { return new (Matriplex::aligned_alloc64(sizeof(TT))) TT; };
45 
46  void destroy(TT *x) {
47  x->~TT();
48  std::free(x);
49  };
50 
51  void addBack(TT *x) { m_stack.push(x); }
52 
53  tbb::concurrent_queue<TT *> m_stack;
54  };
55 
56 } // end namespace mkfit
57 #endif
void free(void *ptr) noexcept
auto makeOrGet()
Definition: Pool.h:34
size_t size() const
Definition: Pool.h:26
constexpr int numThreadsFinder
Definition: Config.h:87
~Pool()
Definition: Pool.h:17
void clear()
Definition: Pool.h:19
Pool()=default
void populate(int threads=Config::numThreadsFinder)
Definition: Pool.h:28
void destroy(TT *x)
Definition: Pool.h:46
void * aligned_alloc64(std::size_t size)
Definition: Memory.h:13
float x
tbb::concurrent_queue< TT * > m_stack
Definition: Pool.h:53
TT * create()
Definition: Pool.h:44
def move(src, dest)
Definition: eostools.py:511
void addBack(TT *x)
Definition: Pool.h:51