Files
FluidSimCpp/tools/thread_benchmark.cpp

55 lines
2.1 KiB
C++

#include "FluidSim/Fluid.hpp"
#include <barrier>
#include <chrono>
#include <iostream>
#include <thread>
using namespace fluidsim::port;
int main() {
for (auto size : {std::pair{6, 10}, std::pair{64, 256}}) {
const int steps = 1000;
for (int workers : {1, 2, 4}) {
std::vector<Pipe1D> p;
for (int i = 0; i < size.first; i++)
p.emplace_back(.001f, 2, size.second);
auto advance = [&](int worker) {
for (int k = worker; k < size.first; k += workers) {
auto &a = p[k];
auto l = a.state(0), r = a.state(size.second - 1);
l.velocity = -l.velocity;
r.velocity = -r.velocity;
a.left_ghost = l;
a.right_ghost = r;
a.advance(.0000005f);
}
};
std::barrier sync(workers + 1);
std::vector<std::jthread> pool;
if (workers > 1)
for (int k = 0; k < workers; k++)
pool.emplace_back([&, k] {
for (int i = 0; i < steps; i++) {
sync.arrive_and_wait();
advance(k);
sync.arrive_and_wait();
}
});
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < steps; i++) {
if (workers == 1)
advance(0);
else {
sync.arrive_and_wait();
sync.arrive_and_wait();
}
}
double sec = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
pool.clear();
double sum = 0;
for (auto &q : p)
sum += q.state(0).pressure;
std::cout << "pipes=" << size.first << " cells/pipe=" << size.second << " workers=" << workers
<< " steps=" << steps << " seconds=" << sec << " checksum=" << sum << '\n';
}
}
}