| Stan Math Library 2.12.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 namespace math {
16
28 template <typename T>
29 bool is_aligned(T* ptr, unsigned int bytes_aligned) {
30 return (reinterpret_cast<uintptr_t>(ptr) % bytes_aligned) == 0U;
31 }
32
33 namespace {
34 const size_t DEFAULT_INITIAL_NBYTES = 1 << 16; // 64KB
35
36 // FIXME: enforce alignment
37 // big fun to inline, but only called twice
38 inline char* eight_byte_aligned_malloc(size_t size) {
39 char* ptr = static_cast<char*>(malloc(size));
40 if (!ptr) return ptr; // malloc failed to alloc
41 if (!is_aligned(ptr, 8U)) {
42 std::stringstream s;
43 s << "invalid alignment to 8 bytes, ptr="
44 << reinterpret_cast<uintptr_t>(ptr)
45 << std::endl;
46 throw std::runtime_error(s.str());
47 }
48 return ptr;
49 }
50 }
51
71 class stack_alloc {
72 private:
73 std::vector<char*> blocks_; // storage for blocks,
74 // may be bigger than cur_block_
75 std::vector<size_t> sizes_; // could store initial & shift for others
76 size_t cur_block_; // index into blocks_ for next alloc
77 char* cur_block_end_; // ptr to cur_block_ptr_ + sizes_[cur_block_]
78 char* next_loc_; // ptr to next available spot in cur
79 // block
80 // next three for keeping track of nested allocations on top of stack:
81 std::vector<size_t> nested_cur_blocks_;
82 std::vector<char*> nested_next_locs_;
83 std::vector<char*> nested_cur_block_ends_;
84
93 char* move_to_next_block(size_t len) {
94 char* result;
95 ++cur_block_;
96 // Find the next block (if any) containing at least len bytes.
97 while ((cur_block_ < blocks_.size()) && (sizes_[cur_block_] < len))
98 ++cur_block_;
99 // Allocate a new block if necessary.
100 if (unlikely(cur_block_ >= blocks_.size())) {
101 // New block should be max(2*size of last block, len) bytes.
102 size_t newsize = sizes_.back() * 2;
103 if (newsize < len)
104 newsize = len;
105 blocks_.push_back(eight_byte_aligned_malloc(newsize));
106 if (!blocks_.back())
107 throw std::bad_alloc();
108 sizes_.push_back(newsize);
109 }
110 result = blocks_[cur_block_];
111 // Get the object's state back in order.
112 next_loc_ = result + len;
113 cur_block_end_ = result + sizes_[cur_block_];
114 return result;
115 }
116
117 public:
127 explicit stack_alloc(size_t initial_nbytes = DEFAULT_INITIAL_NBYTES) :
128 blocks_(1, eight_byte_aligned_malloc(initial_nbytes)),
129 sizes_(1, initial_nbytes),
130 cur_block_(0),
131 cur_block_end_(blocks_[0] + initial_nbytes),
132 next_loc_(blocks_[0]) {
133 if (!blocks_[0])
134 throw std::bad_alloc(); // no msg allowed in bad_alloc ctor
135 }
136
143 ~stack_alloc() {
144 // free ALL blocks
145 for (size_t i = 0; i < blocks_.size(); ++i)
146 if (blocks_[i])
147 free(blocks_[i]);
148 }
149
162 inline void* alloc(size_t len) {
163 // Typically, just return and increment the next location.
164 char* result = next_loc_;
165 next_loc_ += len;
166 // Occasionally, we have to switch blocks.
167 if (unlikely(next_loc_ >= cur_block_end_))
168 result = move_to_next_block(len);
169 return reinterpret_cast<void*>(result);
170 }
171
180 template <typename T>
181 inline
182 T* alloc_array(size_t n) {
183 return static_cast<T*>(alloc(n * sizeof(T)));
184 }
185
192 inline void recover_all() {
193 cur_block_ = 0;
194 next_loc_ = blocks_[0];
195 cur_block_end_ = next_loc_ + sizes_[0];
196 }
197
202 inline void start_nested() {
203 nested_cur_blocks_.push_back(cur_block_);
204 nested_next_locs_.push_back(next_loc_);
205 nested_cur_block_ends_.push_back(cur_block_end_);
206 }
207
211 inline void recover_nested() {
212 if (unlikely(nested_cur_blocks_.empty()))
213 recover_all();
214
215 cur_block_ = nested_cur_blocks_.back();
216 nested_cur_blocks_.pop_back();
217
218 next_loc_ = nested_next_locs_.back();
219 nested_next_locs_.pop_back();
220
221 cur_block_end_ = nested_cur_block_ends_.back();
222 nested_cur_block_ends_.pop_back();
223 }
224
230 inline void free_all() {
231 // frees all BUT the first (index 0) block
232 for (size_t i = 1; i < blocks_.size(); ++i)
233 if (blocks_[i])
234 free(blocks_[i]);
235 sizes_.resize(1);
236 blocks_.resize(1);
237 recover_all();
238 }
239
250 size_t bytes_allocated() {
251 size_t sum = 0;
252 for (size_t i = 0; i <= cur_block_; ++i) {
253 sum += sizes_[i];
254 }
255 return sum;
256 }
257 };
258
259 }
260 }
261 #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:143
stan::math::stack_alloc::recover_nested
void recover_nested()
recover memory back to the last start_nested call.
Definition: stack_alloc.hpp:211
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:230
stan::math::stack_alloc::recover_all
void recover_all()
Recover all the memory used by the stack allocator.
Definition: stack_alloc.hpp:192
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:250
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:182
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:202
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:29
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:127
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:71
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:162

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