@@ -26,22 +26,23 @@ public:
26
26
void Finish ();
27
27
28
28
template <typename T, typename ... Params>
29
- void ParallelFor (uint32_t begin, uint32_t end, T SerialFunction, Params&&... params) {
29
+ void ParallelFor (uint32_t begin, uint32_t end, int32_t n_tasks, T SerialFunction, Params&&... params) {
30
30
31
- int chunk = (end - begin) / m_nthreads;
32
- for (int i = 0 ; i < m_nthreads; ++i) {
31
+ n_tasks = (n_tasks >= m_nthreads) ? n_tasks : m_nthreads;
32
+ int chunk = (end - begin) / n_tasks;
33
+ for (int i = 0 ; i < n_tasks; ++i) {
33
34
m_promises.emplace_back ();
34
35
int mypromise = m_promises.size () - 1 ;
35
36
m_taskQueue.push ([=]{
36
37
uint32_t threadstart = begin + i*chunk;
37
- uint32_t threadstop = (i == m_nthreads - 1 ) ? end : threadstart + chunk;
38
+ uint32_t threadstop = (i == n_tasks - 1 ) ? end : threadstart + chunk;
38
39
for (uint32_t it = threadstart; it < threadstop; ++it) {
39
40
SerialFunction (it, params...);
40
41
}
41
42
m_promises[mypromise].set_value ();
42
43
});
43
44
}
44
- m_taskQueue.pop ()(); // master thread is also a worker
45
+ // m_taskQueue.pop()(); // master thread is also a worker
45
46
Finish ();
46
47
}
47
48
template <typename InputIt, typename T>
@@ -61,7 +62,7 @@ public:
61
62
m_promises[mypromise].set_value (); // master thread is also a worker
62
63
});
63
64
}
64
- m_taskQueue.pop ()();
65
+ // m_taskQueue.pop()();
65
66
Finish ();
66
67
}
67
68
0 commit comments