SHA256
prettify things lab3 partial progress
This commit is contained in:
+30
-32
@@ -1,37 +1,35 @@
|
||||
#include <condition_variable>
|
||||
#include <stdio.h>
|
||||
#include <mutex>
|
||||
//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 <condition_variable>
|
||||
#include <mutex>
|
||||
#include <stdio.h>
|
||||
|
||||
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<max_count)
|
||||
{
|
||||
while(! flushing)
|
||||
cv.wait(lock);
|
||||
}
|
||||
else{
|
||||
flushing = true;
|
||||
cv.notify_all();
|
||||
}
|
||||
if(--count==0){
|
||||
flushing = false;
|
||||
cv.notify_all();
|
||||
}
|
||||
if (--count == 0) {
|
||||
flushing = false;
|
||||
cv.notify_all();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
}
|
||||
int main() {}
|
||||
|
||||
+43
-34
@@ -9,6 +9,10 @@
|
||||
#include <vector>
|
||||
#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<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;
|
||||
}
|
||||
|
||||
//just a different way of allocation. seems simialr in speed
|
||||
unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
|
||||
unsigned acc = 0;
|
||||
unsigned *partial_sums =
|
||||
@@ -104,6 +77,42 @@ unsigned sum_par_rr_no_cache(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 {//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) {
|
||||
std::size_t n = 1u << 29;
|
||||
auto data = std::make_unique<unsigned[]>(n);
|
||||
|
||||
Reference in New Issue
Block a user