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
logging_allocator.h
Go to the documentation of this file.
1 /*
2  * Logging_Allocator.h
3  *
4  * Created on: Jan 20, 2020
5  * Author: joseph
6  */
7 
8 #ifndef BLACKCAT_TENSORS_ALLOCATORS_LOGGING_ALLOCATOR_H_
9 #define BLACKCAT_TENSORS_ALLOCATORS_LOGGING_ALLOCATOR_H_
10 
11 #include "allocator_forwarder.h"
12 
13 namespace bc {
14 namespace allocators {
15 
16 namespace detail {
17  struct log_info {
18  unsigned max_allocated; //maximum number of bytes allocated
20  };
21 }
22 
23 template<class Allocator>
25 
28  using pointer = typename parent_type::pointer;
29 
30  template<class altT>
31  struct rebind
32  {
33  using other = Logging_Allocator<
34  typename parent_type::template rebind<altT>::other>;
35  };
36 
37  std::shared_ptr<detail::log_info> info =
38  std::shared_ptr<detail::log_info>(new detail::log_info {0, 0});
39 
40  template<class U>
41  Logging_Allocator(const Logging_Allocator<U>& other): info(other.info) {}
42  Logging_Allocator() = default;
43  Logging_Allocator(const Logging_Allocator&) = default;
45 
48 
49  pointer allocate(int size)
50  {
51  info->current_allocated += size * sizeof(value_type);
52  if (info->current_allocated > info->max_allocated){
53  info->max_allocated = info->current_allocated;
54  }
55  return parent_type::allocate(size);
56  }
57 
58  void deallocate(pointer ptr, bc::size_t size)
59  {
60  BC_ASSERT(info->current_allocated >= size * sizeof(value_type),
61  "BC_DEALLOCATION ERROR, DOUBLE DUPLICATION");
62  info->current_allocated -= size * sizeof(value_type);
63 
64  parent_type::deallocate(ptr, size);
65  }
66 
67 };
68 
69 }
70 }
71 
72 
73 
74 
75 #endif /* LOGGING_ALLOCATOR_H_ */
pointer allocate(int size)
Definition: logging_allocator.h:49
Definition: logging_allocator.h:31
unsigned current_allocated
Definition: logging_allocator.h:19
BCHOT self_type & operator=(const Expression_Base< Xpr > &param)
Definition: tensor_operations.h:19
int size_t
Definition: common.h:283
unsigned max_allocated
Definition: logging_allocator.h:18
Logging_Allocator(const Logging_Allocator< U > &other)
Definition: logging_allocator.h:41
Definition: logging_allocator.h:17
void deallocate(pointer ptr, bc::size_t size)
Definition: logging_allocator.h:58
Definition: allocator_forwarder.h:22
#define BC_ASSERT(condition, message)
Definition: common.h:185
Definition: logging_allocator.h:24
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22