forked from msupernaw/ATL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableInfo.hpp
224 lines (173 loc) · 5.46 KB
/
VariableInfo.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: VariableInfo.hpp
* Author: matthewsupernaw
*
* Created on November 2, 2016, 9:55 AM
*/
#ifndef VARIABLEINFO_HPP
#define VARIABLEINFO_HPP
#include <stack>
#include <mutex>
#include <vector>
#include <iostream>
namespace atl {
/*!
* Creates a unique identifier for variables. Identifiers are recyclable.
* @return
*/
class VariableIdGenerator {
std::stack<uint32_t> available;
std::atomic<uint32_t> available_size;
static std::mutex mutex_g;
class SpinLock {
std::atomic_flag locked = ATOMIC_FLAG_INIT;
public:
void lock() {
while (locked.test_and_set(std::memory_order_acquire)) {
}
}
void unlock() {
locked.clear(std::memory_order_release);
}
};
SpinLock lock;
public:
static std::shared_ptr<VariableIdGenerator> instance();
const uint32_t next() {
#ifdef ATL_THREAD_SAFE
lock.lock();
#endif
uint32_t ret;
if (!available.empty() > 0) {
ret = available.top();
available.pop();
} else {
ret = ++_id;
}
#ifdef ATL_THREAD_SAFE
lock.unlock();
#endif
return ret; //(++_id);
}
void release(const uint32_t& id) {
#ifdef ATL_THREAD_SAFE
lock.lock();
#endif
available.push(id);
available_size++;
#ifdef ATL_THREAD_SAFE
lock.unlock();
#endif
}
const uint32_t current() {
return _id;
}
// private:
VariableIdGenerator() : available_size(0), _id(1) {
}
std::atomic<uint32_t> _id;
};
std::mutex VariableIdGenerator::mutex_g;
static std::shared_ptr<VariableIdGenerator> only_copy;
inline std::shared_ptr<VariableIdGenerator>
VariableIdGenerator::instance() {
if (!only_copy) {
only_copy = std::make_shared<VariableIdGenerator>();
}
return only_copy;
}
class VISpinLock {
std::atomic_flag locked = ATOMIC_FLAG_INIT;
public:
void lock() {
while (locked.test_and_set(std::memory_order_acquire)) {
}
}
void unlock() {
locked.clear(std::memory_order_release);
}
};
/**
* Holds unique info for \ref Variable objects.
*/
template<typename REAL_T>
struct VariableInfo {
static std::vector<std::shared_ptr<VariableInfo<REAL_T> > > ptrs;
static VISpinLock vinfo_mutex_g;
static std::vector<VariableInfo<REAL_T>* > freed;
std::atomic<int> count;
std::atomic<int> live;
uint32_t id;
REAL_T value;
bool is_nl = false;
long index = -999;
std::vector<REAL_T> tayor_coefficients;
/**
* Constructor
*
* @param value
*/
VariableInfo(REAL_T value = static_cast<REAL_T> (0.0)) :
id(VariableIdGenerator::instance()->next()),live(1), count(1), value(value) {
}
VariableInfo(const VariableInfo<REAL_T>& other) :
count(other.count), id(other.id), value(other.value), is_nl(other.is_nl), index(other.index) {
}
/**
* Destructor.
*/
~VariableInfo() {
// if(VariableIdGenerator::instance(). != 0)
// VariableIdGenerator::instance()->release(id);
}
inline void Aquire() {
count++;
}
inline void Release() {
count--;
if ((count) == 0) {
//store this pointer in the freed list and delete when the gradient
//structure resets.
#ifdef ATL_THREAD_SAFE
VariableInfo<REAL_T>::vinfo_mutex_g.lock();
freed.push_back(this);
VariableInfo<REAL_T>::vinfo_mutex_g.unlock();
#else
freed.push_back(this);
#endif
}
}
static void FreeAll() {
// VariableInfo<REAL_T>::vinfo_mutex_g.lock();
//#pragma unroll
// for (int i = 0; i < freed.size(); i++) {
// if (freed[i] != NULL) {//memory pool may have destructed first
// VariableIdGenerator::instance()->release(freed[i]->id);
// freed[i]->value = 0;
// freed[i]->is_nl = false;
//// freed[i]->id= 0;
// delete freed[i];
// }
// }
// freed.resize(0);
// VariableInfo<REAL_T>::vinfo_mutex_g.unlock();
}
};
template<typename REAL_T>
std::vector<std::shared_ptr<VariableInfo<REAL_T> > > VariableInfo<REAL_T>::ptrs;
template<typename REAL_T>
VISpinLock VariableInfo<REAL_T>::vinfo_mutex_g;
template<typename REAL_T>
std::vector<VariableInfo<REAL_T>* > VariableInfo<REAL_T>::freed;
template<typename REAL_T>
std::ostream& operator<<(std::ostream& out, const VariableInfo<REAL_T>& vi) {
out << vi.id;
return out;
}
}
#endif /* VARIABLEINFO_HPP */