-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurrent.h
More file actions
73 lines (59 loc) · 2.11 KB
/
Copy pathconcurrent.h
File metadata and controls
73 lines (59 loc) · 2.11 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
#pragma once
#include <algorithm>
#include <atomic>
#include <mutex>
#include <thread>
#include <vector>
#include "config.h"
namespace deglib::concurrent {
// Multithreaded executor
template <class FuncType>
inline void parallel_for(size_t start, size_t end, size_t numThreads, size_t batchSize, FuncType fn) {
// default number of threads is half of the hardware concurrency
if (numThreads <= 0) {
numThreads = std::thread::hardware_concurrency() / 2;
}
// default batch size is 1% of the total number of elements per thread
if (batchSize <= 0) {
batchSize = std::max(static_cast<size_t>(1), (end-start) / (static_cast<size_t>(numThreads) * 100));
}
if (numThreads <= 1) {
for (size_t id = start; id < end; id++) {
fn(id, 0);
}
} else {
std::vector<std::thread> threads;
std::atomic<size_t> current(start);
// keep track of exceptions in threads
std::exception_ptr lastException = nullptr;
std::mutex lastExceptMutex;
for (size_t threadId = 0; threadId < numThreads; ++threadId) {
threads.push_back(std::thread([&, threadId] {
while (true) {
size_t batchStart = current.fetch_add(batchSize);
if (batchStart >= end) {
break;
}
size_t batchEnd = std::min(batchStart + batchSize, end);
try {
for (size_t id = batchStart; id < batchEnd; id++) {
fn(id, threadId);
}
} catch (...) {
std::unique_lock<std::mutex> lastExcepLock(lastExceptMutex);
lastException = std::current_exception();
current = end;
break;
}
}
}));
}
for (auto& thread : threads) {
thread.join();
}
if (lastException) {
std::rethrow_exception(lastException);
}
}
}
} // namespace deglib::concurrent