Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 65 additions & 27 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,52 @@
#include <numeric>
#include <algorithm>
#include "ticktock.h"

#include <mutex>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_scan.h>
// TODO: 并行化所有这些 for 循环

template <class T, class Func>
std::vector<T> fill(std::vector<T> &arr, Func const &func) {
TICK(fill);
for (size_t i = 0; i < arr.size(); i++) {
arr[i] = func(i);
}
tbb::parallel_for(tbb::blocked_range<size_t>(0, arr.size()),
[&func, &arr](const tbb::blocked_range<size_t>& r) {
for (auto i = r.begin(); i != r.end(); i++)
{
arr[i] = func(i);
}
});
TOCK(fill);
return arr;
}

template <class T>
void saxpy(T a, std::vector<T> &x, std::vector<T> const &y) {
TICK(saxpy);
for (size_t i = 0; i < x.size(); i++) {
x[i] = a * x[i] + y[i];
}
tbb::parallel_for(tbb::blocked_range<size_t>(0, x.size()),
[a, &x, &y](const tbb::blocked_range<size_t>& r) {
for (auto i = r.begin(); i != r.end(); i++)
{
x[i] = a * x[i] + y[i];
}
});
TOCK(saxpy);
}

template <class T>
T sqrtdot(std::vector<T> const &x, std::vector<T> const &y) {
TICK(sqrtdot);
T ret = 0;
for (size_t i = 0; i < std::min(x.size(), y.size()); i++) {
ret += x[i] * y[i];
}
ret = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, std::min(x.size(), y.size())),
T{},
[&x, &y](const tbb::blocked_range<size_t>& r, T local_val) {
for (size_t i = r.begin(); i != r.end(); i++)
{
local_val += x[i] * y[i];
}
return local_val;
}, std::plus<T>{});
ret = std::sqrt(ret);
TOCK(sqrtdot);
return ret;
Expand All @@ -42,11 +59,15 @@ T sqrtdot(std::vector<T> const &x, std::vector<T> const &y) {
template <class T>
T minvalue(std::vector<T> const &x) {
TICK(minvalue);
T ret = x[0];
for (size_t i = 1; i < x.size(); i++) {
if (x[i] < ret)
ret = x[i];
}
T ret = tbb::parallel_reduce(tbb::blocked_range<size_t>(1, x.size()),
x[0],
[&x](const tbb::blocked_range<size_t>& r, T local_val) {
for (size_t i = r.begin(); i != r.end(); ++i)
if (x[i] < local_val)
local_val = x[i];
return local_val;
},
[](T x, T y) { return std::min(x, y);});
TOCK(minvalue);
return ret;
}
Expand All @@ -55,14 +76,24 @@ template <class T>
std::vector<T> magicfilter(std::vector<T> const &x, std::vector<T> const &y) {
TICK(magicfilter);
std::vector<T> res;
for (size_t i = 0; i < std::min(x.size(), y.size()); i++) {
if (x[i] > y[i]) {
res.push_back(x[i]);
} else if (y[i] > x[i] && y[i] > 0.5f) {
res.push_back(y[i]);
res.push_back(x[i] * y[i]);
}
}
size_t MIN_LEN = std::min(x.size(), y.size());
res.reserve( MIN_LEN * 2 / 3);
std::mutex mut;

tbb::parallel_for(tbb::blocked_range<size_t>(0, MIN_LEN),
[&res, &mut, &x, &y](const tbb::blocked_range<size_t>& r) {
std::vector<T> local_vec;
local_vec.reserve(r.size());
for (auto i = r.begin(); i != r.end(); i++)
if (x[i] > y[i]) {
local_vec.push_back(x[i]);
} else if (y[i] > x[i] && y[i] > 0.5f) {
local_vec.push_back(y[i]);
local_vec.push_back(x[i] * y[i]);
}
std::lock_guard lck(mut);
std::copy(local_vec.begin(), local_vec.end(), std::back_inserter(res));
});
TOCK(magicfilter);
return res;
}
Expand All @@ -71,10 +102,17 @@ template <class T>
T scanner(std::vector<T> &x) {
TICK(scanner);
T ret = 0;
for (size_t i = 0; i < x.size(); i++) {
ret += x[i];
x[i] = ret;
}
ret = tbb::parallel_scan(tbb::blocked_range<size_t>(0, x.size()),
T{},
[&x](const tbb::blocked_range<size_t>& r, T local_res, bool is_final) {
for (size_t i = r.begin(); i != r.end(); i++)
{
local_res += x[i];
if (is_final)
x[i] = local_res;
}
return local_res;
}, std::plus<T>{});
TOCK(scanner);
return ret;
}
Expand Down