From 1fbcc23b1fa57b0ab37d9a06ce1c757af163ff38 Mon Sep 17 00:00:00 2001 From: kai Date: Tue, 22 Apr 2025 15:51:07 +0200 Subject: [PATCH] aufgabe 1 --- lab06/benchmark.csv | 13 ++++++++ lab06/benchmark_dyn.csv | 13 ++++++++ lab06/benchmark_gui.csv | 13 ++++++++ lab06/jacobi_benchmark.cpp | 61 +++++++++++++++++++++++++------------- lab06/plot_jacobi.py | 45 ++++++++-------------------- pyproject.toml | 1 + uv.lock | 51 ++++++++++++++++++++++++++++++- 7 files changed, 143 insertions(+), 54 deletions(-) create mode 100644 lab06/benchmark.csv create mode 100644 lab06/benchmark_dyn.csv create mode 100644 lab06/benchmark_gui.csv diff --git a/lab06/benchmark.csv b/lab06/benchmark.csv new file mode 100644 index 0000000..83c45ce --- /dev/null +++ b/lab06/benchmark.csv @@ -0,0 +1,13 @@ +threads,time +1, 5305.22 +2, 1362.21 +3, 929.622 +4, 682.862 +5, 611.955 +6, 532.807 +7, 448.558 +8, 360.514 +9, 360.63 +10, 358.592 +11, 275.627 +12, 275.853 diff --git a/lab06/benchmark_dyn.csv b/lab06/benchmark_dyn.csv new file mode 100644 index 0000000..fd07e8a --- /dev/null +++ b/lab06/benchmark_dyn.csv @@ -0,0 +1,13 @@ +threads,time +1, 4711.44 +2, 1274.57 +3, 872.423 +4, 655.906 +5, 580.711 +6, 555.452 +7, 483.429 +8, 426.956 +9, 411.524 +10, 413.396 +11, 396.789 +12, 422.115 diff --git a/lab06/benchmark_gui.csv b/lab06/benchmark_gui.csv new file mode 100644 index 0000000..17e56a3 --- /dev/null +++ b/lab06/benchmark_gui.csv @@ -0,0 +1,13 @@ +threads,time +1, 4630.75 +2, 1275.23 +3, 865.195 +4, 660.897 +5, 546.719 +6, 483.822 +7, 432.515 +8, 396.69 +9, 376.153 +10, 373.516 +11, 340.282 +12, 355.148 diff --git a/lab06/jacobi_benchmark.cpp b/lab06/jacobi_benchmark.cpp index d8a9153..007ebc1 100644 --- a/lab06/jacobi_benchmark.cpp +++ b/lab06/jacobi_benchmark.cpp @@ -15,19 +15,31 @@ Matrix jacobi(const Matrix &init, double eps, int maxNumIter) { int t0 = 0; int t1 = 1; - while (dist > eps && nIter < maxNumIter) { - dist = 0; - for (int i = 1; i < n - 1; ++i) { - for (int j = 1; j < m - 1; ++j) { - phi[t1](i, j) = .25 * (phi[t0](i + 1, j) + phi[t0](i - 1, j) + - phi[t0](i, j + 1) + phi[t0](i, j - 1)); - const double diff = phi[t1](i, j) - phi[t0](i, j); - dist = std::max(dist, std::abs(diff)); - } - } - nIter++; - std::swap(t0, t1); +#pragma omp parallel shared(dist, nIter, phi, t0, t1) + { + while (true) { +#pragma omp for reduction(max : dist) schedule(dynamic, 32) + for (int i = 1; i < n - 1; ++i) { + for (int j = 1; j < m - 1; ++j) { + phi[t1](i, j) = 0.25 * (phi[t0](i + 1, j) + phi[t0](i - 1, j) + + phi[t0](i, j + 1) + phi[t0](i, j - 1)); + const double diff = phi[t1](i, j) - phi[t0](i, j); + dist = std::max(dist, std::abs(diff)); + } + } + +#pragma omp single + { + nIter++; + std::swap(t0, t1); + } + +#pragma omp barrier + if (dist <= eps || nIter >= maxNumIter) + break; +#pragma omp barrier + } } std::cout << "Finished Jacobi after " << nIter @@ -176,15 +188,15 @@ double benchmark(int numThreads, JacobiParameters paramters) { } int main() { - const int numThreads = 8; + const int numThreads = 12; JacobiParameters parameters{.n = 1024, .maxNumIter = 1000, .eps = 1e-5}; // Uncomment the following lines to store a matrix on the harddisk as // reference. - const Matrix init = initialCondition(parameters.n); - Matrix phi = jacobi(init, parameters.eps, parameters.maxNumIter); - storeMatrix(phi, "ref.asc"); - std::cout << "Stored reference matrix to 'ref.asc'\n"; + // const Matrix init = initialCondition(parameters.n); + // Matrix phi = jacobi(init, parameters.eps, parameters.maxNumIter); + // storeMatrix(phi, "ref.asc"); + // std::cout << "Stored reference matrix to 'ref.asc'\n"; // Uncomment the following lines to verify the correctness of the // paralleliztion @@ -198,8 +210,15 @@ int main() { // } // // Perform the benchmark - // std::cout << "Starting benchmark\n"; - // const double time = benchmark(numThreads, n, numIter); - // std::cout << "Benchmark results (" << numThreads << "): " << time << - // "ms\n"; + std::cout << "Starting benchmark\n"; + for (int i = 1; i <= numThreads; ++i) { + std::cout << "Running benchmark with " << i << " threads\n"; + // write time in csv + std::ofstream fout("benchmark_dyn.csv", std::ios::app); + const double time = benchmark(i, parameters); + + fout << i << ", "; + fout << time << "\n"; + std::cout << "Benchmark results (" << i << "): " << time << "ms\n"; + } } diff --git a/lab06/plot_jacobi.py b/lab06/plot_jacobi.py index 8078b2d..081ce1a 100644 --- a/lab06/plot_jacobi.py +++ b/lab06/plot_jacobi.py @@ -1,43 +1,24 @@ import json import matplotlib.pyplot as plt +import pandas # Path to the benchmark results JSON -results = "" +file_static = "benchmark.csv" +file_dyn = "benchmark_dyn.csv" +file_gui = "benchmark_gui.csv" -# Load the data -def load_data(filename): - with open(filename, "r") as f: - data = json.load(f) +data_static = pandas.read_csv(file_static) +data_dyn = pandas.read_csv(file_dyn) +data_gui = pandas.read_csv(file_gui) - # Constants - total_flops = 18000 - - # Data storage - thread_counts = [] - flops = [] - - # Extract relevant data - for entry in data["benchmarks"]: - thread_count = int(entry["name"].split("/")[-1]) - time_per_iter_ms = entry["real_time"] - time_total_s = (time_per_iter_ms) / 1000 # convert ms to s - - flops_per_second = total_flops / time_total_s - - thread_counts.append(thread_count) - flops.append(flops_per_second) - - return thread_counts, flops - - -threads_VPRC, flops_VPRC = load_data(results) - -# Plotting +#Plotting plt.figure(figsize=(8, 5)) -plt.plot(threads_VPRC, flops_VPRC, marker="o", color="blue", label="VPRC") +plt.plot(data_static["threads"], data_static["time"], marker="o", color="blue", label="static") +plt.plot(data_dyn["threads"], data_dyn["time"], marker="o", color="red", label="dynamic") +plt.plot(data_gui["threads"], data_gui["time"], marker="o", color="green", label="guided") plt.xlabel("Thread Count") -plt.ylabel("Performance (FLOPS)") -plt.title("Performance (FLOPS) vs Thread Count") +plt.ylabel("Execution Time (ms)") +plt.title("Performance of Jacobi Benchmark") plt.grid(True) plt.tight_layout() plt.show() diff --git a/pyproject.toml b/pyproject.toml index 35a37c8..a3b5932 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,4 +6,5 @@ readme = "README.md" requires-python = ">=3.13" dependencies = [ "matplotlib>=3.10.1", + "pandas>=2.2.3", ] diff --git a/uv.lock b/uv.lock index 81b715e..4ff2d2d 100644 --- a/uv.lock +++ b/uv.lock @@ -65,10 +65,14 @@ version = "0.1.0" source = { virtual = "." } dependencies = [ { name = "matplotlib" }, + { name = "pandas" }, ] [package.metadata] -requires-dist = [{ name = "matplotlib", specifier = ">=3.10.1" }] +requires-dist = [ + { name = "matplotlib", specifier = ">=3.10.1" }, + { name = "pandas", specifier = ">=2.2.3" }, +] [[package]] name = "kiwisolver" @@ -174,6 +178,33 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469 }, ] +[[package]] +name = "pandas" +version = "2.2.3" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "numpy" }, + { name = "python-dateutil" }, + { name = "pytz" }, + { name = "tzdata" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/9c/d6/9f8431bacc2e19dca897724cd097b1bb224a6ad5433784a44b587c7c13af/pandas-2.2.3.tar.gz", hash = "sha256:4f18ba62b61d7e192368b84517265a99b4d7ee8912f8708660fb4a366cc82667", size = 4399213 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/64/22/3b8f4e0ed70644e85cfdcd57454686b9057c6c38d2f74fe4b8bc2527214a/pandas-2.2.3-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:f00d1345d84d8c86a63e476bb4955e46458b304b9575dcf71102b5c705320015", size = 12477643 }, + { url = "https://files.pythonhosted.org/packages/e4/93/b3f5d1838500e22c8d793625da672f3eec046b1a99257666c94446969282/pandas-2.2.3-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:3508d914817e153ad359d7e069d752cdd736a247c322d932eb89e6bc84217f28", size = 11281573 }, + { url = "https://files.pythonhosted.org/packages/f5/94/6c79b07f0e5aab1dcfa35a75f4817f5c4f677931d4234afcd75f0e6a66ca/pandas-2.2.3-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:22a9d949bfc9a502d320aa04e5d02feab689d61da4e7764b62c30b991c42c5f0", size = 15196085 }, + { url = "https://files.pythonhosted.org/packages/e8/31/aa8da88ca0eadbabd0a639788a6da13bb2ff6edbbb9f29aa786450a30a91/pandas-2.2.3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f3a255b2c19987fbbe62a9dfd6cff7ff2aa9ccab3fc75218fd4b7530f01efa24", size = 12711809 }, + { url = "https://files.pythonhosted.org/packages/ee/7c/c6dbdb0cb2a4344cacfb8de1c5808ca885b2e4dcfde8008266608f9372af/pandas-2.2.3-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:800250ecdadb6d9c78eae4990da62743b857b470883fa27f652db8bdde7f6659", size = 16356316 }, + { url = "https://files.pythonhosted.org/packages/57/b7/8b757e7d92023b832869fa8881a992696a0bfe2e26f72c9ae9f255988d42/pandas-2.2.3-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:6374c452ff3ec675a8f46fd9ab25c4ad0ba590b71cf0656f8b6daa5202bca3fb", size = 14022055 }, + { url = "https://files.pythonhosted.org/packages/3b/bc/4b18e2b8c002572c5a441a64826252ce5da2aa738855747247a971988043/pandas-2.2.3-cp313-cp313-win_amd64.whl", hash = "sha256:61c5ad4043f791b61dd4752191d9f07f0ae412515d59ba8f005832a532f8736d", size = 11481175 }, + { url = "https://files.pythonhosted.org/packages/76/a3/a5d88146815e972d40d19247b2c162e88213ef51c7c25993942c39dbf41d/pandas-2.2.3-cp313-cp313t-macosx_10_13_x86_64.whl", hash = "sha256:3b71f27954685ee685317063bf13c7709a7ba74fc996b84fc6821c59b0f06468", size = 12615650 }, + { url = "https://files.pythonhosted.org/packages/9c/8c/f0fd18f6140ddafc0c24122c8a964e48294acc579d47def376fef12bcb4a/pandas-2.2.3-cp313-cp313t-macosx_11_0_arm64.whl", hash = "sha256:38cf8125c40dae9d5acc10fa66af8ea6fdf760b2714ee482ca691fc66e6fcb18", size = 11290177 }, + { url = "https://files.pythonhosted.org/packages/ed/f9/e995754eab9c0f14c6777401f7eece0943840b7a9fc932221c19d1abee9f/pandas-2.2.3-cp313-cp313t-manylinux2014_aarch64.manylinux_2_17_aarch64.whl", hash = "sha256:ba96630bc17c875161df3818780af30e43be9b166ce51c9a18c1feae342906c2", size = 14651526 }, + { url = "https://files.pythonhosted.org/packages/25/b0/98d6ae2e1abac4f35230aa756005e8654649d305df9a28b16b9ae4353bff/pandas-2.2.3-cp313-cp313t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1db71525a1538b30142094edb9adc10be3f3e176748cd7acc2240c2f2e5aa3a4", size = 11871013 }, + { url = "https://files.pythonhosted.org/packages/cc/57/0f72a10f9db6a4628744c8e8f0df4e6e21de01212c7c981d31e50ffc8328/pandas-2.2.3-cp313-cp313t-musllinux_1_2_aarch64.whl", hash = "sha256:15c0e1e02e93116177d29ff83e8b1619c93ddc9c49083f237d4312337a61165d", size = 15711620 }, + { url = "https://files.pythonhosted.org/packages/ab/5f/b38085618b950b79d2d9164a711c52b10aefc0ae6833b96f626b7021b2ed/pandas-2.2.3-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:ad5b65698ab28ed8d7f18790a0dc58005c7629f227be9ecc1072aa74c0c1d43a", size = 13098436 }, +] + [[package]] name = "pillow" version = "11.2.1" @@ -225,6 +256,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892 }, ] +[[package]] +name = "pytz" +version = "2025.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f8/bf/abbd3cdfb8fbc7fb3d4d38d320f2441b1e7cbe29be4f23797b4a2b5d8aac/pytz-2025.2.tar.gz", hash = "sha256:360b9e3dbb49a209c21ad61809c7fb453643e048b38924c765813546746e81c3", size = 320884 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/81/c4/34e93fe5f5429d7570ec1fa436f1986fb1f00c3e0f43a589fe2bbcd22c3f/pytz-2025.2-py2.py3-none-any.whl", hash = "sha256:5ddf76296dd8c44c26eb8f4b6f35488f3ccbf6fbbd7adee0b7262d43f0ec2f00", size = 509225 }, +] + [[package]] name = "six" version = "1.17.0" @@ -233,3 +273,12 @@ sdist = { url = "https://files.pythonhosted.org/packages/94/e7/b2c673351809dca68 wheels = [ { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050 }, ] + +[[package]] +name = "tzdata" +version = "2025.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/95/32/1a225d6164441be760d75c2c42e2780dc0873fe382da3e98a2e1e48361e5/tzdata-2025.2.tar.gz", hash = "sha256:b60a638fcc0daffadf82fe0f57e53d06bdec2f36c4df66280ae79bce6bd6f2b9", size = 196380 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5c/23/c7abc0ca0a1526a0774eca151daeb8de62ec457e77262b66b359c3c7679e/tzdata-2025.2-py2.py3-none-any.whl", hash = "sha256:1a403fada01ff9221ca8044d701868fa132215d84beb92242d9acd2147f667a8", size = 347839 }, +]