Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod: finish homework #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
128 changes: 104 additions & 24 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,55 @@
#include <cmath>
#include <numeric>
#include <algorithm>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_scan.h>
#include <mutex>
#include "ticktock.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()),
[&](tbb::blocked_range<size_t> r) {
for (size_t 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()),
[&](tbb::blocked_range<size_t> r) {
for ( size_t 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)0,
[&](tbb::blocked_range<size_t> r, T local_res) {
for ( size_t i = r.begin(); i < r.end(); ++i ) {
local_res += x[i] * y[i];
}
return local_res;
},
[]( T x, T y) {
return x + y;
});
ret = std::sqrt(ret);
TOCK(sqrtdot);
return ret;
Expand All @@ -43,10 +62,21 @@ 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];
}
ret = tbb::parallel_reduce(
tbb::blocked_range<size_t>(1, x.size()),
x[0],
[&] (tbb::blocked_range<size_t> r, T local_res) {
for ( size_t i = r.begin(); i < r.end(); ++i) {
if ( x[i] < local_res) {
local_res = x[i];
}
}
return local_res;
},
[&] (T x, T y) {
return x < y ? x : y;
}
);
TOCK(minvalue);
return ret;
}
Expand All @@ -55,14 +85,25 @@ 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]);
std::mutex mtx;
res.reserve( 2 * std::min(x.size(), y.size()));
tbb::parallel_for(
tbb::blocked_range<size_t>(0, std::min(x.size(), y.size())),
[&] (tbb::blocked_range<size_t> r) {
std::vector<T> tmp_vec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加个 tmp_vec.reserve(r.size())?

for (size_t i = r.begin(); i < r.end(); i++) {
if (x[i] > y[i]) {
tmp_vec.push_back(x[i]);
} else if (y[i] > x[i] && y[i] > 0.5f) {
tmp_vec.push_back(y[i]);
tmp_vec.push_back(x[i] * y[i]);
}
}

std::lock_guard lck{mtx};
std::copy(tmp_vec.begin(), tmp_vec.end(), std::back_inserter(res));
}
}
);
TOCK(magicfilter);
return res;
}
Expand All @@ -71,10 +112,24 @@ 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;
}
// 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)0,
[&] (tbb::blocked_range<size_t> r, T local_res, auto 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;
},
[] (T x, T y) {
return x + y;
}
);
TOCK(scanner);
return ret;
}
Expand All @@ -100,3 +155,28 @@ int main() {

return 0;
}
// Before
// fill: 1.23536s
// fill: 1.25023s
// saxpy: 0.048762s
// sqrtdot: 0.086141s
// 5165.4
// minvalue: 0.07746s
// -1.11803
// magicfilter: 0.426949s
// 55924034
// scanner: 0.080046s
// 5.28566e+07

// After Modified
// fill: 0.256878s
// fill: 0.264094s
// saxpy: 0.03981s
// sqrtdot: 1.18658s
// 5792.62
// minvalue: 0.01506s
// -1.11803
// magicfilter: 0.183792s
// 55924034
// scanner: 0.032603s
// 5.28591e+07
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
set -e
cmake -B build
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=/Users/yangzhikai/farewell/cpp_review/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build
build/main