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

update codes #8

Open
wants to merge 3 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
20 changes: 14 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)

project(main LANGUAGES CXX)

add_executable(main main.cpp)
project(concurrent_vector LANGUAGES CXX)
file(GLOB_RECURSE SRC_FILES src/concurrent_vector/*.cpp CONFIGURE_DEPENDS true)
add_executable(concurrent_vector ${SRC_FILES})

#find_package(OpenMP REQUIRED)
#target_link_libraries(main PUBLIC OpenMP::OpenMP_CXX)

find_package(TBB REQUIRED)
target_link_libraries(main PUBLIC TBB::tbb)
target_include_directories(concurrent_vector PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/oneapi-tbb-2021.5.0/include ${CMAKE_CURRENT_SOURCE_DIR})
target_link_directories(concurrent_vector PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/oneapi-tbb-2021.5.0/lib/intel64/vc14)

target_link_libraries(concurrent_vector PUBLIC tbb.lib)

# find_package(TBB REQUIRED)
# target_link_libraries(main PUBLIC TBB::tbb)

#find_package(benchmark REQUIRED)
#target_link_libraries(main PUBLIC benchmark::benchmark)
# set(BENCHMARK_ENABLE_TESTING OFF CACHE BOOL "Turn off the fking test")
# add_subdirectory(benchmark)
# target_link_libraries(main PUBLIC benchmark)
#target_link_libraries(main PUBLIC benchmark::benchmark)
135 changes: 109 additions & 26 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,93 @@
#include <cmath>
#include <numeric>
#include <algorithm>
#include <tbb/blocked_range.h>
#include <tbb/parallel_for.h>
#include <tbb/task_group.h>
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_scan.h>
#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);
}
auto n = arr.size();
tbb::parallel_for(tbb::blocked_range<size_t>(0, n),
[&](tbb::blocked_range<size_t> r) {
for (size_t i = r.begin(); i < r.end(); i++) {
arr[i] = func(i);
}
});
//for (size_t i = 0; i < arr.size(); 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];
}
auto n = std::min(x.size(), y.size());
tbb::parallel_for(tbb::blocked_range<size_t>(0, n),
[&](tbb::blocked_range<size_t> r) {
for (size_t i = r.begin(); i < r.end(); i++) {
//arr[i] = func(i);
x[i] = a * x[i] + y[i];
}
});
//for (size_t i = 0; i < x.size(); 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 = std::sqrt(ret);
auto n = std::min(x.size(), y.size());
tbb::task_arena ta(4);
auto res = tbb::parallel_reduce(tbb::blocked_range<size_t>(0, n), (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;
});

//for (size_t i = 0; i < std::min(x.size(), y.size()); i++) {
// ret += x[i] * y[i];
//}
ret = std::sqrt(res);
//ret = std::sqrt(ret);
TOCK(sqrtdot);
return ret;
}

template <class T>
T minvalue(std::vector<T> const &x) {
TICK(minvalue);
auto n = x.size();
T ret = x[0];
for (size_t i = 1; i < x.size(); i++) {
if (x[i] < ret)
ret = x[i];
}

tbb::parallel_for(tbb::blocked_range<size_t>(0, n),
[&](tbb::blocked_range<size_t> r) {
for (size_t i = r.begin(); i < r.end(); i++) {
if (x[i] < ret)
ret = x[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

错了,应该用 parallel_reduce 和 std::min。

}
});

//for (size_t i = 1; i < x.size(); i++) {
// if (x[i] < ret)
// ret = x[i];
//}
TOCK(minvalue);
return ret;
}
Expand All @@ -55,14 +99,35 @@ 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]);
auto n = std::min(x.size(), y.size());
res.reserve(n * 2 / 3);
std::mutex mtx;

tbb::parallel_for(tbb::blocked_range<size_t>(0, n),
[&](tbb::blocked_range<size_t> r) {
std::vector<T> local_a;
local_a.reserve(n);
for (size_t i = r.begin(); i < r.end(); ++i) {
if (x[i] > y[i]) {
local_a.push_back(x[i]);
}
else if (y[i] > x[i] && y[i] > 0.5f) {
local_a.push_back(y[i]);
local_a.push_back(x[i] * y[i]);
}
}
std::lock_guard grd(mtx);
std::copy(local_a.begin(), local_a.end(), std::back_inserter(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]);
// }
//}
TOCK(magicfilter);
return res;
}
Expand All @@ -71,10 +136,26 @@ 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;
//}

auto n = x.size();
ret = tbb::parallel_scan(tbb::blocked_range<size_t>(0, n), (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;
}, [](float x, float y) {
return x + y;
});

TOCK(scanner);
return ret;
}
Expand All @@ -98,5 +179,7 @@ int main() {
scanner(x);
std::cout << std::reduce(x.begin(), x.end()) << std::endl;

std::getchar();

return 0;
}