/
parContextManager.cpp
259 lines (230 loc) · 8.64 KB
/
parContextManager.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//Implementation of the parallel statement execution manager
#include "parContextManager.h"
StatementContext::StatementContext() {
//Do nothing
}
StatementContext::StatementContext(StatementContext&& sc) {
this->int_future_id_map = std::move(sc.int_future_id_map);
this->float_future_id_map = std::move(sc.float_future_id_map);
this->intptr_future_id_map = std::move(sc.intptr_future_id_map);
this->floatptr_future_id_map = std::move(sc.floatptr_future_id_map);
this->void_future_id_map = std::move(sc.void_future_id_map);
}
/*=================================StatementContext=================================*/
void StatementContext::addIntFuture(std::future<int64_t>& f, const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
assert(int_future_id_map.count(id)==0 && "Statement id already exists");
int_future_id_map.insert(std::pair<int64_t,std::future<int64_t>>(id,std::move(f)));
}
void StatementContext::addFloatFuture(std::future<double>& f, const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
assert(float_future_id_map.count(id)==0 && "Statement id already exists");
float_future_id_map.insert(std::pair<int64_t,std::future<double>>(id,std::move(f)));
}
void StatementContext::addIntptrFuture(std::future<int64_t*>& f, const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
assert(intptr_future_id_map.count(id)==0 && "Statement id already exists");
intptr_future_id_map.insert(std::pair<int64_t,std::future<int64_t*>>(id,std::move(f)));
}
void StatementContext::addFloatptrFuture(std::future<double*>& f, const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
assert(floatptr_future_id_map.count(id)==0 && "Statement id already exists");
floatptr_future_id_map.insert(std::pair<int64_t,std::future<double*>>(id,std::move(f)));
}
void StatementContext::addVoidFuture(std::future<void>& f, const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
assert(void_future_id_map.count(id)==0 && "Statement id already exists");
void_future_id_map.insert(std::pair<int64_t,std::future<void>>(id,std::move(f)));
}
std::future<int64_t> StatementContext::getIntFuture(const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
auto f = std::move(int_future_id_map.at(id));
int_future_id_map.erase(id);
return f;
}
std::future<double> StatementContext::getFloatFuture(const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
auto f = std::move(float_future_id_map.at(id));
float_future_id_map.erase(id);
return f;
}
std::future<int64_t*> StatementContext::getIntptrFuture(const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
auto f = std::move(intptr_future_id_map.at(id));
intptr_future_id_map.erase(id);
return f;
}
std::future<double*> StatementContext::getFloatptrFuture(const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
auto f = std::move(floatptr_future_id_map.at(id));
floatptr_future_id_map.erase(id);
return f;
}
std::future<void> StatementContext::getVoidFuture(const int64_t id) {
std::lock_guard<std::mutex> section_monitor(map_mutex);
auto f = std::move(void_future_id_map.at(id));
void_future_id_map.erase(id);
return f;
}
/*=================================ParContextManager=================================*/
ParContextManager::ParContextManager() {
set_max_threads();
thread_count = 0;
next_cid = 0;
}
int64_t ParContextManager::make_context() {
std::lock_guard<std::mutex> section_monitor(mutex);
int64_t cid = next_cid++;
context_map.insert(std::pair<int64_t,StatementContext>(cid,std::move(StatementContext())));
return cid;
}
void ParContextManager::destroy_context(const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
assert(context_map.count(cid)==1 && "Couldnt find exactly one context to destroy");
context_map.erase(cid);
}
void ParContextManager::sched_int(int64_t (*statement)(void*),void* env,const int64_t id,const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
std::future<int64_t> promise;
if (thread_count >= max_threads) {
promise = std::async(std::launch::deferred,statement,env);
} else {
promise = std::async(std::launch::async,statement,env);
thread_count++;
}
context_map.at(cid).addIntFuture(promise,id);
}
void ParContextManager::sched_float(double (*statement)(void*),void* env,const int64_t id,const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
std::future<double> promise;
if (thread_count >= max_threads) {
promise = std::async(std::launch::deferred,statement,env);
} else {
promise = std::async(std::launch::async,statement,env);
thread_count++;
}
context_map.at(cid).addFloatFuture(promise,id);
}
void ParContextManager::sched_intptr(int64_t* (*statement)(void*),void* env,int64_t id,const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
std::future<int64_t*> promise;
if (thread_count >= max_threads) {
promise = std::async(std::launch::deferred,statement,env);
} else {
promise = std::async(std::launch::async,statement,env);
thread_count++;
}
context_map.at(cid).addIntptrFuture(promise,id);
}
void ParContextManager::sched_floatptr(double* (*statement)(void*),void* env,int64_t id,const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
std::future<double*> promise;
if (thread_count >= max_threads) {
promise = std::async(std::launch::deferred,statement,env);
} else {
promise = std::async(std::launch::async,statement,env);
thread_count++;
}
context_map.at(cid).addFloatptrFuture(promise,id);
}
void ParContextManager::sched_void(void (*statement)(void*),void* env,int64_t id,const int64_t cid) {
std::lock_guard<std::mutex> section_monitor(mutex);
std::future<void> promise;
if (thread_count >= max_threads) {
promise = std::async(std::launch::deferred,statement,env);
} else {
promise = std::async(std::launch::async,statement,env);
thread_count++;
}
context_map.at(cid).addVoidFuture(promise,id);
}
int64_t ParContextManager::recon_int(const int64_t original,const int64_t known,
const int64_t id,const int64_t max,const int64_t cid) {
mutex.lock();
std::chrono::milliseconds span(0);
std::future<int64_t> fv = context_map.at(cid).getIntFuture(id);
int64_t v;
mutex.unlock();
if (fv.wait_for(span)==std::future_status::deferred) {
v = fv.get();
} else {
v = fv.get();
mutex.lock();
thread_count--;
mutex.unlock();
}
return v;
}
double ParContextManager::recon_float(const double original,const int64_t known,
const int64_t id,const int64_t max,const int64_t cid) {
mutex.lock();
std::chrono::milliseconds span(0);
std::future<double> fv = context_map.at(cid).getFloatFuture(id);
double v;
mutex.unlock();
if (fv.wait_for(span)==std::future_status::deferred) {
v = fv.get();
} else {
v = fv.get();
mutex.lock();
thread_count--;
mutex.unlock();
}
return v;
}
int64_t* ParContextManager::recon_intptr(const int64_t* original,const int64_t known,
const int64_t id,const int64_t max,const int64_t cid) {
mutex.lock();
std::chrono::milliseconds span(0);
std::future<int64_t*> fv = context_map.at(cid).getIntptrFuture(id);
int64_t* v;
mutex.unlock();
if (fv.wait_for(span)==std::future_status::deferred) {
v = fv.get();
} else {
v = fv.get();
mutex.lock();
thread_count--;
mutex.unlock();
}
return v;
}
double* ParContextManager::recon_floatptr(const double* original,const int64_t known,
const int64_t id,const int64_t max,const int64_t cid) {
mutex.lock();
std::chrono::milliseconds span(0);
std::future<double*> fv = context_map.at(cid).getFloatptrFuture(id);
double* v;
mutex.unlock();
if (fv.wait_for(span)==std::future_status::deferred) {
v = fv.get();
} else {
v = fv.get();
mutex.lock();
thread_count--;
mutex.unlock();
}
return v;
}
void ParContextManager::recon_void(const int64_t id,const int64_t max,const int64_t cid) {
mutex.lock();
std::chrono::milliseconds span(0);
std::future<void> fv = context_map.at(cid).getVoidFuture(id);
mutex.unlock();
if (fv.wait_for(span)==std::future_status::deferred) {
fv.get();
} else {
fv.get();
mutex.lock();
thread_count--;
mutex.unlock();
}
}
void ParContextManager::set_max_threads() {
unsigned long dth = std::thread::hardware_concurrency();
printf("Detected %d compute elements.\n",(int)dth);
if (dth < 2) max_threads = 2;
else if (dth > 4) max_threads = 4;
else max_threads = dth;
printf("Setting max execution threads: %d\n",(int)max_threads);
}