This commit is contained in:
WickedJack99
2025-04-12 14:12:00 +02:00
parent 5710bed6bf
commit 2c8782be9e
4 changed files with 69 additions and 10 deletions

Binary file not shown.

BIN
Lab04/lab04.zip Normal file

Binary file not shown.

58
Lab04/plot.py Normal file
View File

@@ -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()

View File

@@ -26,7 +26,7 @@ void ownMethod(std::vector<double>& A, int start, int end) {
void workerThread(std::vector<double>& A, int start, int end, int threadCount, Statistics& stat) {
std::chrono::duration<double, std::milli> sumTime;
std::chrono::duration<double, std::nano> 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<double>& 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<double> 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";
}