prettify things lab3 partial progress

This commit is contained in:
2026-10-29 21:43:39 +10:00
parent e18440bd58
commit d359357a2b
4 changed files with 75 additions and 66 deletions
+1
View File
@@ -1 +1,2 @@
*.out *.out
.zed
+1
View File
@@ -0,0 +1 @@
a repo for storing labs and all related work for the parallel compute subject
+30 -32
View File
@@ -1,37 +1,35 @@
#include <condition_variable> //Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_count
#include <stdio.h>
#include <mutex>
class Barrier{ #include <condition_variable>
std::mutex mtx; #include <mutex>
std::condition_variable cv; #include <stdio.h>
std::size_t count=0;
std::size_t max_count=0; class Barrier {
bool flushing = false; std::mutex mtx;
Barrier (size_t max_count){ std::condition_variable cv;
max_count=max_count; 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(){ if (--count == 0) {
std::unique_lock lock(mtx); flushing = false;
while(flushing) cv.notify_all();
cv.wait(lock);
if(++count<max_count)
{
while(! flushing)
cv.wait(lock);
}
else{
flushing = true;
cv.notify_all();
}
if(--count==0){
flushing = false;
cv.notify_all();
}
} }
}
}; };
int main(){ int main() {}
}
+43 -34
View File
@@ -9,6 +9,10 @@
#include <vector> #include <vector>
#define CHACHE_LINE 64 #define CHACHE_LINE 64
//par - parallel
//rr - round-robin
// Lab 1
unsigned sum(const unsigned *V, std::size_t n) { unsigned sum(const unsigned *V, std::size_t n) {
unsigned acc = 0; unsigned acc = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
@@ -26,6 +30,8 @@ unsigned sum_par(const unsigned *V, std::size_t n) {
return acc; return acc;
} }
// Lab 2
unsigned sum_par_rr(const unsigned *V, std::size_t n) { unsigned sum_par_rr(const unsigned *V, std::size_t n) {
unsigned acc = 0; unsigned acc = 0;
unsigned *partial_sums = unsigned *partial_sums =
@@ -48,40 +54,7 @@ unsigned sum_par_rr(const unsigned *V, std::size_t n) {
return acc; return acc;
} }
unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { //just a different way of allocation. seems simialr in speed
unsigned T = std::thread::hardware_concurrency();
struct alignas(std::hardware_destructive_interference_size) p_t {
unsigned v;
};
auto p = std::make_unique<p_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 <std::thread> 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;
}
unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) { unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
unsigned acc = 0; unsigned acc = 0;
unsigned *partial_sums = unsigned *partial_sums =
@@ -104,6 +77,42 @@ unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
return acc; 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 {//destructive as we want to minimize the distructive effect of cache interference
unsigned v;
};
auto p = std::make_unique<p_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<std::thread> 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) { int main(int argc, char **argv) {
std::size_t n = 1u << 29; std::size_t n = 1u << 29;
auto data = std::make_unique<unsigned[]>(n); auto data = std::make_unique<unsigned[]>(n);