SHA256
utils update
This commit is contained in:
+1
-1
@@ -32,4 +32,4 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {}
|
// int main() {}
|
||||||
|
|||||||
+86
-8
@@ -1,7 +1,10 @@
|
|||||||
#include "../include/num_thread.cpp"
|
#include "../include/num_thread.cpp"
|
||||||
|
#include "../include/ranges.cpp"
|
||||||
|
#include "barrier.cpp"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <new>
|
#include <new>
|
||||||
#include <omp.h>
|
#include <omp.h>
|
||||||
@@ -80,7 +83,7 @@ unsigned sum_par_rr_no_cache(const unsigned *V, std::size_t n) {
|
|||||||
|
|
||||||
// lab 3
|
// lab 3
|
||||||
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 = get_num_threads();
|
||||||
struct alignas(std::hardware_destructive_interference_size)
|
struct alignas(std::hardware_destructive_interference_size)
|
||||||
p_t { // destructive as we want to minimize the distructive effect of
|
p_t { // destructive as we want to minimize the distructive effect of
|
||||||
// cache interference
|
// cache interference
|
||||||
@@ -88,19 +91,15 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
|||||||
};
|
};
|
||||||
auto p = std::make_unique<p_t[]>(T);
|
auto p = std::make_unique<p_t[]>(T);
|
||||||
auto worker_proc = [V, n, T, &p](const unsigned t) {
|
auto worker_proc = [V, n, T, &p](const unsigned t) {
|
||||||
size_t s = n / T, b = n % T;
|
auto [beg, end] = get_range(n, t, T);
|
||||||
if (t < b)
|
|
||||||
b = ++s * t;
|
|
||||||
else
|
|
||||||
b += s * t;
|
|
||||||
unsigned e = b + s;
|
|
||||||
unsigned sum = 0;
|
unsigned sum = 0;
|
||||||
for (unsigned i = b; i < e; i++) {
|
for (unsigned i = beg; i < end; i++) {
|
||||||
sum += V[i];
|
sum += V[i];
|
||||||
// p[t].v+= V[i];
|
// p[t].v+= V[i];
|
||||||
}
|
}
|
||||||
p[t].v = sum;
|
p[t].v = sum;
|
||||||
};
|
};
|
||||||
|
unsigned sum = 0;
|
||||||
std::vector<std::thread> workers(T);
|
std::vector<std::thread> workers(T);
|
||||||
for (unsigned t = 0; t < T; t++) {
|
for (unsigned t = 0; t < T; t++) {
|
||||||
workers[t] = std::thread(worker_proc, t);
|
workers[t] = std::thread(worker_proc, t);
|
||||||
@@ -115,6 +114,71 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned sum_cpp_bar(const unsigned *V, size_t n) {
|
||||||
|
unsigned T = get_num_threads();
|
||||||
|
Barrier b(T);
|
||||||
|
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 = [T, &b, &p, V, n](unsigned t) {
|
||||||
|
auto [beg, end] = get_range(n, t, T);
|
||||||
|
unsigned U = 0;
|
||||||
|
for (size_t i = beg; i < end; ++i) {
|
||||||
|
U += V[i];
|
||||||
|
}
|
||||||
|
p[t].v = U;
|
||||||
|
b.arrive_and_wait();
|
||||||
|
for (size_t step = 1, next = 2; step < T; step = next, next += next) {
|
||||||
|
if (t + step < T && !(t % next)) {
|
||||||
|
p[t].v += p[t + step].v;
|
||||||
|
}
|
||||||
|
b.arrive_and_wait();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::thread> workers(T - 1);
|
||||||
|
for (size_t t = 0; t < T - 1; ++t) {
|
||||||
|
workers[t] = std::thread(worker_proc, t + 1);
|
||||||
|
}
|
||||||
|
worker_proc(0);
|
||||||
|
for (auto &w : workers) {
|
||||||
|
w.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
return p[0].v;
|
||||||
|
}
|
||||||
|
|
||||||
|
// cpp parallel with custom num threads with mutex
|
||||||
|
|
||||||
|
unsigned sum_cpp_mtx(const unsigned *V, size_t n) {
|
||||||
|
unsigned T = get_num_threads();
|
||||||
|
std::mutex m;
|
||||||
|
unsigned result = 0;
|
||||||
|
|
||||||
|
auto worker_proc = [T, &m, &result, V, n](unsigned t) {
|
||||||
|
auto [beg, end] = get_range(n, t, T);
|
||||||
|
unsigned U = 0;
|
||||||
|
for (size_t i = beg; i < end; ++i)
|
||||||
|
U += V[i];
|
||||||
|
{
|
||||||
|
std::scoped_lock(m);
|
||||||
|
result += U;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
std::vector<std::thread> workers(T - 1);
|
||||||
|
for (size_t t = 0; t < T - 1; ++t) {
|
||||||
|
workers[t] = std::thread(worker_proc, t + 1);
|
||||||
|
}
|
||||||
|
worker_proc(0);
|
||||||
|
for (auto &w : workers) {
|
||||||
|
w.join();
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::size_t n = 1u << 28;
|
std::size_t n = 1u << 28;
|
||||||
auto data = std::make_unique<unsigned[]>(n);
|
auto data = std::make_unique<unsigned[]>(n);
|
||||||
@@ -157,5 +221,19 @@ int main(int argc, char **argv) {
|
|||||||
printf("sum_loc_cpp: :0x%x\n It took %g s \n", r, t1 - t0);
|
printf("sum_loc_cpp: :0x%x\n It took %g s \n", r, t1 - t0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto t0 = omp_get_wtime();
|
||||||
|
auto r = sum_cpp_bar(data.get(), n);
|
||||||
|
auto t1 = omp_get_wtime();
|
||||||
|
printf("sum_cpp_bar: :0x%x\n It took %g s \n", r, t1 - t0);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto t0 = omp_get_wtime();
|
||||||
|
auto r = sum_cpp_bar(data.get(), n);
|
||||||
|
auto t1 = omp_get_wtime();
|
||||||
|
printf("sum_cpp_mtx: :0x%x\n It took %g s \n", r, t1 - t0);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user