Skip to content

Commit faa2f04

Browse files
author
Chris Sullivan
committed
Removed ParallelFor that depended on a static class function,
which is replaced by a new variadic templated ParallelFor. After testing the performance of each, I was able to conclude that they perform identically, so the earlier version is no longer needed.
1 parent a803548 commit faa2f04

File tree

2 files changed

+5
-42
lines changed

2 files changed

+5
-42
lines changed

example.cc

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
#include <iostream>
66

7-
static double* a;
8-
static double* b;
97

108
using namespace std;
119

@@ -19,30 +17,16 @@ int main () {
1917
ThreadPool pool(8);
2018

2119
int N = 1e9;
22-
a = (double*)calloc(N,sizeof(double));
23-
b = (double*)calloc(N,sizeof(double));
20+
auto a = (double*)calloc(N,sizeof(double));
21+
auto b = (double*)calloc(N,sizeof(double));
2422
for (int i=0; i<N; i++) { b[i] = i; }
2523

26-
SERIAL_OPERATION(static_scale, a[i]=4*b[i]);
27-
{
28-
// This is a cold start loop, it is not timed
29-
pool.ParallelFor<static_scale>(0,N);
30-
}
24+
25+
pool.ParallelFor(0,N,raw_scale,a,b); // cold start
3126

3227
int ntrials = 10;
3328
double tperformance = 0.0;
3429
for (int i=0; i<ntrials; i++)
35-
{
36-
Timer timer([&](int elapsed){
37-
cout << "Trial " << i << ": "<< elapsed*1e-6 << " ms\n";
38-
tperformance+=elapsed;
39-
});
40-
pool.ParallelFor<static_scale>(0,N);
41-
}
42-
cout << "Average: " << tperformance*1e-6 / ntrials << " ms\n\n";
43-
44-
tperformance = 0.0;
45-
for (int i=0; i<ntrials; i++)
4630
{
4731
Timer timer([&](int elapsed){
4832
cout << "Trial " << i << ": "<< elapsed*1e-6 << " ms\n";
@@ -53,6 +37,6 @@ int main () {
5337
cout << "Average: " << tperformance*1e-6 / ntrials << " ms\n\n";
5438

5539

56-
cin.get();
40+
5741
return 0;
5842
}

include/ThreadPool.hh

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ public:
2323
void Worker();
2424
void JoinAll();
2525

26-
template <typename ClassFunction>
27-
void ParallelFor(uint32_t begin, uint32_t end) {
28-
29-
int chunk = (end - begin) / m_nthreads;
30-
for (int i = 0; i < m_nthreads; ++i) {
31-
m_promises.emplace_back();
32-
int mypromise = m_promises.size() - 1;
33-
Enqueue([=]{
34-
int threadstart = begin + i*chunk;
35-
int threadstop = (i == m_nthreads - 1) ? end : threadstart + chunk;
36-
for (int it = threadstart; it < threadstop; ++it) {
37-
ClassFunction::SerialFunction(it);
38-
}
39-
m_promises[mypromise].set_value();
40-
});
41-
}
42-
Finish();
43-
}
4426
template <typename T, typename... Params>
4527
void ParallelFor(uint32_t begin, uint32_t end, T SerialFunction, Params&&... params) {
4628

@@ -79,6 +61,3 @@ private:
7961
atomic<int> m_nrunning;
8062

8163
};
82-
83-
// Macro to define a static class function which can be called via ThreadPool::ParallelFor<T>
84-
#define SERIAL_OPERATION(name, function_kernal) class name { public: static void SerialFunction(const int& i) { function_kernal; } };

0 commit comments

Comments
 (0)