forked from frankyeh/TIPL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmt.hpp
More file actions
322 lines (281 loc) · 8.51 KB
/
Copy pathmt.hpp
File metadata and controls
322 lines (281 loc) · 8.51 KB
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#ifndef MULTI_THREAD_HPP
#define MULTI_THREAD_HPP
#include <chrono>
#include <future>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include "def.hpp"
namespace tipl{
class time
{
public:
// Constructor starts the timer immediately
time(const char* msg_) : msg(msg_), t1(std::chrono::high_resolution_clock::now()) {}
time() : t1(std::chrono::high_resolution_clock::now()) {}
void restart() { t1 = std::chrono::high_resolution_clock::now(); }
void start() { t1 = std::chrono::high_resolution_clock::now(); }
void stop() { t2 = std::chrono::high_resolution_clock::now(); }
template<typename T = std::chrono::milliseconds>
auto elapsed() {return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - t1).count();}
template<typename T = std::chrono::milliseconds>
auto total() {stop();return std::chrono::duration_cast<T>(t2 - t1).count();}
std::string to_string() {
auto now = std::chrono::high_resolution_clock::now();
auto duration = now - t1;
auto hours = std::chrono::duration_cast<std::chrono::hours>(duration);
duration -= hours;
auto minutes = std::chrono::duration_cast<std::chrono::minutes>(duration);
duration -= minutes;
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(duration);
std::ostringstream oss;
if (hours.count() > 0) oss << hours.count() << "h";
if (minutes.count() > 0) oss << minutes.count() << "m";
oss << seconds.count() << "s";
return oss.str();
}
~time()
{
if (!msg.empty())
std::cout << msg << elapsed<>() << std::endl;
}
private:
std::string msg;
std::chrono::high_resolution_clock::time_point t1, t2;
};
class estimate_time{
std::string name;
size_t n = 0;
double time_total = 0.0;
std::chrono::high_resolution_clock::time_point s;
public:
estimate_time(const char* name_):name(name_){}
~estimate_time()
{
if(n)
std::cout << name << time_total/double(n) << " microseconds" << std::endl;
}
void start(void)
{
s = std::chrono::high_resolution_clock::now();
}
void stop(void)
{
auto stop = std::chrono::high_resolution_clock::now();
time_total += std::chrono::duration_cast<std::chrono::microseconds>(stop-s).count();
++n;
}
};
inline auto main_thread_id = std::this_thread::get_id();
inline bool is_main_thread(void)
{
return main_thread_id == std::this_thread::get_id();
}
inline int max_thread_count = std::thread::hardware_concurrency();
enum par_for_type{
sequential = 0,
sequential_with_id = 1,
ranged = 2,
ranged_with_id = 3,
dynamic = 4,
dynamic_with_id = 5
};
inline std::atomic<bool> par_for_running = false;
struct par_for_guard
{
bool is_root;
par_for_guard(void)
{
is_root = !par_for_running.exchange(true);
}
~par_for_guard(void)
{
if(is_root)
par_for_running = false;
}
};
template <par_for_type type = dynamic,typename T,
typename Func,typename std::enable_if<
std::is_integral<T>::value ||
std::is_class<T>::value ||
std::is_pointer<T>::value,bool>::type = true>
__HOST__ void par_for(T from,T to,Func&& f,int thread_count)
{
if (from == to)
return;
size_t n = to - from;
thread_count = std::min<int>(thread_count, (int)n);
par_for_guard guard;
if(thread_count >= max_thread_count)
thread_count = (guard.is_root && n > 1) ? thread_count : 1;
#ifdef __CUDACC__
int cur_device = 0;
bool set_cuda_dev = false;
if constexpr(use_cuda)
{
set_cuda_dev = (thread_count > 1 && cudaGetDevice(&cur_device) == cudaSuccess);
}
#endif
std::vector<std::thread> workers;
if constexpr(type == dynamic || type == dynamic_with_id)
{
std::atomic<size_t> next_idx{0};
auto run = [=, &f, &next_idx](size_t id)
{
#ifdef __CUDACC__
if constexpr(use_cuda)
{
if(id && set_cuda_dev)
cudaSetDevice(cur_device);
}
#endif
for (size_t i = next_idx++; i < n; i = next_idx++)
{
if constexpr (type == dynamic_with_id)
f(from + i, id);
else
f(from + i);
}
};
for (int i = 1; i < thread_count; ++i)
workers.emplace_back(run, i);
run(0);
}
else
{
auto run = [=, &f](size_t id)
{
#ifdef __CUDACC__
if constexpr(use_cuda)
{
if(id && set_cuda_dev)
cudaSetDevice(cur_device);
}
#endif
size_t block = n / thread_count, rem = n % thread_count;
T beg = from + (id * block + std::min<size_t>(id, rem));
T end = beg + block + (id < rem);
if constexpr(type == sequential || type == sequential_with_id)
{
for(; beg != end; ++beg)
{
if constexpr(type == sequential_with_id)
f(beg, id);
else
f(beg);
}
}
else if constexpr(type == ranged || type == ranged_with_id)
{
if constexpr(type == ranged_with_id)
f(beg, end, id);
else
f(beg, end);
}
};
for (int i = 1; i < thread_count; ++i)
workers.emplace_back(run, i);
run(0);
}
for (auto& t : workers)
t.join();
}
template <par_for_type type = dynamic, typename T, typename Func,
typename std::enable_if_t<std::is_integral_v<T> || std::is_class_v<T> || std::is_pointer_v<T>, int> = 0>
inline void par_for(T from, T to, Func&& f) {
par_for<type>(from, to, std::forward<Func>(f), par_for_running ? 1 : max_thread_count);
}
template <par_for_type type = dynamic, typename T, typename Func, typename std::enable_if_t<std::is_integral_v<T>, int> = 0>
inline void par_for(T size, Func&& f, int tc = max_thread_count) {
par_for<type>(T(0), size, std::forward<Func>(f), tc);
}
template <typename T>
inline double estimate_run_time(T&& fun)
{
auto start = std::chrono::steady_clock::now();
std::forward<T>(fun)();
auto end = std::chrono::steady_clock::now();
return std::chrono::duration<double, std::micro>(end - start).count();
}
template<typename T>
void aggregate_results(std::vector<std::vector<T> >&& results,std::vector<T>& all_result_)
{
std::vector<size_t> insert_pos;
insert_pos.push_back(0);
for(size_t i = 0,sz = results.size();i < sz;++i)
insert_pos.push_back(insert_pos.back() + results[i].size());
std::vector<T> all_result(insert_pos.back());
tipl::par_for(results.size(),[&](unsigned int index)
{
std::move(results[index].begin(),results[index].end(),all_result.begin()+int64_t(insert_pos[index]));
});
all_result.swap(all_result_);
}
class thread{
std::unique_ptr<std::thread> th;
public:
bool running = false;
bool terminated = false;
#ifdef __CUDACC__
int cur_device = 0;
#endif
public:
thread(void){}
~thread(void)
{
#ifdef __CUDACC__
if constexpr(use_cuda)
cudaDeviceSynchronize();
#endif
clear();
}
void clear(void)
{
if(th)
{
terminated = true;
th->join();
th.reset();
}
terminated = false;
running = false;
}
template<typename lambda_type>
void run(lambda_type&& fun)
{
clear();
#ifdef __CUDACC__
if constexpr(use_cuda)
{
if(cudaGetDevice(&cur_device) != cudaSuccess)
throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
#endif
th = std::make_unique<std::thread>([this,fun = std::forward<lambda_type>(fun)]()
{
running = true;
#ifdef __CUDACC__
if constexpr(use_cuda)
{
if(cudaSetDevice(cur_device) != cudaSuccess)
throw std::runtime_error(cudaGetErrorName(cudaGetLastError()));
}
#endif
try{
fun();
}
catch(...){
running = false;
throw;
}
running = false;
});
}
void join(void)
{
th->join();
}
};
}
#endif // MULTI_THREAD_HPP