diff --git a/.gitignore b/.gitignore index 6ff9e45..fa04d94 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.out +.zed diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..77e477c --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +a repo for storing labs and all related work for the parallel compute subject diff --git a/src/barrier.cpp b/src/barrier.cpp index 3774c95..343ac40 100644 --- a/src/barrier.cpp +++ b/src/barrier.cpp @@ -1,37 +1,35 @@ -#include -#include -#include +//Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_count -class Barrier{ - std::mutex mtx; - std::condition_variable cv; - std::size_t count=0; - std::size_t max_count=0; - bool flushing = false; - Barrier (size_t max_count){ - max_count=max_count; +#include +#include +#include + +class Barrier { + std::mutex mtx; + std::condition_variable cv; + std::size_t count = 0; + std::size_t max_count = 0; + bool flushing = false; + +public: + Barrier(size_t max_count) { this->max_count = max_count; } + + void arrive_and_wait() { + std::unique_lock lock(mtx); + while (flushing) + cv.wait(lock); + if (++count < max_count) { + while (!flushing) + cv.wait(lock); + } else { + flushing = true; + cv.notify_all(); } - void arrive_and_wait(){ - std::unique_lock lock(mtx); - while(flushing) - cv.wait(lock); - if(++count #define CHACHE_LINE 64 +//par - parallel +//rr - round-robin + +// Lab 1 unsigned sum(const unsigned *V, std::size_t n) { unsigned acc = 0; for (int i = 0; i < n; i++) { @@ -26,6 +30,8 @@ unsigned sum_par(const unsigned *V, std::size_t n) { return acc; } +// Lab 2 + unsigned sum_par_rr(const unsigned *V, std::size_t n) { unsigned acc = 0; unsigned *partial_sums = @@ -48,40 +54,7 @@ unsigned sum_par_rr(const unsigned *V, std::size_t n) { return acc; } -unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { - unsigned T = std::thread::hardware_concurrency(); - struct alignas(std::hardware_destructive_interference_size) p_t { - unsigned v; - }; - auto p = std::make_unique(T); - auto worker_proc = [V, n, T, &p](unsigned t) { - size_t s = n / T, b = n % T; - if (t < b) - b = ++s * t; - else - b += s * t; - unsigned e = b + s; - unsigned sum = 0; - for (unsigned i = b; i < e; i++) { - sum += V[i]; - // p[t].v+= V[i]; - } - p[t].v = sum; - }; - std::vector workers(T); - for(unsigned t=0;t(T); + auto worker_proc = [V, n, T, &p](unsigned t) { + size_t s = n / T, b = n % T; + if (t < b) + b = ++s * t; + else + b += s * t; + unsigned e = b + s; + unsigned sum = 0; + for (unsigned i = b; i < e; i++) { + sum += V[i]; + // p[t].v+= V[i]; + } + p[t].v = sum; + }; + std::vector workers(T); + for (unsigned t = 0; t < T; t++) { + workers[t] = std::thread(worker_proc, t); + } + for (auto &w : workers) { + w.join(); + } + unsigned result = 0; + for (size_t i = 0; i < T; i++) { + result += p[i].v; + } + return result; +} + + int main(int argc, char **argv) { std::size_t n = 1u << 29; auto data = std::make_unique(n);