diff --git a/Lab04/HPC-Lab-04.pdf b/Lab04/HPC-Lab-04.pdf index 0dc215f..2380976 100644 Binary files a/Lab04/HPC-Lab-04.pdf and b/Lab04/HPC-Lab-04.pdf differ diff --git a/Lab04/lab04.zip b/Lab04/lab04.zip new file mode 100644 index 0000000..6fb5f5c Binary files /dev/null and b/Lab04/lab04.zip differ diff --git a/Lab04/plot.py b/Lab04/plot.py new file mode 100644 index 0000000..7728542 --- /dev/null +++ b/Lab04/plot.py @@ -0,0 +1,58 @@ +import pandas as pd +import matplotlib.pyplot as plt + +count_threads = [1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64] +average_flops = [] +average_runtimes = [] +for i in range(0, 12): + avg_flops = 0 + avg_runtime = 0 + with open(f"./stats/stats{i}.csv", 'r') as file: + line = file.read() + stats = line.split(";") + for stat in stats: + splited = stat.split(",") + if len(splited) == 2: + avg_flops += float(splited[0]) + avg_runtime += float(splited[1]) + average_flops.append(avg_flops / 1000) + if i > 0: + average_runtimes.append(average_runtimes[0]/avg_runtime) + else: + average_runtimes.append(1) + + +efficiencies = [] + +for j in range(0, 12): + efficiencies.append(average_runtimes[j] / count_threads[j] * 100) + +# First plot: FLOPs vs Threads +plt.figure(figsize=(10, 4)) +plt.plot(count_threads, average_flops, marker='o') +plt.title("Average FLOPs vs Number of Threads") +plt.xlabel("Number of Threads") +plt.ylabel("Average FLOPs") +plt.grid(True) +plt.tight_layout() +plt.show() + +# Second plot: Runtime vs Threads +plt.figure(figsize=(10, 4)) +plt.plot(count_threads, average_runtimes, marker='o', color='orange') +plt.title("Speedup") +plt.xlabel("Number of Threads") +plt.ylabel("Factor in %") +plt.grid(True) +plt.tight_layout() +plt.show() + +# Second plot: Runtime vs Threads +plt.figure(figsize=(10, 4)) +plt.plot(count_threads, efficiencies, marker='o', color='red') +plt.title("Efficiency") +plt.xlabel("Number of Threads") +plt.ylabel("Speedup per Number of Threads (/count)") +plt.grid(True) +plt.tight_layout() +plt.show() \ No newline at end of file diff --git a/Lab04/triad_bench_2.cpp b/Lab04/triad_bench_2.cpp index dde6032..79d0a2b 100644 --- a/Lab04/triad_bench_2.cpp +++ b/Lab04/triad_bench_2.cpp @@ -26,7 +26,7 @@ void ownMethod(std::vector& A, int start, int end) { void workerThread(std::vector& A, int start, int end, int threadCount, Statistics& stat) { - std::chrono::duration sumTime; + std::chrono::duration sumTime; for (int i = 0; i < 20; i++) { auto startTime = std::chrono::system_clock::now(); ownMethod(A, start, end); @@ -35,7 +35,7 @@ void workerThread(std::vector& A, int start, int end, int threadCount, S } stat.avg_time = sumTime.count() / 20; - stat.flops = (2.0 * N) / (stat.avg_time / 1000); + stat.flops = (2.0 * N) / (stat.avg_time / 1000000); } int main() { @@ -56,22 +56,23 @@ int main() { int start = N / countThreads[j] * i; int end = N / countThreads[j] * (i + 1); - threads.emplace_back(workerThread, std::ref(A), start, end, countThreads[j], std::ref(stats[j])); + threads.emplace_back(workerThread, std::ref(A), start, end, countThreads[j], std::ref(stats[i])); } for (auto& t : threads) { t.join(); } - std::cout << A[1179647]; + //std::cout << A[1179647]; volatile std::vector dummy(A); - // std::stringstream fileName; - // fileName << "outcomes-" << start << "-" << end << ".csv"; - // std::ofstream MyFile(fileName.str()); - - // MyFile << "(" << averageTime << "" << flops << ")"; - // MyFile.close(); + std::stringstream fileName; + fileName << "stats" << j << ".csv"; + std::ofstream MyFile(fileName.str()); + for (auto entry : stats) { + MyFile << entry.avg_time << "," << entry.flops << ";"; + } + MyFile.close(); std::cout << "All threads finished.\n"; }