Compare commits
3 Commits
parallelis
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 599b2b8dac | |||
| 560cc4a762 | |||
| 0d6403a0b8 |
14
src/main.cpp
14
src/main.cpp
@ -142,16 +142,22 @@ int main()
|
|||||||
fabs(mouse.y - screenPos.y) < 40)
|
fabs(mouse.y - screenPos.y) < 40)
|
||||||
{
|
{
|
||||||
c.FlipForward(); //<-----Animation Start
|
c.FlipForward(); //<-----Animation Start
|
||||||
//Neu Counter
|
|
||||||
counter++;
|
|
||||||
|
|
||||||
if (state == GameState::Idle) //Neu (Ü4)
|
|
||||||
|
if (state == GameState::Idle) //neu Ü4
|
||||||
|
{
|
||||||
state = GameState::OneFlipped;
|
state = GameState::OneFlipped;
|
||||||
else
|
counter++;
|
||||||
|
}
|
||||||
|
else if (state == GameState::OneFlipped && first && &c != first)
|
||||||
|
{
|
||||||
state = GameState::LockInput;
|
state = GameState::LockInput;
|
||||||
|
counter++;
|
||||||
|
}
|
||||||
|
|
||||||
break; // 🔒 nur EIN Würfel pro Klick
|
break; // 🔒 nur EIN Würfel pro Klick
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +0,0 @@
|
|||||||
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)
|
|
||||||
122
Übung4/main.cpp
122
Übung4/main.cpp
@ -1,122 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <thread>
|
|
||||||
#include <chrono>
|
|
||||||
#include <cstdlib>
|
|
||||||
|
|
||||||
// =====================
|
|
||||||
// Datei einlesen
|
|
||||||
// =====================
|
|
||||||
void readFile(const std::string& filename, std::vector<int>& data)
|
|
||||||
{
|
|
||||||
std::ifstream file(filename);
|
|
||||||
int value;
|
|
||||||
while (file >> value)
|
|
||||||
data.push_back(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// =====================
|
|
||||||
// Serielle Auswertung
|
|
||||||
// =====================
|
|
||||||
int countSerial(const std::vector<int>& 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<int>& messungen, int numThreads)
|
|
||||||
{
|
|
||||||
if (messungen.size() < 2)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
int totalComparisons = messungen.size() - 1;
|
|
||||||
if (numThreads > totalComparisons)
|
|
||||||
numThreads = totalComparisons;
|
|
||||||
|
|
||||||
std::vector<int> results(numThreads, 0);
|
|
||||||
std::vector<std::thread> 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<int> 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<double> 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<double> parallelTime = end - start;
|
|
||||||
std::cout << "Parallel: " << parallelResult
|
|
||||||
<< " | Took: " << parallelTime.count() << " s\n";
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user