Spaces:
Runtime error
Runtime error
File size: 3,753 Bytes
be11144 |
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 |
#pragma once
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_scan.h>
#include <tbb/parallel_sort.h>
#include <tbb/task_scheduler_init.h>
#include <tbb/tick_count.h>
#include <tbb/tbb_thread.h>
#include <cstdef> // For std::size_t.
#include <cassert>
template <typename T>
struct NegateBody
{
void operator()(T& x) const
{
x = -x;
}
};
template <typename Vector>
struct ForBody
{
typedef typename Vector::value_type T;
private:
Vector& v;
public:
ForBody(Vector& x) : v(x) {}
void operator()(tbb::blocked_range<std::size_t> const& r) const
{
for (std::size_t i = r.begin(); i != r.end(); ++i)
v[i] = -v[i];
}
};
template <typename Vector>
struct ReduceBody
{
typedef typename Vector::value_type T;
private:
Vector& v;
public:
T sum;
ReduceBody(Vector& x) : v(x), sum(0) {}
ReduceBody(ReduceBody& x, tbb::split) : v(x.v), sum(0) {}
void operator()(tbb::blocked_range<std::size_t> const& r)
{
for (std::size_t i = r.begin(); i != r.end(); ++i)
sum += v[i];
}
void join(ReduceBody const& x) { sum += x.sum; }
};
template <typename Vector>
struct ScanBody
{
typedef typename Vector::value_type T;
private:
Vector& v;
public:
T sum;
ScanBody(Vector& x) : sum(0), v(x) {}
ScanBody(ScanBody& x, tbb::split) : v(x.v), sum(0) {}
template <typename Tag>
void operator()(tbb::blocked_range<std::size_t> const& r, Tag)
{
T temp = sum;
for (std::size_t i = r.begin(); i < r.end(); ++i)
{
temp = temp + x[i];
if (Tag::is_final_scan())
x[i] = temp;
}
sum = temp;
}
void assign(ScanBody const& x) { sum = x.sum; }
T get_sum() const { return sum; }
void reverse_join(ScanBody const& x) { sum = x.sum + sum;}
};
template <typename Vector>
struct CopyBody
{
typedef typename Vector::value_type T;
private:
Vector &v;
Vector &u;
public:
CopyBody(Vector& x, Vector& y) : v(x), u(y) {}
void operator()(tbb::blocked_range<size_t> const& r) const
{
for (std::size_t i = r.begin(); i != r.end(); ++i)
v[i] = u[i];
}
};
template <typename Vector>
typename Vector::value_type tbb_reduce(Vector& v)
{
ReduceBody<Vector> body(v);
tbb::parallel_reduce(tbb::blocked_range<size_t>(0, v.size()), body);
return body.sum;
}
template <typename Vector>
void tbb_sort(Vector& v)
{
tbb::parallel_sort(v.begin(), v.end());
}
template <typename Vector>
void tbb_transform(Vector& v)
{
ForBody<Vector> body(v);
tbb::parallel_for(tbb::blocked_range<size_t>(0, v.size()), body);
}
template <typename Vector>
void tbb_scan(Vector& v)
{
ScanBody<Vector> body(v);
tbb::parallel_scan(tbb::blocked_range<size_t>(0, v.size()), body);
}
template <typename Vector>
void tbb_copy(Vector& v, Vector& u)
{
CopyBody<Vector> body(v, u);
tbb::parallel_for(tbb::blocked_range<size_t>(0, v.size()), body);
}
void test_tbb()
{
std::size_t elements = 1 << 20;
std::vector<int> A(elements);
std::vector<int> B(elements);
std::vector<int> C(elements);
std::vector<int> D(elements);
randomize(A);
randomize(B);
assert(std::accumulate(A.begin(), A.end(), 0) == tbb_reduce(A));
randomize(A);
randomize(B);
std::transform(A.begin(), A.end(), A.begin(), thrust::negate<int>());
tbb_transform(B);
assert(A == B);
randomize(A);
randomize(B);
std::partial_sum(A.begin(), A.end(), A.begin());
tbb_scan(B);
assert(A == B);
randomize(A);
randomize(B);
std::sort(A.begin(), A.end());
tbb_sort(B);
assert(A == B);
randomize(A);
randomize(B);
randomize(C);
randomize(D);
std::copy(A.begin(), A.end(), C.begin());
tbb_copy(B, D);
assert(A == B);
assert(C == D);
}
|