Stan Math Library: stan/math/memory/stack_alloc.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

stack_alloc.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_MEMORY_STACK_ALLOC_HPP
2 #define STAN_MATH_MEMORY_STACK_ALLOC_HPP
3
4 // TODO(Bob): <cstddef> replaces this ifdef in C++11, until then this
5 // is best we can do to get safe pointer casts to uints.
6 #include <stdint.h>
7 #include <stan/math/prim/scal/meta/likely.hpp>
8 #include <cstdlib>
9 #include <cstddef>
10 #include <sstream>
11 #include <stdexcept>
12 #include <vector>
13
14 namespace stan {
15
16 namespace math {
17
29 template <typename T>
30 bool is_aligned(T* ptr, unsigned int bytes_aligned) {
31 return (reinterpret_cast<uintptr_t>(ptr) % bytes_aligned) == 0U;
32 }
33
34
35 namespace {
36 const size_t DEFAULT_INITIAL_NBYTES = 1 << 16; // 64KB
37
38
39 // FIXME: enforce alignment
40 // big fun to inline, but only called twice
41 inline char* eight_byte_aligned_malloc(size_t size) {
42 char* ptr = static_cast<char*>(malloc(size));
43 if (!ptr) return ptr; // malloc failed to alloc
44 if (!is_aligned(ptr, 8U)) {
45 std::stringstream s;
46 s << "invalid alignment to 8 bytes, ptr="
47 << reinterpret_cast<uintptr_t>(ptr)
48 << std::endl;
49 throw std::runtime_error(s.str());
50 }
51 return ptr;
52 }
53 }
54
74 class stack_alloc {
75 private:
76 std::vector<char*> blocks_; // storage for blocks,
77 // may be bigger than cur_block_
78 std::vector<size_t> sizes_; // could store initial & shift for others
79 size_t cur_block_; // index into blocks_ for next alloc
80 char* cur_block_end_; // ptr to cur_block_ptr_ + sizes_[cur_block_]
81 char* next_loc_; // ptr to next available spot in cur
82 // block
83 // next three for keeping track of nested allocations on top of stack:
84 std::vector<size_t> nested_cur_blocks_;
85 std::vector<char*> nested_next_locs_;
86 std::vector<char*> nested_cur_block_ends_;
87
88
97 char* move_to_next_block(size_t len) {
98 char* result;
99 ++cur_block_;
100 // Find the next block (if any) containing at least len bytes.
101 while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len))
102 ++cur_block_;
103 // Allocate a new block if necessary.
104 if (unlikely(cur_block_ >= blocks_.size())) {
105 // New block should be max(2*size of last block, len) bytes.
106 size_t newsize = sizes_.back() * 2;
107 if (newsize < len)
108 newsize = len;
109 blocks_.push_back(eight_byte_aligned_malloc(newsize));
110 if (!blocks_.back())
111 throw std::bad_alloc();
112 sizes_.push_back(newsize);
113 }
114 result = blocks_[cur_block_];
115 // Get the object's state back in order.
116 next_loc_ = result + len;
117 cur_block_end_ = result + sizes_[cur_block_];
118 return result;
119 }
120
121 public:
131 explicit stack_alloc(size_t initial_nbytes = DEFAULT_INITIAL_NBYTES) :
132 blocks_(1, eight_byte_aligned_malloc(initial_nbytes)),
133 sizes_(1, initial_nbytes),
134 cur_block_(0),
135 cur_block_end_(blocks_[0] + initial_nbytes),
136 next_loc_(blocks_[0]) {
137 if (!blocks_[0])
138 throw std::bad_alloc(); // no msg allowed in bad_alloc ctor
139 }
140
147 ~stack_alloc() {
148 // free ALL blocks
149 for (size_t i = 0; i < blocks_.size(); ++i)
150 if (blocks_[i])
151 free(blocks_[i]);
152 }
153
166 inline void* alloc(size_t len) {
167 // Typically, just return and increment the next location.
168 char* result = next_loc_;
169 next_loc_ += len;
170 // Occasionally, we have to switch blocks.
171 if (unlikely(next_loc_ >= cur_block_end_))
172 result = move_to_next_block(len);
173 return reinterpret_cast<void*>(result);
174 }
175
184 template <typename T>
185 inline
186 T* alloc_array(size_t n) {
187 return static_cast<T*>(alloc(n * sizeof(T)));
188 }
189
190
197 inline void recover_all() {
198 cur_block_ = 0;
199 next_loc_ = blocks_[0];
200 cur_block_end_ = next_loc_ + sizes_[0];
201 }
202
207 inline void start_nested() {
208 nested_cur_blocks_.push_back(cur_block_);
209 nested_next_locs_.push_back(next_loc_);
210 nested_cur_block_ends_.push_back(cur_block_end_);
211 }
212
216 inline void recover_nested() {
217 if (unlikely(nested_cur_blocks_.empty()))
218 recover_all();
219
220 cur_block_ = nested_cur_blocks_.back();
221 nested_cur_blocks_.pop_back();
222
223 next_loc_ = nested_next_locs_.back();
224 nested_next_locs_.pop_back();
225
226 cur_block_end_ = nested_cur_block_ends_.back();
227 nested_cur_block_ends_.pop_back();
228 }
229
235 inline void free_all() {
236 // frees all BUT the first (index 0) block
237 for (size_t i = 1; i < blocks_.size(); ++i)
238 if (blocks_[i])
239 free(blocks_[i]);
240 sizes_.resize(1);
241 blocks_.resize(1);
242 recover_all();
243 }
244
255 size_t bytes_allocated() {
256 size_t sum = 0;
257 for (size_t i = 0; i <= cur_block_; ++i) {
258 sum += sizes_[i];
259 }
260 return sum;
261 }
262 };
263
264 }
265 }
266 #endif
stan::math::sum
fvar< T > sum(const std::vector< fvar< T > > &m)
Return the sum of the entries of the specified standard vector.
Definition: sum.hpp:20
stan
Definition: log_sum_exp.hpp:8
stan::math::stack_alloc::~stack_alloc
~stack_alloc()
Destroy this memory allocator.
Definition: stack_alloc.hpp:147
stan::math::stack_alloc::recover_nested
void recover_nested()
recover memory back to the last start_nested call.
Definition: stack_alloc.hpp:216
unlikely
#define unlikely(x)
Definition: likely.hpp:9
stan::math::stack_alloc::free_all
void free_all()
Free all memory used by the stack allocator other than the initial block allocation back to the syste...
Definition: stack_alloc.hpp:235
stan::math::stack_alloc::recover_all
void recover_all()
Recover all the memory used by the stack allocator.
Definition: stack_alloc.hpp:197
stan::math::stack_alloc::bytes_allocated
size_t bytes_allocated()
Return number of bytes allocated to this instance by the heap.
Definition: stack_alloc.hpp:255
likely.hpp
stan::math::stack_alloc::alloc_array
T * alloc_array(size_t n)
Allocate an array on the arena of the specified size to hold values of the specified template paramet...
Definition: stack_alloc.hpp:186
stan::math::size
int size(const std::vector< T > &x)
Return the size of the specified standard vector.
Definition: size.hpp:17
stan::math::stack_alloc::start_nested
void start_nested()
Store current positions before doing nested operation so can recover back to start.
Definition: stack_alloc.hpp:207
stan::math::is_aligned
bool is_aligned(T *ptr, unsigned int bytes_aligned)
Return true if the specified pointer is aligned on the number of bytes.
Definition: stack_alloc.hpp:30
stan::math::stack_alloc::stack_alloc
stack_alloc(size_t initial_nbytes=DEFAULT_INITIAL_NBYTES)
Construct a resizable stack allocator initially holding the specified number of bytes.
Definition: stack_alloc.hpp:131
stan::math::stack_alloc
An instance of this class provides a memory pool through which blocks of raw memory may be allocated ...
Definition: stack_alloc.hpp:74
stan::math::stack_alloc::alloc
void * alloc(size_t len)
Return a newly allocated block of memory of the appropriate size managed by the stack allocator...
Definition: stack_alloc.hpp:166

[ Stan Home Page ] © 2011–2016, Stan Development Team.