diff --git a/Übung4/CMakeLists.txt b/Übung4/CMakeLists.txt new file mode 100644 index 0000000..56513ce --- /dev/null +++ b/Übung4/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.20) + +project(prog3b_652) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +add_executable(prog3b main.cpp) diff --git a/Übung4/main.cpp b/Übung4/main.cpp new file mode 100644 index 0000000..56bac9b --- /dev/null +++ b/Übung4/main.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include + +// ===================== +// Datei einlesen +// ===================== +void readFile(const std::string& filename, std::vector& data) +{ + std::ifstream file(filename); + int value; + while (file >> value) + data.push_back(value); +} + +// ===================== +// Serielle Auswertung +// ===================== +int countSerial(const std::vector& messungen) +{ + if (messungen.size() < 2) + return 0; + + int increases = 0; + for (size_t i = 1; i < messungen.size(); ++i) + if (messungen[i] > messungen[i - 1]) + ++increases; + + return increases; +} + +// ===================== +// Parallele Auswertung +// ===================== +int countParallel(const std::vector& messungen, int numThreads) +{ + if (messungen.size() < 2) + return 0; + + int totalComparisons = messungen.size() - 1; + if (numThreads > totalComparisons) + numThreads = totalComparisons; + + std::vector results(numThreads, 0); + std::vector threads; + + int base = totalComparisons / numThreads; + int remainder = totalComparisons % numThreads; + + int idx = 1; + + for (int t = 0; t < numThreads; ++t) + { + int startIdx = idx; + int count = base + (t < remainder ? 1 : 0); + int endIdx = idx + count - 1; + idx = endIdx + 1; + + threads.emplace_back([&, t, startIdx, endIdx]() + { + int localCount = 0; + for (int i = startIdx; i <= endIdx; ++i) + if (messungen[i] > messungen[i - 1]) + ++localCount; + + results[t] = localCount; + }); + } + + for (auto& th : threads) + th.join(); + + int total = 0; + for (int r : results) + total += r; + + return total; +} + +// ===================== +// main +// ===================== +int main() +{ + std::vector messungen; + + // ===== Variante A: Datei ===== + readFile("measurements.txt", messungen); + + // ===== Variante B: künstliche Daten (statt Datei) ===== + /* + int value = 0; + for (size_t i = 0; i < 100000000; ++i) + messungen.push_back(value += (rand() % 3 - 1)); + */ + + // ===== Seriell ===== + auto start = std::chrono::steady_clock::now(); + int serialResult = countSerial(messungen); + auto end = std::chrono::steady_clock::now(); + + std::chrono::duration serialTime = end - start; + std::cout << "Seriell: " << serialResult + << " | Took: " << serialTime.count() << " s\n"; + + // ===== Parallel ===== + int numThreads = std::thread::hardware_concurrency(); + if (numThreads == 0) numThreads = 4; + + start = std::chrono::steady_clock::now(); + int parallelResult = countParallel(messungen, numThreads); + end = std::chrono::steady_clock::now(); + + std::chrono::duration parallelTime = end - start; + std::cout << "Parallel: " << parallelResult + << " | Took: " << parallelTime.count() << " s\n"; + + return 0; +}