From 55cb27578e9174edd7c81f2bc2f5ddf93fbca52dfba97699dd9d62d8171047bf Mon Sep 17 00:00:00 2001 From: OkunElya Date: Thu, 26 Nov 2026 09:55:56 +1000 Subject: [PATCH] fix measurement func --- .clangd | 4 +- include/num_thread.cpp | 96 +++++++++++++++++++++++------------------- src/main.cpp | 3 +- 3 files changed, 55 insertions(+), 48 deletions(-) diff --git a/.clangd b/.clangd index 1ce5b1b..6a9ff6f 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,2 @@ - CompileFlags: - Add: [./include/num_threads.cpp -std=c++20, -fopenmp] +CompileFlags: + Add: [-std=c++20, -fopenmp] diff --git a/include/num_thread.cpp b/include/num_thread.cpp index 5a39c08..33eca99 100644 --- a/include/num_thread.cpp +++ b/include/num_thread.cpp @@ -1,55 +1,63 @@ -#include -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include +#include unsigned g_threads = std::thread::hardware_concurrency(); const unsigned max_threads = g_threads; -unsigned get_num_threads(){ - return g_threads; +unsigned get_num_threads() { return g_threads; } + +void set_num_threads(const unsigned T) { + if (T == 0 || T > max_threads) + return; + g_threads = T; + omp_set_num_threads(T); } -void set_num_threads(const unsigned T){ - if(T==0 || T > max_threads) - return ; - g_threads = T; - omp_set_num_threads(T); -} - -struct measurement{ - unsigned T; - double time; - double speedup; - unsigned result; +struct measurement { + unsigned T; + double time; + unsigned result; + double speedup; }; -template f > -std::vector measure_time (f, const unsigned* V, size_t n) -{ - std::vector ret; - const unsigned max_threads = gen_num_threads(); - for(unsigned T = 1;T<=max_threads();T++){ - set_num_threads(T); - double t0 = omp_get_wtime(); - const unsigned result = f(V, n) - double t1 = omp_get_wtime(); - ret.push_back(measure{ - .T = T, - .time = t1-t0, - .result = result, - .speedup = ret.size()>0? ret.back()/ret[0] : 1 - }); - } - sen_num_threads(max_threads); - return ret; +template F> +std::vector measure_time(F f, const unsigned *V, size_t n) { + std::vector ret; + const unsigned max_threads = get_num_threads(); + for (unsigned T = 1; T <= max_threads; T++) { + set_num_threads(T); + double t0 = omp_get_wtime(); + const unsigned result = f(V, n); + double t1 = omp_get_wtime(); + ret.push_back(measurement{ + .T = T, + .time = t1 - t0, + .result = result, + .speedup = ret.size() > 0 ? ret.back().time / ret[0].time : 1}); + } + set_num_threads(max_threads); + return ret; } -void to_csv(std::ostream& os, const std::vector & m ){ - os << "T, time, speedups, result, \n"; - for ( auto i : m){ - os << i.T<<", "< &m) { + os << "T, time, speedups, result, \n"; + for (auto i : m) { + os << i.T << ", " << i.time << ", " << i.speedup << ", " << i.result + << "\n"; + } +} + +template F> +void measure_time(F f, const unsigned *V, size_t n, std::string name) { + std::filesystem::create_directory(std::string("measurements/") + name + + ".csv"); + std::filebuf fb; + fb.open("test.txt", std::ios::out); + std::ostream os(&fb); + to_csv(os, measure_time(f, V, n)); } diff --git a/src/main.cpp b/src/main.cpp index 39e7091..dcc2394 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "../include/num_thread.cpp" #include #include #include @@ -7,7 +8,6 @@ #include #include #include -#include "../include/num_thread.cpp" #define CHACHE_LINE 64 // par - parallel @@ -115,7 +115,6 @@ unsigned sum_loc_cpp(const unsigned *V, std::size_t n) { return result; } - int main(int argc, char **argv) { std::size_t n = 1u << 28; auto data = std::make_unique(n);