SHA256
prettify things lab3 partial progress
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
*.out
|
*.out
|
||||||
|
.zed
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
a repo for storing labs and all related work for the parallel compute subject
|
||||||
+19
-21
@@ -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>
|
||||||
|
#include <mutex>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
class Barrier {
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
std::condition_variable cv;
|
std::condition_variable cv;
|
||||||
std::size_t count=0;
|
std::size_t count = 0;
|
||||||
std::size_t max_count=0;
|
std::size_t max_count = 0;
|
||||||
bool flushing = false;
|
bool flushing = false;
|
||||||
Barrier (size_t max_count){
|
|
||||||
max_count=max_count;
|
public:
|
||||||
}
|
Barrier(size_t max_count) { this->max_count = max_count; }
|
||||||
void arrive_and_wait(){
|
|
||||||
|
void arrive_and_wait() {
|
||||||
std::unique_lock lock(mtx);
|
std::unique_lock lock(mtx);
|
||||||
while(flushing)
|
while (flushing)
|
||||||
cv.wait(lock);
|
cv.wait(lock);
|
||||||
if(++count<max_count)
|
if (++count < max_count) {
|
||||||
{
|
while (!flushing)
|
||||||
while(! flushing)
|
|
||||||
cv.wait(lock);
|
cv.wait(lock);
|
||||||
}
|
} else {
|
||||||
else{
|
|
||||||
flushing = true;
|
flushing = true;
|
||||||
cv.notify_all();
|
cv.notify_all();
|
||||||
}
|
}
|
||||||
if(--count==0){
|
if (--count == 0) {
|
||||||
flushing = false;
|
flushing = false;
|
||||||
cv.notify_all();
|
cv.notify_all();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(){
|
int main() {}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
+43
-34
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user