BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
polymorphic_allocator.h
Go to the documentation of this file.
1 /*
2  * Polymorphic_Allocator.h
3  *
4  * Created on: Mar 1, 2019
5  * Author: joseph
6  */
7 
8 #ifndef BC_CORE_CONTEXT_MEMORY_MANAGER_POLYMORPHIC_ALLOCATOR_H_
9 #define BC_CORE_CONTEXT_MEMORY_MANAGER_POLYMORPHIC_ALLOCATOR_H_
10 
11 #include <memory>
12 #include <typeinfo>
13 namespace bc {
14 namespace allocators {
24 namespace pa_detail {
25 
26 template<class ValueType, class SystemTag>
28 {
29  using system_tag = SystemTag;
30  using value_type = ValueType;
31 
32  virtual
34  virtual void deallocate(value_type* data, std::size_t sz) = 0;
35 
36  virtual Allocator_Base* clone() const = 0;
37  virtual ~Allocator_Base() {};
38 
39  virtual bool operator == (const Allocator_Base& other) const = 0;
40  virtual bool operator != (const Allocator_Base& other) const {
41  return !((*this) == other);
42  }
43  virtual std::string rebound_to_byte_hash() const = 0;
44 };
45 
46 template<class Allocator>
49  typename bc::allocator_traits<Allocator>::value_type,
50  typename bc::allocator_traits<Allocator>::system_tag>
51 {
54  using value_type = typename traits_type::value_type;
55 
56 private:
57 
60 
61  Allocator m_allocator;
62 
63 public:
64 
65  template<class altT>
66  struct rebind
67  {
68  using other = Derived_Allocator<
69  typename traits_type::template rebind_alloc<altT>>;
70  };
71 
73  return new Derived_Allocator(m_allocator);
74  }
75 
77 
79  m_allocator(alloc) {}
80 
82  m_allocator(alloc) {}
83 
84  virtual ~Derived_Allocator() override {}
85 
86 
87  static std::string static_hash()
88  {
89  return __PRETTY_FUNCTION__ ;
90  }
91 
92  virtual std::string rebound_to_byte_hash() const final {
93  using hash_t = std::conditional_t<
94  std::is_same<value_type, bc::allocators::Byte>::value,
95  self_type,
97  return hash_t::static_hash();
98  }
99 
100  virtual value_type* allocate(std::size_t sz) override final {
101  return m_allocator.allocate(sz);
102  }
103 
104  virtual void deallocate(value_type* data, std::size_t sz) override final {
105  m_allocator.deallocate(data, sz);
106  }
107 
108  virtual bool operator == (const Allocator_Base<value_type, system_tag>& other) const override
109  {
110  //same derived class
111  if (rebound_to_byte_hash() == other.rebound_to_byte_hash()) {
112  if (traits_type::is_always_equal::value)
113  return true;
114 
115  auto& cast_other = static_cast<const self_type&>(other);
116  return m_allocator == cast_other.m_allocator;
117  } else {
118  return false;
119  }
120  }
121 };
122 
123 
124 }
125 
126 template<class ValueType, class SystemTag>
128 {
129  using system_tag = SystemTag;
130  using value_type = ValueType;
131 
132 private:
133 
134  template<class... Args>
136 
137  template<class... Args>
138  using Allocator_Base = pa_detail::Allocator_Base<Args...>;
139 
141  using allocator_pointer_type = std::unique_ptr<allocator_type>;
143 
144  allocator_pointer_type m_allocator;
145 
146 public:
147 
148  template<class Allocator>
150  m_allocator(allocator_pointer_type(
151  new Derived_Allocator<Allocator>(alloc))) {}
152 
154  m_allocator(allocator_pointer_type(
155  new Derived_Allocator<default_allocator_type>())) {}
156 
158  m_allocator(pa.m_allocator->clone()) {}
159 
161  this->set_allocator(other.m_allocator->clone());
162  return *this;
163  }
164 
166  this->m_allocator = std::move(other.m_allocator);
167  return *this;
168  }
169 
171  return m_allocator->allocate(sz);
172  }
173 
175  m_allocator->deallocate(data, sz);
176  }
177 
178  template<class Allocator>
179  void set_allocator(const Allocator& alloc)
180  {
181  using traits = bc::allocator_traits<Allocator>;
182  using alloc_rb_t = typename traits::template rebind_alloc<value_type>;
183 
184  auto alloc_rebound = alloc_rb_t(alloc);
185 
186  m_allocator = allocator_pointer_type(
187  new Derived_Allocator<alloc_rb_t>(alloc_rebound));
188  }
189 
190  template<class AltT>
192  {
193  return *m_allocator == *(other.m_allocator);
194  }
195 
196  template<class AltT>
198  {
199  return !(*this == other);
200  }
201 
202 
203 };
204 
205 }
206 }
207 
208 
209 
210 #endif /* POLYMORPHIC_ALLOCATOR_H_ */
virtual std::string rebound_to_byte_hash() const final
Definition: polymorphic_allocator.h:92
virtual Allocator_Base * clone() const =0
virtual void deallocate(value_type *data, std::size_t sz)=0
Allocator_Base< value_type, system_tag > * clone() const override
Definition: polymorphic_allocator.h:72
system_tag system_tag
Definition: polymorphic_allocator.h:129
Polymorphic_Allocator(const Polymorphic_Allocator &pa)
Definition: polymorphic_allocator.h:157
Derived_Allocator(Allocator &&alloc)
Definition: polymorphic_allocator.h:81
virtual ~Derived_Allocator() override
Definition: polymorphic_allocator.h:84
static std::string static_hash()
Definition: polymorphic_allocator.h:87
bc::traits::conditional_detected_t< bc::traits::query_system_tag, Allocator, host_tag > system_tag
Definition: allocator_traits.h:23
typename traits_type::value_type value_type
Definition: polymorphic_allocator.h:54
void set_allocator(const Allocator &alloc)
Definition: polymorphic_allocator.h:179
virtual value_type * allocate(std::size_t sz)=0
typename traits_type::system_tag system_tag
Definition: polymorphic_allocator.h:53
BCHOT self_type & operator=(const Expression_Base< Xpr > &param)
Definition: tensor_operations.h:19
Polymorphic_Allocator(const Allocator &alloc)
Definition: polymorphic_allocator.h:149
Definition: polymorphic_allocator.h:127
Definition: allocator_traits.h:20
int size_t
Definition: common.h:283
Definition: allocators.h:20
Definition: polymorphic_allocator.h:47
Derived_Allocator()
Definition: polymorphic_allocator.h:76
virtual ~Allocator_Base()
Definition: polymorphic_allocator.h:37
virtual bool operator!=(const Allocator_Base &other) const
Definition: polymorphic_allocator.h:40
Derived_Allocator(const Allocator &alloc)
Definition: polymorphic_allocator.h:78
value_type value_type
Definition: polymorphic_allocator.h:130
Polymorphic_Allocator()
Definition: polymorphic_allocator.h:153
void deallocate(value_type *data, std::size_t sz)
Definition: polymorphic_allocator.h:174
virtual void deallocate(value_type *data, std::size_t sz) override final
Definition: polymorphic_allocator.h:104
value_type * allocate(std::size_t sz)
Definition: polymorphic_allocator.h:170
Definition: polymorphic_allocator.h:27
virtual bool operator==(const Allocator_Base &other) const =0
Definition: polymorphic_allocator.h:66
virtual std::string rebound_to_byte_hash() const =0
virtual value_type * allocate(std::size_t sz) override final
Definition: polymorphic_allocator.h:100
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22