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
|
||||||
+10
-12
@@ -1,6 +1,8 @@
|
|||||||
|
//Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_count
|
||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <stdio.h>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
class Barrier {
|
class Barrier {
|
||||||
std::mutex mtx;
|
std::mutex mtx;
|
||||||
@@ -8,19 +10,18 @@ class Barrier{
|
|||||||
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();
|
||||||
}
|
}
|
||||||
@@ -31,7 +32,4 @@ class Barrier{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main(){
|
int main() {}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
+31
-22
@@ -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,9 +54,33 @@ unsigned sum_par_rr(const unsigned *V, std::size_t n) {
|
|||||||
return acc;
|
return acc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//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 =
|
||||||
|
(unsigned *)calloc(omp_get_num_procs(), sizeof(unsigned));
|
||||||
|
#pragma omp parallel
|
||||||
|
{
|
||||||
|
int T = omp_get_num_threads();
|
||||||
|
int t = omp_get_thread_num();
|
||||||
|
partial_sums[t * CHACHE_LINE / sizeof(unsigned)] = 0;
|
||||||
|
|
||||||
|
for (size_t i = t; i < n; i += T) {
|
||||||
|
partial_sums[t] += V[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < omp_get_num_procs(); i++) {
|
||||||
|
acc += partial_sums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
||||||
unsigned T = std::thread::hardware_concurrency();
|
unsigned T = std::thread::hardware_concurrency();
|
||||||
struct alignas(std::hardware_destructive_interference_size) p_t {
|
struct alignas(std::hardware_destructive_interference_size) p_t {//destructive as we want to minimize the distructive effect of cache interference
|
||||||
unsigned v;
|
unsigned v;
|
||||||
};
|
};
|
||||||
auto p = std::make_unique<p_t[]>(T);
|
auto p = std::make_unique<p_t[]>(T);
|
||||||
@@ -82,27 +112,6 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
|
|
||||||
unsigned acc = 0;
|
|
||||||
unsigned *partial_sums =
|
|
||||||
(unsigned *)calloc(omp_get_num_procs(), sizeof(unsigned));
|
|
||||||
#pragma omp parallel
|
|
||||||
{
|
|
||||||
int T = omp_get_num_threads();
|
|
||||||
int t = omp_get_thread_num();
|
|
||||||
partial_sums[t * CHACHE_LINE / sizeof(unsigned)] = 0;
|
|
||||||
|
|
||||||
for (size_t i = t; i < n; i += T) {
|
|
||||||
partial_sums[t] += V[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < omp_get_num_procs(); i++) {
|
|
||||||
acc += partial_sums[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return acc;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::size_t n = 1u << 29;
|
std::size_t n = 1u << 29;
|
||||||
|
|||||||
Reference in New Issue
Block a user