SHA256
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#include <concepts>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <omp.h>
|
|
#include <ostream>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
unsigned g_threads = std::thread::hardware_concurrency();
|
|
const unsigned max_threads = g_threads;
|
|
|
|
unsigned get_num_threads() { return g_threads; }
|
|
|
|
void set_num_threads(const unsigned T) {
|
|
if (T == 0 || T > max_threads)
|
|
return;
|
|
g_threads = T;
|
|
omp_set_num_threads(T);
|
|
}
|
|
|
|
struct measurement {
|
|
unsigned T;
|
|
double time;
|
|
unsigned result;
|
|
double speedup;
|
|
};
|
|
|
|
template <std::invocable<const unsigned *, size_t> F>
|
|
std::vector<measurement> measure_time(F f, const unsigned *V, size_t n) {
|
|
std::vector<measurement> ret;
|
|
const unsigned max_threads = get_num_threads();
|
|
for (unsigned T = 1; T <= max_threads; T++) {
|
|
set_num_threads(T);
|
|
double t0 = omp_get_wtime();
|
|
const unsigned result = f(V, n);
|
|
double t1 = omp_get_wtime();
|
|
ret.push_back(measurement{
|
|
.T = T,
|
|
.time = t1 - t0,
|
|
.result = result,
|
|
.speedup = ret.size() > 0 ? ret.back().time / ret[0].time : 1});
|
|
}
|
|
set_num_threads(max_threads);
|
|
return ret;
|
|
}
|
|
|
|
void to_csv(std::ostream &os, const std::vector<measurement> &m) {
|
|
os << "T, time, speedups, result, \n";
|
|
for (auto i : m) {
|
|
os << i.T << ", " << i.time << ", " << i.speedup << ", " << i.result
|
|
<< "\n";
|
|
}
|
|
}
|
|
|
|
template <std::invocable<const unsigned *, size_t> F>
|
|
void measure_time(F f, const unsigned *V, size_t n, std::string name) {
|
|
std::filesystem::create_directory(std::string("measurements/") + name +
|
|
".csv");
|
|
std::filebuf fb;
|
|
fb.open("test.txt", std::ios::out);
|
|
std::ostream os(&fb);
|
|
to_csv(os, measure_time(f, V, n));
|
|
}
|