From 6b025a81f8549ccd7f3aca45c163b5ef9c6bb50969b2d0c18795bb6a0da5460e Mon Sep 17 00:00:00 2001 From: = Date: Thu, 26 Nov 2026 13:00:41 +1000 Subject: [PATCH] utils update --- src/barrier.cpp | 2 +- src/main.cpp | 94 ++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/src/barrier.cpp b/src/barrier.cpp index 343ac40..bc5b1d2 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -32,4 +32,4 @@ public: } }; -int main() {} +// int main() {} diff --git a/src/main.cpp b/src/main.cpp index dcc2394..027a4f5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,10 @@ #include "../include/num_thread.cpp" +#include "../include/ranges.cpp" +#include "barrier.cpp" #include #include #include +#include #include #include #include @@ -80,7 +83,7 @@ unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) { // lab 3 unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { - unsigned T = std::thread::hardware_concurrency(); + unsigned T = get_num_threads(); struct alignas(std::hardware_destructive_interference_size) p_t { // destructive as we want to minimize the distructive effect of // cache interference @@ -88,19 +91,15 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { }; auto p = std::make_unique(T); auto worker_proc = [V, n, T, &p](const unsigned t) { - size_t s = n / T, b = n % T; - if (t < b) - b = ++s * t; - else - b += s * t; - unsigned e = b + s; + auto [beg, end] = get_range(n, t, T); unsigned sum = 0; - for (unsigned i = b; i < e; i++) { + for (unsigned i = beg; i < end; i++) { sum += V[i]; // p[t].v+= V[i]; } p[t].v = sum; }; + unsigned sum = 0; std::vector workers(T); for (unsigned t = 0; t < T; t++) { workers[t] = std::thread(worker_proc, t); @@ -115,6 +114,71 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { return result; } +unsigned sum_cpp_bar(const unsigned *V, size_t n) { + unsigned T = get_num_threads(); + Barrier b(T); + struct alignas(std::hardware_destructive_interference_size) + p_t { // destructive as we want to minimize the distructive effect of + // cache interference + unsigned v; + }; + auto p = std::make_unique(T); + auto worker_proc = [T, &b, &p, V, n](unsigned t) { + auto [beg, end] = get_range(n, t, T); + unsigned U = 0; + for (size_t i = beg; i < end; ++i) { + U += V[i]; + } + p[t].v = U; + b.arrive_and_wait(); + for (size_t step = 1, next = 2; step < T; step = next, next += next) { + if (t + step < T && !(t % next)) { + p[t].v += p[t + step].v; + } + b.arrive_and_wait(); + } + }; + + std::vector workers(T - 1); + for (size_t t = 0; t < T - 1; ++t) { + workers[t] = std::thread(worker_proc, t + 1); + } + worker_proc(0); + for (auto &w : workers) { + w.join(); + } + + return p[0].v; +} + +// cpp parallel with custom num threads with mutex + +unsigned sum_cpp_mtx(const unsigned *V, size_t n) { + unsigned T = get_num_threads(); + std::mutex m; + unsigned result = 0; + + auto worker_proc = [T, &m, &result, V, n](unsigned t) { + auto [beg, end] = get_range(n, t, T); + unsigned U = 0; + for (size_t i = beg; i < end; ++i) + U += V[i]; + { + std::scoped_lock(m); + result += U; + } + }; + std::vector workers(T - 1); + for (size_t t = 0; t < T - 1; ++t) { + workers[t] = std::thread(worker_proc, t + 1); + } + worker_proc(0); + for (auto &w : workers) { + w.join(); + } + return result; +} + int main(int argc, char **argv) { std::size_t n = 1u << 28; auto data = std::make_unique(n); @@ -157,5 +221,19 @@ int main(int argc, char **argv) { printf("sum_loc_cpp: :0x%x\n It took %g s \n", r, t1 - t0); } + { + auto t0 = omp_get_wtime(); + auto r = sum_cpp_bar(data.get(), n); + auto t1 = omp_get_wtime(); + printf("sum_cpp_bar: :0x%x\n It took %g s \n", r, t1 - t0); + } + + { + auto t0 = omp_get_wtime(); + auto r = sum_cpp_bar(data.get(), n); + auto t1 = omp_get_wtime(); + printf("sum_cpp_mtx: :0x%x\n It took %g s \n", r, t1 - t0); + } + return 0; }