From 5df59e937663f478f06c4fb81379f07613039fa5 Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 26 Apr 2025 13:22:56 +0200 Subject: [PATCH] adjust ... --- lab06/MonteCarloPiThreads.cpp | 41 ++++++++++--------- lab06/MonteCarloPiThreadsDyn.cpp | 69 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 18 deletions(-) create mode 100644 lab06/MonteCarloPiThreadsDyn.cpp diff --git a/lab06/MonteCarloPiThreads.cpp b/lab06/MonteCarloPiThreads.cpp index 220ce63..92b1ee7 100644 --- a/lab06/MonteCarloPiThreads.cpp +++ b/lab06/MonteCarloPiThreads.cpp @@ -1,4 +1,3 @@ -#include "matrix.h" #include #include #include @@ -8,19 +7,22 @@ struct bench_result { double time; - double pi; + int threads; }; double monteCarloPi(int64_t N) { int localCount = 0; - std::mt19937 generator(std::random_device{}()); - std::uniform_real_distribution dist(0.0, 1.0); -#pragma omp parallel for reduction(+ : localCount) - for (int i = 0; i < N; ++i) { - double x = dist(generator); - double y = dist(generator); - if (x * x + y * y <= 1.0) { - localCount++; +#pragma omp parallel + { + std::mt19937 generator(std::random_device{}() + omp_get_thread_num()); + std::uniform_real_distribution dist(0.0, 1.0); +#pragma omp for reduction(+ : localCount) + for (int i = 0; i < N; ++i) { + double x = dist(generator); + double y = dist(generator); + if (x * x + y * y <= 1.0) { + localCount++; + } } } @@ -32,32 +34,35 @@ bench_result benchmark(int numThreads, int64_t N, double function(int64_t)) { // start time measurement auto start = std::chrono::system_clock::now(); - auto result = bench_result{0.0, 0.0}; + auto result = bench_result{0.0, 0}; // perform benchmark const int maxNumExec = 10; for (int nExec = 0; nExec < maxNumExec; ++nExec) { - result.pi = function(N); + volatile double pi = function(N); } auto end = std::chrono::system_clock::now(); std::chrono::duration diff = end - start; result.time = diff.count() / maxNumExec; + result.threads = numThreads; return result; } int main() { const int64_t max_n = 10000000; - const int numThreads = 12; - std::ofstream fout("multi_monte.csv", std::ios::app); - fout << "pi" << ","; + const int max_threads = 12; + std::ofstream fout("diff_theads.csv", std::ios::app); + fout << "threads" << ","; + fout << "max_n" << ","; fout << "time" << "\n"; - for (int64_t i = 1000; i <= max_n; i += 1000) { - const bench_result res = benchmark(numThreads, i, monteCarloPi); + for (int i = 1; i <= max_threads; i++) { + const bench_result res = benchmark(i, max_n, monteCarloPi); - fout << res.pi << ","; + fout << res.threads << ","; + fout << max_n << ","; fout << res.time << "\n"; } } diff --git a/lab06/MonteCarloPiThreadsDyn.cpp b/lab06/MonteCarloPiThreadsDyn.cpp new file mode 100644 index 0000000..1621041 --- /dev/null +++ b/lab06/MonteCarloPiThreadsDyn.cpp @@ -0,0 +1,69 @@ +#include "matrix.h" +#include +#include +#include +#include +#include +#include + +struct bench_result { + double time; + int threads; +}; + +double monteCarloPi(int64_t N) { + int localCount = 0; +#pragma omp parallel + { + std::mt19937 generator(std::random_device{}() + omp_get_thread_num()); + std::uniform_real_distribution dist(0.0, 1.0); +#pragma omp for reduction(+ : localCount) + for (int i = 0; i < N; ++i) { + double x = dist(generator); + double y = dist(generator); + if (x * x + y * y <= 1.0) { + localCount++; + } + } + } + + return 4.0 * localCount / N; +} + +bench_result benchmark(int numThreads, int64_t N, double function(int64_t)) { + omp_set_num_threads(numThreads); + + // start time measurement + auto start = std::chrono::system_clock::now(); + auto result = bench_result{0.0, 0}; + + // perform benchmark + const int maxNumExec = 10; + for (int nExec = 0; nExec < maxNumExec; ++nExec) { + volatile double pi = function(N); + } + + auto end = std::chrono::system_clock::now(); + std::chrono::duration diff = end - start; + result.time = diff.count() / maxNumExec; + result.threads = numThreads; + + return result; +} + +int main() { + const int64_t max_n = 1000000; + const int max_threads = 12; + std::ofstream fout("dyn_n.csv", std::ios::app); + fout << "threads" << ","; + fout << "max_n" << ","; + fout << "time" << "\n"; + + for (int i = 1; i <= max_threads; i++) { + const bench_result res = benchmark(i, max_n * i, monteCarloPi); + + fout << res.threads << ","; + fout << (max_n * i) << ","; + fout << res.time << "\n"; + } +}