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() {
18  TT *x = nullptr;
19  while (m_stack.try_pop(x)) {
20  destroy(x);
21  }
22  }
23 
24  size_t size() const { return m_stack.unsafe_size(); }
25 
26  void populate(int threads = Config::numThreadsFinder) {
27  for (int i = 0; i < threads; ++i) {
28  m_stack.push(create());
29  }
30  }
31 
32  auto makeOrGet() {
33  TT *x = nullptr;
34  if (not m_stack.try_pop(x)) {
35  x = create();
36  }
37  auto deleter = [this](TT *ptr) { this->addBack(ptr); };
38  return std::unique_ptr<TT, decltype(deleter)>(x, std::move(deleter));
39  }
40 
41  private:
42  TT *create() { return new (Matriplex::aligned_alloc64(sizeof(TT))) TT; };
43 
44  void destroy(TT *x) {
45  x->~TT();
46  std::free(x);
47  };
48 
49  void addBack(TT *x) { m_stack.push(x); }
50 
51  tbb::concurrent_queue<TT *> m_stack;
52  };
53 
54 } // end namespace mkfit
55 #endif
auto makeOrGet()
Definition: Pool.h:32
size_t size() const
Definition: Pool.h:24
constexpr int numThreadsFinder
Definition: Config.h:85
~Pool()
Definition: Pool.h:17
Pool()=default
void populate(int threads=Config::numThreadsFinder)
Definition: Pool.h:26
void destroy(TT *x)
Definition: Pool.h:44
void * aligned_alloc64(std::size_t size)
Definition: Memory.h:13
float x
tbb::concurrent_queue< TT * > m_stack
Definition: Pool.h:51
TT * create()
Definition: Pool.h:42
def move(src, dest)
Definition: eostools.py:511
void addBack(TT *x)
Definition: Pool.h:49