-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel_reduce.cpp
67 lines (50 loc) · 1.59 KB
/
parallel_reduce.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
//
// Created by blackgeorge on 7/3/19.
//
#include <vector>
#include <chrono>
#include "foo.h"
#include "random_data.h"
#include <tbb/blocked_range.h>
#include <tbb/parallel_reduce.h>
class SumFoo {
std::vector<int>* my_a;
public:
long int sum;
explicit SumFoo(std::vector<int>* a): my_a{a}, sum{0} {};
SumFoo(SumFoo& x, tbb::split) : my_a{x.my_a}, sum{0} {};
void operator()(const tbb::blocked_range<int>& r) {
std::vector<int>* a = my_a;
for (auto i = r.begin(); i != r.end(); ++i)
sum += Foo((*a)[i]);
}
void join(const SumFoo& y) { sum += y.sum; }
};
long int SerialSumFoo(std::vector<int>* a, int n)
{
int sum = 0;
for (auto x : *a)
sum += Foo(x);
return sum;
}
long int ParallelSumFoo(std::vector<int>* a, int n)
{
SumFoo sf(a);
tbb::parallel_reduce(tbb::blocked_range<int>(0, n, 1000), sf);
return sf.sum;
}
int main()
{
int N = 100000000;
auto a = create_random_data(N);
auto t0 = std::chrono::high_resolution_clock::now();
long res = SerialSumFoo(&a, N);
auto tf = std::chrono::high_resolution_clock::now();
std::cout << "Sum is: " << res << std::endl;
std::cout << "Done in: " << std::chrono::duration_cast<std::chrono::milliseconds>(tf-t0).count() << " ms" << std::endl;
t0 = std::chrono::high_resolution_clock::now();
res = ParallelSumFoo(&a, N);
tf = std::chrono::high_resolution_clock::now();
std::cout << "Sum is: " << res << std::endl;
std::cout << "Done in: " << std::chrono::duration_cast<std::chrono::milliseconds>(tf-t0).count() << " ms" << std::endl;
}