alpaqa 0.0.1
Nonconvex constrained optimization
alloc.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <algorithm>
6#include <cassert>
7#include <stack>
8#include <utility>
9#include <vector>
10
11namespace alpaqa {
12
14 private:
15 size_t num_vec;
16 Eigen::Index n;
17 std::vector<real_t> storage;
18 struct findstack : std::stack<real_t *, std::vector<real_t *>> {
19 using stack::stack;
20
21 auto begin() { return this->c.begin(); }
22 auto begin() const { return this->c.begin(); }
23 auto end() { return this->c.end(); }
24 auto end() const { return this->c.end(); }
25
26 auto cbegin() const { return this->c.cbegin(); }
27 auto cend() const { return this->c.cend(); }
28
29 auto rbegin() { return this->c.rbegin(); }
30 auto rbegin() const { return this->c.rbegin(); }
31 auto rend() { return this->c.rend(); }
32 auto rend() const { return this->c.rend(); }
33
34 auto crbegin() const { return this->c.crbegin(); }
35 auto crend() const { return this->c.crend(); }
37 size_t highwatermark = 0;
38
39 public:
40 vec_allocator(size_t num_vec, Eigen::Index n)
41 : num_vec(num_vec), n(n), storage(num_vec * n, NaN) {
42 for (auto it = storage.begin(); it < storage.end(); it += n)
43 stack.push(&*it);
44 }
45
46 vec_allocator(const vec_allocator &) = delete;
50
52 using mvec = Eigen::Map<vec>;
55
57 : v{dptr, n}, alloc{alloc} {}
59 : v{std::move(v)}, alloc{alloc} {}
61 assert(alloc);
62 alloc->free(v);
63 }
66 : v{std::exchange(o.v, {nullptr, 0})}, //
67 alloc{std::exchange(o.alloc, nullptr)} {}
70 this->v = std::exchange(o.v, {nullptr, 0});
71 this->alloc = std::exchange(o.alloc, nullptr);
72 return *this;
73 }
74
76 this->v = v;
77 return *this;
78 }
79 operator crvec() const { return v; }
80 operator rvec() { return v; }
81 };
82
83 auto alloc() {
84 if (stack.empty())
85 throw std::bad_alloc();
86 auto dptr = stack.top();
87 stack.pop();
88 highwatermark = std::max(used_space(), highwatermark);
89 return Eigen::Map<vec>(dptr, n);
90 }
91
92 alloc_raii_wrapper alloc_raii() { return {alloc(), this}; }
93
94 void free(rvec v) {
95 auto dptr = v.data();
96 assert(dptr >= &*storage.begin());
97 assert(dptr <= &*storage.end() - n);
98 assert(std::find(stack.begin(), stack.end(), dptr) == stack.end() &&
99 "double free");
100 stack.push(dptr);
101 }
102
103 template <class... Vecs>
104 void free(rvec first, Vecs &&...vecs) {
105 free(first);
106 free(vecs...);
107 }
108
109 size_t size() const { return stack.size(); }
110 size_t used_space() const { return num_vec - size(); }
111
112 Eigen::Index vector_size() const { return n; }
113
114 size_t highwater() const { return highwatermark; }
115};
116
117} // namespace alpaqa
alloc_raii_wrapper alloc_raii()
Definition: alloc.hpp:92
size_t size() const
Definition: alloc.hpp:109
Eigen::Index vector_size() const
Definition: alloc.hpp:112
void free(rvec v)
Definition: alloc.hpp:94
alpaqa::vec_allocator::findstack stack
vec_allocator(const vec_allocator &)=delete
vec_allocator(size_t num_vec, Eigen::Index n)
Definition: alloc.hpp:40
size_t highwatermark
Definition: alloc.hpp:37
vec_allocator & operator=(vec_allocator &&)=delete
void free(rvec first, Vecs &&...vecs)
Definition: alloc.hpp:104
vec_allocator(vec_allocator &&)=delete
Eigen::Index n
Definition: alloc.hpp:16
size_t highwater() const
Definition: alloc.hpp:114
vec_allocator & operator=(const vec_allocator &)=delete
std::vector< real_t > storage
Definition: alloc.hpp:17
size_t used_space() const
Definition: alloc.hpp:110
int n
Definition: test.py:40
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
constexpr real_t NaN
Not a number.
Definition: vec.hpp:28
double real_t
Default floating point type.
Definition: vec.hpp:8
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
alloc_raii_wrapper(alloc_raii_wrapper &&o)
Definition: alloc.hpp:65
alloc_raii_wrapper & operator=(crvec v)
Definition: alloc.hpp:75
alloc_raii_wrapper(real_t *dptr, Eigen::Index n, vec_allocator *alloc)
Definition: alloc.hpp:56
alloc_raii_wrapper(mvec &&v, vec_allocator *alloc)
Definition: alloc.hpp:58
alloc_raii_wrapper & operator=(const alloc_raii_wrapper &)=delete
alloc_raii_wrapper & operator=(alloc_raii_wrapper &&o)
Definition: alloc.hpp:69
alloc_raii_wrapper(const alloc_raii_wrapper &)=delete